-
Notifications
You must be signed in to change notification settings - Fork 9
/
kninja.py
executable file
·368 lines (287 loc) · 11.6 KB
/
kninja.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python3
import argparse
import logging
import os
import re
import shlex
import subprocess
import fnmatch
import sys
import re
import ninja_syntax
import ninja_internal
# These objects either get modified in the final link stage (which is opaque to
# ninja) or generate files that are later used in some rules which are not
# currently handled by ninja. We ignore these to ensure that interchangably
# running make and ninja without any changes to the tree does not cause any
# rebuilds. Use "make V=2" and "ninja -d explain -v" to debug this.
IGNORES = [
'init/version.o',
'lib/gen_crc32table',
'vmlinux.o',
'usr/gen_init_cpio',
]
WILDCARD_IGNORES = [
'arch/arm/boot/*',
'arch/*/vdso/*',
'arch/x86/boot/*',
'arch/x86/entry/vdso/*',
'arch/x86/realmode/*',
'arch/x86/tools/*',
'*/lib-ksyms.o',
# These depend on include/generated/utsrelease.h which make changes
'*.mod.o',
'scripts/*',
]
IGNORE_UNHANDLED = [
'System.map',
'Module.symvers',
'arch/x86/boot/voffset.h',
'usr/.initramfs_data.cpio.d',
'.version',
]
class Kninja(object):
def __init__(self):
pass
def fixname(self, name):
return name.replace('/', '_')
def fixup_obj(self, obj):
if obj in IGNORES:
logging.debug('Ignoring %s', obj)
return None
for ign in WILDCARD_IGNORES:
if fnmatch.fnmatch(obj, ign):
logging.debug('Ignoring %s', obj)
return None
# ARM oprofile objects have .. in their paths
if '..' in obj:
return os.path.relpath(obj)
return obj
def _parse_tokens(self, line):
tokens = []
generated = []
for token in re.split('\s', line):
if not token:
continue
if token.endswith(':') and line.startswith(token) and len(line.split(' ')) > 1:
generated.append(token.rstrip(':'))
continue
if not re.match('^[-_./A-Za-z0-9]+$', token):
continue
if token[0] == '/' or \
token[0] == '-' or \
token.endswith('..') or \
token.startswith('CONFIG_') or \
token.startswith('cmd_') or \
token.startswith('deps_') or \
token.endswith('.cmd') or \
token.endswith('modules.order') or \
token.endswith('.conf') or \
any(t for t in IGNORE_UNHANDLED if token.endswith(t)) or \
len(token) < 4:
continue
tokens.append(token)
return tokens, generated
def convert(self, makedb):
gotvmlinux = False
rulenames = []
builds = []
rules = []
alldeps = []
cmds = []
srctree = ''
objtree = ''
tokens = set()
generated = set()
handledfiles = set(['vmlinux'])
rules.append({'name': 'KNINJA',
'command': ' '.join(sys.argv),
'generator': 1,
'pool': 'console'})
for line in makedb:
newtokens, newgenerated = self._parse_tokens(line)
tokens.update(newtokens)
generated.update(newgenerated)
if not gotvmlinux and line.startswith('vmlinux: '):
deps = line.rstrip().replace('vmlinux: ', '').split(' ')
deps = [d for d in deps
if d not in ('autoksyms_recursive', 'vmlinux_prereq', 'FORCE')]
# This makes make ignore the dependencies for vmlinux, since
# those are already taken care of by ninja by the time this
# gets run.
makeall = r"cat %s | sed -e '/^$(vmlinux-dirs)/,+1d' " \
r"| make -f - all"
makefile = 'Makefile'
if srctree and objtree:
makefile = os.path.join(srctree, makefile)
makeall += ' -C%s O=%s' % (srctree, objtree)
makeall = makeall % makefile
logging.debug('vmlinux make command: %s', makeall)
rules.append({'name': 'cmd_vmlinux',
'command': makeall.replace('$', '$$'),
'pool': 'console'})
builds.append({'outputs': 'vmlinux',
'rule': 'cmd_vmlinux',
'inputs': deps})
cmds.append(('vmlinux', os.stat('vmlinux').st_mtime_ns, makeall))
gotvmlinux = True
continue
elif line.startswith('KBUILD_SRC = '):
srctree = line.rstrip().replace('KBUILD_SRC =', '').rstrip()
continue
elif line.startswith('O = '):
objtree = line.rstrip().replace('O = ', '')
continue
# built-in.o and other mod/obj files which just combine obj files
if ('.o: ' in line or '.ko: ' in line or '.a:' in line) \
and 'FORCE' in line \
and '%' not in line \
and '.h' not in line \
and '.S' not in line \
and '.c' not in line \
and 'objcopy.o' not in line: # lkdtm_ro_objcopy.o
obj, deps = line.rstrip().split(': ')
obj = self.fixup_obj(obj)
if not obj:
continue
deps = [d for d in deps.split(' ') if d != 'FORCE' and d not in IGNORES]
handledfiles.update(deps)
fixed = self.fixname(obj)
builds.append({'outputs': obj,
'rule': 'cmd_' + fixed,
'inputs': deps})
continue
try:
var, val = line.rstrip('\n').split(' := ')
except ValueError:
continue
if var.startswith('cmd_'):
obj = var.replace('cmd_', '', 1)
if obj in ('files', 'vmlinux'):
continue
obj = self.fixup_obj(obj)
if not obj:
continue
cmdname = 'cmd_' + self.fixname(obj)
args = shlex.split(val)
md = [arg for arg in args if '-MD' in arg]
if md:
depfile = md[0].split('-MD,')[1]
deps = 'gcc'
else:
depfile = None
deps = None
if cmdname in rulenames:
logging.debug('Ignoring duplicate rule %s', var)
continue
cmd = ' '.join(val.split())
rulenames.append(cmdname)
rules.append({'name': cmdname,
'command': cmd,
'deps': deps,
'depfile': depfile})
if deps:
handledfiles.update(deps)
cmds.append((obj, os.stat(obj).st_mtime_ns, cmd))
elif var.startswith('deps_'):
obj = var.replace('deps_', '', 1)
obj = self.fixup_obj(obj)
if not obj:
continue
try:
mtime = os.stat(obj).st_mtime_ns
except OSError:
continue
val = re.sub(r'\$\(subst[^)]+\)', '', val)
val = re.sub(r'\$\(wildcard[^)]+\)', '', val)
deps = [p for p in val.split(' ')
if p and not p.startswith('include/config/')]
handledfiles.update(deps)
alldeps.append((obj, mtime, deps))
elif var.startswith('source_'):
obj = var.replace('source_', '', 1)
obj = self.fixup_obj(obj)
if not obj:
continue
name = self.fixname(obj)
builds.append({'outputs': obj,
'rule': 'cmd_' + name,
'inputs': val.split(' ')})
handledfiles.update(val.split(' '))
logging.info('Finding unhandled files (%d tokens)', len(tokens))
tokens = set([os.path.realpath(t) for t in tokens])
tokens.difference_update([os.path.realpath(f) for f in generated])
tokens.difference_update([os.path.realpath(f) for f in handledfiles])
if objtree:
# Every make invocation results in a regeneration of the Makefile
# in the objdir
tokens.remove(os.path.realpath('Makefile'))
unhandledfiles = sorted([f for f in tokens if os.path.isfile(f)])
builds.append({'outputs': 'build.ninja',
'rule': 'KNINJA',
'inputs': unhandledfiles})
return rules, builds, alldeps, cmds
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--cache', action='store_true')
parser.add_argument('--verbose', '-v', default=0, action='count')
parser.add_argument('--path', default='')
parser.add_argument('makeargs', nargs='*')
args = parser.parse_args()
if args.verbose >= 1:
level = logging.DEBUG
else:
level = logging.INFO
logging.basicConfig(format='[%(levelname)s] %(message)s', level=level)
makedb = []
cachefile = os.path.join(args.path, '.makedb')
if args.cache:
if os.path.exists(cachefile):
logging.info('Using cached make database %s', cachefile)
with open(cachefile, 'r') as f:
makedb = f.readlines()
if not makedb:
makeargs = ['-C', args.path] if args.path else []
makeargs.extend(args.makeargs)
cmd = ['make', '-j', '%d' % os.cpu_count()] + makeargs
logging.info('Ensuring full build: %s', ' '.join(cmd))
subprocess.check_call(cmd)
cmd = ['make', '-p'] + makeargs
logging.info('Generating make database: %s', ' '.join(cmd))
out = subprocess.check_output(cmd).decode('utf-8')
makedb = [l for l in out.splitlines() if l and l[0] != '#']
logging.info('Caching make database to %s', cachefile)
with open(cachefile, 'w+') as f:
f.write('\n'.join(makedb))
kn = Kninja()
logging.info('Parsing make database (%d lines)', len(makedb))
rules, builds, alldeps, cmds = kn.convert(makedb)
ninjafile = os.path.join(args.path, 'build.ninja')
with open(ninjafile, 'w+') as f:
w = ninja_syntax.Writer(output=f)
for rule in rules:
w.rule(**rule)
for build in builds:
w.build(**build)
logging.info('Wrote build.ninja (%d rules, %d build statements)',
len(rules), len(builds))
depsfile = os.path.join(args.path, '.ninja_deps')
with open(depsfile, 'wb') as f:
ninja_internal.write_deps(f, alldeps)
logging.info('Wrote .ninja_deps (%d targets, %d deps)',
len(alldeps), sum([len(d) for _, _, d in alldeps]))
logfile = os.path.join(args.path, '.ninja_log')
with open(logfile, 'w') as f:
ninja_internal.write_log(f, cmds)
logging.info('Wrote .ninja_log (%d commands)', len(cmds))
cmd = ['ninja', '-d', 'explain', '-n']
logging.info('Checking ninja status: %s', ' '.join(cmd))
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT).decode('utf-8')
if 'no work' in out:
logging.info('All OK')
else:
logging.error('%s\n...', '\n'.join(out.splitlines()[:25]))
logging.error('ninja should be clean, but has work!')
sys.exit(1)
if __name__ == '__main__':
main()