-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
103 lines (90 loc) · 4.6 KB
/
CMakeLists.txt
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
cmake_minimum_required(VERSION 3.14)
project(chibicc C)
set(CMAKE_C_STANDARD 17)
# build chibicc
file(GLOB SRCS *.c)
add_executable(chibicc ${SRCS})
# build stage2
set(STAGE2_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/stage2")
file(MAKE_DIRECTORY ${STAGE2_OUTPUT_DIR})
set(STAGE2_OUTPUT_FILES "")
foreach (SRC ${SRCS})
get_filename_component(SRC_NAME ${SRC} NAME_WE)
set(STAGE2_OUTPUT_C_FILE "${STAGE2_OUTPUT_DIR}/${SRC_NAME}.c")
set(STAGE2_OUTPUT_S_FILE "${STAGE2_OUTPUT_DIR}/${SRC_NAME}.s")
set(STAGE2_OUTPUT_OBJ_FILE "${STAGE2_OUTPUT_DIR}/${SRC_NAME}.o")
add_custom_command(
OUTPUT ${STAGE2_OUTPUT_OBJ_FILE}
COMMAND ./chibicc -I../include -c -S -o ${STAGE2_OUTPUT_S_FILE} ${SRC}
COMMAND ./chibicc -o ${STAGE2_OUTPUT_OBJ_FILE} ${STAGE2_OUTPUT_S_FILE}
DEPENDS chibicc ${SRC}
)
list(APPEND STAGE2_OUTPUT_FILES ${STAGE2_OUTPUT_OBJ_FILE})
endforeach ()
add_custom_target(stage2 ALL DEPENDS ${STAGE2_OUTPUT_FILES})
add_executable(chibicc2 ${STAGE2_OUTPUT_FILES})
set_target_properties(chibicc2 PROPERTIES LINKER_LANGUAGE C)
add_dependencies(chibicc2 stage2)
# build stage3
set(STAGE3_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/stage3")
file(MAKE_DIRECTORY ${STAGE3_OUTPUT_DIR})
set(STAGE3_OUTPUT_FILES "")
foreach (SRC ${SRCS})
get_filename_component(SRC_NAME ${SRC} NAME_WE)
set(STAGE3_OUTPUT_C_FILE "${STAGE3_OUTPUT_DIR}/${SRC_NAME}.c")
set(STAGE3_OUTPUT_S_FILE "${STAGE3_OUTPUT_DIR}/${SRC_NAME}.s")
set(STAGE3_OUTPUT_OBJ_FILE "${STAGE3_OUTPUT_DIR}/${SRC_NAME}.o")
add_custom_command(
OUTPUT ${STAGE3_OUTPUT_OBJ_FILE}
COMMAND ./chibicc2 -I../include -c -S -o ${STAGE3_OUTPUT_S_FILE} ${SRC}
COMMAND ./chibicc2 -o ${STAGE3_OUTPUT_OBJ_FILE} ${STAGE3_OUTPUT_S_FILE}
DEPENDS chibicc2 ${SRC}
)
list(APPEND STAGE3_OUTPUT_FILES ${STAGE3_OUTPUT_OBJ_FILE})
endforeach ()
add_custom_target(stage3 ALL DEPENDS ${STAGE3_OUTPUT_FILES})
add_executable(chibicc3 ${STAGE3_OUTPUT_FILES})
set_target_properties(chibicc3 PROPERTIES LINKER_LANGUAGE C)
add_dependencies(chibicc3 stage3)
# build tests
enable_testing()
file(GLOB TEST_SRCS test/*.c)
set(TEST_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/test")
file(MAKE_DIRECTORY ${TEST_OUTPUT_DIR})
add_test(NAME driver COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test/driver.sh ${CMAKE_CURRENT_BINARY_DIR}/chibicc)
foreach (TEST_SRC ${TEST_SRCS})
get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE)
set(TEST_OUTPUT_S_FILE "${TEST_OUTPUT_DIR}/${TEST_NAME}.s")
set(TEST_OUTPUT_OBJ_FILE "${TEST_OUTPUT_DIR}/${TEST_NAME}.o")
set(TEST_OUTPUT_EXE_FILE "${TEST_OUTPUT_DIR}/${TEST_NAME}.exe")
add_custom_target(
${TEST_NAME} ALL
COMMAND ./chibicc -I../test -I../include -c -S -o ${TEST_OUTPUT_S_FILE} ${TEST_SRC}
COMMAND ./chibicc -o ${TEST_OUTPUT_OBJ_FILE} ${TEST_OUTPUT_S_FILE}
COMMAND clang -o ${TEST_OUTPUT_EXE_FILE} ${TEST_OUTPUT_OBJ_FILE} -x c ${CMAKE_CURRENT_SOURCE_DIR}/test/common
DEPENDS chibicc ${TEST_SRC}
)
add_test(NAME ${TEST_NAME} COMMAND ${TEST_OUTPUT_EXE_FILE})
endforeach ()
# build stage2 tests
set(STAGE2_TEST_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/stage2/test")
file(MAKE_DIRECTORY ${STAGE2_TEST_OUTPUT_DIR})
add_test(NAME stage2_driver COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test/driver.sh ${CMAKE_CURRENT_BINARY_DIR}/chibicc2)
foreach (TEST_SRC ${TEST_SRCS})
get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE)
set(STAGE2_TEST_OUTPUT_S_FILE "${STAGE2_TEST_OUTPUT_DIR}/${TEST_NAME}.s")
set(STAGE2_TEST_OUTPUT_OBJ_FILE "${STAGE2_TEST_OUTPUT_DIR}/${TEST_NAME}.o")
set(STAGE2_TEST_OUTPUT_EXE_FILE "${STAGE2_TEST_OUTPUT_DIR}/${TEST_NAME}.exe")
add_custom_target(
stage2_${TEST_NAME} ALL
COMMAND ./chibicc2 -I../test -I../include -c -S -o ${STAGE2_TEST_OUTPUT_S_FILE} ${TEST_SRC}
COMMAND ./chibicc2 -o ${STAGE2_TEST_OUTPUT_OBJ_FILE} ${STAGE2_TEST_OUTPUT_S_FILE}
COMMAND clang -o ${STAGE2_TEST_OUTPUT_EXE_FILE} ${STAGE2_TEST_OUTPUT_OBJ_FILE} -x c ${CMAKE_CURRENT_SOURCE_DIR}/test/common
DEPENDS chibicc2 ${TEST_SRC}
)
add_test(NAME stage2_${TEST_NAME} COMMAND ${STAGE2_TEST_OUTPUT_EXE_FILE})
endforeach ()
# compare stage2 and stage3
add_custom_target(stage2_bin ALL COMMAND ./chibicc -I../include -o ${STAGE2_OUTPUT_DIR}/chibicc ${SRCS} DEPENDS chibicc ${SRCS})
add_custom_target(stage3_bin ALL COMMAND ./stage2/chibicc -I../include -o ${STAGE3_OUTPUT_DIR}/chibicc ${SRCS} DEPENDS stage2_bin ${SRCS})
add_test(NAME compare_stage2_stage3 COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test/compare.sh ${STAGE2_OUTPUT_DIR}/chibicc ${STAGE3_OUTPUT_DIR}/chibicc)