forked from GeodynamicWorldBuilder/WorldBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
358 lines (292 loc) · 15.2 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
cmake_minimum_required (VERSION 2.8.4)
project(WorldBuilder C CXX)
# SWIG: use standard target name.
if(POLICY CMP0078)
cmake_policy(SET CMP0078 NEW)
endif()
## SWIG: use SWIG_MODULE_NAME property.
if(POLICY CMP0086)
cmake_policy(SET CMP0086 NEW)
endif()
# load in version info and export it
SET(WORLD_BUILDER_SOURCE_DIR ${CMAKE_SOURCE_DIR})
INCLUDE("${CMAKE_SOURCE_DIR}/cmake/version.cmake")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
message(STATUS "Building the Geodynamic World Builder version ${WORLD_BUILDER_VERSION} in ${CMAKE_BUILD_TYPE} mode")
set (WORLD_BUILDER_SOURCE_DIR ${PROJECT_SOURCE_DIR})
# generate version.cc
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/source/config.cc.in" "${CMAKE_BINARY_DIR}/source/config.cc" @ONLY)
# finding support for different languages
# finding a fortran compiler
if(${CMAKE_VERSION} VERSION_LESS "2.8.8")
message(STATUS "Fortran not supported with cmake version lower than 2.8.8. Please upgrade your cmake version if you need it. Disabling Fortran wrapper and tests.")
SET(CMAKE_Fortran_COMPILER FALSE)
else()
include(CheckLanguage)
check_language(Fortran)
if(CMAKE_Fortran_COMPILER)
enable_language(Fortran)
message(STATUS "Found Fortran support. Enabling Fortran wrapper and tests.")
else()
message(STATUS "Did not find Fortran support. Disabling Fortran wrapper and tests.")
endif()
endif()
#finding support for makeing a python wrapper
SET(MAKE_PYTHON_WRAPPER FALSE)
#find swig
FIND_PACKAGE(SWIG)
if(SWIG_FOUND)
INCLUDE(${SWIG_USE_FILE})
SET(CMAKE_SWIG_FLAGS "")
else()
message(STATUS "SWIG was not found, disabling python wrapper compilation.")
endif()
#find python libs
if(${CMAKE_VERSION} VERSION_LESS "3.12.0")
FIND_PACKAGE(PythonLibs 3.0)
FIND_PACKAGE(PythonInterp 3.0)
if(PYTHONLIBS_FOUND AND PYTHONINTERP_FOUND)
SET(Python_FOUND TRUE)
SET(Python_EXECUTABLE ${PYTHON_EXECUTABLE})
SET(Python_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS})
SET(Python_LIBRARIES ${PYTHON_LIBRARIES})
SET(Python_LIBRARIES_RELEASE ${PYTHON_LIBRARIES_RELEASE})
include_directories(${PYTHON_INCLUDE_DIRS})
else()
SET(Python_FOUND FALSE)
message(STATUS "Python was not found. Disabling python wrapper compilation.")
endif()
else()
FIND_PACKAGE(Python COMPONENTS Interpreter Development)
if(Python_FOUND)
include_directories(${Python_INCLUDE_DIRS})
else()
message(STATUS "Python was not found, disabling python wrapper compilation.")
endif()
endif()
if(SWIG_FOUND AND Python_FOUND)
SET(MAKE_PYTHON_WRAPPER TRUE)
message(STATUS "Both SWIG and Python are found, enableling python wrapper compilation.")
endif()
# Add dependencies are found, now set compiler output directories.
# This needs to be done before the add library and add executable functions.
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Debug
CACHE STRING "Choose the type of build : None Debug Release RelWithDebInfo MinSizeRel Coverage."
FORCE)
ENDIF(NOT CMAKE_BUILD_TYPE)
IF(NOT CMAKE_RUN_APP_TESTS)
SET(CMAKE_RUN_APP_TESTS TRUE
CACHE STRING "Whether to run or not to run the app tests."
)
ENDIF()
IF ( CMAKE_BUILD_TYPE STREQUAL Coverage )
MARK_AS_ADVANCED(
CMAKE_CXX_FLAGS_COVERAGE
CMAKE_C_FLAGS_COVERAGE
CMAKE_Fortran_FLAGS_COVERAGE
CMAKE_EXE_LINKER_FLAGS_COVERAGE
CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
ENDIF()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/bin)
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/lib)
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/lib)
endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )
if(MAKE_PYTHON_WRAPPER)
set(CMAKE_SWIG_OUTDIR ${CMAKE_BINARY_DIR}/lib)
endif()
if(CMAKE_Fortran_COMPILER)
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/mod)
endif()
if(MAKE_PYTHON_WRAPPER)
# Files to install with Python
if(WIN32)
set(PYTHON_INSTALL_FILES
${CMAKE_CURRENT_BINARY_DIR}/lib/gwb.py
${CMAKE_CURRENT_BINARY_DIR}/lib/_gwb.pyd)
else()
set(PYTHON_INSTALL_FILES
${CMAKE_CURRENT_BINARY_DIR}/lib/gwb.py
${CMAKE_CURRENT_BINARY_DIR}/lib/_gwb.so)
endif()
# Configure setup.py and copy to output directory
set(SETUP_PY_IN ${CMAKE_CURRENT_SOURCE_DIR}/cmake/setup.py.in)
set(SETUP_PY_OUT ${CMAKE_CURRENT_BINARY_DIR}/lib/setup.py)
configure_file(${SETUP_PY_IN} ${SETUP_PY_OUT})
endif()
# Add include directory
include_directories("include/" "tests/")
# Add source directory
file(GLOB_RECURSE SOURCES_CXX "source/*.cc" "${CMAKE_BINARY_DIR}/source/*.cc")
if(CMAKE_Fortran_COMPILER)
file(GLOB_RECURSE SOURCES_FORTAN "source/*.f90")
endif()
set(SOURCES ${SOURCES_CXX} ${SOURCES_FORTAN})
add_library(WorldBuilder ${SOURCES})
add_executable(WorldBuilderApp "${CMAKE_CURRENT_SOURCE_DIR}/app/main.cc")
add_executable(WorldBuilderVisualization "${CMAKE_CURRENT_SOURCE_DIR}/visualization/main.cc")
if(MAKE_PYTHON_WRAPPER)
if(POLICY CMP0078)
SET(TARGET gwb)
else()
SET(TARGET _gwb)
endif()
# Add swig module
SET_SOURCE_FILES_PROPERTIES(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/source/wrapper_cpp.i PROPERTIES CPLUSPLUS ON)
if(${CMAKE_VERSION} VERSION_LESS "3.8.0")
swig_add_module(gwb python ${CMAKE_CURRENT_SOURCE_DIR}/source/wrapper_cpp.i)
else()
swig_add_library(gwb LANGUAGE python SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/source/wrapper_cpp.i)
endif()
endif()
# Provide "indent" target for indenting all headers and source files
ADD_CUSTOM_TARGET(indent
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ./doc/indent
COMMENT "Indenting all the World Builder header and source files..."
)
IF ( CMAKE_BUILD_TYPE STREQUAL Coverage )
SET(CMAKE_VERBOSE_MAKEFILE on )
SET(WB_COMPILER_OPTIONS_PRIVATE_COVERAGE_NEW --coverage -fprofile-arcs -ftest-coverage)
SET(WB_COMPILER_OPTIONS_PRIVATE_COVERAGE_OLD "--coverage -fprofile-arcs -ftest-coverage")
## for old versions of cmake
SET(WB_FORTRAN_COMPILER_FLAGS_COVERAGE "--coverage")
endif()
SET(WB_LINKER_OPTIONS "")
SET(WB_VISU_LINKER_OPTIONS "")
SET(WB_COMPILER_OPTIONS_INTERFACE "")
SET(WB_COMPILER_OPTIONS_PRIVATE "")
SET(WB_VISU_COMPILER_OPTIONS_PRIVATE "")
if (NOT MSVC AND NOT APPLE)
#TODO: if other compiles need to be threaded, see https://computing.llnl.gov/tutorials/pthreads/#Compiling to add the correct cases.
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER MATCHES "[Cc]lang")
# Preventing issues with older cmake compilers (<2.8.11) which do not support VERSION_GREATER_EQUAL
# cmake vesrion 3.12.0 introduces COMPILE_LANGUAGE:FORTAN, otherwise this would be >=2.8.11
if(NOT ${CMAKE_VERSION} VERSION_LESS "2.8.12")
SET(WB_COMPILER_OPTIONS_PRIVATE -pedantic -fPIC -Wall -Wextra
$<$<COMPILE_LANGUAGE:CXX>:-std=c++11 -Wmost -Wconversion -Wunreachable-code -Wuninitialized -Wold-style-cast -Wshadow -Wfloat-equal -Wpointer-arith -Wwrite-strings
-Wsynth -Wsign-compare -Woverloaded-virtual -Wliteral-range -Wparentheses -Wunused-local-typedefs -Wcast-qual -fstrict-aliasing -Werror=uninitialized -Wundef
-Wcast-align -Wmissing-declarations -Wredundant-decls -Wdiv-by-zero -Wdisabled-optimization -Wswitch-default -Wunused>)
else()
SET(WB_COMPILER_OPTIONS_PRIVATE "-std=c++11 -pedantic -fPIC -Wall -Wextra -Wmost -Wconversion -Wunreachable-code -Wuninitialized -Wold-style-cast -Wshadow -Wfloat-equal -Wpointer-arith -Wwrite-strings -Wsynth -Wsign-compare -Woverloaded-virtual -Wliteral-range -Wparentheses -Wunused-local-typedefs -Wcast-qual -fstrict-aliasing -Werror=uninitialized -Wundef -Wcast-align -Wmissing-declarations -Wredundant-decls -Wdiv-by-zero -Wdisabled-optimization -Wswitch-default -Wunused")
endif()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
SET(WB_COMPILER_OPTIONS_PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-std=c++11 -pthread -pedantic -fPIC -Wall -Wextra -Wpointer-arith -Wwrite-strings -Wsign-compare -Woverloaded-virtual -Wno-parentheses -Wcast-qual -fstrict-aliasing -Wmaybe-uninitialized -Werror=maybe-uninitialized>)
else()
# gcc linux
# Preventing issues with older cmake compilers (<2.8.11) which do not support VERSION_GREATER_EQUAL
# cmake vesrion 3.12.0 introduces COMPILE_LANGUAGE:FORTAN, otherwise this would be >=2.8.11
if(NOT ${CMAKE_VERSION} VERSION_LESS "2.8.12")
SET(WB_COMPILER_OPTIONS_PRIVATE -pedantic -fPIC -Wall -Wextra $<$<COMPILE_LANGUAGE:CXX>:-std=c++11 -Wpointer-arith -Wwrite-strings -Wsynth -Wsign-compare -Woverloaded-virtual -Wno-placement-new -Wno-literal-suffix -Wno-parentheses -Wno-unused-local-typedefs -Wcast-qual -fstrict-aliasing -Wmaybe-uninitialized -Werror=maybe-uninitialized -Wparentheses -Wfloat-equal -Wundef -Wcast-align -Wlogical-op -Wmissing-declarations -Wredundant-decls -Wdiv-by-zero -Wdisabled-optimization -Wswitch-default -Wno-unused>)
else()
SET(WB_COMPILER_OPTIONS_PRIVATE "-std=c++11 -pedantic -fPIC -Wall -Wextra -Wpointer-arith -Wwrite-strings -Wsynth -Wsign-compare -Woverloaded-virtual -Wno-placement-new -Wno-literal-suffix -Wno-parentheses -Wno-unused-local-typedefs -Wcast-qual -fstrict-aliasing -Wmaybe-uninitialized -Werror=maybe-uninitialized -Wparentheses -Wfloat-equal -Wundef -Wcast-align -Wlogical-op -Wmissing-declarations -Wredundant-decls -Wdiv-by-zero -Wdisabled-optimization -Wswitch-default -Wno-unused>")
endif()
# adding flags is a hassle before cmake 3.12.0, and these flags are mostly useful for development, so just ignore those flags.
if(NOT ${CMAKE_VERSION} VERSION_LESS "2.8.12") # Preventing issues with older cmake compilers which do not support VERSION_GREATER_EQUAL
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-Wsuggest-override SUGGEST_OVERWRITE_CXX_COMPILE_FLAG)
if(SUGGEST_OVERWRITE_CXX_COMPILE_FLAG)
SET(WB_COMPILER_OPTIONS_PRIVATE ${WB_COMPILER_OPTIONS_PRIVATE} $<$<COMPILE_LANGUAGE:CXX>:-Wsuggest-override>)
endif()
check_cxx_compiler_flag(-Wno-placement-new NO_PLACEMENT_NEW_CXX_COMPILE_FLAG)
if(NO_PLACEMENT_NEW_CXX_COMPILE_FLAG)
SET(WB_COMPILER_OPTIONS_PRIVATE ${WB_COMPILER_OPTIONS_PRIVATE} $<$<COMPILE_LANGUAGE:CXX>:-Wno-placement-new>)
endif()
# untill 4.9.0 Wshadow is too strict: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57709,
# but still some issues in 4.9.0, so only add it after 5.0.0.
if(CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0.0)
SET(WB_COMPILER_OPTIONS_PRIVATE ${WB_COMPILER_OPTIONS_PRIVATE} $<$<COMPILE_LANGUAGE:CXX>:-Wshadow>)
endif()
endif()
endif()
IF ( CMAKE_BUILD_TYPE STREQUAL Coverage )
if(NOT ${CMAKE_VERSION} VERSION_LESS "3.13.0") # Preventing issues with older cmake compilers which do not support VERSION_GREATER_EQUAL
SET(WB_LINKER_OPTIONS -lstdc++ --coverage -fprofile-arcs -ftest-coverage)
else()
SET(WB_LINKER_OPTIONS "-lstdc++ --coverage -fprofile-arcs -ftest-coverage")
endif()
else()
SET(WB_LINKER_OPTIONS -lstdc++)
endif()
SET(WB_VISU_LINKER_OPTIONS "-pthread")
elseif(APPLE)
SET(WB_COMPILER_OPTIONS_PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-std=c++11 -Wpointer-arith -Wwrite-strings -Wsynth -Wsign-compare -Woverloaded-virtual -Wno-literal-range -Wno-parentheses -Wno-unused-local-typedefs -Wcast-qual -fstrict-aliasing -stdlib=libc++ -Wuninitialized -Werror=uninitialized -Wdangling-else>)
SET(WB_LINKER_OPTIONS -lc++)
else()
#MSVS
SET(WB_COMPILER_OPTIONS_PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/W3 /EHsc -std=c++11>)
SET(WB_LINKER_OPTIONS /WHOLEARCHIVE:${CMAKE_CURRENT_BINARY_DIR}/lib/WorldBuilder.lib)
endif()
if(${CMAKE_VERSION} VERSION_LESS "2.8.12")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WB_COMPILER_OPTIONS_INTERFACE} ${WB_COMPILER_OPTIONS_PRIVATE} ${WB_COMPILER_OPTIONS_PRIVATE_COVERAGE_OLD}")
else()
target_compile_options(WorldBuilder INTERFACE ${WB_COMPILER_OPTIONS_INTERFACE} PRIVATE ${WB_COMPILER_OPTIONS_PRIVATE} ${WB_COMPILER_OPTIONS_PRIVATE_COVERAGE_NEW})
target_compile_options(WorldBuilderApp INTERFACE ${WB_COMPILER_OPTIONS_INTERFACE} PRIVATE ${WB_COMPILER_OPTIONS_PRIVATE} ${WB_COMPILER_OPTIONS_PRIVATE_COVERAGE_NEW})
target_compile_options(WorldBuilderVisualization INTERFACE ${WB_COMPILER_OPTIONS_INTERFACE} PRIVATE ${WB_COMPILER_OPTIONS_PRIVATE} ${WB_COMPILER_OPTIONS_PRIVATE_COVERAGE_NEW})
endif()
if(${CMAKE_VERSION} VERSION_LESS "3.13.0")
SET(CMAKE_EXE_LINKER_FLAGS "${WB_LINKER_OPTIONS} ${WB_VISU_LINKER_OPTIONS} ${WB_COMPILER_OPTIONS_PRIVATE_COVERAGE_OLD}")
else()
target_link_options(WorldBuilder INTERFACE ${WB_LINKER_OPTIONS})
target_link_options(WorldBuilderApp INTERFACE ${WB_LINKER_OPTIONS} )
target_link_options(WorldBuilderVisualization INTERFACE ${WB_LINKER_OPTIONS})
target_link_options(WorldBuilderVisualization PRIVATE ${WB_VISU_LINKER_OPTIONS})
endif()
# Make sure that the whole library is loaded, so the registration is done correctly.
if(NOT APPLE AND NOT MSVC)
SET(GWB_LIBRARY_WHOLE -Wl,--whole-archive WorldBuilder -Wl,--no-whole-archive)
elseif(MSVC)
SET(GWB_LIBRARY_WHOLE WorldBuilder)
else()
SET(GWB_LIBRARY_WHOLE -Wl,-force_load WorldBuilder)
endif()
target_link_libraries (WorldBuilderApp ${GWB_LIBRARY_WHOLE})
target_link_libraries (WorldBuilderVisualization ${GWB_LIBRARY_WHOLE})
if(MAKE_PYTHON_WRAPPER)
# see https://github.com/swig/swig/issues/1259 for more info.
if(SWIG_VERSION VERSION_LESS "4.0.1" AND NOT MSVC)
set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "-Wno-cast-qual")
endif()
IF ( CMAKE_BUILD_TYPE STREQUAL Coverage )
set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "--coverage -fprofile-arcs -ftest-coverage")
endif()
# visual studio has a problem with finding the debug version of python (pythonxx_d.dll),
# so force release usage.
if(WIN32 AND MSVC)
SET(CMAKE_SWIG_FLAGS "-D_SWIG_WIN32 /WHOLEARCHIVE:${CMAKE_CURRENT_BINARY_DIR}/lib/WorldBuilder.lib")
swig_link_libraries(gwb ${GWB_LIBRARY_WHOLE} ${Python_LIBRARY_RELEASE})
else()
IF ( CMAKE_BUILD_TYPE STREQUAL Coverage )
swig_link_libraries(gwb ${GWB_LIBRARY_WHOLE} ${Python_LIBRARIES} --coverage -fprofile-arcs -ftest-coverage)
else()
swig_link_libraries(gwb ${GWB_LIBRARY_WHOLE} ${Python_LIBRARIES})
endif()
endif()
set_target_properties(${TARGET} PROPERTIES PREFIX "_" OUTPUT_NAME "gwb")
# Install target to call setup.py
add_custom_target(install-python
DEPENDS _gwb
COMMAND ${Python_EXECUTABLE} ${SETUP_PY_OUT} install)
endif()
# binary:
install(CODE "message(Installing...)")
install (TARGETS WorldBuilder EXPORT WorldBuilder DESTINATION bin)
install (EXPORT WorldBuilder DESTINATION bin)
if(CMAKE_Fortran_COMPILER)
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/mod/worldbuilder.mod DESTINATION include)
endif()
if(MAKE_PYTHON_WRAPPER)
install(CODE "execute_process(COMMAND ${Python_EXECUTABLE} ${SETUP_PY_OUT} install)")
endif()
# headers:
INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
DESTINATION include
COMPONENT includes
FILES_MATCHING PATTERN "*.h")
enable_testing()
add_subdirectory(tests)