summaryrefslogtreecommitdiff
path: root/setup.lua
blob: 7779bc5975b2ae6c977aac76dc9d8b1235ceb913 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/lua

-- 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('test -f config.lua') then
	os.execute('cp '..basedir..'/config.def.lua config.lua')
end

dofile(basedir..'/ninja.lua')
config = dofile 'config.lua'
if not config.prefix then
	config.prefix = ''
end

local recurse = not arg[1]

function subgen(dir)
	local file = '$gendir/'..dir..'/local.ninja'
	subninja(file)
	table.insert(pkg.inputs.ninja, '$gendir/'..dir..'/ninja')
	table.insert(pkg.inputs.index, '$outdir/'..dir..'/root.index')
	table.insert(pkg.inputs.perms, '$outdir/'..dir..'/root.perms')
	table.insert(pkg.inputs.fspec, '$outdir/'..dir..'/root.fspec')
	local cmd = string.format('test -f %s/%s/local.ninja', pkg.gendir, dir)
	if recurse or not os.execute(cmd) then
		local oldpkg, oldout = pkg, io.output()
		if pkg.gendir ~= '.' then
			dir = pkg.gendir..'/'..dir
		end
		gen(dir)
		pkg = oldpkg
		io.output(oldout)
	end
end

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={
			perms={},
			index={},
			fspec={implicit={}},
			gen={
				'$basedir/ninja.lua',
				'$basedir/sets.lua',
				'$basedir/setup.lua',
				'config.lua',
			},
			ninja={'$gendir/local.ninja'},
			fetch={},
		},
		perms={},
		fspec={},
	}
	assert(os.execute('mkdir -p '..gendir))
	assert(os.execute('mkdir -p '..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('gen', '$gendir/local.ninja', {'|', pkg.inputs.gen})
	phony('ninja', pkg.inputs.ninja)

	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.perms) then
		table.sort(pkg.perms, function(s1, s2)
			return s1:sub(8) < s2:sub(8)
		end)
		local f = assert(io.open(outdir..'/local.perms', 'w'))
		table.insert(pkg.perms, '')
		f:write(table.concat(pkg.perms, '\n'))
		table.insert(pkg.inputs.perms, '$outdir/local.perms')
		f:close()
	end
	if next(pkg.fspec) then
		local f = assert(io.open(outdir..'/local.fspec', 'w'))
		for _, path in ipairs(table.keys(pkg.fspec)) do
			f:write(('/%s\n'):format(path))
			for k, v in pairs(pkg.fspec[path]) do
				f:write(('%s=%s\n'):format(k, v))
			end
			f:write('\n')
		end
		f:close()
		table.insert(pkg.inputs.fspec, '$outdir/local.fspec')
	end
	if next(pkg.inputs.perms) then
		build('mergeperms', '$outdir/root.perms', pkg.inputs.perms)
	else
		build('empty', '$outdir/root.perms')
	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
		if next(pkg.inputs.fspec.implicit) then
			table.insert(pkg.inputs.fspec, {'|', pkg.inputs.fspec.implicit})
		end
		build('cat', '$outdir/root.fspec', pkg.inputs.fspec, {
			description = '	FSPEC	$outdir/root.fspec',
		})
	else
		build('empty', '$outdir/root.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('ln -sf local.ninja build.ninja')
	end
end

gen(arg[1] or '.')