Files
oasis-linux-mirror/setup.lua
T
2026-04-11 15:51:27 -07:00

155 lines
3.7 KiB
Lua
Executable File

#!/bin/lua
-- prevent accidental use of undefined globals
setmetatable(_G, {__index=function(_, var) error('undefined global \''..var..'\'') end})
-- Lua 5.1 compatibility
do
local os_execute = os.execute
os.execute = function(cmd)
local ret = os_execute(cmd)
return ret == true or ret == 0
end
end
basedir = arg[0]:match('(.*)/') or '.'
if not os.execute('exec test -f config.lua') then
os.execute('exec cp '..basedir..'/config.def.lua config.lua')
end
dofile(basedir..'/ninja.lua')
config = dofile 'config.lua'
if not config.prefix then
config.prefix = ''
end
if not config.distdir then
config.distdir = 'dist'
end
local function shellpath(path, base)
if path == '.' then return base end
local abs = path:find('^/')
path = "'"..path:gsub([[']], [['\'']]).."'"
if abs then return path end
return base..'/'..path
end
local f = io.open('paths.sh', 'w')
f:write(string.format([[
basedir=%s
builddir=%s
distdir=%s
]], shellpath(basedir, '$PWD'), shellpath(config.builddir, '$PWD'), shellpath(config.distdir, '$basedir')))
f:close()
local function gen(gendir)
local dir = basedir..'/'..gendir
local outdir = config.builddir..'/'..gendir
pkg={
name=gendir:match('[^/]*$'),
dir=dir,
gendir=gendir,
srcdir=dir..'/src',
outdir=outdir,
inputs={
index={},
fspec={},
gen={},
fetch={},
},
fspec={},
}
assert(os.execute(('exec mkdir -p %s %s'):format(gendir, outdir)))
io.output(gendir..'/local.ninja.tmp')
set('gendir', gendir)
if gendir ~= '.' then
set('dir', '$basedir/$gendir')
set('outdir', '$builddir/$gendir')
set('srcdir', '$dir/src')
end
load('gen.lua')
build('phony', '$gendir/gen', pkg.inputs.gen)
if pkg.hdrs then
phony('headers', pkg.hdrs)
if pkg.hdrs.install then
for hdr in iterstrings(pkg.hdrs) do
if not hdr:hasprefix('$outdir/include/') then
error('header is not in $outdir/include: '..hdr)
end
file(hdr:sub(9), '644', hdr)
end
end
end
if pkg.deps then
phony('deps', pkg.deps)
end
if next(pkg.fspec) then
local out = outdir..'/local.fspec'
local tmp = out..'.tmp'
local f = assert(io.open(tmp, 'w'))
local srcs = {}
for _, path, fspec in sortedpairs(pkg.fspec) do
f:write(('/%s\n'):format(path))
for _, k in ipairs{'type', 'mode', 'source', 'target'} do
local v = fspec[k]
if v then
f:write(('%s=%s\n'):format(k, v))
end
end
f:write('\n')
local src = fspec.source
if src then
srcs[#srcs + 1] = src
end
end
f:close()
if os.execute(('exec cmp -s %s %s'):format(tmp, out)) then
os.remove(tmp)
else
os.rename(tmp, out)
end
build('fspec-hash', '$outdir/local-hashed.fspec', {'$outdir/local.fspec', '|', '$builddir/pkg/fspec-sync/host/fspec-hash', srcs})
table.insert(pkg.inputs.fspec, '$outdir/local-hashed.fspec')
end
if next(pkg.inputs.index) then
build('cat', '$outdir/root.index', pkg.inputs.index, {
description=' INDEX $outdir/root.index',
})
else
build('empty', '$outdir/root.index')
end
if next(pkg.inputs.fspec) then
build('cat', '$outdir/tree.fspec', pkg.inputs.fspec, {
description = ' FSPEC $outdir/tree.fspec',
})
else
build('empty', '$outdir/tree.fspec')
end
build('phony', '$dir/root', pkg.inputs.root)
io.close()
os.rename(gendir..'/local.ninja.tmp', gendir..'/local.ninja')
if gendir == '.' then
os.execute('exec ln -sf local.ninja build.ninja')
end
end
function subgen(dir)
local file = '$gendir/'..dir..'/local.ninja'
subninja(file)
table.insert(pkg.inputs.gen, '$gendir/'..dir..'/gen')
table.insert(pkg.inputs.index, '$outdir/'..dir..'/root.index')
table.insert(pkg.inputs.fspec, '$outdir/'..dir..'/tree.fspec')
local oldpkg, oldout = pkg, io.output()
if pkg.gendir ~= '.' then
dir = pkg.gendir..'/'..dir
end
gen(dir)
pkg = oldpkg
io.output(oldout)
end
gen('.')