opkg

statically linked package installer
git clone anongit@rnpnr.xyz:opkg.git
Log | Files | Refs | Feed | Submodules | README | LICENSE

setup.lua (3548B)


      1 #!/bin/lua
      2 
      3 -- Lua 5.1 compatibility
      4 do
      5 	local os_execute = os.execute
      6 	os.execute = function(cmd)
      7 		local ret = os_execute(cmd)
      8 		return ret == true or ret == 0
      9 	end
     10 end
     11 
     12 basedir = arg[0]:match('(.*)/') or '.'
     13 
     14 if not os.execute('exec test -f config.lua') then
     15 	os.execute('exec cp '..basedir..'/config.def.lua config.lua')
     16 end
     17 
     18 dofile(basedir..'/ninja.lua')
     19 config = dofile 'config.lua'
     20 if not config.prefix then
     21 	config.prefix = ''
     22 end
     23 
     24 local recurse = not arg[1]
     25 
     26 local function gen(gendir)
     27 	local dir = basedir..'/'..gendir
     28 	local outdir = config.builddir..'/'..gendir
     29 	pkg={
     30 		name=gendir:match('[^/]*$'),
     31 		dir=dir,
     32 		gendir=gendir,
     33 		srcdir=dir..'/src',
     34 		outdir=outdir,
     35 		inputs={
     36 			index={},
     37 			fspec={},
     38 			gen={
     39 				'$basedir/ninja.lua',
     40 				'$basedir/sets.lua',
     41 				'$basedir/setup.lua',
     42 				'config.lua',
     43 			},
     44 			ninja={'$gendir/local.ninja'},
     45 			fetch={},
     46 		},
     47 		fspec={},
     48 	}
     49 	assert(os.execute(('exec mkdir -p %s %s'):format(gendir, outdir)))
     50 	io.output(gendir..'/local.ninja.tmp')
     51 	set('gendir', gendir)
     52 	if gendir ~= '.' then
     53 		set('dir', '$basedir/$gendir')
     54 		set('outdir', '$builddir/$gendir')
     55 		set('srcdir', '$dir/src')
     56 	end
     57 	load('gen.lua')
     58 
     59 	build('gen', '$gendir/local.ninja', {'|', pkg.inputs.gen})
     60 	phony('ninja', pkg.inputs.ninja)
     61 
     62 	if pkg.hdrs then
     63 		phony('headers', pkg.hdrs)
     64 		if pkg.hdrs.install then
     65 			for hdr in iterstrings(pkg.hdrs) do
     66 				if not hdr:hasprefix('$outdir/include/') then
     67 					error('header is not in $outdir/include: '..hdr)
     68 				end
     69 				file(hdr:sub(9), '644', hdr)
     70 			end
     71 		end
     72 	end
     73 	if pkg.deps then
     74 		phony('deps', pkg.deps)
     75 	end
     76 
     77 	if next(pkg.fspec) then
     78 		local out = outdir..'/local.fspec'
     79 		local tmp = out..'.tmp'
     80 		local f = assert(io.open(tmp, 'w'))
     81 		local srcs = {}
     82 		for _, path, fspec in sortedpairs(pkg.fspec) do
     83 			f:write(('/%s\n'):format(path))
     84 			for _, k in ipairs{'type', 'mode', 'source', 'target'} do
     85 				local v = fspec[k]
     86 				if v then
     87 					f:write(('%s=%s\n'):format(k, v))
     88 				end
     89 			end
     90 			f:write('\n')
     91 			local src = fspec.source
     92 			if src then
     93 				srcs[#srcs + 1] = src
     94 			end
     95 		end
     96 		f:close()
     97 		if os.execute(('exec cmp -s %s %s'):format(tmp, out)) then
     98 			os.remove(tmp)
     99 		else
    100 			os.rename(tmp, out)
    101 		end
    102 		build('fspec-hash', '$outdir/local-hashed.fspec', {'$outdir/local.fspec', '|', '$builddir/pkg/devel/fspec-sync/host/fspec-hash', srcs})
    103 		table.insert(pkg.inputs.fspec, '$outdir/local-hashed.fspec')
    104 	end
    105 	if next(pkg.inputs.index) then
    106 		build('cat', '$outdir/root.index', pkg.inputs.index, {
    107 			description='	INDEX	$outdir/root.index',
    108 		})
    109 	else
    110 		build('empty', '$outdir/root.index')
    111 	end
    112 	if next(pkg.inputs.fspec) then
    113 		build('cat', '$outdir/tree.fspec', pkg.inputs.fspec, {
    114 			description = '	FSPEC	$outdir/tree.fspec',
    115 		})
    116 	else
    117 		build('empty', '$outdir/tree.fspec')
    118 	end
    119 	build('phony', '$dir/root', pkg.inputs.root)
    120 	io.close()
    121 	os.rename(gendir..'/local.ninja.tmp', gendir..'/local.ninja')
    122 	if gendir == '.' then
    123 		os.execute('exec ln -sf local.ninja build.ninja')
    124 	end
    125 end
    126 
    127 function subgen(dir)
    128 	local file = '$gendir/'..dir..'/local.ninja'
    129 	subninja(file)
    130 	table.insert(pkg.inputs.ninja, '$gendir/'..dir..'/ninja')
    131 	table.insert(pkg.inputs.index, '$outdir/'..dir..'/root.index')
    132 	table.insert(pkg.inputs.fspec, '$outdir/'..dir..'/tree.fspec')
    133 	local cmd = ('exec test -f %s/%s/local.ninja'):format(pkg.gendir, dir)
    134 	if recurse or not os.execute(cmd) then
    135 		local oldpkg, oldout = pkg, io.output()
    136 		if pkg.gendir ~= '.' then
    137 			dir = pkg.gendir..'/'..dir
    138 		end
    139 		gen(dir)
    140 		pkg = oldpkg
    141 		io.output(oldout)
    142 	end
    143 end
    144 
    145 gen(arg[1] or '.')