-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
193 lines (171 loc) · 4.57 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
cmake_minimum_required(VERSION 3.13)
project(
rk4_solver
VERSION 1.4.0
LANGUAGES CXX
)
#* dependencies
include(FetchContent)
set(matrix_op_VERSION 1.1.0) #* matrix_op, used for basic matrix operations
set(matrix_rw_VERSION 1.1.0) #* matrix_rw, used for testing
FetchContent_Declare(
matrix_op
URL https://github.com/cinaral/matrix_op/releases/download/v${matrix_op_VERSION}/src.zip
)
FetchContent_MakeAvailable(matrix_op)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
FetchContent_Declare(
matrix_rw
URL https://github.com/cinaral/matrix_rw/releases/download/v${matrix_rw_VERSION}/src.zip
)
FetchContent_MakeAvailable(matrix_rw)
endif()
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
option(DEBUG_BUILD "Build everything in debug mode" ON)
option(BUILD_TESTS "Build tests" ON)
option(BUILD_EXAMPLES "Build examples" ON)
option(BUILD_BENCHMARKS "Build benchmarks" ON)
option(USE_SINGLE_PRECISION "Use single precision floats" OFF)
option(DO_NOT_USE_HEAP "Disable heap allocation" OFF)
#* where to look for the project header and source files
set(INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/include)
set(SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/src)
set(TEST_DIR ${CMAKE_CURRENT_LIST_DIR}/test)
set(EXAMPLE_DIR ${CMAKE_CURRENT_LIST_DIR}/examples)
set(BENCHMARK_DIR ${CMAKE_CURRENT_LIST_DIR}/benchmarks)
#* tests, examples, benchmarks to compile
set(TEST_NAMES
sine-test
motor-test
ball-test
first_order-test
)
set(EXAMPLE_NAMES
step-example
loop-example
event-example
first_order-example
)
set(BENCHMARK_NAMES
step-benchmark
loop-benchmark
)
#* files to package
set(PACKAGE_FILES
include/${PROJECT_NAME}/
include/${PROJECT_NAME}.hpp
CMakeLists.txt
LICENSE
)
#* set up output directories
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/dat)
#* project compile options
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_compile_options(
-fdiagnostics-color=always #* colored debug output
-fmessage-length=0 #* disable line wrapping (default=72 char)
-Wall #* all warnings
-Wextra #* more warnings
-Wpedantic #* more warnings
-Wno-array-bounds #* disable false array bounds warning
)
if(DEBUG_BUILD)
add_compile_options(
-g #* debug symbols
-O0 #* no optimization, -Og may work better with some debuggers
)
else()
add_compile_options(
-O3 #* release optimization
)
endif()
if(USE_SINGLE_PRECISION)
add_compile_options(-DUSE_SINGLE_PRECISION) #* use single precision floats
endif()
if(DO_NOT_USE_HEAP)
add_compile_options(-DDO_NOT_USE_HEAP) #* do not use single heap allocation
endif()
#***********#
#* Testing *#
#***********#
if(BUILD_TESTS)
enable_testing()
foreach(ELEMENT ${TEST_NAMES})
add_executable(
${ELEMENT}
${TEST_DIR}/${ELEMENT}.cpp
)
target_include_directories(
${ELEMENT} PRIVATE
${INCLUDE_DIR}
${matrix_op_SOURCE_DIR}/include
${matrix_rw_SOURCE_DIR}/include
)
target_compile_options(
${ELEMENT} PRIVATE
)
add_test(
NAME ${ELEMENT}
COMMAND ${ELEMENT}
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)
endforeach(ELEMENT ${TEST_NAMES})
endif()
#************#
#* Examples *#
#************#
if(BUILD_EXAMPLES)
foreach(ELEMENT ${EXAMPLE_NAMES})
add_executable(
${ELEMENT}
${EXAMPLE_DIR}/${ELEMENT}.cpp
)
target_include_directories(
${ELEMENT} PRIVATE
${INCLUDE_DIR}
${matrix_op_SOURCE_DIR}/include
)
endforeach(ELEMENT ${EXAMPLE_NAMES})
endif()
#*************#
#* Benchmark *#
#*************#
if(BUILD_BENCHMARKS)
foreach(ELEMENT ${BENCHMARK_NAMES})
add_executable(
${ELEMENT}
${BENCHMARK_DIR}/${ELEMENT}.cpp
)
target_include_directories(
${ELEMENT} PRIVATE
${INCLUDE_DIR}
${matrix_op_SOURCE_DIR}/include
)
target_compile_options(
${ELEMENT} PRIVATE
-O3 #* optimization level
-m64 #* x64
-mavx2 #* enable avx2
-fno-exceptions #* disable exceptions
#-fno-math-errno #* disable errno
#-ffast-math #* feeling brave? (may not improve performance)
)
endforeach(ELEMENT ${BENCHMARK_NAMES})
endif()
#************************#
#* Packaged source code *#
#************************#
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/src.zip
COMMAND ${CMAKE_COMMAND} -E tar c ${CMAKE_CURRENT_BINARY_DIR}/src.zip --format=zip -- ${PACKAGE_FILES}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${PACKAGE_FILES}
)
add_custom_target(
src_package
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/src.zip
)
endif()