Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
610 lines (524 loc) · 26.8 KB
/
Copy pathMakefile
File metadata and controls
610 lines (524 loc) · 26.8 KB
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# -fwrapv is required, not probed: hashmap_hash() carries the FNV-1a
# accumulator in a signed int, because shecc has no 'unsigned' to compile
# itself with, and the multiply there overflows by design.
CFLAGS := -O2 -g \
-std=c99 -pedantic -fwrapv
# Every -Wno- that used to sit here has been earned away rather than renewed:
# the tree is clean under gcc and clang with nothing switched off, so a warning
# that appears from now on is about the code and not about the flag list.
CFLAGS_TO_CHECK := \
-Wall -Wextra \
-Wshadow
SUPPORTED_CFLAGS :=
# Check if a specific compiler flag is supported, attempting a dummy compilation
# with flags. If successful, it returns the flag string; otherwise, it returns
# an empty string.
# Usage: $(call check_flag, -some-flag)
check_flag = $(shell $(CC) $(1) -S -o /dev/null -xc /dev/null 2>/dev/null; \
if test $$? -eq 0; then echo "$(1)"; fi)
# Iterate through the list of all potential flags, effectively filtering out all
# unsupported flags. Half a second of $(CC) probing that the style and hook
# targets have no use for, so skip it when nothing is being compiled.
STYLE_GOALS := check-style check-newline check-comments check-format check-shell \
indent install-hooks uninstall-hooks check-hooks
VIR_FRONTEND_FIXTURES := $(sort $(wildcard tests/vir-frontend-*.c))
VIR_NATIVE_SHARED_FIXTURES := $(sort $(wildcard tests/vir-direct-global-*.c) \
$(wildcard tests/vir-direct-branch-*.c) \
$(wildcard tests/vir-direct-narrow-global.c) \
tests/vir-direct-external-pointer-origin.c \
tests/vir-direct-external-typed-pointer.c \
tests/vir-direct-external-typed-pointer-offset.c)
VIR_NATIVE_X64_FIXTURES := $(sort $(wildcard tests/vir-direct-x64*.c) \
$(VIR_NATIVE_SHARED_FIXTURES))
VIR_NATIVE_ARM64_FIXTURES := $(sort $(wildcard tests/vir-direct-arm64*.c) \
$(VIR_NATIVE_SHARED_FIXTURES))
ifneq ($(filter-out $(STYLE_GOALS),$(or $(MAKECMDGOALS),all)),)
$(foreach flag, $(CFLAGS_TO_CHECK), $(eval CFLAGS += $(call check_flag, $(flag))))
endif
BUILD_SESSION := .session.mk
-include $(BUILD_SESSION)
STAGE0 := shecc
STAGE1 := shecc-stage1.elf
STAGE2 := shecc-stage2.elf
USE_QEMU ?= 1
OUT ?= out
# Every architecture that can be selected as a build target. The first is the
# default when ARCH is not given.
ARCHS = arm arm64 riscv x64
SRCDIR := src
SRCFILES := $(shell find $(SRCDIR) -name '*.[ch]')
LIBDIR := $(shell find lib -type d)
BUILTIN_LIBC_SOURCE ?= c.c
BUILTIN_LIBC_HEADER := c.h
# The translation timestamp belongs to generated configuration rather than the
# compiler's host clock. A single value is consequently embedded in stages 0,
# 1, and 2, which keeps bootstrap byte-for-byte reproducible. Rebuilders can
# supply SOURCE_DATE_EPOCH for a stable timestamp across separate invocations.
# The fallback is read once: a recursive "?=" would rerun date for the date
# and again for the time, which then disagree across a second boundary.
ifeq ($(origin SOURCE_DATE_EPOCH),undefined)
SOURCE_DATE_EPOCH := $(shell date -u +%s)
endif
# The epoch is spliced into the date command below, so anything but decimal
# digits would be run by the shell there. $(value) keeps make from expanding a
# supplied $(...) before it is checked.
EPOCH_NONDIGITS := $(value SOURCE_DATE_EPOCH)
$(foreach d,0 1 2 3 4 5 6 7 8 9,$(eval EPOCH_NONDIGITS := $$(subst $(d),,$$(EPOCH_NONDIGITS))))
ifneq ($(if $(strip $(value SOURCE_DATE_EPOCH)),$(EPOCH_NONDIGITS),empty),)
$(error SOURCE_DATE_EPOCH must be a Unix epoch in decimal seconds)
endif
# GNU date converts an epoch given as -d @SECONDS, which BSD and macOS date do
# not accept; they take the seconds as -r SECONDS instead. Ask for the Unix
# epoch itself to learn which spelling this date understands.
ifeq ($(shell date -u -d @0 +%s 2>/dev/null),0)
EPOCH_DATE = LC_ALL=C TZ=UTC date -u -d "@$(SOURCE_DATE_EPOCH)"
else
EPOCH_DATE = LC_ALL=C TZ=UTC date -u -r "$(SOURCE_DATE_EPOCH)"
endif
TRANSLATION_DATE := $(shell $(EPOCH_DATE) '+%b %e %Y')
TRANSLATION_TIME := $(shell $(EPOCH_DATE) '+%H:%M:%S')
ifeq ($(strip $(TRANSLATION_DATE)$(TRANSLATION_TIME)),)
$(error SOURCE_DATE_EPOCH must be a Unix epoch accepted by date)
endif
TRANSLATION_DEFS = "\#define SHECC_TRANSLATION_DATE \"$(TRANSLATION_DATE)\"\n\#define SHECC_TRANSLATION_TIME \"$(TRANSLATION_TIME)\"\n"
# --dump-ir is what makes out/shecc-stage1.log the IR of the stage 1 build
# rather than an empty file. It is the only thing in the tree that exercises
# vir_print(), and it is what a failed CI run uploads.
STAGE0_FLAGS ?= --dump-ir
STAGE1_FLAGS ?=
# - DYNLINK: 0 -> static linking, 1 -> dynamic linking
# - BINDING: (undefined), "lazy" -> lazy binding, "now" -> immediate binding
# If DYNLINK is 0 but BINDING is set to "lazy" or "now",
# the subsequent ifneq/ifeq checks will halt execution because
# the binding mode does not apply to static linking.
#
# By default, DYNLINK is set to 0 and BINDING is undefined.
DYNLINK ?= 0
COMMENTFLOW ?= commentflow
SHFMT ?= shfmt
ifeq ($(DYNLINK),1)
# If dynamic linking is enabled, lazy binding is used by default.
BINDING ?= lazy
STAGE0_FLAGS += --dynlink -z $(BINDING)
STAGE1_FLAGS += --dynlink -z $(BINDING)
# Accepted cases:
# $ make DYNLINK=1
# $ make DYNLINK=1 BINDING=now
# $ make DYNLINK=1 BINDING=lazy
#
# Rejected cases:
# $ make DYNLINK=1 BINDING= # BINDING is empty
# $ make DYNLINK=1 BINDING="lazy now" # Multiple words
# $ make DYNLINK=1 BINDING=n # Neither lazy nor now
ifneq (1,$(words $(BINDING)))
$(error BINDING should contain exactly one word)
endif
ifeq (,$(filter lazy now,$(BINDING)))
$(error BINDING is neither "lazy" nor "now")
endif
else
ifneq ($(DYNLINK),0)
# Reject the build if DYNLINK is neither 0 nor 1.
$(error DYNLINK should be 0 or 1 (static linking or dynamic linking))
endif
# If BINDING is specified on the command line or via an environment variable
# (e.g., "make ... BINDING=...", "BINDING=... make ..."),
# this variable must be empty in the static linking mode.
ifneq (,$(filter command line environment,$(origin BINDING)))
ifneq (,$(BINDING))
$(error BINDING is not used for static linking)
endif
endif
endif
SRCS := $(SRCDIR)/main.c
OBJS := $(SRCS:%.c=$(OUT)/%.o)
# The sanitizer build keeps its objects apart from the normal one. Sharing them
# lets whichever ran last decide what the other links: a plain "make" after it
# fails outright on the missing runtime, and -- quietly, which is worse --
# "make sanitizer" after a plain build relinks an uninstrumented object, so
# check-sanitizer then passes having checked nothing.
SAN_OUT := $(OUT)/sanitize
SAN_OBJS := $(SRCS:%.c=$(SAN_OUT)/%.o)
deps := $(OBJS:%.o=%.o.d) $(SAN_OBJS:%.o=%.o.d)
all: config bootstrap
# Both goals carry the flags, because a target-specific variable reaches only
# that target and its prerequisites: "make check-sanitizer" on its own would
# otherwise build $(SAN_OBJS) with the ordinary CFLAGS and run the suite against
# a binary that instruments nothing.
SAN_CFLAGS := -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer -O0
SAN_LDFLAGS := -fsanitize=address -fsanitize=undefined
sanitizer check-sanitizer: CFLAGS += $(SAN_CFLAGS)
sanitizer check-sanitizer: LDFLAGS += $(SAN_LDFLAGS)
sanitizer: config $(OUT)/$(STAGE0)-sanitizer
$(VECHO) " Built stage 0 compiler with sanitizers\n"
# If ARCH is undefined, the default value is set to the first item in ARCHS.
ifeq ($(ARCH),)
ARCH := $(firstword $(ARCHS))
endif
ifeq (,$(filter $(ARCH),$(ARCHS)))
$(error Unsupported ARCH "$(ARCH)". Select one of: $(ARCHS))
endif
# The config target writes the following files:
# - "config": contains multiple macro definitions including the target
# architecture. Note that compiler sources need to include
# this file.
# - ".session.mk": is a build session file to record the build configurations.
# (ARCH/DYNLINK/BINDING)
#
# The build system uses the build session to detect configuration switches
# and to perform a rebuild or reconfiguration if necessary.
NEED_RECONFIGURE := $(if $(wildcard config),0,1)
CONFIGURED_ARCH := $(shell sed -n 's/^ARCH?=//p' $(BUILD_SESSION) 2>/dev/null)
CONFIGURED_DYNLINK := $(shell sed -n 's/^DYNLINK?=//p' $(BUILD_SESSION) 2>/dev/null)
CONFIGURED_BINDING := $(shell sed -n 's/^BINDING?=//p' $(BUILD_SESSION) 2>/dev/null)
# The first build must trigger a configuration. The snippets below set the
# NEED_RECONFIGURE variable if one of the following cases is met
# - Case 1: switch the target architecture.
# - Case 2: switch the linking mode.
# - Case 3: switch the binding mode (if dynamic linking is enabled).
#
# If NEED_RECONFIGURE is set, this means that a reconfiguration
# is needed to run.
ifneq ($(CONFIGURED_ARCH),$(ARCH))
NEED_RECONFIGURE := 1
endif
ifneq ($(CONFIGURED_DYNLINK),$(DYNLINK))
NEED_RECONFIGURE := 1
endif
ifneq ($(CONFIGURED_BINDING),$(BINDING))
# The cases that specify BINDING when DYNLINK is 0 have already been
# handled and rejected in the snippets above, so the static linking
# case does not need to be considered here.
#
# Directly set up the flag when changing the binding mode in the
# dynamic linking case.
NEED_RECONFIGURE := 1
endif
# The following two variables are used by the config target to output
# hint messages.
HINT_LINKING_MODE := static
ifeq ($(DYNLINK),1)
HINT_LINKING_MODE := dynamic
HINT_BINDING_MODE := lazy
ifeq ($(BINDING),now)
HINT_BINDING_MODE := immediate
endif
endif
include mk/$(ARCH).mk
include mk/common.mk
ARCH_DEFS_SHA256_FILE := archdefs.sha256
SHA256_CMD := $(if $(shell command -v sha256sum),sha256sum,$(if $(shell command -v shasum),shasum -a 256,))
ifeq (,$(SHA256_CMD))
$(error Unable to access a SHA-256 checksum command)
endif
ifneq (,$(wildcard archdefs.sha256))
ARCH_DEFS_CHANGED := $(shell $(PRINTF) $(ARCH_DEFS) | $(SHA256_CMD) | awk '{print $$1}' | diff - $(ARCH_DEFS_SHA256_FILE))
ifneq (,$(ARCH_DEFS_CHANGED))
NEED_RECONFIGURE := 1
endif
endif
# The existing "config" file may be removed by the distclean target, but the
# config target may still retain the configuration. Running both targets in
# the same invocation can trigger this situation.
#
# Therefore, the build system must force a reconfiguration when running a
# command such as "make distclean config ...".
ifneq (,$(filter distclean,$(MAKECMDGOALS)))
NEED_RECONFIGURE := 1
endif
# The build system uses the NEED_RECONFIGURE variable to determine whether
# the config target needs to be run. The initial build or configuration, or
# a configuration switch, can trigger this target to write the "config"
# file and the build session file.
.PHONY: config
config:
ifeq ($(NEED_RECONFIGURE),1)
$(Q)$(PRINTF) $(ARCH_DEFS) > $@
$(Q)$(PRINTF) $(TRANSLATION_DEFS) >> $@
$(Q)$(PRINTF) $(ARCH_DEFS) | $(SHA256_CMD) | awk '{print $$1}' > $(ARCH_DEFS_SHA256_FILE)
$(Q)$(PRINTF) "ARCH?=$(ARCH)\n" > $(BUILD_SESSION)
$(Q)$(PRINTF) "DYNLINK?=$(DYNLINK)\n" >> $(BUILD_SESSION)
ifeq ($(DYNLINK),0)
$(VECHO) "Configurations: (%s, %s linking)\n" $(ARCH) $(HINT_LINKING_MODE)
else
$(Q)$(PRINTF) "BINDING?=$(BINDING)\n" >> $(BUILD_SESSION)
$(VECHO) "Configurations: (%s, %s linking, %s binding)\n" $(ARCH) $(HINT_LINKING_MODE) $(HINT_BINDING_MODE)
endif
$(Q)$(CONFIG_CHECK_CMD)
else
$(Q)$(VECHO) "Keep configurations\n"
endif
.PHONY: $(STYLE_GOALS)
.PHONY: check-vir-frontend-stage0 check-vir-frontend-stage2 \
check-vir-stage0 check-vir-stage2 \
check-vir-frontend-policy-stage0 check-vir-frontend-policy-stage2 \
check-vir-opt-baseline
check: check-stage0 check-stage2 check-abi-stage0 check-abi-stage2 \
check-vir-core check-vir-essential check-vir-frontend-stage0 check-vir-frontend-stage2 check-vir-fuzz check-vir-stage0 check-vir-stage2 \
check-vir-multi-input-stage0 check-vir-multi-input-stage2 \
check-vir-opt-baseline \
check-vir-corpus-stage0 check-fuzz-stage0 \
check-vir-frontend-policy-stage0 check-vir-frontend-policy-stage2
# Random programs checked against the host compiler and built by shecc.
# FUZZ_SEEDS and FUZZ_START widen or move the run; a failing program is kept
# as out/fuzz-<seed>.c.
.PHONY: check-fuzz-stage0
check-fuzz-stage0: $(OUT)/$(STAGE0) tests/fuzz.sh tests/fuzz-gen.c
TARGET_EXEC="$(TARGET_EXEC)" CC="$(CC)" bash tests/fuzz.sh 0
# Optimization must preserve each corpus program's behavior under --no-opt.
# Only stage 0 is part of check: under emulation, stage 2 compiles the corpus
# twice inside qemu, which costs more than the other checks.
.PHONY: check-vir-corpus-stage0 check-vir-corpus-stage2
check-vir-corpus-stage0: $(OUT)/$(STAGE0) tests/vir-corpus.sh tests/vir-corpus-exclusions.txt tests/vir-runtime-workloads.txt
TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-corpus.sh 0 $(ARCH)
check-vir-corpus-stage2: $(OUT)/$(STAGE2) tests/vir-corpus.sh tests/vir-corpus-exclusions.txt tests/vir-runtime-workloads.txt
TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-corpus.sh 2 $(ARCH)
.PHONY: check-vir-multi-input-stage0 check-vir-multi-input-stage2
check-vir-multi-input-stage0: $(OUT)/$(STAGE0) tests/vir-multi-input.sh tests/vir-multi-input-root.c tests/vir-multi-input-main.c
TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-multi-input.sh 0
check-vir-multi-input-stage2: $(OUT)/$(STAGE2) tests/vir-multi-input.sh tests/vir-multi-input-root.c tests/vir-multi-input-main.c
TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-multi-input.sh 2
ifeq ($(ARCH),x64)
.PHONY: check-vir-x64-encoders check-vir-x64-and-flags
check: check-vir-x64-encoders check-vir-x64-and-flags
check-vir-x64-encoders check-vir-x64-and-flags: check-vir-x64-%: $(OUT)/$(STAGE0) tests/vir-x64-%.c config $(SRCFILES)
$(CC) $(CFLAGS) tests/vir-x64-$*.c -o $(OUT)/vir-x64-$*
$(OUT)/vir-x64-$*
.PHONY: check-vir-x64-fold
check: check-vir-x64-fold
check-vir-x64-fold: $(OUT)/$(STAGE0) tests/vir-x64-fold.c tests/vir-x64-liveout.c tests/vir-x64-liveout.sh tests/vir-x64-shift-counts.c config $(SRCFILES)
$(CC) $(CFLAGS) tests/vir-x64-fold.c -o $(OUT)/vir-x64-fold
$(OUT)/vir-x64-fold
CC="$(CC)" bash tests/vir-x64-liveout.sh $(OUT)/$(STAGE0)
.PHONY: check-vir-native-x64-stage0 check-vir-native-x64-stage2
check: check-vir-native-x64-stage0 check-vir-native-x64-stage2
check-vir-native-x64-stage0: $(OUT)/$(STAGE0) $(VIR_NATIVE_X64_FIXTURES) tests/vir-direct-x64.sh tests/vir-direct-common.sh
bash tests/vir-direct-x64.sh 0
check-vir-native-x64-stage2: $(OUT)/$(STAGE2) $(VIR_NATIVE_X64_FIXTURES) tests/vir-direct-x64.sh tests/vir-direct-common.sh
bash tests/vir-direct-x64.sh 2
# A foreign callee may leave garbage above a narrow return value, so the
# caller has to extend it. The callee is host assembly, preloaded at run time.
.PHONY: check-vir-call-return
check: check-vir-call-return
check-vir-call-return: $(OUT)/$(STAGE0) tests/vir-call-return.sh tests/vir-call-return.c
CC="$(CC)" bash tests/vir-call-return.sh $(OUT)/$(STAGE0)
endif
ifeq ($(ARCH),arm64)
.PHONY: check-vir-native-arm64-stage0 check-vir-native-arm64-stage2
check: check-vir-native-arm64-stage0 check-vir-native-arm64-stage2
check: check-vir-arm64-branch-frame check-vir-arm64-startup-bank
.PHONY: check-vir-arm64-branch-frame check-vir-arm64-startup-bank
check: check-vir-arm64-select
check: check-vir-arm64-register-memory
.PHONY: check-vir-arm64-register-memory
check-vir-arm64-register-memory: $(OUT)/$(STAGE0) tests/arm64-register-memory-unit.c tests/arm64-register-memory.sh
CC="$(CC)" bash tests/arm64-register-memory.sh
.PHONY: check-vir-arm64-select
check-vir-arm64-select: $(OUT)/$(STAGE0) tests/vir-arm64-select-direct.c tests/vir-arm64-logical-gas.py
$(CC) $(CFLAGS) -Isrc tests/vir-arm64-select-direct.c -o $(OUT)/vir-arm64-select-direct
python3 tests/vir-arm64-logical-gas.py $(OUT)/vir-arm64-select-direct
check-vir-arm64-branch-frame: $(OUT)/$(STAGE0) tests/vir-arm64-branch-frame.c
$(CC) $(CFLAGS) -Isrc tests/vir-arm64-branch-frame.c -o $(OUT)/vir-arm64-branch-frame
$(OUT)/vir-arm64-branch-frame
check-vir-arm64-startup-bank: $(OUT)/$(STAGE0) tests/vir-arm64-startup-bank.sh
CC="$(CC)" A64_CC="$(if $(filter 1,$(USE_QEMU)),$(or $(ARCH_CC),aarch64-linux-gnu-gcc),$(CC))" TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-arm64-startup-bank.sh
check-vir-native-arm64-stage0: $(OUT)/$(STAGE0) $(VIR_NATIVE_ARM64_FIXTURES) tests/vir-direct-arm64.sh tests/vir-direct-common.sh
TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-direct-arm64.sh 0
check-vir-native-arm64-stage2: $(OUT)/$(STAGE2) $(VIR_NATIVE_ARM64_FIXTURES) tests/vir-direct-arm64.sh tests/vir-direct-common.sh
TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-direct-arm64.sh 2
endif
# Run the complete check -- driver and ABI suites at stages 0 and 2 -- on every
# backend, as CI does. Driver cases that need 64-bit values are gated on the
# target's pointer width, so each target runs everything it can represent.
# Configuration is global to this worktree, so keep recursive invocations
# sequential and restore the caller's architecture even when a target fails.
# Each target is rebuilt after its own `config`, preventing an old compiler from
# being paired with a newly selected backend. This make exported the emulator
# for the caller's architecture, so each recursive make has to derive its own:
# an inherited qemu-arm would run the x64 binaries.
.PHONY: check-all-targets
check-all-targets:
$(Q)set -e; \
unset TARGET_EXEC; \
active_arch='$(or $(CONFIGURED_ARCH),$(ARCH))'; \
trap '$(MAKE) config ARCH="$$active_arch"' EXIT; \
for target_arch in $(ARCHS); do \
$(MAKE) config ARCH="$$target_arch"; \
$(MAKE) check ARCH="$$target_arch"; \
done
.PHONY: vir-opt-baseline
vir-opt-baseline: $(OUT)/$(STAGE0) tests/vir-opt-baseline.sh tests/benchmark-time.sh \
tests/vir-opt-baseline-workloads.txt
tests/vir-opt-baseline.sh 0
.PHONY: vir-licm-profile
VIR_LICM_PROFILE_SAMPLES ?= 7
VIR_LICM_PROFILE_REPEATS ?= 5
vir-licm-profile: $(OUT)/vir-licm-profile
$(OUT)/vir-licm-profile $(VIR_LICM_PROFILE_SAMPLES) $(VIR_LICM_PROFILE_REPEATS)
$(OUT)/vir-licm-profile: tests/vir-licm-profile.c src/optimizer/ir.c src/optimizer/ir.h
$(CC) $(CFLAGS) -Isrc tests/vir-licm-profile.c src/optimizer/ir.c -o $@
# One checker per target: they share nothing, so "make -j check-style" runs them
# concurrently and finishes in the time the slowest one takes.
check-style: check-newline check-comments check-format check-shell
check-newline:
$(Q).ci/check-newline.sh
check-comments:
$(Q)COMMENTFLOW=$(COMMENTFLOW) .ci/check-commentflow.sh
check-format:
$(Q).ci/check-format.sh
check-shell:
$(Q)SHFMT=$(SHFMT) .ci/check-shell.sh
check-hooks:
$(Q)scripts/test-git-hooks.sh
# Naming both goals would otherwise run each --write pass beside the checker
# reading the same files, so the rewrite goes first and the checkers then report
# on a tree that has stopped moving. Neither goal alone is affected.
ifneq ($(filter indent,$(MAKECMDGOALS)),)
check-newline check-comments check-format check-shell: | indent
endif
# The checkers own both halves: which files they cover and which tool rewrites
# them. Naming either one here again would only be a second place to update.
indent:
$(Q).ci/check-newline.sh --write
$(Q)SHFMT=$(SHFMT) .ci/check-shell.sh --write
$(Q)COMMENTFLOW=$(COMMENTFLOW) .ci/check-commentflow.sh --write
$(Q).ci/check-format.sh --write
install-hooks:
$(Q)scripts/install-git-hooks.sh
uninstall-hooks:
$(Q)scripts/install-git-hooks.sh --uninstall
check-stage0: $(OUT)/$(STAGE0) tests/driver.sh
$(VECHO) " TEST STAGE 0\n"
tests/driver.sh 0 $(DYNLINK) $(BINDING)
check-stage2: $(OUT)/$(STAGE2) tests/driver.sh
$(VECHO) " TEST STAGE 2\n"
tests/driver.sh 2 $(DYNLINK) $(BINDING)
check-sanitizer: $(OUT)/$(STAGE0)-sanitizer tests/driver.sh check-vir-sanitizer
$(VECHO) " TEST STAGE 0 (with sanitizers)\n"
SHECC_STAGE0="$(OUT)/$(STAGE0)-sanitizer" tests/driver.sh 0 $(DYNLINK) $(BINDING)
check-abi-stage0: $(OUT)/$(STAGE0)
tests/$(ARCH)-abi.sh 0 $(DYNLINK) $(BINDING);
check-abi-stage2: $(OUT)/$(STAGE2)
tests/$(ARCH)-abi.sh 2 $(DYNLINK) $(BINDING);
check-vir-core: tests/vir-core.c src/optimizer/ir.c src/optimizer/ir.h
$(CC) $(CFLAGS) -Isrc tests/vir-core.c src/optimizer/ir.c -o $(OUT)/vir-core
$(OUT)/vir-core
.PHONY: check-vir-essential
check-vir-essential: tests/vir-essential-core.c src/optimizer/ir.c src/optimizer/ir.h
$(CC) $(CFLAGS) -Isrc tests/vir-essential-core.c src/optimizer/ir.c -o $(OUT)/vir-essential-core
$(OUT)/vir-essential-core
check-vir-frontend-stage0: $(OUT)/$(STAGE0) tests/vir-frontend.sh tests/vir-frontend-workloads.txt $(VIR_FRONTEND_FIXTURES)
TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-frontend.sh 0
check-vir-frontend-stage2: $(OUT)/$(STAGE2) tests/vir-frontend.sh tests/vir-frontend-workloads.txt $(VIR_FRONTEND_FIXTURES)
TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-frontend.sh 2
check-vir-frontend-policy-stage0: $(OUT)/$(STAGE0) tests/vir-frontend-policy.sh tests/vir-frontend-gvn.c tests/vir-frontend-licm.c tests/vir-frontend-nested-loop.c tests/vir-frontend-multi-latch.c
TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-frontend-policy.sh 0
check-vir-frontend-policy-stage2: $(OUT)/$(STAGE2) tests/vir-frontend-policy.sh tests/vir-frontend-gvn.c tests/vir-frontend-licm.c tests/vir-frontend-nested-loop.c tests/vir-frontend-multi-latch.c
TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-frontend-policy.sh 2
check-vir-fuzz: tests/vir-fuzz.c src/optimizer/ir.c src/optimizer/ir.h
$(CC) $(CFLAGS) -Isrc tests/vir-fuzz.c src/optimizer/ir.c -o $(OUT)/vir-fuzz
$(OUT)/vir-fuzz
check-vir-sanitizer: tests/vir-core.c tests/vir-essential-core.c tests/vir-fuzz.c src/optimizer/ir.c src/optimizer/ir.h
$(CC) $(CFLAGS) $(SAN_CFLAGS) -Isrc tests/vir-core.c src/optimizer/ir.c -o $(OUT)/vir-core-sanitize
ASAN_OPTIONS=detect_leaks=1 $(OUT)/vir-core-sanitize
$(CC) $(CFLAGS) $(SAN_CFLAGS) -Isrc tests/vir-fuzz.c src/optimizer/ir.c -o $(OUT)/vir-fuzz-sanitize
ASAN_OPTIONS=detect_leaks=1 $(OUT)/vir-fuzz-sanitize
$(CC) $(CFLAGS) $(SAN_CFLAGS) -Isrc tests/vir-essential-core.c src/optimizer/ir.c -o $(OUT)/vir-essential-sanitize
ASAN_OPTIONS=detect_leaks=1 $(OUT)/vir-essential-sanitize
.PHONY: check-vir
check-vir: check-vir-core check-vir-essential check-vir-frontend-stage0 \
check-vir-frontend-stage2 check-vir-fuzz check-vir-stage0 check-vir-stage2 \
check-vir-frontend-policy-stage0 \
check-vir-frontend-policy-stage2 \
check-vir-sanitizer
check-vir-stage0: $(OUT)/$(STAGE0) tests/vir-stage0.c src/optimizer/ir.c src/optimizer/ir.h
$(OUT)/$(STAGE0) -o $(OUT)/vir-stage0 tests/vir-stage0.c
$(TARGET_EXEC) $(OUT)/vir-stage0
check-vir-stage2: $(OUT)/$(STAGE2) tests/vir-stage0.c src/optimizer/ir.c src/optimizer/ir.h
$(TARGET_EXEC) $(OUT)/$(STAGE2) -o $(OUT)/vir-stage2 tests/vir-stage0.c
$(TARGET_EXEC) $(OUT)/vir-stage2
.PHONY: check-vir-native-regressions-stage0 check-vir-native-regressions-stage2
check: check-vir-native-regressions-stage0 check-vir-native-regressions-stage2 check-vir-lower
check-vir-native-regressions-stage0: $(OUT)/$(STAGE0) tests/vir-native-regressions.sh tests/vir-runtime-workloads.txt
TARGET_EXEC="$(TARGET_EXEC)" tests/vir-native-regressions.sh $(OUT)/$(STAGE0)
check-vir-native-regressions-stage2: $(OUT)/$(STAGE2) tests/vir-native-regressions.sh tests/vir-runtime-workloads.txt
TARGET_EXEC="$(TARGET_EXEC)" tests/vir-native-regressions.sh $(TARGET_EXEC) $(OUT)/$(STAGE2)
.PHONY: check-vir-lower
check-vir-lower: $(OUT)/$(STAGE0) tests/vir-lower.c tests/vir-lower-smoke.c tests/vir-lower.sh
CC="$(CC)" TARGET_EXEC="$(TARGET_EXEC)" bash tests/vir-lower.sh
# Each workload is compiled twice at both levels by each stage, and the two
# compilers must agree on the output and on the VIR dump. The stats lines are
# left out because they report host-dependent arena sizes.
check-vir-opt-baseline: $(OUT)/$(STAGE0) $(OUT)/$(STAGE2) tests/vir-opt-baseline.sh \
tests/benchmark-time.sh tests/vir-opt-baseline-workloads.txt
@set -e; stage0=$$(mktemp); stage2=$$(mktemp); trap 'rm -f "$$stage0" "$$stage2"' EXIT; \
VIR_OPT_BASELINE_NO_TIMING=1 VIR_OPT_BASELINE_RUNS=2 tests/vir-opt-baseline.sh 0 > "$$stage0"; \
TARGET_EXEC="$(TARGET_EXEC)" VIR_OPT_BASELINE_NO_TIMING=1 VIR_OPT_BASELINE_RUNS=2 tests/vir-opt-baseline.sh 2 > "$$stage2"; \
sed -i 's/ stage=[02] / /' "$$stage0" "$$stage2"; \
diff "$$stage0" "$$stage2"
# Configuration must precede compilation, including parallel builds that switch
# targets. The generated libc include is also needed by the object itself, not
# just the final link.
$(OBJS) $(SAN_OBJS): $(OUT)/libc.inc
$(OUT)/%.o: %.c | config $(OUT)/libc.inc
$(VECHO) " CC\t$@\n"
$(Q)$(CC) -o $@ $(CFLAGS) -c -MMD -MP -MF $@.d $<
SHELL_HACK := $(shell mkdir -p $(OUT) $(OUT)/$(SRCDIR) $(OUT)/tests \
$(SAN_OUT)/$(SRCDIR))
$(OUT)/norm-lf: tools/norm-lf.c
$(VECHO) " CC+LD\t$@\n"
$(Q)$(CC) $(CFLAGS) -o $@ $^
$(OUT)/libc.inc: $(OUT)/inliner $(OUT)/norm-lf $(LIBDIR)/$(BUILTIN_LIBC_SOURCE) $(LIBDIR)/$(BUILTIN_LIBC_HEADER)
$(VECHO) " GEN\t$@\n"
$(Q)$(OUT)/norm-lf $(LIBDIR)/$(BUILTIN_LIBC_SOURCE) $(OUT)/c.normalized.c
$(Q)$(OUT)/norm-lf $(LIBDIR)/$(BUILTIN_LIBC_HEADER) $(OUT)/c.normalized.h
$(Q)$(OUT)/inliner $(OUT)/c.normalized.c $(OUT)/c.normalized.h $@
$(Q)$(RM) $(OUT)/c.normalized.c $(OUT)/c.normalized.h
$(OUT)/inliner: tools/inliner.c
$(VECHO) " CC+LD\t$@\n"
$(Q)$(CC) $(CFLAGS) -o $@ $^
$(OUT)/$(STAGE0): $(OUT)/libc.inc $(OBJS)
$(VECHO) " LD\t$@\n"
$(Q)$(CC) $(OBJS) $(LDFLAGS) -o $@
$(SAN_OUT)/%.o: %.c | config $(OUT)/libc.inc
$(VECHO) " CC\t$@\n"
$(Q)$(CC) -o $@ $(CFLAGS) -c -MMD -MP -MF $@.d $<
$(OUT)/$(STAGE0)-sanitizer: $(OUT)/libc.inc $(SAN_OBJS)
$(VECHO) " LD\t$@ (with sanitizers)\n"
$(Q)$(CC) $(SAN_OBJS) $(LDFLAGS) -o $@
$(OUT)/$(STAGE1): $(OUT)/$(STAGE0)
$(Q)$(STAGE1_CHECK_CMD)
$(VECHO) " SHECC\t$@\n"
$(Q)$(OUT)/$(STAGE0) $(STAGE0_FLAGS) -o $@ $(SRCDIR)/main.c > $(OUT)/shecc-stage1.log
$(Q)chmod a+x $@
# The mode belongs here rather than on "bootstrap", because "check" builds
# stage2 through this rule without going through that target. Statically the
# question does not arise: shecc's own libc opens the output 0775. Linked
# dynamically the open is glibc's, which gives 0666 before the umask, so a
# "make check" on a tree that had never been bootstrapped produced a stage2
# nothing could execute and every stage-2 test failed to compile.
$(OUT)/$(STAGE2): $(OUT)/$(STAGE1)
$(VECHO) " SHECC\t$@\n"
$(Q)$(TARGET_EXEC) $(OUT)/$(STAGE1) $(STAGE1_FLAGS) -o $@ $(SRCDIR)/main.c
$(Q)chmod 775 $@
bootstrap: $(OUT)/$(STAGE2)
$(Q)if ! diff -q $(OUT)/$(STAGE1) $(OUT)/$(STAGE2); then \
echo "Unable to bootstrap. Aborting"; false; \
fi
.PHONY: clean
clean:
-$(RM) $(OUT)/$(STAGE0) $(OUT)/$(STAGE1) $(OUT)/$(STAGE2)
-$(RM) $(OUT)/$(STAGE0)-sanitizer
-$(RM) $(OBJS) $(SAN_OBJS) $(deps)
-$(RM) $(TESTBINS) $(OUT)/tests/*.log $(OUT)/tests/*.lst
-$(RM) $(OUT)/shecc*.log
-$(RM) $(OUT)/libc.inc
distclean: clean
-$(RM) $(OUT)/inliner $(OUT)/norm-lf $(OUT)/target config $(BUILD_SESSION) $(ARCH_DEFS_SHA256_FILE) src/codegen.c
-$(RM) DOM.dot CFG.dot
-include $(deps)
# Manual native hardware measurement; excluded from correctness checks.
.PHONY: benchmark-native-throughput
benchmark-native-throughput: $(OUT)/$(STAGE0)
$(Q)python3 tests/native-throughput.py --shecc $(OUT)/$(STAGE0) \
--output $(OUT)/native-throughput $(NATIVE_BENCH_FLAGS)