-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
177 lines (153 loc) · 6.62 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
cmake_minimum_required(VERSION 3.0)
project(YOBEMAG C)
set(PRODUCT_NAME yobemag)
string(TOUPPER ${PRODUCT_NAME} UPPER_PRODUCT_NAME)
include_directories(src)
set(SRC_FILES
src/cpu.c
src/main.c
src/mmu.c
src/lcd.c
src/rom.c
src/log.c
src/cli.c)
add_executable(${PRODUCT_NAME} ${SRC_FILES})
message("[${UPPER_PRODUCT_NAME}] Using ${CMAKE_GENERATOR}")
message("[${UPPER_PRODUCT_NAME}] Using ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_VERSION}")
if (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "DEBUG" OR CMAKE_BUILD_TYPE STREQUAL "debug")
message("[${UPPER_PRODUCT_NAME}] Using -g")
target_compile_options(${PRODUCT_NAME} PUBLIC -g)
target_compile_definitions(${PRODUCT_NAME} PUBLIC YOBEMAG_DEBUG)
endif ()
target_compile_options(${PRODUCT_NAME} PUBLIC
# Standard GCC/Clang warnings
-Wall
-Wextra
-Werror
-Wunused
-Wshadow
-Wcast-align
-Wconversion
-Wdouble-promotion
-fstack-protector -Wstack-protector
-Wredundant-decls
-Wpacked
-Wmissing-declarations
-Wmissing-prototypes
-Wstrict-prototypes
-Wwrite-strings
-Wcast-qual
-Wundef
-Wfloat-equal
-Wstrict-overflow=4
-Wswitch-enum
-Wswitch-default
-Wmissing-include-dirs
-Wformat=2)
if (DEFINED OPTIMIZE)
message("[${UPPER_PRODUCT_NAME}] Using -O${OPTIMIZE}")
target_compile_options(${PRODUCT_NAME} PUBLIC -O${OPTIMIZE})
endif ()
if (CMAKE_C_COMPILER_ID STREQUAL "Clang")
target_compile_options(${PRODUCT_NAME} PUBLIC
-Weverything
-Wno-declaration-after-statement
-Wno-padded
-Wno-gnu-designator
-ferror-limit=0)
elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU")
target_compile_options(${PRODUCT_NAME} PUBLIC
# Standard GCC warnings: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
-Wlogical-op
-Wno-unknown-pragmas
-Warray-bounds=2
-Wjump-misses-init
-Wunsafe-loop-optimizations
-Wno-aggressive-loop-optimizations
-Wjump-misses-init
-Wtrampolines
-Wsuggest-attribute=cold
-Wsuggest-attribute=malloc
-Wsuggest-attribute=pure
-Wsuggest-attribute=const
-Wsuggest-attribute=noreturn
-Wsuggest-attribute=format
-fmax-errors=0)
endif ()
set_property(TARGET ${PRODUCT_NAME} PROPERTY C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
target_link_libraries(${PRODUCT_NAME} PUBLIC -lm -lSDL2 -pthread)
install(TARGETS ${PRODUCT_NAME} DESTINATION ~/.local/bin/ PERMISSIONS OWNER_EXECUTE)
################################################################################
### Unit Tests ###
################################################################################
if (${TEST})
set(TEST_NAME ${PRODUCT_NAME}_test)
set(TEST_FILES
test/fixtures/cpu_mmu.c
test/common/util.c
test/common/alu.c
test/8bit_lds_test.c
test/alu_8bit_add_test.c
test/alu_8bit_adc_test.c
test/alu_8bit_and_test.c
test/alu_8bit_sub_test.c
test/alu_8bit_sbc_test.c
test/alu_8bit_or_test.c
test/alu_8bit_xor_test.c
test/alu_8bit_cp_test.c
test/alu_8bit_dec_test.c
test/alu_8bit_inc_test.c
test/cli_test.c
test/rom_test.c
test/mmu_test.c
test/log_test.c
test/jr_cc_n.c
test/jp_cc_n.c)
set(TEST_SOURCES ${TEST_FILES} ${SRC_FILES})
list(REMOVE_ITEM TEST_SOURCES "src/main.c")
add_executable(${TEST_NAME} ${TEST_SOURCES})
target_compile_definitions(${TEST_NAME} PUBLIC YOBEMAG_TEST)
if (${COVERAGE})
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "GNU")
message("[${UPPER_PRODUCT_NAME}] Adding coverage flags")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 --coverage")
else ()
message(FATAL_ERROR "[${MSG_PREFIX}] GCC is required to output coverage reports")
endif ()
endif ()
target_link_libraries(${TEST_NAME} PUBLIC -lm -lSDL2 -pthread -lcriterion)
add_custom_target(test COMMAND ./${TEST_NAME} DEPENDS ${TEST_NAME})
endif ()
if (DEFINED SANITIZE)
if (CMAKE_C_COMPILER_ID STREQUAL "Clang")
if (NOT ${SANITIZE} STREQUAL "valgrind")
message("[${UPPER_PRODUCT_NAME}] Adding -fsanitize=${SANITIZE} and necessary compiler flags")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=${SANITIZE}")
if (${SANITIZE} STREQUAL "memory")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-memory-track-origins -fno-omit-frame-pointer -g -O2")
else ()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -fno-omit-frame-pointer -g")
endif ()
if (${TEST})
add_custom_target(sanitize COMMAND ./${TEST_NAME} DEPENDS ${TEST_NAME})
else ()
add_custom_target(sanitize COMMAND ./${PRODUCT_NAME} DEPENDS ${PRODUCT_NAME})
endif ()
else ()
message(FATAL_ERROR "[${UPPER_PRODUCT_NAME}] GCC is required to run valgrind")
endif ()
elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU")
if (${SANITIZE} STREQUAL "valgrind")
message("[${UPPER_PRODUCT_NAME}] Adding no compile flags for ${SANITIZE}")
if (${TEST})
add_custom_target(sanitize COMMAND valgrind --leak-check=yes --trace-children=yes ./${TEST_NAME} DEPENDS ${TEST_NAME})
else ()
add_custom_target(sanitize COMMAND valgrind --leak-check=yes --trace-children=yes ./${PRODUCT_NAME} DEPENDS ${PRODUCT_NAME})
endif ()
else ()
message(FATAL_ERROR "[${UPPER_PRODUCT_NAME}] clang is required to run address, memory, or undefined sanitizers")
endif ()
endif ()
endif ()