-
Notifications
You must be signed in to change notification settings - Fork 12
/
CMakeLists.txt
218 lines (184 loc) · 6.31 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
cmake_minimum_required(VERSION 3.16)
cmake_policy(VERSION 3.16)
if(NOT SONATA_VERSION)
execute_process(COMMAND git describe --tags
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_VERSION_FAILED
OUTPUT_VARIABLE GIT_PKG_VERSION_FULL
ERROR_VARIABLE GIT_VERSION_ERROR
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(GIT_VERSION_FAILED)
message(
FATAL_ERROR
"Could not retrieve version from command 'git describe --tags'\n"
${GIT_VERSION_ERROR})
endif()
# keep last line of command output
string(REPLACE "\n"
";"
GIT_PKG_VERSION_FULL
"${GIT_PKG_VERSION_FULL}")
list(GET GIT_PKG_VERSION_FULL -1 SONATA_VERSION)
endif()
# keep MAJOR.MINOR.PATCH (PATCH being optional)
string(REGEX
REPLACE "v?([0-9]+\\.[0-9]+(\\.[0-9]+)?).*"
"\\1"
SONATA_VERSION
"${SONATA_VERSION}")
# Get "major.minor" from string "major.minor.version"
string(REGEX MATCH "^(.*)\\.[^.]*$" dummy ${SONATA_VERSION})
set(SONATA_VERSION_ABI ${CMAKE_MATCH_1})
project(sonata VERSION ${SONATA_VERSION})
option(EXTLIB_FROM_SUBMODULES "Use Git submodules for header-only dependencies" OFF)
option(SONATA_PYTHON "Build Python extensions" OFF)
option(SONATA_TESTS "Build tests" ON)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(SONATA_ENABLE_COVERAGE_DEFAULT ON)
else()
set(SONATA_ENABLE_COVERAGE_DEFAULT OFF)
endif()
option(SONATA_ENABLE_COVERAGE "Enable measuring test coverage." ${SONATA_ENABLE_COVERAGE_DEFAULT})
# For `Findcoverage.cmake` and friends.
set(ENABLE_COVERAGE ${SONATA_ENABLE_COVERAGE})
option(SONATA_CXX_WARNINGS "Compile C++ with warnings as errors, for glibcxx turn on assertions" ON)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake)
set(SONATA_CXX_MINIMUM_STANDARD 14)
if(NOT "${CMAKE_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD ${SONATA_CXX_MINIMUM_STANDARD})
elseif("${CMAKE_CXX_STANDARD}" LESS SONATA_CXX_MINIMUM_STANDARD)
message(FATAL_ERROR
"SONATA requires at 'CMAKE_CXX_STANDARD=${SONATA_CXX_MINIMUM_STANDARD}' or newer.")
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(SONATA_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(SONATA_COMPILE_OPTIONS -Wall -Wextra -pedantic)
if(SONATA_CXX_WARNINGS)
set(SONATA_COMPILE_OPTIONS ${SONATA_COMPILE_OPTIONS} -Werror -Wp,-D_GLIBCXX_ASSERTIONS)
endif()
# =============================================================================
# Dependencies
# =============================================================================
if (EXTLIB_FROM_SUBMODULES)
add_subdirectory(extlib EXCLUDE_FROM_ALL)
else()
if (SONATA_TESTS)
find_package(Catch2 REQUIRED)
endif()
find_package(HighFive REQUIRED)
find_package(fmt REQUIRED)
find_package(nlohmann_json REQUIRED)
endif()
# =============================================================================
# Targets
# =============================================================================
set(SONATA_SRC
src/common.cpp
src/config.cpp
src/edge_index.cpp
src/edges.cpp
src/hdf5_mutex.cpp
src/hdf5_reader.cpp
src/node_sets.cpp
src/nodes.cpp
src/population.cpp
src/report_reader.cpp
src/selection.cpp
src/utils.cpp
${CMAKE_CURRENT_BINARY_DIR}/src/version.cpp
)
configure_file (
${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp.in
${CMAKE_CURRENT_BINARY_DIR}/src/version.cpp
)
add_library(sonata_shared SHARED ${SONATA_SRC})
add_library(sonata_static STATIC ${SONATA_SRC})
foreach(TARGET sonata_shared sonata_static)
set_target_properties(${TARGET}
PROPERTIES
POSITION_INDEPENDENT_CODE ON
CXX_VISIBILITY_PRESET hidden
OUTPUT_NAME "sonata"
)
target_include_directories(${TARGET}
PUBLIC
$<BUILD_INTERFACE:${SONATA_INCLUDE_DIR}>
$<INSTALL_INTERFACE:include>
)
target_compile_options(${TARGET}
PRIVATE ${SONATA_COMPILE_OPTIONS}
)
if (ENABLE_COVERAGE)
target_compile_options(${TARGET}
PRIVATE -g -O0 --coverage -fprofile-arcs -ftest-coverage
)
target_link_libraries(${TARGET}
PRIVATE gcov
)
endif()
add_library(sonata::${TARGET} ALIAS ${TARGET})
endforeach(TARGET)
set_target_properties(sonata_shared
PROPERTIES
VERSION ${SONATA_VERSION}
SOVERSION ${SONATA_VERSION_ABI}
)
target_compile_definitions(sonata_shared
PUBLIC SONATA_DLL
PRIVATE SONATA_DLL_EXPORTS
)
target_link_libraries(sonata_shared
PRIVATE HighFive
PRIVATE fmt::fmt-header-only
PRIVATE nlohmann_json::nlohmann_json
)
target_compile_definitions(sonata_static
PRIVATE FMT_HEADER_ONLY=1
)
target_include_directories(sonata_static
PRIVATE $<TARGET_PROPERTY:fmt::fmt-header-only,INTERFACE_INCLUDE_DIRECTORIES>
PRIVATE $<TARGET_PROPERTY:HighFive,INTERFACE_INCLUDE_DIRECTORIES>
PRIVATE $<TARGET_PROPERTY:nlohmann_json::nlohmann_json,INTERFACE_INCLUDE_DIRECTORIES>
)
# =============================================================================
# Install
# =============================================================================
install(TARGETS sonata_shared sonata_static
EXPORT sonata-targets
LIBRARY
DESTINATION lib
ARCHIVE
DESTINATION lib
)
install(DIRECTORY ${SONATA_INCLUDE_DIR}/bbp
DESTINATION include
)
install(FILES CMake/sonata-config.cmake
DESTINATION share/sonata/CMake
)
install(EXPORT sonata-targets
DESTINATION share/sonata/CMake
NAMESPACE sonata::
)
# =============================================================================
# Testing
# =============================================================================
if (SONATA_TESTS)
enable_testing()
add_subdirectory(tests)
if (ENABLE_COVERAGE)
include(CodeCoverage)
set(COVERAGE_LCOV_EXCLUDES '/usr/*' '${PROJECT_SOURCE_DIR}/include/*' '${PROJECT_SOURCE_DIR}/extlib/*')
SETUP_TARGET_FOR_COVERAGE_LCOV(
NAME coverage
EXECUTABLE ctest
DEPENDENCIES unittests
)
endif()
endif()
# =============================================================================
# Python bindings
# =============================================================================
if (SONATA_PYTHON)
add_subdirectory(python)
endif()