Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
75 lines (61 loc) · 2.37 KB
/
Copy pathMakefile
File metadata and controls
75 lines (61 loc) · 2.37 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
MODE ?= release
CXX ?= c++
ifeq ($(MODE),release)
OPT := -O2 -flto=auto
else ifeq ($(MODE),debug)
OPT := -O0 -g
else
$(error MODE must be release or debug)
endif
export NIX_HARDENING_ENABLE :=
FLAGS := -std=c++17 -Wall -Wextra -pthread $(OPT)
# use mimalloc when available
MIMALLOC := $(shell echo "int main(){}" | $(CXX) -x c++ - -o /dev/null -l:libmimalloc.a 2>/dev/null && echo -l:libmimalloc.a)
STATIC := -static-libstdc++ -static-libgcc
OBJ := build/$(MODE)
INC := -iquote src/base -iquote src/backend -iquote src/linker -iquote src/compiler -iquote $(OBJ)
# source groups
base_srcs := src/base/test_harness.cpp
rat_srcs := $(shell find src/backend/analysis src/backend/codegen src/backend/ir \
src/backend/pass src/backend/target -name '*.cpp')
link_srcs := src/linker/linker.cpp src/linker/elf_write.cpp src/linker/elf_read.cpp
cc_srcs := $(filter-out src/compiler/main.cpp,$(wildcard src/compiler/*.cpp)) \
$(shell find src/compiler/emit src/compiler/lex src/compiler/parse -name '*.cpp')
objs = $(patsubst %.cpp,$(OBJ)/%.o,$(1))
base_o := $(call objs,$(base_srcs))
rat_o := $(call objs,$(rat_srcs))
link_o := $(call objs,$(link_srcs))
cc_o := $(call objs,$(cc_srcs))
driver_o = $(OBJ)/src/$(1).o
all_o := $(base_o) $(rat_o) $(link_o) $(cc_o) \
$(call driver_o,backend/main) $(call driver_o,backend/test/runner) \
$(call driver_o,linker/main) $(call driver_o,compiler/main)
all: bin/rat bin/rat-test bin/link bin/cc
$(OBJ)/git_hash.h: FORCE
@mkdir -p $(OBJ)
@h=$$(git rev-parse --short=12 HEAD 2>/dev/null || echo unknown); \
if ! git diff --quiet HEAD -- 2>/dev/null || ! git diff --cached --quiet 2>/dev/null; then \
h="$$h-dirty"; \
fi; \
new="#define GIT_HASH \"$$h\""; \
{ [ -f $@ ] && [ "$$(cat $@)" = "$$new" ]; } || echo "$$new" > $@
$(OBJ)/%.o: %.cpp | $(OBJ)/git_hash.h
@mkdir -p $(dir $@)
@echo "cc $<"
@$(CXX) $(FLAGS) $(INC) -MMD -MP -c $< -o $@
-include $(all_o:.o=.d)
bin/%:
@mkdir -p bin
@echo "link $@"
@$(CXX) $(FLAGS) -o $@ $^ $(MIMALLOC) $(STATIC)
bin/rat: $(base_o) $(rat_o) $(call driver_o,backend/main)
bin/rat-test: $(base_o) $(rat_o) $(call driver_o,backend/test/runner)
bin/link: $(base_o) $(link_o) $(call driver_o,linker/main)
bin/cc: $(base_o) $(rat_o) $(cc_o) $(call driver_o,compiler/main)
test: all
./src/compiler/test/run.py $(SUITES)
bench: all
./src/compiler/test/run.py bench $(SUITES)
clean:
rm -rf build bin
.PHONY: all test bench clean FORCE