-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.py
executable file
·346 lines (290 loc) · 11.9 KB
/
configure.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python3
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from tools.ninja_syntax import Writer as NinjaWriter
import argparse
import sys
import tomllib
class TargetKind(Enum):
Phony = 'phony'
Command = 'command'
Executable = 'executable'
StaticLibrary = 'static_library'
ObjectLibrary = 'object_library'
@dataclass
class Target:
name: str
kind: TargetKind
dir_path: Path
output: str
deps: list[str]
sources: list[str]
flags: dict[str, str]
uses_console: bool
commands: list[str]
def add_flags(flags: dict[str, str], obj):
if 'as_flags' in obj:
flags['as'] += ' ' + obj['as_flags']
if 'cxx_flags' in obj:
flags['cxx'] += ' ' + obj['cxx_flags']
if 'ld_flags' in obj:
flags['ld'] += ' ' + obj['ld_flags']
if 'nasm_flags' in obj:
flags['nasm'] += ' ' + obj['nasm_flags']
def build_target(definition, dir_path: Path, flags: dict[str, str], sources: list[str], kind: TargetKind | None) -> Target:
if kind is None:
kind = {
'static': TargetKind.StaticLibrary,
'object': TargetKind.ObjectLibrary,
}[definition.get('type', 'static')]
name = definition.get('name', None) or definition['output']
if kind == TargetKind.StaticLibrary:
output = str(dir_path.joinpath('lib' + name).with_suffix('.a'))
else:
output = str(dir_path.joinpath(name))
commands = []
if kind == TargetKind.Command:
commands.append(definition['command'])
elif kind == TargetKind.Phony:
commands.extend(definition.get('commands', []))
if definition.get('no_inherit', False):
flags = {'as': '', 'cxx': '', 'ld': '', 'nasm': ''}
# Add flags for this target.
add_flags(flags, definition)
return Target(
name=name,
kind=kind,
dir_path=dir_path,
output=output,
deps=definition.get('deps', []),
sources=sources + definition.get('sources', []),
flags=flags,
uses_console=definition.get('uses_console', False),
commands=commands,
)
class Context(NinjaWriter):
source_root: Path
build_root: Path
preset: str
enabled_options: list[str]
toml_paths: list[str] = []
targets: list[Target] = []
def __init__(self, source_root, build_root, preset, enabled_options):
self.source_root = source_root
self.build_root = build_root
self.preset = preset
self.enabled_options = enabled_options
self.build_root.mkdir(exist_ok=True)
output_path = self.build_root.joinpath('build.ninja')
output_file = output_path.open('w')
super().__init__(output_file, width=120)
def build_tree(self, dir_path: Path, flags: dict[str, str], indent: int):
# Add toml path to list for the auto-reconfigure rule.
toml_path = dir_path.joinpath('build.toml')
self.toml_paths.append(f'$root/{str(toml_path)}')
# Load and parse toml.
print(f'{" " * indent}Building {str(toml_path)}')
with self.source_root.joinpath(toml_path).open('rb') as file:
toml = tomllib.load(file)
# Add flags global to this toml file.
add_flags(flags, toml)
# Add flags for the current preset.
presets = toml.get('preset', {})
if presets:
if dir_path != Path('.'):
print('Preset in non-root build.toml!')
sys.exit(1)
if self.preset not in presets:
print(f'No preset {self.preset}')
sys.exit(1)
add_flags(flags, presets[self.preset])
# Add flags for any enabled options.
options = toml.get('option', {})
for enabled_option in filter(lambda o: o in options, self.enabled_options):
print(f'{" " * indent} Enabling option \'{enabled_option}\'')
add_flags(flags, options[enabled_option])
# Recurse through subdirectories.
for subdir in toml.get('subdirs', []):
self.build_tree(dir_path.joinpath(subdir), flags.copy(), indent + 2)
# Get sources global to each target in this toml file.
sources = toml.get('sources', [])
# Create targets.
for command in toml.get('command', []):
self.targets.append(build_target(command, dir_path, {}, [], TargetKind.Command))
for executable in toml.get('executable', []):
self.targets.append(build_target(executable, dir_path, flags.copy(), sources, TargetKind.Executable))
for library in toml.get('library', []):
self.targets.append(build_target(library, dir_path, flags.copy(), sources, None))
for phony in toml.get('phony', []):
self.targets.append(build_target(phony, dir_path, {}, [], TargetKind.Phony))
def generate_target(self, target: Target):
self.comment(target.name)
explicit_dependencies = []
implicit_dependencies = []
order_only_dependencies = []
deps = [t for t in self.targets if t.name in target.deps]
for dep in deps:
if dep.kind != TargetKind.ObjectLibrary:
implicit_dependencies.append(dep.output)
continue
order_only_dependencies.append(dep.output)
for dep_source in dep.sources:
# TODO: Duplicated.
object_path = dep.dir_path.joinpath(dep.name + '-objs').joinpath(dep_source + '.o')
explicit_dependencies.append(str(object_path))
for source in target.sources:
object_path = target.dir_path.joinpath(target.name + '-objs').joinpath(source + '.o')
explicit_dependencies.append(str(object_path))
source_path = target.dir_path.joinpath(source)
source_type = {
'.S': 'as',
'.asm': 'nasm',
'.cc': 'cxx',
}[str(source_path.suffix)]
variables = {}
flags = target.flags[source_type].strip()
if flags != '':
variables['flags'] = flags
root_path = '$root/'
for t in self.targets:
if str(source_path) == t.output:
root_path = ''
self.build(outputs=str(object_path),
rule=source_type,
inputs=root_path + str(source_path),
variables=variables)
rule = {
TargetKind.Phony: 'phony',
TargetKind.Command: 'custom_command',
TargetKind.Executable: 'link',
TargetKind.StaticLibrary: 'link-static',
TargetKind.ObjectLibrary: 'phony',
}[target.kind]
variables = {}
if target.kind == TargetKind.Command:
variables['command'] = target.commands[0]
variables['description'] = f'Building {target.output}'
elif target.kind == TargetKind.Executable:
variables['flags'] = target.flags['cxx'] + ' ' + target.flags['ld']
variables['libs'] = ''
for dep in filter(lambda d: d.kind == TargetKind.StaticLibrary, deps):
variables['libs'] += dep.output + ' '
pool = 'console' if target.uses_console else None
# Build command target.
if target.kind == TargetKind.Phony and len(target.commands) != 0:
command_target = f'{target.name}_commands'
concatenated_commands = ''
for i, command in enumerate(target.commands):
if i != 0:
concatenated_commands += ' && '
concatenated_commands += command
self.newline()
self.build(outputs=command_target,
rule='custom_command',
order_only=explicit_dependencies + implicit_dependencies + order_only_dependencies,
variables={
'command': concatenated_commands,
'description': f'Commands for phony target {target.name}',
},
pool=pool)
explicit_dependencies.append(command_target)
self.newline()
self.build(outputs=target.output,
rule=rule,
inputs=explicit_dependencies,
implicit=implicit_dependencies,
order_only=order_only_dependencies,
variables=variables,
pool=pool)
self.newline()
def generate_targets(self):
for target in self.targets:
self.generate_target(target)
def main():
arg_parser = argparse.ArgumentParser(prog=sys.argv[0], description='Configure umbongo build')
arg_parser.add_argument('-B', default='build', dest='build_dir')
arg_parser.add_argument('-p', '--preset', default='debug')
arg_parser.add_argument('-e', '--enable', action='append', help='Enable option', default=[])
args = arg_parser.parse_args()
context = Context(
source_root=Path(__file__).parent,
build_root=Path(args.build_dir),
preset=args.preset,
enabled_options=args.enable,
)
context.build_tree(Path('.'), {
'as': '',
'cxx': '',
'ld': '',
'nasm': '',
}, 0)
context.targets.append(Target(
name='all',
kind=TargetKind.Phony,
dir_path=Path('.'),
output='all',
deps=[target.name for target in context.targets
if target.kind not in [TargetKind.Phony, TargetKind.ObjectLibrary]],
sources=[],
flags={},
uses_console=False,
commands=[],
))
context.comment('Generated by umbongo\'s configure.py')
context.variable('ninja_required_version', '1.11')
context.newline()
context.variable('build_preset', context.preset)
context.variable('build_root', str(context.build_root.absolute()))
context.variable('root', str(context.source_root))
context.newline()
context.comment('Rules')
context.rule('clean', '/usr/bin/ninja -t clean')
context.newline()
context.rule('custom_command',
'$command',
description='$description')
context.newline()
context.rule('as',
'clang++ $flags -MD -MT $out -MF $out.d -o $out -c $in',
depfile='$out.d',
deps='gcc',
description='Building AS object $out')
context.newline()
context.rule('cxx',
'clang++ $flags -MD -MT $out -MF $out.d -o $out -c $in',
depfile='$out.d',
deps='gcc',
description='Building CXX object $out')
context.newline()
context.rule('link',
'clang++ $flags $in -o $out $libs',
description='Linking executable $out')
context.newline()
context.rule('link-static',
'rm -f $out && llvm-ar qcs $out $in',
description='Linking static library $out')
context.newline()
context.rule('nasm',
'nasm -MD $out.d -MT $out -f elf64 -o $out $in',
depfile='$out.d',
deps='gcc',
description='Building ASM object $out')
context.newline()
# Write ninja for targets.
context.generate_targets()
# Write auto-reconfigure rule.
option_command_line = ' '.join([f'-e {option}' for option in context.enabled_options])
context.comment('Regenerate build files if python or toml changes.')
context.rule('configure',
command=f'$root/configure.py -B $build_root -p $build_preset {option_command_line}',
description='Reconfiguring...',
generator=1)
context.build('build.ninja', 'configure', pool='console',
implicit=['$root/configure.py', '$root/tools/ninja_syntax.py'] + context.toml_paths)
context.newline()
context.build('clean', 'clean')
context.default('all')
context.close()
if __name__ == '__main__':
main()