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
|
#!/usr/bin/python
import os
from pathlib import Path
from threading import local
import shutil
def find_program(prog, required=True):
path = shutil.which(prog)
if path == None and required:
raise Exception("Required program '%s' not found on the system" % (prog))
return File(path)
ROOT = Path.cwd()
BUILD = Path("bld").resolve().relative_to(ROOT)
DEFDESC = lambda o, i, cmd: "%s" % (cmd)
class FileMeta(type):
def __call__(cls, path, *rest, **krest):
# If none, return none
if path == None:
return None
# If already a file, return it
if issubclass(type(path), File):
return path
# Adjust name to be a path
if type(path) == str:
p = Path(path).resolve()
if p.is_relative_to(ROOT):
path = p.relative_to(ROOT)
else:
path = p
idx = str(path) + cls.__name__
if idx in cls.fdb:
return cls.fdb[idx]
obj = cls.__new__(cls, path, *rest, **krest)
cls.__init__(obj, path, *rest, **krest)
cls.fdb[idx] = obj
return obj
class File(metaclass = FileMeta):
fdb = dict()
def __init__(self, path):
# The name corresponding to
# the target: this can be
# a path or an abstract name
self.path = path
def __str__(self):
return str(self.path)
def resolve_inner(tgt, arg):
if type(arg) == list or type(arg) == tuple:
for a in arg:
File.resolve_inner(tgt, a)
elif type(arg) == str:
tgt.append(File(arg))
elif issubclass(type(arg), File):
tgt.append(arg)
def resolve(*args):
tgt = []
for a in args:
File.resolve_inner(tgt, a)
return tgt
class Process(File):
def __init__(self, path, machine, inputs, extra_args = None):
super().__init__(path)
# The generator that created this process
self.machine = machine
# The inputs
self.inputs = inputs
# The extra arguments taken
self.extra_args = extra_args
def gen_makefile(self):
inputs = " ".join([str(x) for x in self.inputs])
extra_args = self.extra_args if self.extra_args else ""
out = "%s: %s\n" % (self.path, inputs)
out += "\t@$(%s) %s\n" % (self.machine.name, self.machine.mkargs("$@", "$^", extra_args))
out += "\t@echo '%s'" % (self.machine.desc(self, inputs, extra_args))
return out
def gen_ninja(self):
inputs = " ".join([str(x) for x in self.inputs])
out = "build %s: %s %s" % (self.path, self.machine.name, inputs)
if self.extra_args != None:
out += "\n extra = %s" % (self.extra_args)
return out
class MachineMeta(type):
def __call__(cls, name, *rest, **krest):
# If already a file, return it
if issubclass(type(name), Machine):
return name
if name in cls.mdb:
return cls.mdb[name]
obj = cls.__new__(cls, name, *rest, **krest)
cls.__init__(obj, name, *rest, **krest)
cls.mdb[name] = obj
return obj
class Machine(metaclass = MachineMeta):
mdb = dict()
def __init__(self, name, exe, mkargs, desc=DEFDESC):
# The name of the machine
self.name = name
# Executable used in the process
self.exe = File(exe)
# The arguments that is used to launch the process
self.mkargs = mkargs
# The description
self.desc = desc
def __call__(self, name, *args, extra_args=None):
return self.gen(name, *args, extra_args=extra_args)
def gen(self, name, *args, extra_args=None):
inputs = File.resolve(args)
path = BUILD / Path.cwd().relative_to(ROOT) / name
return Process(path, self, inputs, extra_args)
def gen_makefile(self):
return "%s = %s" % (self.name, self.exe)
def gen_ninja(self):
out = "rule %s\n" % (self.name)
cmd = "%s %s" % (self.exe, self.mkargs("$out", "$in", "$extra"))
out += " command = %s\n" % (cmd)
out += " description = %s" % (self.desc("$out", "$in", cmd))
return out
class Vec(Machine):
def __init__(self, name, exe, args, mkout, desc=DEFDESC):
super().__init__(name, exe, args, desc=desc)
self.mkout = mkout
def __call__(self, *args, extra_args=None):
return self.gen(*args, extra_args=extra_args)
def gen(self, *args, extra_args=None):
procs = []
inputs = File.resolve(args)
for i in inputs:
path = BUILD / i.path.parent / self.mkout(i.path)
procs.append(Process(path, self, [i], extra_args))
return procs
def find_compiler(compilers):
for c in compilers:
cc = find_program(c, required=False)
if cc:
return cc
return None
c_compiler = find_compiler(["cc", "gcc", "clang", "clang.exe", "clang-cl.exe", "cl.exe"])
cc = Vec(
"cc",
c_compiler,
lambda o, i, ea: "-o %s -c %s %s" % (o, i, ea),
lambda i: i.name + ".o",
desc=lambda o, i, ea: "CC %s" % (o),
)
ld = Machine(
"ld",
c_compiler,
lambda o, i, ea: "-o %s %s %s" % (o, i, ea),
desc=lambda o, i, ea: "LD %s" % (o)
)
def subdir(dir):
# Save the previous location
PWD = Path.cwd()
# Go into subdir
sub = PWD / dir
os.chdir(sub)
# Set helper path
CWD = Path.cwd().relative_to(ROOT)
exec(open('.build').read())
# Revert the current directory;
os.chdir(PWD)
subdir(".")
def gen_bld():
for proc in File.fdb.values():
if not issubclass(type(proc), Process):
continue
proc.path.parent.mkdir(parents=True, exist_ok=True)
def gen_makefile():
out = ""
for mach in Machine.mdb.values():
out += mach.gen_makefile()
out += "\n"
all = []
for proc in File.fdb.values():
if not issubclass(type(proc), Process):
continue
out += "\n"
out += proc.gen_makefile()
out += "\n"
all.append(str(proc))
all = " ".join(all)
out += "\n.PHONY: clean\nclean:\n\trm -f %s" % (all)
return out
def gen_ninja():
out = ""
for mach in Machine.mdb.values():
out += mach.gen_ninja()
out += "\n"
out += "\n"
for proc in File.fdb.values():
if not issubclass(type(proc), Process):
continue
out += proc.gen_ninja()
out += "\n"
return out
# Generate everything
gen_bld()
open(ROOT / BUILD / ".gitignore", "w").write("*")
open(ROOT / 'build.ninja', 'w').write(gen_ninja())
|