-
Notifications
You must be signed in to change notification settings - Fork 301
/
G4BuildSettings.cmake
469 lines (419 loc) · 16.9 KB
/
G4BuildSettings.cmake
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#.rst:
# G4BuildSettings.cmake
# ---------------------
#
# Set defaults for core CMake settings for compiling and linking
# Geant4 libraries and tests,
#
# In addition to the core compiler/linker flags (configured in the
# Geant4MakeRules_<LANG>.cmake files) for Geant4, the build may require
# further configuration. This module performs this task whicj includes:
#
# 1) Extra build modes for developers
# 2) Additional compiler definitions to assist visualization or optimize
# performance.
# 3) Additional compiler flags which may be added optionally.
# 4) Whether to build shared and/or static libraries.
# 5) Whether to build libraries in global or granular format.
#
#-----------------------------------------------------------------
# License and Disclaimer
#
# The Geant4 software is copyright of the Copyright Holders of
# the Geant4 Collaboration. It is provided under the terms and
# conditions of the Geant4 Software License, included in the file
# LICENSE and available at http://cern.ch/geant4/license . These
# include a list of copyright holders.
#
# Neither the authors of this software system, nor their employing
# institutes,nor the agencies providing financial support for this
# work make any representation or warranty, express or implied,
# regarding this software system or assume any liability for its
# use. Please see the license in the file LICENSE and URL above
# for the full disclaimer and the limitation of liability.
#
# This code implementation is the result of the scientific and
# technical work of the GEANT4 collaboration.
# By using, copying, modifying or distributing the software (or
# any work based on the software) you agree to acknowledge its
# use in resulting scientific publications, and indicate your
# acceptance of all terms of the Geant4 Software license.
#
#-----------------------------------------------------------------
if(NOT __G4BUILDSETTINGS_INCLUDED)
set(__G4BUILDSETTINGS_INCLUDED TRUE)
else()
return()
endif()
#-----------------------------------------------------------------------
#.rst:
# Included Modules
# ^^^^^^^^^^^^^^^^
#
# This module includes the following modules:
#
include(CheckCXXSourceCompiles)
include(IntelCompileFeatures)
#-----------------------------------------------------------------------
#.rst:
# Build Modes
# ^^^^^^^^^^^
#
# Geant4 supports the standard CMake build types/configurations of
#
# - ``Release``
# - ``Debug``
# - ``MinSizeRel``
# - ``RelWithDebInfo``
#
# For single build type tools like `make` and `ninja`, the build
# defaults to ``Release`` mode unless ``CMAKE_BUILD_TYPE`` is set.
# Multi build type tools like Visual Studio and Xcode will default
# to
#
# On non-WIN32 platforms, two types specifically for development are added:
#
if(NOT WIN32)
#.rst:
# - ``TestRelease``:
# For trial production and extended testing. It has verbose
# output, has debugging symbols, and adds definitions to allow FPE
# and physics conservation law testing where supported.
#
set(CMAKE_CXX_FLAGS_TESTRELEASE "${CMAKE_CXX_FLAGS_TESTRELEASE_INIT}"
CACHE STRING "Flags used by the compiler during TestRelease builds"
)
mark_as_advanced(CMAKE_CXX_FLAGS_TESTRELEASE)
#.rst:
# - ``Maintainer``:
# For development of the toolkit. It adds debugging, and enables the use
# of library specific debugging via standardized definitions.
#
set(CMAKE_CXX_FLAGS_MAINTAINER "${CMAKE_CXX_FLAGS_MAINTAINER_INIT}"
CACHE STRING "Flags used by the compiler during Maintainer builds"
)
mark_as_advanced(CMAKE_CXX_FLAGS_MAINTAINER)
#.rst:
# Compiler flags specific to these build types are set in the cache, and
# the types are added to the ``CMAKE_BUILD_TYPES`` cache string and to
# ``CMAKE_CONFIGURATION_TYPES`` if appropriate to the build tool being used.
#
if(NOT CMAKE_CONFIGURATION_TYPES)
# Single mode build tools like Make, Ninja,
if(NOT CMAKE_BUILD_TYPE)
# Default to a Release build if nothing else...
set(CMAKE_BUILD_TYPE Release
CACHE STRING "Choose the type of build, options are: None Release TestRelease MinSizeRel Debug RelWithDebInfo MinSizeRel Maintainer."
FORCE
)
else()
# Force to the cache, but use existing value.
set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}"
CACHE STRING "Choose the type of build, options are: None Release TestRelease MinSizeRel Debug RelWithDebInfo MinSizeRel Maintainer."
FORCE
)
endif()
else()
# Multimode tools like VS, Xcode
list(APPEND CMAKE_CONFIGURATION_TYPES TestRelease)
list(APPEND CMAKE_CONFIGURATION_TYPES Maintainer)
list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}"
CACHE STRING "Geant4 configurations for multimode build tools"
FORCE
)
endif()
endif() #NOT WIN32
#-----------------------------------------------------------------------
#.rst:
# Compilation Options
# ^^^^^^^^^^^^^^^^^^^
#
# The following options affect the compilation of the Geant4 libraries
#
#.rst
# - ``GEANT4_BUILD_CXXSTD`` (Allowed values: 11, 14, 17, c++11, c++14, c++17)
#
# - Choose C++ Standard to build against from supported list.
#
enum_option(GEANT4_BUILD_CXXSTD
DOC "C++ Standard to compile against"
VALUES 11 14 17 c++11 c++14 c++17
CASE_INSENSITIVE
)
string(REGEX REPLACE "^c\\+\\+" "" GEANT4_BUILD_CXXSTD "${GEANT4_BUILD_CXXSTD}")
mark_as_advanced(GEANT4_BUILD_CXXSTD)
geant4_add_feature(GEANT4_BUILD_CXXSTD "Compiling against C++ Standard '${GEANT4_BUILD_CXXSTD}'")
# Require at least C++11 with no extensions and the following features
set(CMAKE_CXX_EXTENSIONS OFF)
set(GEANT4_TARGET_COMPILE_FEATURES
cxx_alias_templates
cxx_auto_type
cxx_delegating_constructors
cxx_enum_forward_declarations
cxx_explicit_conversions
cxx_final
cxx_lambdas
cxx_nullptr
cxx_override
cxx_range_for
cxx_strong_enums
cxx_uniform_initialization
# Features that MSVC 18.0 cannot support but in list of Geant4 coding
# guidelines - to be required once support for that compiler is dropped.
# Version 10.2 is coded without these being required.
#cxx_deleted_functions
#cxx_generalized_initializers
#cxx_constexpr
#cxx_inheriting_constructors
)
# If a standard higher than 11 has been selected, check that compiler has
# at least one feature from that standard and append these to the required
# feature list
if(GEANT4_BUILD_CXXSTD GREATER 11)
if(CMAKE_CXX${GEANT4_BUILD_CXXSTD}_COMPILE_FEATURES)
list(APPEND GEANT4_TARGET_COMPILE_FEATURES ${CMAKE_CXX${GEANT4_BUILD_CXXSTD}_COMPILE_FEATURES})
else()
message(FATAL_ERROR "Geant4 requested to be compiled against C++ standard '${GEANT4_BUILD_CXXSTD}'\nbut detected compiler '${CMAKE_CXX_COMPILER_ID}', version '${CMAKE_CXX_COMPILER_VERSION}'\ndoes not support, or CMake (${CMAKE_VERSION}) is not aware of, any features of that standard.")
endif()
endif()
# - Check for Standard Library Implementation Features
# Smart pointers are a library implementation feature
# Hashed containers are a library implementation feature
# Random numbers are a library implementation feature?
# - Thread local? Yes, though on AppleClang platforms, see this:
#http://stackoverflow.com/questions/28094794/why-does-apple-clang-disallow-c11-thread-local-when-official-clang-supports
# An example of where a workaround is needed
# Rest of concurrency a library implementation feature
# Add Definition to flags for temporary back compatibility
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DG4USE_STD11")
# Hold any appropriate compile flag(s) in variable for later export to
# config files. Needed to support clients using late CMake 2.8 where compile features
# are not available.
set(GEANT4_CXXSTD_FLAGS "${CMAKE_CXX${GEANT4_BUILD_CXXSTD}_STANDARD_COMPILE_OPTION}")
#-----------------------------------------------------------------------
# Multithreading
#-----------------------------------------------------------------------
#.rst:
# - ``GEANT4_BUILD_MULTITHREADED`` (Default: ``OFF``)
#
# If set to ``ON``, compile Geant4 with support for multithreading.
# At present, this is only supported on Unix platforms.
#
option(GEANT4_BUILD_MULTITHREADED "Enable multithreading in Geant4" OFF)
if(WIN32)
mark_as_advanced(GEANT4_BUILD_MULTITHREADED)
endif()
geant4_add_feature(GEANT4_BUILD_MULTITHREADED "Build multithread enabled libraries")
#.rst:
# - ``GEANT4_BUILD_TLS_MODEL`` (Default: ``initial-exec``)
#
# If ``GEANT4_BUILD_MULTITHREADED`` is ``ON`` and a GNU/Clang/Intel
# compiler is being used, then this option may be used to choose the
# Thread Local Storage model. A default of ``initial-exec`` is used
# to provide optimize performance for applications directly linking
# to the Geant4 libraries
#
if(GEANT4_BUILD_MULTITHREADED)
# - Need Thread Local Storage support (POSIX)
if(UNIX)
check_cxx_source_compiles("__thread int i; int main(){return 0;}" HAVE_TLS)
if(NOT HAVE_TLS)
message(FATAL_ERROR "Configured compiler ${CMAKE_CXX_COMPILER} does not support thread local storage")
endif()
endif()
# - Emit warning on Windows - message will format oddly on CMake prior
# to 2.8, but still print
if(WIN32)
message(WARNING "GEANT4_BUILD_MULTITHREADED IS NOT SUPPORTED on Win32. This option should only be activated by developers")
endif()
# - Allow advanced users to select the thread local storage model,
# if the compiler supports it, defaulting to that recommended by Geant4
if(TLSMODEL_IS_AVAILABLE)
enum_option(GEANT4_BUILD_TLS_MODEL
DOC "Build libraries with Thread Local Storage model"
VALUES ${TLSMODEL_IS_AVAILABLE}
CASE_INSENSITIVE
)
mark_as_advanced(GEANT4_BUILD_TLS_MODEL)
geant4_add_feature(GEANT4_BUILD_TLS_MODEL "Building with TLS model '${GEANT4_BUILD_TLS_MODEL}'")
set(GEANT4_MULTITHREADED_CXX_FLAGS "${GEANT4_MULTITHREADED_CXX_FLAGS} ${${GEANT4_BUILD_TLS_MODEL}_FLAGS}")
endif()
# Set Defs/Compiler Flags
# TODO: Migrate def to header
add_definitions(-DG4MULTITHREADED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GEANT4_MULTITHREADED_CXX_FLAGS}")
endif()
#-----------------------------------------------------------------------
# Miscellaneous
#-----------------------------------------------------------------------
#.rst:
# - ``GEANT4_BUILD_STORE_TRAJECTORY`` (Default: ON)
#
# - Switching off can improve performance. Needs to be on
# for visualization to work fully. Mark as advanced because most users
# should not need to worry about it.
#
option(GEANT4_BUILD_STORE_TRAJECTORY
"Store trajectories in event processing. Switch off for improved performance but note that visualization of trajectories will not be possible"
ON)
mark_as_advanced(GEANT4_BUILD_STORE_TRAJECTORY)
# TODO: Migrate this to header
if(GEANT4_BUILD_STORE_TRAJECTORY)
add_definitions(-DG4_STORE_TRAJECTORY)
endif()
#.rst:
# - ``GEANT4_BUILD_VERBOSE_CODE`` (Default: ON)
#
# - Switching off can improve performance, but at the cost
# of fewer informational or warning messages.
# Mark as advanced because most users should not need to worry about it.
#
option(GEANT4_BUILD_VERBOSE_CODE
"Enable verbose output from Geant4 code. Switch off for better performance at the cost of fewer informational messages or warnings"
ON)
mark_as_advanced(GEANT4_BUILD_VERBOSE_CODE)
# TODO: Migrate this to header
if(GEANT4_BUILD_VERBOSE_CODE)
add_definitions(-DG4VERBOSE)
endif()
#.rst:
# - ``GEANT4_BUILD_MUONIC_ATOMS_IN_USE`` (Default: OFF)
#
# - Switched off by default to improve performance when not using
# Muonic Atom code. It should be switched on if the project requires
# support for Muonic Atoms.
# Mark as advanced because most users should not need to worry about it
#
option(GEANT4_BUILD_MUONIC_ATOMS_IN_USE
"Enable turning on some if statements in track and event code. Switch on if using new Muonic Atom code."
OFF)
mark_as_advanced(GEANT4_BUILD_MUONIC_ATOMS_IN_USE)
# TODO: Migrate this to header
if(GEANT4_BUILD_MUONIC_ATOMS_IN_USE)
add_definitions(-DG4MUATOMS_INUSE)
endif()
#.rst:
# - ``GEANT4_BUILD_MSVC_MP`` (Windows only, Default: OFF)
#
# - Provide optional file level parallelization with MSVC compiler.
#
# NB: This will only work if the build tool performs compilations
# with multiple sources, e.g. "cl.exe a.cc b.cc c.cc"
if(MSVC)
option(GEANT4_BUILD_MSVC_MP "Use /MP option with MSVC for file level parallel builds" OFF)
mark_as_advanced(GEANT4_BUILD_MSVC_MP)
if(GEANT4_BUILD_MSVC_MP)
set(CMAKE_CXX_FLAGS "/MP ${CMAKE_CXX_FLAGS}")
endif()
endif()
#-----------------------------------------------------------------------
#.rst:
# Library Mode and Link Options
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
#.rst:
# - ``BUILD_SHARED_LIBS`` (Default: ``ON``)
#
# - Build shared libraries (`.so` on Unix, `.dylib` on macOS, `.dll/.lib` on Windows)
#
# - ``BUILD_STATIC_LIBS`` (Default: ``OFF``)
#
# - Build static libraries (`.a` on Unices, `.lib` on Windows)
#
# Both options are adavanced and may be selected together to build
# both types. If neither is selected, an error is emitted.
#
option(BUILD_SHARED_LIBS "Build Geant4 shared libraries" ON)
option(BUILD_STATIC_LIBS "Build Geant4 static libraries" OFF)
mark_as_advanced(BUILD_SHARED_LIBS BUILD_STATIC_LIBS)
# Because both could be switched off accidently, FATAL_ERROR if neither
# option has been selected.
if(NOT BUILD_STATIC_LIBS AND NOT BUILD_SHARED_LIBS)
message(FATAL_ERROR "Neither static nor shared libraries will be built")
endif()
# Always build global libraries - always FATAL_ERROR if old
# granular library switch is set, e.g. from command line
if(GEANT4_BUILD_GRANULAR_LIBS)
message(FATAL_ERROR " Granular libraries are no longer supported!")
endif()
# On WIN32, we need to build the genwindef application to create export
# def files for building DLLs.
# We only use it as a helper application at the moment so we exclude it from
# the ALL target.
# TODO: We could move this section into the Geant4MacroLibraryTargets.cmake
# if it can be protected so that the genwindef target wouldn't be defined
# more than once... Put it here for now...
# TODO: Investigate use of CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS from CMake 3.4
# and/or other forms of symbol visibility
if(WIN32)
# Assume the sources are co-located
get_filename_component(_genwindef_src_dir ${CMAKE_CURRENT_LIST_FILE} PATH)
add_executable(genwindef EXCLUDE_FROM_ALL
${_genwindef_src_dir}/genwindef/genwindef.cpp
${_genwindef_src_dir}/genwindef/LibSymbolInfo.h
${_genwindef_src_dir}/genwindef/LibSymbolInfo.cpp)
endif()
#------------------------------------------------------------------------
#.rst:
# Build Output Directories
# ------------------------
#
# Because of the highly nested structure of Geant4, built libraries and
# programs will be distributed throughout the build tree. To simplify
# use and testing of the build, CMake is setup to output these products
# to a directory hierarchy based on the configured install locations.
# - Match output layout, so must have variables from GNUInstallDirs...
if((NOT DEFINED CMAKE_INSTALL_BINDIR) OR (NOT DEFINED CMAKE_INSTALL_LIBDIR))
message(FATAL_ERROR "Cannot configure build output dirs as install directories have not yet been defined")
endif()
#.rst
# In all cases, build products are stored in a directory tree rooted
# in a directory named ``BuildProducts`` under the ``PROJECT_BINARY_DIRECTORY``.
#
set(BASE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/BuildProducts")
#.rst:
# For single mode build generators (make, ninja), the following
# hierarchy is used:
#
# .. code-block:: console
#
# +- <PROJECT_BINARY_DIR>/
# +- BuildProducts/
# +- <CMAKE_INSTALL_BINDIR>/
# +- ... "runtime" targets ...
# +- <CMAKE_INSTALL_LIBDIR>/
# +- ... "library" and "archive" targets ...
#
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BASE_OUTPUT_DIRECTORY}/${CMAKE_INSTALL_BINDIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${BASE_OUTPUT_DIRECTORY}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${BASE_OUTPUT_DIRECTORY}/${CMAKE_INSTALL_LIBDIR}")
#.rst:
# For multimode build generators (Xcode, Visual Studio), each mode
# is separated using the hierarchy
#
# .. code-block:: console
#
# +- <PROJECT_BINARY_DIR>
# +- BuildProducts/
# +- <CONFIG>/
# +- <CMAKE_INSTALL_BINDIR>/
# +- ... "runtime" targets ...
# +- <CMAKE_INSTALL_LIBDIR>/
# +- ... "library" and "archive" targets ...
# +- ...
#
# where ``<CONFIG>`` is repeated for each build configuration listed in
# :cmake:variable:`CMAKE_CONFIGURATION_TYPES <cmake:variable:CMAKE_CONFIGURATION_TYPES>`, e.g. Release, Debug, RelWithDebInfo etc.
foreach(_conftype ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${_conftype} _conftype_uppercase)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${_conftype_uppercase}
"${BASE_OUTPUT_DIRECTORY}/${_conftype}/${CMAKE_INSTALL_BINDIR}"
)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${_conftype_uppercase}
"${BASE_OUTPUT_DIRECTORY}/${_conftype}/${CMAKE_INSTALL_LIBDIR}"
)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${_conftype_uppercase}
"${BASE_OUTPUT_DIRECTORY}/${_conftype}/${CMAKE_INSTALL_LIBDIR}"
)
endforeach()