-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.ninja
521 lines (442 loc) · 13.5 KB
/
common.ninja
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# Common rules and variables for the TM4C LLVM Toolchain.
# Picking a higher version than is probably necessary to be safe.
# 1.6 for rule scoping.
#
# We've switched to using our own fork of ninja so this is kind of moot now.
# 1.9+ for compdb (it'll be in 1.10).
# 1.9+ for subninja w/chdir (PR not merged; we use a patch _on top of_ it).
ninja_required_version = 1.9
# Everything here is 'common' amongst all projects that refer to this folder as
# their common folder.
#
# In other words, this file is the right place to specify things we want to
# keep in sync between all the modules and projects that can interface with
# each other.
#
# build.ninja files should `include` this file (not `subninja` it - this is so
# that we can specify default values here; when `subninja`-ing, the parent's
# variables are lowest in the lookup order which means anything we specify here
# will have precedence over the parent's variables) and should do so _before_
# they set their own variables (with the except of common_dir). Setting
# variables after ensures that we can specify _defaults_ here.
#
# Though we could have build.ninja files set variables that we use in setting
# our variables below (by including this file _after_ setting variables), we're
# going to choose to limit ourselves to only being able to override variables
# that are in rules; this should be clearer and easier to maintain. As an added
# bonus, this lets the section below double as a listing of all the variables
# build.ninja files can override.
#
# Note: include exports rules (and everything) to the parent, subninja only
# exposes build targets. This is really _the_ reason why we're using include for
# common.ninja.
# Variables:
# Not all of these are actually initialized here but they are still listed for
# documentation purposes. This should contain all the variables that
# build.ninja files downstream can set to influence the behavior of rules from
# this file.
# 🖥️ | 🐋
mode = 🖥️ $
# Tools:
# Note: cc is used for .S files too (assembly w/preprocessor)
cc = clang
as = clang
cxx = clang++
ar = llvm-ar
ld = ld.lld
opt = opt
objcopy = llvm-objcopy
size = llvm-size
openocd = openocd
nm = llvm-nm
grep = grep
numfmt = numfmt
git = git
cargo = cargo
gdb = gdb-multiarch
lldb = lldb
screen = screen
dot = dot
fmt = clang-format
tidy = clang-tidy
jq = jq
run-clang-format = run-clang-format
clang-apply-replacements = clang-apply-replacements
# Not currently used
run-clang-tidy = run-clang-tidy
builddir = target
docker_prefix =
docker_mounts =
docker_flags =
docker_cntnr =
linker_script = "${common_dir}/misc/tm4c.ld"
gdb_script = "${common_dir}/misc/gdb-script"
lldb_script = "${common_dir}/misc/lldb-script"
# This has to end with an extension clang-format understands.
unused_file = .c
# gen.sh should specify these explicitly, but here are defaults anyways:
c_opt_level = 0
lto_opt_level = 0
# Shared flags between the assembler and C/C++ compilers:
# Note that debug symbols are always included!
c_flags = $
--target=thumbv7em-unknown-none-eabi $
-mthumb $
-mcpu=cortex-m4 $
-mfpu=fpv4-sp-d16 $
-mfloat-abi=hard $
-flto $
-g
cc_flags = $c_flags $
-ffunction-sections $
-fdata-sections $
-fno-exceptions $
-fcolor-diagnostics $
-std=c99 $
-Dgcc $
-DPART_LM4F120H5QR $
-DTARGET_IS_BLIZZARD_RA1
# TODO: verify
as_flags = $c_flags
# TODO: verify
cxx_flags = $c_flags $
-ffunction-sections $
-fdata-sections $
-fno-exceptions $
-fcolor-diagnostics $
-std=c++17 $
-Dgcc $
-DPART_LM4F120H5QR $
-DTARGET_IS_BLIZZARD_RA1
ar_flags =
ld_flags = $
--color-diagnostics $
--gc-sections
objcopy_flags = $
-O binary
cargo_flags =
size_flags = $
--color
# Note: this file is identical between the TM4C and the LM4F
openocd_flags = $
-c "source [find board/ek-tm4c123gxl.cfg]"
gdb_flags =
lldb_flags =
fmt_flags = $
--style=file $
--fallback-style=Google
fmt_fix_flags = $
-i
tidy_flags = $
--checks=bugprone*,modernize* $
--format-style=file $
--system-headers
tidy_fix_flags = $
--fix $
--fix-errors
clang_apply_replacements_flags = $
--style=file $
--format
nm_flags = $
--demangle $
--color $
--no-weak $
--radix=d
grep_colors = mt=1;33;39
grep_flags = $
--color
numfmt_flags = $
--to=iec-i $
--suffix=B
header_search_dirs =
libs = $
-lclang_rt.builtins $
-lc $
-lm $
-lnosys $
-lclang_rt.builtins $
ld_search_dirs =
compiler_rt_dir = /usr/arm-compiler-rt/lib/armv7e-m/fpu
newlib_dir = /usr/newlib-nano/arm-none-eabi/lib
ninja_browse_flags =
ninja_graph_target =
# Arguments/flags passed to gen.sh; used when regenerating build.ninja.
gen_vars =
gen_args =
# Rules:
# .d (dependency files) will be put in the same directory as the object files,
# however since they'll be deleted right away (`deps = gcc`) this shouldn't be
# too much of an annoyance.
# C Compiler
rule cc
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $cc $
$cc_flags $
-O${c_opt_level} $
-MMD -MT $out -MF $out.d $
-I${newlib_dir}/../include $
$header_search_dirs $
-c $in $
-o $out
description = $mode CC $in → $out
depfile = $out.d
deps = gcc
# Assembler without the preprocessor (.s files)
rule as
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $as $
$as_flags $
-O${c_opt_level} $
-c $in $
-o $out
description = $mode AS $in → $out
# Assembler *with* the preprocessor (.S files)
rule asp
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $cc $
$cc_flags $
-O${c_opt_level} $
-MMD -MT $out -MF $out.d $
-I${newlib_dir}/../include $
$header_search_dirs $
-c $in $
-o $out
description = $mode AS (w/preprocessor) $in → $out
depfile = $out.d
deps = gcc
rule cxx
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $cxx $
$cxx_flags $
-O${c_opt_level} $
-MMD -MT $out -MF $out.d $
-I${newlib_dir}/../include $
$header_search_dirs $
-c $in $
-o $out
description = $mode CXX $in → $out
depfile = $out.d
deps = gcc
rule link
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $ld $
-T ${linker_script} $
$in $
${ld_flags} $
--lto-O${lto_opt_level} $
${libs} $
${ld_search_dirs} $
-L ${compiler_rt_dir} $
-L ${newlib_dir} $
-o $out
description = $mode LINK $in → $out
rule ar
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $ar $
$ar_flags $
rcs $in $out
description = $mode AR $in → $out
rule objcopy
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $objcopy $
$objcopy_flags $
$in $out
description = $mode OBJCOPY $in → $out
rule cargo
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr bash -c " $
export RUSTFLAGS='-Clinker-plugin-lto'; $
$cargo $
build $
--color always $
$cargo_flags $
$cargo_extra_flags $
--manifest-path $in $
--package $package $
--lib $
--target thumbv7em-none-eabihf $
--target-dir $builddir/cargo/$package $
&& { $
for f in $builddir/cargo/$package/thumbv7em-none-eabihf/${profile}/deps/lib${package}*; do $
[[ \$$f -nt \$$newest ]] && newest=\$$f; $
done; $
cp \$$newest $out; $
} $
"
description = $mode [🦀] CARGO $package → $out
depfile = $builddir/cargo/$package/thumbv7em-none-eabihf/${profile}/lib${package}.d
deps = gcc
rule sections
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $size $
$size_flags $
$in
description = $mode SECTIONS $in
pool = console
# nm and numfmt will run inside the container, cut and grep will run outside.
rule size
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $nm $
$nm_flags $
--print-size $
--size-sort $
$in $
| grep -v " U " $
| $docker_prefix $docker_mounts $docker_flags $docker_cntnr $numfmt $
$numfmt_flags $
--field 2 $
--format '-%10f' $
| cut -d- -f2- $
| GREP_COLORS="$grep_colors" grep $
$grep_flags $
"^[[:space:]]*[0-9.iKMGT]*B"
description = $mode SIZE $in
pool = console
rule flash
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $openocd $
$openocd_flags $
-c "init" $
-c "halt" $
-c "reset init" $
-c "sleep 100" $
-c "flash probe 0" $
-c "flash write_image erase ${in}" $
-c "sleep 100" $
-c "verify_image ${in}" $
-c "halt" $
-c "shutdown"
description = $mode OPENOCD (flash) $in
rule flash-lm4flash
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $
$lm4flash $
-v $
$in
description = $mode LM4FLASH $in
rule start
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $openocd $
$openocd_flags $
-c "init" $
-c "halt" $
-c "reset init" $
-c "resume; shutdown"
description = $mode OPENOCD (start) $in
rule gdb
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $gdb $
$gdb_flags $
-x $gdb_script $
$in
description = $mode GDB $in
pool = console
rule lldb
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $
$lldb_script $
$openocd $
$lldb $
$gdb $
$in $
$builddir $
$openocd_flags
description = $mode LLDB $in
pool = console
# TODO:
#rule uart
rule rm
command = rm -rf $in/*
description = RM $in
rule format
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $fmt $
$fmt_flags $
$fmt_fix_flags $
$in
description = $mode FMT $in
rule format_check
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $
$run-clang-format $
--clang-format-executable $fmt $
--color always $
$in $
--pass $fmt_flags
description = $mode FMT (check) $in
pool = console
# Requires compdb to be generated.
# (this could switch to run-clang-tidy to do away with $in)
rule tidy
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $tidy $
$tidy_fix_flags $
$tidy_flags $
$in
description = $mode TIDY $in
pool = console
# Requires compdb to be generated.
# (this could switch to run-clang-tidy to do away with $in)
rule tidy_check
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $tidy $
$tidy_flags $
$in
description = $mode TIDY (check) $in
pool = console
# Requires compdb to be generated.
# (not currently used)
rule apply_replacements
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $
$clang-apply-replacements $
$clang_apply_replacements_flags $
$in
description = $mode TIDY + FMT
rule touch
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr touch $
$out
description = $mode TOUCH $out
rule browse
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr ninja $
-t browse $ninja_browse_flags
description = $mode NINJA -t browse
pool = console
# Intentionally runs ninja in container (if we're running in a container) and
# dot outside; the container doesn't ship with graphviz (+ ~30MB) so this will
# only work with native and hybrid configurations (or if you use a container
# that has graphviz!).
#
# Update: this is no longer true, but we intentionally stick the generated png
# file outside the container.
rule graph
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr ninja $
-t graph $ninja_graph_target $
| $docker_prefix $docker_mounts $docker_flags $docker_cntnr $dot $
-Tpng > $out
description = $mode NINJA + DOT > $out
# Ninja's compdb generator uses the first file in $in as the "file" key in the
# compilation database. This is problematic for us since certain files will show
# up multiple times (i.e. for targets like format and tidy). The multiple
# matching "file" keys for source code files confuse clang and friends, so we're
# going to make a fake file that isn't actually used in the project solely so
# that we can pass said file into clang-format and clang-tidy and its friends
# and have that file be the "file" key for such commands.
#
# We're also going to purge all entries with this file as the key from the
# generated compdb.
build $builddir/$unused_file: touch
# Once again, this intentionally redirects to a file outside the container. If
# mounted volumes are configured correctly there shouldn't be a real difference
# and this saves us from starting up another bash instance.
#
# The extra jq filtering here purges $builddir/$unused_file from compilation
# database (see above).
rule compdb
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr ninja $
-t compdb $
| $docker_prefix $docker_mounts $docker_flags $docker_cntnr $jq $
'[.[] | select(.file != "$builddir/$unused_file")]' > $out
description = $mode NINJA -t compdb > $out
# Remove build artifacts.
rule scrub
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr ninja $
-t clean
description = $mode NINJA -t clean
# Remove all gitignore'd files.
rule powerwash
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $git $
clean -ndiX
description = $mode GIT clean -X
# Remove everything that isn't tracked by git (gitignore'd and untracked files).
rule pristine
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr $git $
clean -ndix
description = $mode GIT clean -x
rule regenerate
command = $docker_prefix $docker_mounts $docker_flags $docker_cntnr bash -c "$
$gen_vars $
${common_dir}/misc/gen.sh $
$gen_args"
description = $mode GEN
generator = 1