CMakeLists.txt 8.79 KB
Newer Older
Simon Spannagel's avatar
Simon Spannagel committed
1
2
3
4
5
# CMake file for the Corryvreckan framework
CMAKE_MINIMUM_REQUIRED(VERSION 3.4.3 FATAL_ERROR)
IF(COMMAND CMAKE_POLICY)
  CMAKE_POLICY(SET CMP0003 NEW) # change linker path search behaviour
  CMAKE_POLICY(SET CMP0048 NEW) # set project version
6
  CMAKE_POLICY(SET CMP0077 NEW) # allow overwriting options with normal variables
Simon Spannagel's avatar
Simon Spannagel committed
7
8
9
ENDIF(COMMAND CMAKE_POLICY)

# Set default version
Jens Kroeger's avatar
Jens Kroeger committed
10
SET(CORRYVRECKAN_VERSION "v1.0")
Simon Spannagel's avatar
Simon Spannagel committed
11
12
13
14
15
16
17
18
19
20
21
22
23

# Set default build type
IF(NOT CMAKE_BUILD_TYPE)
  SET(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
ENDIF(NOT CMAKE_BUILD_TYPE)

# Overwrite with the version from git if found
INCLUDE("cmake/tools.cmake")
GET_VERSION(CORRYVRECKAN_VERSION)

# Print version
MESSAGE(STATUS "Building Corryvreckan version ${CORRYVRECKAN_VERSION}.")

24
25
26
# Gather information about build time:
STRING(TIMESTAMP BUILD_TIME "%Y-%m-%d, %H:%M:%S UTC" UTC)

Simon Spannagel's avatar
Simon Spannagel committed
27
28
29
30
31
# Define the project with the simple version
STRING(REGEX MATCH "([0-9.]+)+" SIMPLE_VERSION "${CORRYVRECKAN_VERSION}")
# Set languages to NONE to allow the documentation to be built without CXX compiler:
PROJECT(Corryvreckan VERSION ${SIMPLE_VERSION} LANGUAGES NONE)
# Access the project name (for install locations) in the source
32
ADD_DEFINITIONS(-DCORRYVRECKAN_PROJECT_NAME="${CMAKE_PROJECT_NAME}" -DCORRYVRECKAN_PROJECT_VERSION="${CORRYVRECKAN_VERSION}" -DCORRYVRECKAN_BUILD_TIME="${BUILD_TIME}")
Simon Spannagel's avatar
Simon Spannagel committed
33

34
35
36
INCLUDE(cmake/CPackConfig.cmake)
INCLUDE(CPack)

37
38
39
40
41
42
43
44
45
46
47
48
49
###############################################
# Setup the environment for the documentation #
###############################################

OPTION(BUILD_DOCS_ONLY "Build documentation only" OFF)

# Add targets for Doxygen code reference and LaTeX User manual
ADD_SUBDIRECTORY(doc)

# If only building docs, stop processing the rest of the CMake file:
IF(BUILD_DOCS_ONLY)
  RETURN()
ENDIF()
Simon Spannagel's avatar
Simon Spannagel committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

###############################
# Setup the build environment #
###############################

# Enable CXX as project language to perform compiler checks:
ENABLE_LANGUAGE(CXX)

# Additional packages to be searched for by cmake
LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

# Configure the installation prefix to allow both local as system-wide installation
IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    SET(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}" CACHE PATH "Prefix prepended to install directories" FORCE)
ENDIF()

# Set up the RPATH so executables find the libraries even when installed in non-default location
SET(CMAKE_MACOSX_RPATH TRUE)
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
69
SET(CMAKE_SKIP_INSTALL_RPATH FALSE)
Simon Spannagel's avatar
Simon Spannagel committed
70
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
71
SET(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_FULL_LIBDIR})
Simon Spannagel's avatar
Simon Spannagel committed
72
73
74
75
76
77
78
79
80
81

# Add the automatically determined parts of the RPATH which point to directories outside the build tree to the install RPATH
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# The RPATH to be used when installing, but only if it's not a system directory
LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" IsSystemDir)
IF("${IsSystemDir}" STREQUAL "-1")
   SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
ENDIF("${IsSystemDir}" STREQUAL "-1")

82
83
84
85
86
IF(APPLE)
  SET(CMAKE_INSTALL_NAME_DIR "@rpath")
  SET(CMAKE_INSTALL_RPATH "@loader_path/../lib") # self relative LIBDIR
ENDIF()

87
88
89
# Include corry cmake functions
INCLUDE("cmake/corryvreckan.cmake")

Simon Spannagel's avatar
Simon Spannagel committed
90
91
92
93
94
#########################################
# Define build flags for Corryvreckan   #
#########################################

# Set standard build flags
95
SET(COMPILER_FLAGS -pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wconversion -Wuseless-cast -Wctor-dtor-privacy -Wzero-as-null-pointer-constant -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wswitch-default -Wundef -Wshadow -Wformat-security -Wdeprecated -fdiagnostics-color=auto -Wheader-hygiene)
Simon Spannagel's avatar
Simon Spannagel committed
96

97
98
99
100
101
102
103
104
105
INCLUDE("cmake/compiler-flag-checks.cmake")

# Check for C++17 and require C++17 if available else fallback to C++14 (should currently be sufficient)
CHECK_CXX_COMPILER_FLAG(-std=c++17 SUPPORT_STD_CXX17)
IF(SUPPORT_STD_CXX17)
    SET(CMAKE_CXX_STANDARD 17)
ELSE()
    SET(CMAKE_CXX_STANDARD 14)
ENDIF()
Simon Spannagel's avatar
Simon Spannagel committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_EXTENSIONS OFF)

# Set some debug flags
# FIXME: not using the flag checker now because it wrongly rejects a sanitizer flag..
IF(CMAKE_BUILD_TYPE MATCHES Debug AND ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")))
    MESSAGE(STATUS "Running debug build - Adding extra sanitizer flags")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address -fsanitize=undefined")
    # FIXME: remove earlier shared linker flags, because they can break the sanitizer debug build
    SET(CMAKE_SHARED_LINKER_FLAGS "-fsanitize=address -fsanitize=undefined")
ENDIF()

###################################
# Prerequisistes for Corryvreckan #
###################################

# Define the libraries
SET(CORRYVRECKAN_LIBRARIES "")

# ROOT is required for vector and persistency etc
126
FIND_PACKAGE(ROOT REQUIRED COMPONENTS Minuit Minuit2 Gui GenVector Geom Core Hist RIO NO_MODULE)
Simon Spannagel's avatar
Simon Spannagel committed
127
128
129
130
131
IF(NOT ROOT_FOUND)
    MESSAGE(FATAL_ERROR "Could not find ROOT, make sure to source the ROOT environment\n"
    "$ source YOUR_ROOT_DIR/bin/thisroot.sh")
ENDIF()

132
# Downgrade to C++14 if ROOT is not build with C++17 support
133
IF(ROOT_CXX_FLAGS MATCHES ".*std=c\\+\\+1[7z].*")
134
135
136
    IF(NOT SUPPORT_STD_CXX17)
        MESSAGE(FATAL_ERROR "ROOT was built with C++17 support but current compiler doesn't support it")
    ENDIF()
137
ELSEIF(ROOT_CXX_FLAGS MATCHES ".*std=c\\+\\+1[14y].*")
138
139
140
141
142
143
144
    SET(CMAKE_CXX_STANDARD 14)
ELSEIF(ROOT_CXX_FLAGS MATCHES ".*std=c\\+\\+.*")
    MESSAGE(FATAL_ERROR "ROOT was built with an unsupported C++ version: ${ROOT_CXX_FLAGS}")
ELSE()
    MESSAGE(FATAL_ERROR "Could not deduce ROOT's C++ version from build flags: ${ROOT_CXX_FLAGS}")
ENDIF()

Simon Spannagel's avatar
Simon Spannagel committed
145
146
147
148
149
# Check ROOT version
IF (NOT ${ROOT_VERSION} VERSION_GREATER "6.0")
    MESSAGE(FATAL_ERROR "ROOT versions below 6.0 are not supported")
ENDIF()

Lennart Huth's avatar
Lennart Huth committed
150
# Eigen3
151
FIND_PACKAGE(Eigen3 REQUIRED NO_MODULE)
Lennart Huth's avatar
Lennart Huth committed
152

153
154
155
156
# Prepare ROOT and EIGEN3 Targets if necessary:
CORRYVRECKAN_SETUP_ROOT_TARGETS()
CORRYVRECKAN_SETUP_EIGEN_TARGETS()

157
158
ADD_SUBDIRECTORY(3rdparty/GeneralBrokenLines)

Simon Spannagel's avatar
Simon Spannagel committed
159
# Set the dependencies
160
SET(CORRYVRECKAN_DEPS_INCLUDE_DIRS ${ROOT_INCLUDE_DIRS} ${EIGEN3_INCLUDE_DIR})
161
SET(CORRYVRECKAN_DEPS_LIBRARIES Eigen3::Eigen ROOT::Core ROOT::GenVector ROOT::Geom ROOT::RIO ROOT::Hist ROOT::Minuit ROOT::Minuit2 GBL)
Simon Spannagel's avatar
Simon Spannagel committed
162

163
164
165
166
167
# Add the LCG view as dependency if set:
IF(DEFINED ENV{LCG_VIEW})
    ADD_RUNTIME_DEP($ENV{LCG_VIEW})
ENDIF()

168
169
# Add "thisroot.sh" as runtime dependency for setup.sh file:
ADD_RUNTIME_DEP(thisroot.sh)
Simon Spannagel's avatar
Simon Spannagel committed
170

Simon Spannagel's avatar
Simon Spannagel committed
171

Simon Spannagel's avatar
Simon Spannagel committed
172
173
174
175
###################################
# Load cpp format and check tools #
###################################

Simon Spannagel's avatar
Simon Spannagel committed
176
# Set the clang-format version required by the CI for correct formatting:
177
178
179
SET(CLANG_FORMAT_VERSION "10")
# Set the clang-tidy version of the linter required by the CI:
SET(CLANG_TIDY_VERSION "10")
Simon Spannagel's avatar
Simon Spannagel committed
180

Simon Spannagel's avatar
Simon Spannagel committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Set the source files to clang-format (FIXME: determine this better)
FILE(GLOB_RECURSE
     CHECK_CXX_SOURCE_FILES
        src/*.[tch]pp src/*.h src/*.C
     )
INCLUDE("cmake/clang-cpp-checks.cmake")

#########################################
# Define build targets for Corryvreckan #
#########################################

# Always include sources from top directory
INCLUDE_DIRECTORIES(src)

195
196
SET(CORRYVRECKAN_LIBRARIES ${CORRYVRECKAN_LIBRARIES} CorryvreckanUtilities)

197
198
199
200
# Build objects library
ADD_SUBDIRECTORY(src/objects)
SET(CORRYVRECKAN_LIBRARIES ${CORRYVRECKAN_LIBRARIES} CorryvreckanObjects)

Simon Spannagel's avatar
Simon Spannagel committed
201
202
203
204
205
206
# Build core Corryvreckan library
ADD_SUBDIRECTORY(src/core)
SET(CORRYVRECKAN_LIBRARIES ${CORRYVRECKAN_LIBRARIES} CorryvreckanCore)


# Build required modules
207
ADD_SUBDIRECTORY(src/modules)
Simon Spannagel's avatar
Simon Spannagel committed
208
209
210

# Build the executable
ADD_SUBDIRECTORY(src/exec)
211

212
213
214
215
216
217
218
219
220
221
###################################
# Setup tests for allpix          #
###################################

# Enable testing
ENABLE_TESTING()

# Include all tests
ADD_SUBDIRECTORY(testing)

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#############################
# Create a local setup file #
#############################

# Add compiler if necessary:
GET_FILENAME_COMPONENT(CMP_PATH ${CMAKE_CXX_COMPILER} DIRECTORY)
GET_FILENAME_COMPONENT(PARENT_PATH ${CMP_PATH} DIRECTORY)
IF(EXISTS ${PARENT_PATH}/setup.sh)
  LIST(APPEND CORRY_RUNTIME_DEPS "${PARENT_PATH}/setup.sh")
ENDIF()

# Build configuration string with commands
FOREACH(dep ${CORRY_RUNTIME_DEPS})
    SET(SETUP_FILE_DEPS "${SETUP_FILE_DEPS}source ${dep}\n")
ENDFOREACH()
FOREACH(dep ${CORRY_RUNTIME_LIBS})
    SET(SETUP_FILE_DEPS "${SETUP_FILE_DEPS}export LD_LIBRARY_PATH=\"${dep}:$LD_LIBRARY_PATH\"\n")
ENDFOREACH()
# Create setup file
CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/cmake/setup.cmake.sh" "${CMAKE_CURRENT_BINARY_DIR}/setup/setup.sh" @ONLY)