logo

oasis

Own branch of Oasis Linux (upstream: <https://git.sr.ht/~mcf/oasis/>) git clone https://anongit.hacktivis.me/git/oasis.git

setup.lua (3807B)


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