-
Notifications
You must be signed in to change notification settings - Fork 123
/
Makefile
136 lines (108 loc) · 3.9 KB
/
Makefile
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
CC ?= gcc
CFLAGS := -O -g \
-std=c99 -pedantic \
-Wall -Wextra \
-Wno-unused-but-set-variable \
-Wno-variadic-macros \
-Wno-uninitialized \
-Wno-strict-prototypes \
-Wno-declaration-after-statement \
-Wno-format \
-Wno-format-pedantic
BUILD_SESSION := .session.mk
include mk/common.mk
include mk/arm.mk
include mk/riscv.mk
-include $(BUILD_SESSION)
STAGE0 := shecc
STAGE1 := shecc-stage1.elf
STAGE2 := shecc-stage2.elf
OUT ?= out
ARCHS = arm riscv
ARCH ?= $(firstword $(ARCHS))
SRCDIR := $(shell find src -type d)
LIBDIR := $(shell find lib -type d)
SRCS := $(wildcard $(patsubst %,%/main.c, $(SRCDIR)))
OBJS := $(SRCS:%.c=$(OUT)/%.o)
deps := $(OBJS:%.o=%.o.d)
TESTS := $(wildcard tests/*.c)
TESTBINS := $(TESTS:%.c=$(OUT)/%.elf)
SNAPSHOTS := $(foreach SNAPSHOT_ARCH,$(ARCHS), $(patsubst tests/%.c, tests/snapshots/%-$(SNAPSHOT_ARCH).json, $(TESTS)))
all: config bootstrap
ifeq (,$(filter $(ARCH),$(ARCHS)))
$(error Support ARM and RISC-V only. Select the target with "ARCH=arm" or "ARCH=riscv")
endif
ifneq ("$(wildcard $(PWD)/config)","")
TARGET_EXEC := $($(shell head -1 config | sed 's/.*: \([^ ]*\).*/\1/')_EXEC)
endif
export TARGET_EXEC
config:
$(Q)ln -s $(PWD)/$(SRCDIR)/$(ARCH)-codegen.c $(SRCDIR)/codegen.c
$(call $(ARCH)-specific-defs) > $@
$(VECHO) "Target machine code switch to %s\n" $(ARCH)
$(Q)$(MAKE) $(BUILD_SESSION) --silent
$(OUT)/tests/%.elf: tests/%.c $(OUT)/$(STAGE0)
$(VECHO) " SHECC\t$@\n"
$(Q)$(OUT)/$(STAGE0) --dump-ir -o $@ $< > $(basename $@).log ; \
chmod +x $@ ; $(PRINTF) "Running $@ ...\n"
$(Q)$(TARGET_EXEC) $@ && $(call pass)
check: check-stage0 check-stage2
check-stage0: $(OUT)/$(STAGE0) $(TESTBINS) tests/driver.sh
$(VECHO) " TEST STAGE 0\n"
tests/driver.sh 0
check-stage2: $(OUT)/$(STAGE2) $(TESTBINS) tests/driver.sh
$(VECHO) " TEST STAGE 2\n"
tests/driver.sh 2
check-snapshots: $(OUT)/$(STAGE0) $(SNAPSHOTS) tests/check-snapshots.sh
$(Q)$(foreach SNAPSHOT_ARCH, $(ARCHS), $(MAKE) distclean config check-snapshot ARCH=$(SNAPSHOT_ARCH) --silent;)
$(VECHO) "Switching backend back to %s\n" $(ARCH)
$(Q)$(MAKE) distclean config ARCH=$(ARCH) --silent
check-snapshot: $(OUT)/$(STAGE0) tests/check-snapshots.sh
$(VECHO) "Checking snapshot for %s\n" $(ARCH)
tests/check-snapshots.sh $(ARCH)
$(VECHO) " OK\n"
update-snapshots: tests/update-snapshots.sh
$(Q)$(foreach SNAPSHOT_ARCH, $(ARCHS), $(MAKE) distclean config update-snapshot ARCH=$(SNAPSHOT_ARCH) --silent;)
$(VECHO) "Switching backend back to %s\n" $(ARCH)
$(Q)$(MAKE) distclean config ARCH=$(ARCH) --silent
update-snapshot: $(OUT)/$(STAGE0) tests/update-snapshots.sh
$(VECHO) "Updating snapshot for %s\n" $(ARCH)
tests/update-snapshots.sh $(ARCH)
$(VECHO) " OK\n"
$(OUT)/%.o: %.c
$(VECHO) " CC\t$@\n"
$(Q)$(CC) -o $@ $(CFLAGS) -c -MMD -MF [email protected] $<
SHELL_HACK := $(shell mkdir -p $(OUT) $(OUT)/$(SRCDIR) $(OUT)/tests)
$(OUT)/libc.inc: $(OUT)/inliner $(LIBDIR)/c.c
$(VECHO) " GEN\t$@\n"
$(Q)$(OUT)/inliner $(LIBDIR)/c.c $@
$(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) -o $@
$(OUT)/$(STAGE1): $(OUT)/$(STAGE0)
$(VECHO) " SHECC\t$@\n"
$(Q)$(OUT)/$(STAGE0) --dump-ir -o $@ $(SRCDIR)/main.c > $(OUT)/shecc-stage1.log
$(Q)chmod a+x $@
$(OUT)/$(STAGE2): $(OUT)/$(STAGE1)
$(VECHO) " SHECC\t$@\n"
$(Q)$(TARGET_EXEC) $(OUT)/$(STAGE1) -o $@ $(SRCDIR)/main.c
bootstrap: $(OUT)/$(STAGE2)
$(Q)chmod 775 $(OUT)/$(STAGE2)
$(Q)if ! diff -q $(OUT)/$(STAGE1) $(OUT)/$(STAGE2); then \
echo "Unable to bootstrap. Aborting"; false; \
fi
$(BUILD_SESSION):
$(PRINTF) "ARCH=$(ARCH)" > $@
.PHONY: clean
clean:
-$(RM) $(OUT)/$(STAGE0) $(OUT)/$(STAGE1) $(OUT)/$(STAGE2)
-$(RM) $(OBJS) $(deps)
-$(RM) $(TESTBINS) $(OUT)/tests/*.log $(OUT)/tests/*.lst
-$(RM) $(OUT)/shecc*.log
-$(RM) $(OUT)/libc.inc
distclean: clean
-$(RM) $(OUT)/inliner $(OUT)/target $(SRCDIR)/codegen.c config $(BUILD_SESSION)
-include $(deps)