-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
199 lines (175 loc) · 4.52 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
194
195
196
197
198
199
cmake_minimum_required(VERSION 3.20)
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.27")
cmake_policy(SET CMP0135 NEW)
endif()
project(
template_cpp_project
VERSION 1.0.0
LANGUAGES CXX
)
#* dependencies
Include(FetchContent)
set(matrix_rw_VERSION 1.2.1) #* matrix_rw, used for testing
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()
find_package (Eigen3 3.3 REQUIRED)
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)
#* 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
example_test
)
set(EXAMPLE_NAMES
example
)
set(BENCHMARK_NAMES
)
#* source files
file (GLOB_RECURSE CPP_PROJECT_TEMPLATE_SRC CONFIGURE_DEPENDS "${SRC_DIR}/cpp_project_template/*.cpp")
#* 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)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/data)
#* 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
)
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()
#***************#
#* Executables *#
#***************#
add_executable(
main
${CPP_PROJECT_TEMPLATE_SRC}
)
target_include_directories(
main PRIVATE
${INCLUDE_DIR}
)
target_compile_options(
main PRIVATE
)
target_link_libraries(main PRIVATE
Eigen3::Eigen
)
#***********#
#* 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_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_rw_SOURCE_DIR}/include
)
target_compile_options(
${ELEMENT} PRIVATE
)
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}
)
target_compile_options(
${ELEMENT} PRIVATE
-O3 #* optimization level, alternative: -Ofast (turns on ffast-math etc.)
-m64 #* x64
-mavx2 #* enable avx2
)
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()