-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
227 lines (201 loc) · 6.29 KB
/
CMakeLists.txt
File metadata and controls
227 lines (201 loc) · 6.29 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Copyright 2026, Cinar, A. L.
# SPDX-License-Identifier: GPL-3.0-only
cmake_minimum_required(VERSION 3.21)
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(PROJECT_IS_TOP_LEVEL)
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(PROJECT_IS_TOP_LEVEL)
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)
-pipe # avoid temporary files
)
# warnings:
# https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
# https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html
add_compile_options(
-Wall # all warnings
-Wextra # more warnings
-Wpedantic # MORE warnings
-Wconversion # warn for implicit conversions
-Wsign-conversion # warn for implicity conversion of signed/unsigned ints
-Wcast-qual # "Warn whenever a pointer is cast so as to remove a type qualifier from the target type"
-Wformat=2 # check calls to printf and scanf etc make sense (equivalent to -Wformat -Wformat-nonliteral -Wformat-security -Wformat-y2k)
-Wundef # warn for undefined identifiers in #if directives
-Werror=float-equal # turn float equality comparisons to errors
-Wshadow # warn for all instances of shadowing of another variable, parameter, type, class member
-Wcast-align # warn for pointer cast alignment mismatch
-Wunused # warn for unused (-Wall implies this already)
-Wnull-dereference # warn for undefined dereferencing a null pointer
-Wdouble-promotion # warn if float is implicitly promoted to double
-Wimplicit-fallthrough # warn for switch case fallthrough
-Wextra-semi # warn about redundant semicolons
-Woverloaded-virtual # warn when a function declaration hides virtual functions from a base class
-Wnon-virtual-dtor # warn when a class is possible to but unsafe to delete and instance of derived class
-Wold-style-cast # warn if C-style cast to non-void type is used in C++
)
if(DEBUG_BUILD)
add_compile_options(
-O0 # no optimization, -Og may work better with some debuggers
-g # debug symbols
-grecord-gcc-switches # store compiler flags in debugging information
)
else()
add_compile_options(
-O3 # release optimization
-fhardened # equivalent to: (-D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS -ftrivial-auto-var-init=zero -fPIE -pie -Wl,-z,relro,-z,now -fstack-protector-strong -fstack-clash-protection -fcf-protection=full (x86 GNU/Linux only)) https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html
)
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()