This repository has been archived by the owner on Aug 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
156 lines (127 loc) · 4.73 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
SRC_PATH ?= src
INC_PATH += include
BUILD_PATH ?= build
TEST_PATH ?= test/level1-1
OBJ_PATH ?= $(BUILD_PATH)/obj
BINARY ?= $(BUILD_PATH)/compiler
SYSLIB_PATH ?= sysyruntimelibrary
INC = $(addprefix -I, $(INC_PATH))
SRC = $(shell find $(SRC_PATH) -name "*.cpp")
CFLAGS = -O0 -g -Wall -std=c++11 $(INC)
FLEX ?= $(SRC_PATH)/lexer.l
LEXER ?= $(addsuffix .cpp, $(basename $(FLEX)))
BISON ?= $(SRC_PATH)/parser.y
PARSER ?= $(addsuffix .cpp, $(basename $(BISON)))
SRC += $(LEXER)
SRC += $(PARSER)
OBJ = $(SRC:$(SRC_PATH)/%.cpp=$(OBJ_PATH)/%.o)
PARSERH ?= $(INC_PATH)/$(addsuffix .h, $(notdir $(basename $(PARSER))))
TESTCASE = $(shell find $(TEST_PATH) -name "*.sy")
TESTCASE_NUM = $(words $(TESTCASE))
LLVM_IR = $(addsuffix _std.ll, $(basename $(TESTCASE)))
GCC_ASM = $(addsuffix _std.s, $(basename $(TESTCASE)))
OUTPUT_LAB4 = $(addsuffix .toks, $(basename $(TESTCASE)))
OUTPUT_LAB5 = $(addsuffix .ast, $(basename $(TESTCASE)))
OUTPUT_LAB6 = $(addsuffix .ll, $(basename $(TESTCASE)))
OUTPUT_LAB7 = $(addsuffix .s, $(basename $(TESTCASE)))
OUTPUT_RES = $(addsuffix .res, $(basename $(TESTCASE)))
OUTPUT_BIN = $(addsuffix .bin, $(basename $(TESTCASE)))
OUTPUT_LOG = $(addsuffix .log, $(basename $(TESTCASE)))
.phony:all app run gdb testlab4 testlab5 testlab6 testlab7 test clean clean-all clean-test clean-app llvmir gccasm
all:app
$(LEXER):$(FLEX)
@flex -o $@ $<
$(PARSER):$(BISON)
@bison -o $@ $< --warnings=error=all --defines=$(PARSERH)
$(OBJ_PATH)/%.o:$(SRC_PATH)/%.cpp
@mkdir -p $(OBJ_PATH)
@g++ $(CFLAGS) -c -o $@ $<
$(BINARY):$(OBJ)
@g++ -O0 -g -o $@ $^
app:$(LEXER) $(PARSER) $(BINARY)
run:app
@$(BINARY) -o example.s -S example.sy
gdb:app
@gdb $(BINARY)
$(OBJ_PATH)/lexer.o:$(SRC_PATH)/lexer.cpp
@mkdir -p $(OBJ_PATH)
@g++ $(CFLAGS) -c -o $@ $<
$(TEST_PATH)/%.toks:$(TEST_PATH)/%.sy
@$(BINARY) $< -o $@ -t
$(TEST_PATH)/%.ast:$(TEST_PATH)/%.sy
@$(BINARY) $< -o $@ -a
$(TEST_PATH)/%.ll:$(TEST_PATH)/%.sy
@$(BINARY) $< -o $@ -i
$(TEST_PATH)/%_std.ll:$(TEST_PATH)/%.sy
@clang -x c $< -S -m32 -emit-llvm -o $@
$(TEST_PATH)/%_std.s:$(TEST_PATH)/%.sy
@arm-linux-gnueabihf-gcc -x c $< -S -o $@
$(TEST_PATH)/%.s:$(TEST_PATH)/%.sy
@timeout 5s $(BINARY) $< -o $@ -S 2>$(addsuffix .log, $(basename $@))
@[ $$? != 0 ] && echo "\033[1;31mCOMPILE FAIL:\033[0m $(notdir $<)" || echo "\033[1;32mCOMPILE SUCCESS:\033[0m $(notdir $<)"
llvmir:$(LLVM_IR)
gccasm:$(GCC_ASM)
testlab4:app $(OUTPUT_LAB4)
testlab5:app $(OUTPUT_LAB5)
testlab6:app $(OUTPUT_LAB6)
testlab7:app $(OUTPUT_LAB7)
.ONESHELL:
test:app
@success=0
@for file in $(sort $(TESTCASE))
do
ASM=$${file%.*}.s
LOG=$${file%.*}.log
BIN=$${file%.*}.bin
RES=$${file%.*}.res
IN=$${file%.*}.in
OUT=$${file%.*}.out
FILE=$${file##*/}
FILE=$${FILE%.*}
timeout 5s $(BINARY) $${file} -o $${ASM} -S 2>$${LOG}
RETURN_VALUE=$$?
if [ $$RETURN_VALUE = 124 ]; then
echo "\033[1;31mFAIL:\033[0m $${FILE}\t\033[1;31mCompile Timeout\033[0m"
continue
else if [ $$RETURN_VALUE != 0 ]; then
echo "\033[1;31mFAIL:\033[0m $${FILE}\t\033[1;31mCompile Error\033[0m"
continue
fi
fi
arm-linux-gnueabihf-gcc -mcpu=cortex-a72 -o $${BIN} $${ASM} $(SYSLIB_PATH)/libsysy.a >>$${LOG} 2>&1
if [ $$? != 0 ]; then
echo "\033[1;31mFAIL:\033[0m $${FILE}\t\033[1;31mAssemble Error\033[0m"
else
if [ -f "$${IN}" ]; then
timeout 2s qemu-arm -L /usr/arm-linux-gnueabihf $${BIN} <$${IN} >$${RES} 2>>$${LOG}
else
timeout 2s qemu-arm -L /usr/arm-linux-gnueabihf $${BIN} >$${RES} 2>>$${LOG}
fi
RETURN_VALUE=$$?
FINAL=`tail -c 1 $${RES}`
[ $${FINAL} ] && echo "\n$${RETURN_VALUE}" >> $${RES} || echo "$${RETURN_VALUE}" >> $${RES}
if [ "$${RETURN_VALUE}" = "124" ]; then
echo "\033[1;31mFAIL:\033[0m $${FILE}\t\033[1;31mExecute Timeout\033[0m"
else if [ "$${RETURN_VALUE}" = "127" ]; then
echo "\033[1;31mFAIL:\033[0m $${FILE}\t\033[1;31mExecute Error\033[0m"
else
diff -Z $${RES} $${OUT} >/dev/null 2>&1
if [ $$? != 0 ]; then
echo "\033[1;31mFAIL:\033[0m $${FILE}\t\033[1;31mWrong Answer\033[0m"
else
success=$$((success + 1))
echo "\033[1;32mPASS:\033[0m $${FILE}"
fi
fi
fi
fi
done
echo "\033[1;33mTotal: $(TESTCASE_NUM)\t\033[1;32mAccept: $${success}\t\033[1;31mFail: $$(($(TESTCASE_NUM) - $${success}))\033[0m"
[ $(TESTCASE_NUM) = $${success} ] && echo "\033[5;32mAll Accepted. Congratulations!\033[0m"
:
clean-app:
@rm -rf $(BUILD_PATH) $(PARSER) $(LEXER) $(PARSERH)
clean-test:
@rm -rf $(OUTPUT_LAB4) $(OUTPUT_LAB5) $(OUTPUT_LAB6) $(OUTPUT_LAB7) $(OUTPUT_LOG) $(OUTPUT_BIN) $(OUTPUT_RES) $(LLVM_IR) $(GCC_ASM) ./example.ast ./example.ll ./example.s
clean-all:clean-test clean-app
clean:clean-all