-
Notifications
You must be signed in to change notification settings - Fork 1
/
makefile
102 lines (80 loc) · 2.61 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
# Based on https://matansilver.com/2017/08/29/universal-makefile/
# Modified by MaxXing CuWO4
MAKEFLAGS += -j20
FB_EXT := .cpp
# Flags
CXXFLAGS := -Wall -Wno-register -std=c++17
FFLAGS :=
BFLAGS := -d
LDFLAGS :=
# Debug flags
DEBUG ?= 1
ifeq ($(DEBUG), 0)
CXXFLAGS += -O2
else
CFLAGS += -g -O0
CXXFLAGS += -g -O0
endif
# Compilers
CXX := clang++
FLEX := flex
BISON := bison
# Directories
TOP_DIR := ${CURDIR}
TARGET_EXEC := compiler
SRC_DIR := $(TOP_DIR)/src
BUILD_DIR ?= $(TOP_DIR)/build
INC_DIR ?= $(CDE_INCLUDE_PATH)
# Source files & target files
FB_SRCS := $(patsubst $(SRC_DIR)/%.l, $(BUILD_DIR)/%.lex$(FB_EXT), $(wildcard $(SRC_DIR)/*.l))
FB_SRCS += $(patsubst $(SRC_DIR)/%.y, $(BUILD_DIR)/%.tab$(FB_EXT), $(wildcard $(SRC_DIR)/*.y))
SRCS := $(FB_SRCS) $(wildcard $(SRC_DIR)/*.cpp)
OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(BUILD_DIR)/%.cpp.o, $(SRCS))
OBJS := $(patsubst $(BUILD_DIR)/%.cpp, $(BUILD_DIR)/%.cpp.o, $(OBJS))
# Header directories & dependencies
INC_DIRS := include/
INC_FLAGS := $(addprefix -I, $(INC_DIRS))
CPPFLAGS = $(INC_FLAGS) -MMD -MP
# Main target
$(BUILD_DIR)/$(TARGET_EXEC): $(FB_SRCS) $(OBJS)
$(CXX) $(OBJS) $(LDFLAGS) -o $@
# C++ source
define cxx_recipe
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
endef
$(BUILD_DIR)/%.cpp.o: $(SRC_DIR)/%.cpp | $(BUILD_DIR); $(cxx_recipe)
$(BUILD_DIR)/%.cpp.o: $(BUILD_DIR)/%.cpp | $(BUILD_DIR); $(cxx_recipe)
# Flex
$(BUILD_DIR)/%.lex$(FB_EXT): $(SRC_DIR)/%.l | $(BUILD_DIR)
$(FLEX) -o$@ $(FFLAGS) $<
# Bison
$(BUILD_DIR)/%.tab$(FB_EXT): $(SRC_DIR)/%.y | $(BUILD_DIR)
$(BISON) $(BFLAGS) -o $@ $<
$(BUILD_DIR) :
mkdir "$(BUILD_DIR)"
DEPS := $(OBJS:.o=.d)
-include $(DEPS)
#release
RELEASE_TARGET_EXEC := sysyc
RELEASE_FLAGS := -O3 -flto
release: $(FB_SRCS) $(SRCS) | $(BUILD_DIR)/release
$(CXX) $(INC_FLAGS) $(SRCS) $(RELEASE_FLAGS) -o $(BUILD_DIR)/release/$(RELEASE_TARGET_EXEC)
$(BUILD_DIR)/release:
mkdir "$@"
run-koopa :
koopac testcases/hello/hello.koopa | llc --filetype=obj -o build/hello.o
clang build/hello.o -L$$CDE_LIBRARY_PATH/native -lsysy -o build/hello
build/hello
run-riscv :
clang testcases/hello/hello.S -c -o build/hello.o -target riscv32-unknown-linux-elf -march=rv32im -mabi=ilp32
ld.lld build/hello.o -L$$CDE_LIBRARY_PATH/riscv32 -lsysy -o build/hello
qemu-riscv32-static build/hello
once: $(FB_SRCS) | $(BUILD_DIR)
$(CXX) $(SRCS) $(LDFLAGS) -lpthread -ldl -o $(BUILD_DIR)/$(TARGET_EXEC)
clean:
-rm -rf $(BUILD_DIR)
MODE ?= koopa
lldb: $(BUILD_DIR)/$(TARGET_EXEC)
lldb $(BUILD_DIR)/$(TARGET_EXEC) -- -$(MODE) ./testcases/hello/hello.c -o ./testcases/hello/hello.koopa
docker :
docker run -it --rm -v ${CURDIR}:/root/compiler maxxing/compiler-dev bash