forked from blond-org/BLonD-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
440 lines (366 loc) · 15.9 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# usage: cmake -DWITH_FFTW=True -DWITH_GOOGLETEST=True -DWITH_BENCHMARK=True
# cmake -DWITH_FFTW=True -DWITH_GOOGLETEST=True -DWITH_BENCHMARK=True -DCMAKE_BUILD_TYPE=Debug to use asan
# for clion like editors that build in remote folders:
# -DDATAFILES_DIR_DEMOS="path/to/demos/input_files" -DDATAFILES_DIR_TESTS="unit-tests/references"; Default "../demos/input_files" and "../unit-tests/references"
# Requirements list
cmake_minimum_required(VERSION 2.8)
project(BLonD-minimal-cpp CXX)
include(CheckCXXCompilerFlag)
include(ExternalProject)
include(ProcessorCount)
include(CTest)
# Code editors usabilety feature
set_property(GLOBAL PROPERTY USE_FOLDERS On)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "We have optemized configuration for Debug and Release configurations")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "We have optemized configuration for Debug and Release configurations")
# Variables default values list
set(CMAKE_BUILD_TYPE Release CACHE STRING "default build type for Make-like generators")
set(SHARED_LIBRARY_ENABLED "True")
set(EXTERNAL_INSTALL_DIR ${PROJECT_SOURCE_DIR}/external/install)
set(TEST_ENABLED "True" CACHE STRING "Cerate unit-test projects")
set(BENCHMARK_ENABLED "True" CACHE STRING "Cerate benchmark projects")
if (NOT DEFINED ARCHITECTURE)
set(ARCHITECTURE "Default" CACHE STRING "Set architecture 'x86' or 'x64'") # todo: find usages and fix comment
# Project architecture
if ((${CMAKE_GENERATOR} MATCHES ".*(64).*") OR (${CMAKE_SIZEOF_VOID_P} MATCHES ".*(64).*"))
set(ARCHITECTURE "x64")
message(STATUS "architecture: ${ARCHITECTURE} detected from (${CMAKE_GENERATOR} and ${CMAKE_SIZEOF_VOID_P}) ")
endif ()
endif ()
set(WITH_COMPUTE "False" CACHE STRING "Should Build Boost.Compute library from source")
set(BOOST_ROOT ${PROJECT_SOURCE_DIR}/external/build/boost CACHE STRING "Boost Library root")
if (NOT ABSOLUTE_DATA_PATHS)
set(DATAFILES_DIR_DEMOS "../demos/input_files" CACHE STRING "Set demos input files path")
set(DATAFILES_DIR_TESTS "../unit-tests/references" CACHE STRING "Set unit-tests input files path")
else ()
set(DATAFILES_DIR_DEMOS "${CMAKE_CURRENT_LIST_DIR}/demos/input_files" CACHE STRING "Set demos input files path")
set(DATAFILES_DIR_TESTS "${CMAKE_CURRENT_LIST_DIR}/unit-tests/references" CACHE STRING "Set unit-tests input files path")
endif ()
# Packages
set(EXTERNAL_INSTALL_DIR ${PROJECT_SOURCE_DIR}/external/install CACHE STRING "External libraries installation folder (with subfolders lib and include)")
# Abilety to build external libraries from source or install them from releases
if (NOT DEFINED WITH_FFTW)
if (MSVC)
set(WITH_FFTW "True" CACHE STRING "Should Build FFTW library from source")
else ()
set(WITH_FFTW "False" CACHE STRING "Should Build FFTW library from source")
endif ()
endif ()
if (NOT DEFINED WITH_GOOGLETEST)
if (MSVC)
set(WITH_GOOGLETEST "True" CACHE STRING "Should Build GoogleTest library from source")
else ()
set(WITH_GOOGLETEST "False" CACHE STRING "Should Build GoogleTest library from source")
endif ()
endif ()
if (NOT DEFINED WITH_BENCHMARK)
if (MSVC)
set(WITH_BENCHMARK "True" CACHE STRING "Should Build GoogleBenchmark library from source")
else ()
set(WITH_BENCHMARK "False" CACHE STRING "Should Build GoogleBenchmark library from source")
endif ()
endif ()
set(GOOGLETEST_ROOT ${PROJECT_SOURCE_DIR}/external/build/googletest/ CACHE STRING "Google Test source root")
set(BENCHMARK_ROOT ${PROJECT_SOURCE_DIR}/external/build/benchmark CACHE STRING "Google Benchmark Library root")
set(WITH_FORMAT "False" CACHE STRING "Clang-Format all project files on build (requires clang-format)")
# Package settings
set(USE_FFTW_OMP "False" CACHE STRING "Should use OpenMP flavour of FFTW library (linux only)") #todo: test and configure
add_definitions(-DDEMO_FILES="${DATAFILES_DIR_DEMOS}") #demos datafiles
# CPU for build count
ProcessorCount(N)
if (NOT N EQUAL 0)
set(ctest_test_args ${ctest_test_args} PARALLEL_LEVEL ${N})
endif ()
#Compiler, Linker options
#Compiler settings
#MSVC vs Other
add_definitions(-D_USE_MATH_DEFINES) #needed for VDT
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj -Zm500 /MP${N} /openmp /fp:precise ")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GR-")
else ()
check_cxx_compiler_flag(-std=c++11 COMPILER_HAS_CXX11)
if (NOT COMPILER_HAS_CXX11)
message(FATAL_ERROR "ERROR: Compiler is lacking support for c++11")
endif ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++11 -fopenmp -Wall -pedantic -fno-rtti -Wunused -ffast-math")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3 -O0 -fno-omit-frame-pointer -fprofile-arcs -ftest-coverage") # -fsanitize=address
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native")
if (TEST_ENABLED STREQUAL "True")
LIST(APPEND LIBRARIES gcov)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --coverage")
else ()
message(STATUS "code test coveredge is enabeled only in Debug mode")
endif ()
if (USE_FFTW_OMP)
LIST(APPEND LIBRARIES fftw3_omp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_FFTW_OMP")
endif ()
endif ()
#GLANG and GCC specific
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# TODO is 4.8 really the minimum requirement?
# TODO do we really want to check for this its 2016 already?
if ((CMAKE_C_COMPILER_ID == "GNU") AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.8.0"))
message(FATAL_ERROR "ERROR: Insufficient compiler version, need at lest 4.8 to continue")
endif ()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -flto")
elseif (CMAKE_C_COMPILER_ID MATCHES "Clang")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.8)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
else ()
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast")
endif ()
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}-flto")
endif ()
# Default packages
# required packages
find_package(OpenMP REQUIRED)
if (NOT WITH_FFTW)
find_path(FFTW_INCLUDES fftw3.h)
find_library(FFTW_LIB NAMES fftw fftw3 libfftw3 libfftw-3.3 REQUIRED)
endif ()
# message(STATUS "---------------- WITH_FFTW ${WITH_FFTW} ${FFTW_LIB}")
#not required
if ((NOT WITH_GOOGLETEST) AND TEST_ENABLED)
find_package(GTEST REQUIRED)
find_library(GTEST_LIB
NAMES gtest libgtest
)
find_library(GTEST_MAIN_LIB
NAMES gtest_main libgtest_main
)
endif ()
if ((NOT WITH_BENCHMARK) AND BENCHMARK_ENABLED)
find_path(BENCHMARK_INCLUDES benchmark/benchmark.h)
find_library(BENCHMARK_LIB NAME benchmark REQUIRED)
endif ()
if (BUILD_EXTERNALS)
if (NOT (EXISTS ${PROJECT_SOURCE_DIR}/external/build))
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/external/build)
else ()
execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${PROJECT_SOURCE_DIR}/external/build/)
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/external/build)
endif ()
message(STATUS "Building required started...")
execute_process(
COMMAND ${CMAKE_COMMAND}
#options
-DWITH_FFTW=${WITH_FFTW}
-DWITH_BENCHMARK=${WITH_BENCHMARK}
-DWITH_GOOGLETEST=${WITH_GOOGLETEST}
-DWITH_COMPUTE=${WITH_COMPUTE}
#destinations
-DINSTALL_DIR=${EXTERNAL_INSTALL_DIR}
-DCOMPILE_FLAGS=${COMPILE_FLAGS}
-DGOOGLETEST_ROOT=${GOOGLETEST_ROOT}
-DBENCHMARK_ROOT=${BENCHMARK_ROOT}
-DGOOGLETEST_ROOT=${GOOGLETEST_ROOT}
-DBOOST_ROOT=${BOOST_ROOT}
#forward all collected compilation arguments
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} # relevant only for Make-like build files
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}
-DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
-DCMAKE_EXE_LINKER_FLAGS_RELEASE=${CMAKE_EXE_LINKER_FLAGS_RELEASE}
-DCMAKE_EXE_LINKER_FLAGS_DEBUG=${CMAKE_EXE_LINKER_FLAGS_DEBUG}
-v
-G "${CMAKE_GENERATOR}"
${PROJECT_SOURCE_DIR}/external/
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/external/build
)
message(STATUS "building external projects begins")
execute_process(
COMMAND cmake --build . --config Release
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/external/build
)
if (WIN32 AND NOT MINGW)
execute_process(
COMMAND cmake --build . --config Debug
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/external/build
)
endif ()
message(STATUS "building external projects ended")
endif ()
message(STATUS "Building required librasries complete")
# path to external libraries and headers
include_directories(${EXTERNAL_INSTALL_DIR}/include)
link_directories(${EXTERNAL_INSTALL_DIR}/lib)
if (WITH_FFTW)
if (WIN32 AND NOT MINGW)
link_directories(${EXTERNAL_INSTALL_DIR}/lib/fftw/$(Configuration)/)
find_library(FFTW_LIB
NAMES fftw fftw3 libfftw3 libfftw-3.3
PATHS "${EXTERNAL_INSTALL_DIR}/lib/fftw/Debug"
NO_DEFAULT_PATH
)
get_filename_component(FFTW_LIB ${FFTW_LIB} NAME)
else ()
find_library(FFTW_LIB
NAMES fftw fftw3 libfftw3 libfftw-3.3
PATHS "${EXTERNAL_INSTALL_DIR}/lib/"
NO_DEFAULT_PATH
)
endif ()
endif ()
# Code editors usabilety function (structure code as it is placed in folders)
function(assign_source_group)
foreach (_source IN ITEMS ${ARGN})
if (IS_ABSOLUTE ${_source})
file(RELATIVE_PATH _source_rel ${CMAKE_CURRENT_SOURCE_DIR} ${_source})
else ()
set(source_rel ${_source})
endif ()
get_filename_component(_source_path ${_source_rel} PATH)
string(REPLACE "/" "\\" _source_path_msvc ${_source_path})
source_group("${_source_path_msvc}" FILES ${_source})
endforeach ()
endfunction(assign_source_group)
# main library
include_directories(include)
LIST(APPEND LIBRARIES
${FFTW_LIB}
)
file(GLOB_RECURSE SOURCES
"src/*"
"include/*"
)
set(SOURCE_FILES ${SOURCES})
assign_source_group(${SOURCE_FILES})
# foreach(source ${SOURCE_FILES})
# set_source_files_properties(${source} PROPERTIES COMPILE_FLAGS "-fopt-info-vec=report.txt")
# endforeach()
if (SHARED_LIBRARY_ENABLED)
add_library(blond SHARED ${SOURCE_FILES})
target_compile_definitions(blond PRIVATE SHARED=1)
else ()
add_library(blond STATIC ${SOURCE_FILES})
target_compile_definitions(blond PRIVATE STATIC=1)
endif ()
target_link_libraries(blond ${LIBRARIES})
if (WITH_FFTW)
if (MSVC)
add_custom_command(TARGET blond PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${EXTERNAL_INSTALL_DIR}/bin/fftw/$(Configuration)" $<TARGET_FILE_DIR:blond>)
else ()
add_custom_command(TARGET blond PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${EXTERNAL_INSTALL_DIR}/bin/" $<TARGET_FILE_DIR:blond>)
endif ()
endif ()
#SET_TARGET_PROPERTIES(blond PROPERTIES LINKER_LANGUAGE CXX)
# demo executables
LIST(APPEND LIBRARIES blond)
file(GLOB EXECUTABLE_SOURCES "demos/*.cpp")
set(EXECUTABLE_FILES ${EXECUTABLE_SOURCES})
# demo dependencies
foreach (src ${EXECUTABLE_FILES})
file(RELATIVE_PATH testfile ${CMAKE_CURRENT_SOURCE_DIR} ${src})
string(REPLACE ".cpp" "" testname ${testfile})
string(REPLACE "demos/" "" testname ${testname})
add_executable(${testname} ${src})
target_link_libraries(${testname} ${LIBRARIES})
endforeach ()
# testing
if (TEST_ENABLED)
add_definitions(-DTEST_FILES="${DATAFILES_DIR_TESTS}") #demos datafiles
if (WITH_GOOGLETEST)
if (WIN32 AND NOT MINGW)
link_directories(${EXTERNAL_INSTALL_DIR}/lib/gtest/$(Configuration)/)
find_library(GTEST_LIB
NAMES gtest libgtest
PATHS "${EXTERNAL_INSTALL_DIR}/lib/gtest/Debug"
NO_DEFAULT_PATH
)
find_library(GTEST_MAIN_LIB
NAMES gtest_main libgtest_main
PATHS "${EXTERNAL_INSTALL_DIR}/lib/gtest/Debug"
NO_DEFAULT_PATH
)
get_filename_component(GTEST_MAIN_LIB ${GTEST_MAIN_LIB} NAME)
get_filename_component(GTEST_LIB ${GTEST_LIB} NAME)
else ()
find_library(GTEST_LIB
NAMES gtest libgtest
PATHS "${EXTERNAL_INSTALL_DIR}/lib/"
NO_DEFAULT_PATH
)
find_library(GTEST_MAIN_LIB
NAMES gtest_main libgtest_main
PATHS "${EXTERNAL_INSTALL_DIR}/lib/"
NO_DEFAULT_PATH
)
endif ()
endif ()
message(STATUS "generating test executables")
LIST(APPEND LIBRARIES ${GTEST_LIB})
LIST(APPEND LIBRARIES ${GTEST_MAIN_LIB})
if (UNIX)
set(CTEST_MEMORYCHECK_COMMAND, "usr/bin/valgrind")
set(CTEST_MEMORYCHECK_COMMAND_OPTIONS, "--leak-check=full")
endif ()
file(GLOB UNIT_TEST_SOURCES "./unit-tests/*.cpp")
set(UNIT_TEST_FILES ${UNIT_TEST_SOURCES})
foreach (src ${UNIT_TEST_FILES})
file(RELATIVE_PATH testfile ${CMAKE_CURRENT_SOURCE_DIR} ${src})
string(REPLACE ".cpp" "" testname ${testfile})
string(REPLACE "unit-tests/" "" testname ${testname})
add_executable(${testname} ${src})
target_link_libraries(${testname} ${LIBRARIES})
add_test(${testname} ${testname})
endforeach ()
enable_testing()
endif ()
# benchmarks
if (BENCHMARK_ENABLED)
if (WITH_BENCHMARK)
if (WIN32 AND NOT MINGW)
link_directories(${EXTERNAL_INSTALL_DIR}/lib/benchmark/$(Configuration)/)
find_library(BENCHMARK_LIB
NAMES benchmark libbenchmark
PATHS "${EXTERNAL_INSTALL_DIR}/lib/benchmark/Debug"
NO_DEFAULT_PATH
)
get_filename_component(BENCHMARK_LIB ${BENCHMARK_LIB} NAME)
else ()
find_library(BENCHMARK_LIB
NAMES benchmark libbenchmark
PATHS "${EXTERNAL_INSTALL_DIR}/lib/"
NO_DEFAULT_PATH
)
endif ()
endif ()
message(STATUS "generating benchmark executables")
LIST(APPEND LIBRARIES ${BENCHMARK_LIB})
if (MSVC OR MINGW)
LIST(APPEND LIBRARIES shlwapi)
else ()
LIST(APPEND LIBRARIES pthread)
endif ()
file(GLOB BENCHMARK_SOURCES "./benchmarks/*.cpp")
file(GLOB_RECURSE BENCHMARK_PROTOTYPES "./benchmarks/prototypes/*.h")
include_directories(benchmarks/prototypes/)
set(BENCHMARK_FILES ${BENCHMARK_SOURCES})
file(GLOB BENCHMARK_CONFIGURATIONS "./benchmarks/*.cmake")
include(${BENCHMARK_CONFIGURATIONS})
foreach (src ${BENCHMARK_FILES})
file(RELATIVE_PATH testfile ${CMAKE_CURRENT_SOURCE_DIR} ${src})
string(REPLACE ".cpp" "" testname ${testfile})
string(REPLACE "benchmarks/" "" testname ${testname})
add_executable(${testname} ${src} ${BENCHMARK_PROTOTYPES})
target_link_libraries(${testname} ${LIBRARIES})
endforeach ()
enable_testing()
endif ()
install(TARGETS blond
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static
INCLUDES DESTINATION include
CONFIGURATIONS Debug|Release
)
include(clang-format.cmake)