-
Notifications
You must be signed in to change notification settings - Fork 2
/
configure
executable file
·109 lines (89 loc) · 2.98 KB
/
configure
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
#!/usr/bin/env python3
"""Script that generates a build.ninja file for Ilo."""
import platform
from os import walk
from os.path import join
from shutil import which
class NinjaFile:
RULES = (
("bootstrap", "cp $in $out"),
("ilo", "$in --format $iloformat > $out"),
("asm", "$asmtool $asmflags $in -o $out"),
("bin", "$bintool $in -o $out"),
)
def __init__(
self,
filename="build.ninja",
builddir="build",
system=None,
machine=None,
):
self.filename = filename
self.builddir = builddir
self.system = system or platform.system()
self.machine = machine or platform.machine()
self.dependencies = []
def generate(self):
with open(self.filename, "w") as file:
file.write(self.content)
for dependency in self.dependencies:
if not which(dependency):
raise ValueError(f"Please install {dependency}")
@staticmethod
def get_files(location):
return [
join(dirpath, filename)
for (dirpath, _, filenames) in walk(location)
for filename in filenames
]
@property
def is_linux(self):
return self.system == "Linux"
@property
def is_x86_64(self):
return self.machine == "x86_64"
@property
def variables(self):
if self.is_linux and self.is_x86_64:
self.dependencies.append("nasm")
self.dependencies.append("ld")
return {
"asmflags": "-felf64",
"asmtool": "nasm",
"bintool": "ld",
"iloformat": "linux_x86_64",
}
else:
raise NotImplementedError(
f"{self.machine}-{self.system} is not supported yet"
)
@property
def builds(self):
yield ("../bootstrap/ilo0.asm", "ilo0.asm", "bootstrap")
for ilo_bin, ilo_name in [
(None, "ilo0"), ("ilo0", "ilo1"), ("ilo1", "ilo")
]:
if ilo_bin:
yield (
f"{ilo_bin} src/ilo.ilo | {' '.join(self.get_files('lib/'))}",
f"{ilo_name}.asm",
"ilo",
)
yield (f"{ilo_name}.asm", f"{ilo_name}.o", "asm")
yield (f"{ilo_name}.o", f"{ilo_name}", "bin")
@property
def content(self):
def _format_variable(key, value):
return f"{key} = {value}"
def _format_rule(name, command):
return f"rule {name}\n command = {command}"
def _format_build(in_file, out_file, tool):
return f"build $builddir/{out_file}: {tool} $builddir/{in_file}"
return "\n".join(
[_format_variable("builddir", self.builddir)] +
[_format_variable(k, v) for k, v in self.variables.items()] +
[_format_rule(*r) for r in self.RULES] +
[_format_build(*b) for b in self.builds]
) + "\n"
n = NinjaFile()
n.generate()