-
Notifications
You must be signed in to change notification settings - Fork 5
/
CMakeLists.txt
226 lines (179 loc) · 6.75 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
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
cmake_minimum_required(VERSION 3.9.0)
# Check + Enforce build type
set(default_build_type "Release")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE
STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Release")
endif()
if (CMAKE_BUILD_TYPE MATCHES "^((p|P)(rofile|rof))|(release)$")
message(STATUS "Setting build type to 'Release' instead of '${CMAKE_BUILD_TYPE}'.")
set(CMAKE_BUILD_TYPE "Release" CACHE
STRING "Choose the type of build." FORCE)
endif()
if (CMAKE_BUILD_TYPE MATCHES "^debug$")
message(STATUS "Setting build type to 'Debug' instead of '${CMAKE_BUILD_TYPE}'.")
set(CMAKE_BUILD_TYPE "Debug" CACHE
STRING "Choose the type of build." FORCE)
endif()
if (NOT CMAKE_BUILD_TYPE MATCHES "^(Debug|Release)$")
message(FATAL_ERROR "'${CMAKE_BUILD_TYPE}' is not a valid build type. Please choose either 'Release' or 'Debug'.")
endif()
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}" )
# enable c++17 standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED on)
# make sure CMake is able to find ROOT
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR})
set(CMAKE_FIND_LIBRARY_PREFIXES lib)
set(CMAKE_FIND_LIBRARY_SUFFIXES .dylib .so)
MESSAGE("CMAKE_FIND_LIBRARY_PREFIXES =${CMAKE_FIND_LIBRARY_PREFIXES}")
MESSAGE("CMAKE_FIND_LIBRARY_SUFFIXES =${CMAKE_FIND_LIBRARY_SUFFIXES}")
MESSAGE("CMAKE_SOURCE_DIR =${CMAKE_SOURCE_DIR}")
MESSAGE("CMAKE_MODULE_PATH =${CMAKE_MODULE_PATH}")
# check for KinKal
find_package(KINKAL REQUIRED)
IF(NOT KINKAL_FOUND)
MESSAGE(FATAL_ERROR "Could not find KinKal. You need to source (KinKal build directory)/setup.sh")
ENDIF()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# versioning
set (_build_ver_maj 0)
set (_build_ver_min 0)
set (_build_ver_patch 0)
set (_build_ver_commits_since_tag 0)
set (_build_ver_commit_short 0)
# find git information
find_package(Git)
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
OUTPUT_VARIABLE _build_version
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
message( STATUS "git describes this revision as: ${_build_version}")
if (_build_version MATCHES "^[vV]?([0-9]+)[._]([0-9]+)[._]([0-9]+)(.*)$")
set (_build_ver_maj ${CMAKE_MATCH_1})
set (_build_ver_min ${CMAKE_MATCH_2})
set (_build_ver_patch ${CMAKE_MATCH_3})
set (_build_ver_commits_since_tag ${CMAKE_MATCH_4})
# set (_build_ver_commit_short ${CMAKE_MATCH_5})
message( STATUS "Version string correctly matched: major= ${_build_ver_maj}, minor= ${_build_ver_min}, patch= ${_build_ver_patch}, other=${_build_ver_commits_since_tag}")
else()
message ("Version not parsed!")
endif()
else()
message( STATUS "Git not found!")
endif()
# Set project name and version
project (TrackToy
LANGUAGES CXX
VERSION ${_build_ver_maj}.${_build_ver_min}.${_build_ver_patch}
DESCRIPTION "Toy MC for studying tracking detector performance"
)
message( STATUS "Project Version: ${CMAKE_PROJECT_VERSION}")
message( STATUS "Commits made since last tag: ${_build_ver_commits_since_tag}")
message( STATUS "Commit short hash: ${_build_ver_commit_short}")
# find the ROOT dependency
find_package(ROOT REQUIRED COMPONENTS
Core RIO Net Hist GenVector MLP Graf Graf3d Gpad Tree
Rint Postscript Matrix Physics MathCore Thread Geom Gui)
# include "useful" ROOT cmake functions
# which are useful for building dictionaries
include(${ROOT_USE_FILE})
# set RPATH options
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# output shared libraries and executables in lib/ and bin/
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
# enable tests
enable_testing()
# compiler flags
add_compile_options(
# flags applied to ALL build types
"-Wall"
"-Wno-unused-parameter"
"-Wno-unused-local-typedefs"
"-Werror"
"-gdwarf-2"
"-Werror=return-type"
"-Winit-self"
"-Woverloaded-virtual"
"-ftrapping-math"
"-fdiagnostics-color=always"
# "-Wshadow"
# debug flags
"$<$<CONFIG:DEBUG>:-O0;-g>"
# release flags
"$<$<CONFIG:RELEASE>:-O3;-DNDEBUG;-fno-omit-frame-pointer>"
)
# install rules
include(GNUInstallDirs)
# clang tidy
if(ENABLE_CLANG_TIDY)
set(CMAKE_CXX_CLANG_TIDY "clang-tidy")
endif()
if(ENABLE_CLANG_TIDY_WITH_FIXES)
set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-fix")
endif()
if (CMAKE_BUILD_TYPE MATCHES "^Debug$")
if (COVERAGE)
add_compile_options("-fprofile-arcs" "-ftest-coverage")
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# using Clang / LLVM
link_libraries(-fprofile-instr-generate)
add_custom_target(coverage
COMMAND gcovr --gcov-executable "llvm-cov gcov" -r ${CMAKE_SOURCE_DIR} ${PROJECT_BINARY_DIR} -s --exclude-directories Mains
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
link_libraries(-lgcov) # gcov symbols
add_custom_target(coverage
COMMAND gcovr -r ${CMAKE_SOURCE_DIR} ${PROJECT_BINARY_DIR} -s --exclude-directories Mains
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
)
endif()
endif()
endif()
# add shared library targets
add_subdirectory(Detector)
add_subdirectory(Spectra)
add_subdirectory(General)
add_subdirectory(Test)
add_subdirectory(Mains)
install(TARGETS Spectra Detector General Test
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# install headers
# Globbing here is fine because it does not influence build behaviour
install(DIRECTORY "${CMAKE_SOURCE_DIR}/"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/TrackToy
FILES_MATCHING PATTERN "*.hh"
PATTERN ".git*" EXCLUDE
)
message (STATUS "Writing setup.sh...")
file(WRITE ${PROJECT_BINARY_DIR}/setup.sh "
#
# Do not hand edit this file. It was generated by CMake.
(return 0 2>/dev/null) && sourced=1 || sourced=0
if [ \"\$sourced\" = \"0\" ];then
echo \"You should be sourcing this file, not executing it.\"
exit 1
fi
export DEBUG_LEVEL=${CMAKE_BUILD_TYPE}
# Linux:
export LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/lib:\${LD_LIBRARY_PATH}
# MacOS:
export DYLD_FALLBACK_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/lib:\${ROOT_LIB}:\${DYLD_FALLBACK_LIBRARY_PATH}
# Define the TrackToy package for others:
export TRACKTOY_SOURCE_DIR=${CMAKE_SOURCE_DIR}
export TRACKTOY_LIBRARY_DIR=${CMAKE_CURRENT_BINARY_DIR}/lib
")