forked from david-cortes/isotree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
319 lines (300 loc) · 9.99 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
cmake_minimum_required (VERSION 3.12.4)
project (isotree VERSION 0.5.17)
set(CMAKE_BUILD_TYPE Release)
if ("${CMAKE_CXX_COMPILE_FEATURES}" MATCHES "cxx_std_17")
set(CMAKE_CXX_STANDARD 17)
else()
if ("${CMAKE_CXX_COMPILE_FEATURES}" MATCHES "cxx_std_14")
set(CMAKE_CXX_STANDARD 14)
else()
set(CMAKE_CXX_STANDARD 11)
endif()
endif()
set(CMAKE_CXX_STANDARD_REQUIRED False)
set(SRC_FILES ${PROJECT_SOURCE_DIR}/src/instantiate_template_headers.cpp
${PROJECT_SOURCE_DIR}/src/oop_interface.cpp
${PROJECT_SOURCE_DIR}/src/c_interface.cpp
${PROJECT_SOURCE_DIR}/src/indexer.cpp
${PROJECT_SOURCE_DIR}/src/merge_models.cpp
${PROJECT_SOURCE_DIR}/src/subset_models.cpp
${PROJECT_SOURCE_DIR}/src/serialize.cpp
${PROJECT_SOURCE_DIR}/src/sql.cpp)
set(BUILD_SHARED_LIBS True)
add_library(isotree SHARED ${SRC_FILES})
target_include_directories(isotree PRIVATE ${PROJECT_SOURCE_DIR}/src)
set(HEADER_FILES "${PROJECT_SOURCE_DIR}/include/isotree.hpp"
"${PROJECT_SOURCE_DIR}/include/isotree_oop.hpp"
"${PROJECT_SOURCE_DIR}/include/isotree_c.h")
set_target_properties(isotree PROPERTIES PUBLIC_HEADER "${HEADER_FILES}")
add_compile_definitions(ISOTREE_COMPILE_TIME)
## set to OFF to use the system's default RNG engine
option(USE_XOSHIRO "Use the xoshiro RNG family" ON)
if (USE_XOSHIRO)
message(STATUS "Using the Xoshiro random engine family")
add_compile_definitions(_USE_XOSHIRO)
else()
message(STATUS "Using the compiler's default random engine")
endif()
## set to ON to avoid compiling with 'long double' type
## (its usage is optional and selectable at runtime, but makes
## the compiled binaries much heavier)
option(NO_LONG_DOUBLE "Avoid adding support for 'long double' type" OFF)
if (NO_LONG_DOUBLE)
message(STATUS "Avoid adding support for 'long double' type.")
add_compile_definitions(NO_LONG_DOUBLE)
endif()
## set to ON to compile for only 'double' and 'int' types
## (otherwise, compiles also for 'float', 'int64_t', 'size_t')
option(NO_TEMPLATED_VERSIONS "Don't create multiple templated versions for different data types" OFF)
if (NO_TEMPLATED_VERSIONS)
message(STATUS "Building with support for only 'double' and 'int' types.")
add_compile_definitions(NO_TEMPLATED_VERSIONS)
endif()
## set to OFF to export all symbols
include(CheckCXXSourceCompiles)
option(HIDE_INTERNAL_SYMBOLS "Set hidden visibility for non-exported symbols" ON)
if (HIDE_INTERNAL_SYMBOLS AND NOT WIN32)
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS " -fvisibility=hidden")
check_cxx_source_compiles(
"
[[gnu::visibility(\"default\")]]
int myfun() {
return 0;
}
int main(int argc, char **argv)
{
return myfun();
}
"
SUPPORTS_FVISIBILITY_HIDDEN
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_FVISIBILITY_HIDDEN)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fvisibility=hidden")
add_compile_definitions(EXPLICITLTY_EXPORT_SYMBOLS)
else()
message(STATUS "Hidden symbols visibility not supported by the compiler, will export all symbols.")
endif()
else()
message(STATUS "Making all symbols in the library visible.")
endif()
## https://cliutils.gitlab.io/modern-cmake/chapters/packages/OpenMP.html
find_package(OpenMP)
if (OpenMP_CXX_FOUND)
target_link_libraries(isotree PUBLIC OpenMP::OpenMP_CXX)
else()
message(STATUS "OpenMP not found - will compile without multi-threading support")
endif()
# check if the robin-map library is available under /src
set(CMAKE_REQUIRED_INCLUDES ${PROJECT_SOURCE_DIR}/src)
check_cxx_source_compiles(
"
#include \"robinmap/include/tsl/robin_growth_policy.h\"
int main(int argc, char **argv)
{
return 0;
}
"
HAS_ROBIN_MAP
)
if (HAS_ROBIN_MAP)
add_compile_definitions(_USE_ROBIN_MAP)
else()
# if it's not under /src, check if there's a system install
find_package(tsl-robin-map)
if (tsl-robin-map_FOUND)
target_link_libraries(isotree PRIVATE tsl::robin_map)
add_compile_definitions(_USE_SYSTEM_ROBIN)
else()
message(WARNING "Robin-Map Headers not found - will use STL hashmaps which are slower.")
endif()
endif()
# For handling large files with MinGW
if (WIN32)
if (CMAKE_SIZEOF_VOID_P GREATER_EQUAL 8 AND (MSYS OR MINGW OR GCC))
add_compile_definitions(_FILE_OFFSET_BITS=64)
endif()
endif()
# Compiler optimizations
check_cxx_source_compiles(
"
#include <cstddef>
int main(int argc, char **argv)
{
double *__restrict x = nullptr;
return 0;
}
"
SUPPORTS_RESTRICT
)
if (SUPPORTS_RESTRICT)
add_compile_definitions(SUPPORTS_RESTRICT=1)
endif()
if (MSVC)
if (NOT (${CMAKE_CXX_FLAGS_RELEASE} MATCHES "/O2"))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "/O2")
check_cxx_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_O2
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_O2)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2")
endif()
endif()
if (NOT (${CMAKE_CXX_FLAGS_RELEASE} MATCHES "/fp:except-"))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "/fp:except-")
check_cxx_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_FPNOEX
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_FPNOEX)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /fp:except-")
endif()
endif()
else()
if (NOT (${CMAKE_CXX_FLAGS_RELEASE} MATCHES "-O3 "))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "-O3")
check_cxx_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_O3
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_O3)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
else()
set(CMAKE_REQUIRED_FLAGS "-O2")
check_cxx_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_O2
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_O2)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
endif()
endif()
endif()
if (NOT (${CMAKE_CXX_FLAGS_RELEASE} MATCHES "-fno-trapping-math"))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "-fno-trapping-math")
check_cxx_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_FNTM
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_FNTM)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-trapping-math")
endif()
endif()
if (NOT (${CMAKE_CXX_FLAGS_RELEASE} MATCHES "-fno-math-errno"))
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "-fno-math-errno")
check_cxx_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_FNE
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_FNE)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-math-errno")
endif()
endif()
endif()
option(USE_MARCH_NATIVE "Build with -march=native" OFF)
if (USE_MARCH_NATIVE AND NOT MSVC)
set(OLD_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "-march=native")
check_cxx_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_MARCH_NATIVE
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_MARCH_NATIVE)
message(STATUS "Adding flag -march=native.")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native")
else()
set(CMAKE_REQUIRED_FLAGS "-mcpu=native")
check_cxx_source_compiles(
"
int main(int argc, char **argv)
{
return 0;
}
"
SUPPORTS_MCPU_NATIVE
)
set(CMAKE_REQUIRED_FLAGS ${OLD_FLAGS})
if (SUPPORTS_MCPU_NATIVE)
message(STATUS "Adding flag -mcpu=native.")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -mcpu=native")
else()
message(WARNING "Flags -march=native and -mcpu=native not supported by the compiler.")
endif()
endif()
endif()
# Link-time optimization if supported
# https://stackoverflow.com/questions/31355692/how-do-i-enable-link-time-optimization-lto-with-cmake
include(CheckIPOSupported)
check_ipo_supported(RESULT LTO_SUPPORTED)
if (LTO_SUPPORTED)
set_property(TARGET isotree PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()
include(GNUInstallDirs)
install(
TARGETS isotree
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
configure_file(isotree.pc.in isotree.pc @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/isotree.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
# uninstall target
if (NOT TARGET uninstall)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY
)
add_custom_target(
uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
)
endif()