Back to home page

EIC code displayed by LXR

 
 

    


Warning, /geant4/cmake/Modules/G4BuildSettings.cmake is written in an unsupported language. File is not indexed.

0001 #.rst:
0002 # G4BuildSettings.cmake
0003 # ---------------------
0004 #
0005 # Set defaults for core CMake settings for compiling and linking
0006 # Geant4 libraries and tests,
0007 #
0008 # In addition to the core compiler/linker flags (configured in the
0009 # G4MakeRules_<LANG>.cmake files) for Geant4, the build may require
0010 # further configuration. This module performs this task whicj includes:
0011 #
0012 #  1) Extra build modes for developers
0013 #  2) Additional compiler definitions to assist visualization or optimize
0014 #     performance.
0015 #  3) Additional compiler flags which may be added optionally.
0016 #  4) Whether to build shared and/or static libraries.
0017 #  5) Whether to build libraries in global or granular format.
0018 #
0019 
0020 #-----------------------------------------------------------------
0021 # License and Disclaimer
0022 #
0023 # The  Geant4 software  is  copyright of the Copyright Holders  of
0024 # the Geant4 Collaboration.  It is provided  under  the terms  and
0025 # conditions of the Geant4 Software License,  included in the file
0026 # LICENSE and available at  http://cern.ch/geant4/license .  These
0027 # include a list of copyright holders.
0028 #
0029 # Neither the authors of this software system, nor their employing
0030 # institutes,nor the agencies providing financial support for this
0031 # work  make  any representation or  warranty, express or implied,
0032 # regarding  this  software system or assume any liability for its
0033 # use.  Please see the license in the file  LICENSE  and URL above
0034 # for the full disclaimer and the limitation of liability.
0035 #
0036 # This  code  implementation is the result of  the  scientific and
0037 # technical work of the GEANT4 collaboration.
0038 # By using,  copying,  modifying or  distributing the software (or
0039 # any work based  on the software)  you  agree  to acknowledge its
0040 # use  in  resulting  scientific  publications,  and indicate your
0041 # acceptance of all terms of the Geant4 Software license.
0042 #
0043 #-----------------------------------------------------------------
0044 
0045 if(NOT __G4BUILDSETTINGS_INCLUDED)
0046   set(__G4BUILDSETTINGS_INCLUDED TRUE)
0047 else()
0048   return()
0049 endif()
0050 
0051 #-----------------------------------------------------------------------
0052 #.rst:
0053 # Included Modules
0054 # ^^^^^^^^^^^^^^^^
0055 #
0056 # This module includes the following modules:
0057 #
0058 include(CheckCXXSourceCompiles)
0059 include(IntelCompileFeatures)
0060 include(MSVCCompileFeatures)
0061 
0062 #-----------------------------------------------------------------------
0063 #.rst:
0064 # Build Modes
0065 # ^^^^^^^^^^^
0066 #
0067 # Geant4 supports the standard CMake build types/configurations of
0068 #
0069 # - ``Release``
0070 # - ``Debug``
0071 # - ``MinSizeRel``
0072 # - ``RelWithDebInfo``
0073 #
0074 # For single build type tools like `make` and `ninja`, the build
0075 # defaults to ``Release`` mode unless ``CMAKE_BUILD_TYPE`` is set.
0076 # Multi build type tools like Visual Studio and Xcode will default
0077 # to
0078 #
0079 # On non-WIN32 platforms, two types specifically for development are added:
0080 #
0081 if(NOT WIN32)
0082 
0083 #.rst:
0084 # - ``Debug_FPE``
0085 #   For debugging with full Floating Point Exception checking
0086 #
0087 set(CMAKE_CXX_FLAGS_DEBUG_FPE "${CMAKE_CXX_FLAGS_DEBUG_FPE_INIT}"
0088   CACHE STRING "Flags used by the compiler during Debug_FPE builds"
0089   )
0090 mark_as_advanced(CMAKE_CXX_FLAGS_DEBUG_FPE)
0091 
0092 #.rst:
0093 # - ``TestRelease``:
0094 #   For trial production and extended testing. It has verbose
0095 #   output, has debugging symbols, and adds definitions to allow FPE
0096 #   and physics conservation law testing where supported.
0097 #
0098 set(CMAKE_CXX_FLAGS_TESTRELEASE "${CMAKE_CXX_FLAGS_TESTRELEASE_INIT}"
0099   CACHE STRING "Flags used by the compiler during TestRelease builds"
0100   )
0101 mark_as_advanced(CMAKE_CXX_FLAGS_TESTRELEASE)
0102 
0103 #.rst:
0104 # - ``FullRelWithDebInfo``:
0105 #   For trial production and extended testing. Maximum optimization
0106 #   and debugging symbols
0107 #
0108 set(CMAKE_CXX_FLAGS_FULLRELWITHDEBINFO "${CMAKE_CXX_FLAGS_FULLRELWITHDEBINFO_INIT}"
0109   CACHE STRING "Flags used by the compiler during FullRelWithDebInfo builds"
0110   )
0111 mark_as_advanced(CMAKE_CXX_FLAGS_FULLRELWITHDEBINFO)
0112 
0113 #.rst:
0114 # Compiler flags specific to these build types are set in the cache, and
0115 # the types are added to the ``CMAKE_BUILD_TYPES`` cache string and to
0116 # ``CMAKE_CONFIGURATION_TYPES`` if appropriate to the build tool being used.
0117 #
0118 if(NOT CMAKE_CONFIGURATION_TYPES)
0119   # Single mode build tools like Make, Ninja,
0120   set(__g4buildmodes "" Release TestRelease MinSizeRel Debug Debug_FPE RelWithDebInfo FullRelWithDebInfo)
0121   if(NOT CMAKE_BUILD_TYPE)
0122     # Default to a Release build if nothing else...
0123     set(CMAKE_BUILD_TYPE Release
0124       CACHE STRING "Choose the type of build, options are: None Release TestRelease MinSizeRel Debug Debug_FPE RelWithDebInfo FullRelWithDebInfo."
0125       FORCE
0126       )
0127   else()
0128     # Force to the cache, but use existing value.
0129     set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}"
0130       CACHE STRING "Choose the type of build, options are: None Release TestRelease MinSizeRel Debug Debug_FPE RelWithDebInfo FullRelWithDebInfo."
0131       FORCE
0132       )
0133   endif()
0134   set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${__g4buildmodes})
0135 else()
0136   # Multimode tools like VS, Xcode
0137   list(APPEND CMAKE_CONFIGURATION_TYPES Debug_FPE)
0138   list(APPEND CMAKE_CONFIGURATION_TYPES TestRelease)
0139   list(APPEND CMAKE_CONFIGURATION_TYPES FullRelWithDebInfo)
0140   list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
0141   set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}"
0142     CACHE STRING "Geant4 configurations for multimode build tools"
0143     FORCE
0144     )
0145 endif()
0146 
0147 endif() #NOT WIN32
0148 
0149 
0150 #-----------------------------------------------------------------------
0151 #.rst:
0152 # Compilation Options
0153 # ^^^^^^^^^^^^^^^^^^^
0154 #
0155 # The following options affect the compilation of the Geant4 libraries
0156 #
0157 
0158 #.rst
0159 # - ``CMAKE_CXX_STANDARD`` (Allowed values: 17, 20, 23)
0160 # - ``GEANT4_BUILD_CXXSTD`` (DEPRECATED)
0161 #
0162 #   - Choose C++ Standard to build against from supported list.
0163 #   - C++23 awareness is only available from CMake 3.20 onwards
0164 set(__g4_default_cxxstd 17 20)
0165 
0166 if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.20)
0167   list(APPEND __g4_default_cxxstd 23)
0168 endif()
0169 
0170 if(GEANT4_BUILD_CXXSTD)
0171   message(WARNING
0172     "The GEANT4_BUILD_CXXSTD argument is deprecated.\n"
0173     "Please use CMAKE_CXX_STANDARD to set the required C++ Standard. "
0174     "The value supplied will be used to set CMAKE_CXX_STANDARD, but this behaviour will "
0175     "be removed in future releases.")
0176   set(CMAKE_CXX_STANDARD ${GEANT4_BUILD_CXXSTD})
0177 endif()
0178 
0179 enum_option(CMAKE_CXX_STANDARD
0180   DOC "C++ Standard to compile against"
0181   VALUES ${__g4_default_cxxstd}
0182   CASE_INSENSITIVE
0183   )
0184 
0185 mark_as_advanced(CMAKE_CXX_STANDARD)
0186 geant4_add_feature(CMAKE_CXX_STANDARD "Compiling against C++ Standard '${CMAKE_CXX_STANDARD}'")
0187 
0188 # Setup required CXX variables: must not use vendor extensions, must require standard
0189 # Set convenience variable for compile features to use
0190 set(CMAKE_CXX_EXTENSIONS OFF)
0191 set(CMAKE_CXX_STANDARD_REQUIRED ON)
0192 set(GEANT4_TARGET_COMPILE_FEATURES cxx_std_${CMAKE_CXX_STANDARD})
0193 
0194 # Emit early fatal error if we don't have a record of the requested standard
0195 # in CMake's known features. CMake *will* check this for us as well, but doing
0196 # the check here only emits one error. We can also give a better hint on the
0197 # cause/resolution
0198 if(NOT ("${CMAKE_CXX_COMPILE_FEATURES}" MATCHES "cxx_std_${CMAKE_CXX_STANDARD}"))
0199   message(FATAL_ERROR
0200     "Geant4 requested compilation using C++ standard '${CMAKE_CXX_STANDARD}' with compiler\n"
0201     "'${CMAKE_CXX_COMPILER_ID}', version '${CMAKE_CXX_COMPILER_VERSION}'\n"
0202     "but CMake ${CMAKE_VERSION} is not aware of any support for that standard by this compiler. You may need a newer CMake and/or compiler.\n")
0203 endif()
0204 
0205 # - Check for Language/Standard Library Implementation Features
0206 #
0207 # - A very dumb wrapper round try_compile to give simple reporting
0208 function(check_cxx_feature _flag _bindir _src)
0209   try_compile(${_flag} ${_bindir} ${_src} ${ARGN})
0210   # Can be simplified after 3.17 with CHECK_PASS/FAIL
0211   set(__preamble "Checking C++ feature ${_flag} -")
0212   if(${_flag})
0213     message(STATUS "${__preamble} Success")
0214   else()
0215     message(STATUS "${__preamble} Failed")
0216   endif()
0217 endfunction()
0218 
0219 # - Filesystem may be experimental/filesystem, and in stdc++fs (GCC/libstdc++), c++fs (Clang/libc++)
0220 #   G4global will provide this as a PUBLIC dependency, if required
0221 #   - native stdlib (nothing needed)
0222 #   - GNU: stdc++fs
0223 #   - Clang: c++fs
0224 check_cxx_feature(CXXSTDLIB_FILESYSTEM_NATIVE
0225   ${PROJECT_BINARY_DIR}/cxx_filesystem/native
0226   ${PROJECT_SOURCE_DIR}/cmake/Modules/check_cxx_filesystem.cc)
0227 
0228 if(NOT CXXSTDLIB_FILESYSTEM_NATIVE)
0229   # GNU libstdc++fs
0230   check_cxx_feature(CXXSTDLIB_FILESYSTEM_STDCXXFS
0231     ${PROJECT_BINARY_DIR}/cxx_filesystem/stdc++fs
0232     ${PROJECT_SOURCE_DIR}/cmake/Modules/check_cxx_filesystem.cc
0233     LINK_LIBRARIES stdc++fs)
0234   # LLVM libc++fs
0235   check_cxx_feature(CXXSTDLIB_FILESYSTEM_CXXFS
0236     ${PROJECT_BINARY_DIR}/cxx_filesystem/c++fs
0237     ${PROJECT_SOURCE_DIR}/cmake/Modules/check_cxx_filesystem.cc
0238     LINK_LIBRARIES c++fs)
0239 endif()
0240 # Derive name of library to link to
0241 # Default is native...
0242 if(CXXSTDLIB_FILESYSTEM_NATIVE)
0243   set(GEANT4_CXX_FILESYSTEM_LIBRARY )
0244 elseif(CXXSTDLIB_FILESYSTEM_STDCXXFS)
0245   set(GEANT4_CXX_FILESYSTEM_LIBRARY "stdc++fs")
0246 elseif(CXXSTDLIB_FILESYSTEM_CXXFS)
0247   set(GEANT4_CXX_FILESYSTEM_LIBRARY "c++fs")
0248 else()
0249   message(FATAL_ERROR "No support for C++ filesystem found for compiler '${CMAKE_CXX_COMPILER_ID}', '${CMAKE_CXX_COMPILER_VERSION}'")
0250 endif()
0251 
0252 # Hold any appropriate compile flag(s) in variable for later export to
0253 # non-cmake config files
0254 set(GEANT4_CXXSTD_FLAGS "${CMAKE_CXX${CMAKE_CXX_STANDARD}_STANDARD_COMPILE_OPTION}")
0255 
0256 #.rst:
0257 # - ``GEANT4_BUILD_SANITIZER`` (Allowed values: none address thread undefined)
0258 #
0259 #   - Build libraries with instrumentation of the given sanitizer
0260 #   - Only for GNU/Clang at the moment
0261 #   - VS 2019 16.9 supports asan: https://devblogs.microsoft.com/cppblog/address-sanitizer-for-msvc-now-generally-available/)
0262 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|.*Clang")
0263   enum_option(GEANT4_BUILD_SANITIZER
0264     DOC "Build libraries with sanitizer instrumentation"
0265     VALUES none address thread undefined
0266     CASE_INSENSITIVE)
0267 
0268   mark_as_advanced(GEANT4_BUILD_SANITIZER)
0269 
0270   if(NOT (GEANT4_BUILD_SANITIZER STREQUAL "none"))
0271     # Add flags - longer term, make compile/link options
0272     # frame pointer flag to get more meaningful stack traces
0273     # May need others for better/reliable output
0274     set(__geant4_sanitizer_flags "-fno-omit-frame-pointer -fsanitize=${GEANT4_BUILD_SANITIZER}")
0275     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${__geant4_sanitizer_flags}")
0276     # Xcode does not forward CXX_FLAGS to the linker, and sanitizer link flags need propagating to final
0277     # link steps
0278     if(CMAKE_GENERATOR MATCHES Xcode)
0279       set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${__geant4_sanitizer_flags}")
0280       set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${__geant4_sanitizer_flags}")
0281     endif()
0282 
0283     geant4_add_feature(GEANT4_BUILD_SANITIZER "Compiling/linking with sanitizer '${GEANT4_BUILD_SANITIZER}'")
0284   endif()
0285 endif()
0286 
0287 #.rst:
0288 # - ``GEANT4_BUILD_ENABLE_ASSERTIONS``
0289 #   - Build libraries with assertions enabled even in release build types (Release, RelWithDebInfo, MinSizeRel)
0290 #
0291 option(GEANT4_BUILD_ENABLE_ASSERTIONS "Enable assertions regardless of build mode" OFF)
0292 geant4_add_feature(GEANT4_BUILD_ENABLE_ASSERTIONS "Compiling Geant4 with assertions enabled in all build types")
0293 mark_as_advanced(GEANT4_BUILD_ENABLE_ASSERTIONS)
0294 
0295 # "Dumb" strip of NDEBUG definition from build type flags
0296 if(GEANT4_BUILD_ENABLE_ASSERTIONS)
0297   foreach(__build_type Release MinSizeRel RelWithDebInfo)
0298     string(TOUPPER ${__build_type} __build_type_uc)
0299     foreach(__lang C CXX)
0300       set(FLAGSET_TO_MODIFY "CMAKE_${__lang}_FLAGS_${__build_type_uc}")
0301       string(REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " " new_flags "${${FLAGSET_TO_MODIFY}}")
0302       set(${FLAGSET_TO_MODIFY} "${new_flags}")
0303     endforeach()
0304   endforeach()
0305 endif()
0306 
0307 # Copy .clang-tidy file to build dir
0308 # - clang-tidy/run-clang-tidy state that by default:
0309 #
0310 #   "clang-tidy will attempt to find a file named .clang-tidy for each source file in its parent directories."
0311 #
0312 #   Whilst this does appear to be the case, `run-clang-tidy` will report enabled checks
0313 #   as the default set, or that of any .clang-tidy found in parent directories of the build dir
0314 #   To avoid confusion, copy .clang-tidy into build directory so that run-clang-tidy
0315 #   should report correctly
0316 #
0317 if(EXISTS "${PROJECT_SOURCE_DIR}/.clang-tidy")
0318   configure_file("${PROJECT_SOURCE_DIR}/.clang-tidy" "${PROJECT_BINARY_DIR}/.clang-tidy" COPYONLY)
0319 endif()
0320 
0321 #-----------------------------------------------------------------------
0322 # Multithreading
0323 #-----------------------------------------------------------------------
0324 #.rst:
0325 # - ``GEANT4_BUILD_MULTITHREADED`` (Default: ``OFF``)
0326 #
0327 #   If set to ``ON``, compile Geant4 with support for multithreading.
0328 #   On Win32, this option requires use of static libraries only
0329 #
0330 option(GEANT4_BUILD_MULTITHREADED "Enable multithreading in Geant4" ON)
0331 geant4_add_feature(GEANT4_BUILD_MULTITHREADED "Build multithread enabled libraries")
0332 
0333 #.rst:
0334 # - ``GEANT4_BUILD_TLS_MODEL`` (Default: ``initial-exec``)
0335 #
0336 #   If ``GEANT4_BUILD_MULTITHREADED`` is ``ON`` and a GNU/Clang/Intel
0337 #   compiler is being used, then this option may be used to choose the
0338 #   Thread Local Storage model. A default of ``initial-exec`` is used
0339 #   to provide optimal performance for applications directly linking
0340 #   to the Geant4 libraries. The dummy ``auto`` model may be selected
0341 #   to leave model selection to the compiler, with no additional flag(s)
0342 #   passed to the compiler.
0343 #
0344 if(GEANT4_BUILD_MULTITHREADED)
0345   # - Need Thread Local Storage support (POSIX)
0346   if(UNIX)
0347     check_cxx_source_compiles("__thread int i; int main(){return 0;}" HAVE_TLS)
0348     if(NOT HAVE_TLS)
0349       message(FATAL_ERROR "Configured compiler ${CMAKE_CXX_COMPILER} does not support thread local storage")
0350     endif()
0351   endif()
0352 
0353   # - Allow advanced users to select the thread local storage model,
0354   # if the compiler supports it, defaulting to that recommended by Geant4
0355   if(TLSMODEL_IS_AVAILABLE)
0356     enum_option(GEANT4_BUILD_TLS_MODEL
0357       DOC "Build libraries with Thread Local Storage model"
0358       VALUES ${TLSMODEL_IS_AVAILABLE}
0359       CASE_INSENSITIVE
0360     )
0361     mark_as_advanced(GEANT4_BUILD_TLS_MODEL)
0362 
0363     if(GEANT4_BUILD_TLS_MODEL STREQUAL "auto")
0364       geant4_add_feature(GEANT4_BUILD_TLS_MODEL "Building without explicit TLS model")
0365     else()
0366       geant4_add_feature(GEANT4_BUILD_TLS_MODEL "Building with TLS model '${GEANT4_BUILD_TLS_MODEL}'")
0367     endif()
0368 
0369     set(GEANT4_MULTITHREADED_CXX_FLAGS "${GEANT4_MULTITHREADED_CXX_FLAGS} ${${GEANT4_BUILD_TLS_MODEL}_TLSMODEL_FLAGS}")
0370   endif()
0371 
0372   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GEANT4_MULTITHREADED_CXX_FLAGS}")
0373 endif()
0374 
0375 #-----------------------------------------------------------------------
0376 # Link-time optimization
0377 #-----------------------------------------------------------------------
0378 # If LTO is enabled via CMAKE_INTERPROCEDURAL_OPTIMIZATION, then add
0379 # additional flags to force identify/check ODR violations for GCC only (at present)
0380 # Note that these are _nominally_ enabled by default in GCC with flto, so
0381 # they are added explicitly for the sake of clarity. Also, some are only valid for 
0382 # older GCC versions, but they do not break things.
0383 # TODO: Review with experiments what recommended options are here.
0384 if(CMAKE_INTERPROCEDURAL_OPTIMIZATION AND (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
0385   string(APPEND CMAKE_CXX_FLAGS " -Wodr -fipa-icf -flto-odr-type-merging") 
0386 endif()
0387 
0388 #-----------------------------------------------------------------------
0389 # Physics
0390 #-----------------------------------------------------------------------
0391 #.rst:
0392 # - ``GEANT4_BUILD_PHP_AS_HP`` (Default: OFF)
0393 #
0394 #   - Build ParticleHP as HP.
0395 #
0396 set(_default_build_php_as_hp OFF)
0397 option(GEANT4_BUILD_PHP_AS_HP "Build ParticleHP as HP" ${_default_build_php_as_hp})
0398 mark_as_advanced(GEANT4_BUILD_PHP_AS_HP)
0399 geant4_add_feature(GEANT4_BUILD_PHP_AS_HP "Building ParticleHP as HP")
0400 
0401 #-----------------------------------------------------------------------
0402 # Miscellaneous
0403 #-----------------------------------------------------------------------
0404 #.rst:
0405 # - ``GEANT4_BUILD_STORE_TRAJECTORY`` (Default: ON)
0406 #
0407 #   - Switching off can improve performance. Needs to be on
0408 #     for visualization to work fully. Mark as advanced because most users
0409 #     should not need to worry about it.
0410 #
0411 option(GEANT4_BUILD_STORE_TRAJECTORY
0412   "Store trajectories in event processing. Switch off for improved performance but note that visualization of trajectories will not be possible"
0413   ON)
0414 mark_as_advanced(GEANT4_BUILD_STORE_TRAJECTORY)
0415 
0416 #.rst:
0417 # - ``GEANT4_BUILD_VERBOSE_CODE`` (Default: ON)
0418 #
0419 #   - Switching off can improve performance, but at the cost
0420 #     of fewer informational or warning messages.
0421 #     Mark as advanced because most users should not need to worry about it.
0422 #
0423 option(GEANT4_BUILD_VERBOSE_CODE
0424   "Enable verbose output from Geant4 code. Switch off for better performance at the cost of fewer informational messages or warnings"
0425   ON)
0426 mark_as_advanced(GEANT4_BUILD_VERBOSE_CODE)
0427 
0428 #.rst:
0429 # - ``GEANT4_BUILD_BUILTIN_BACKTRACE`` (Unix only, Default: OFF)
0430 #
0431 #   - Setting to ``ON`` will build in automatic signal handling for ``G4RunManager``
0432 #     through ``G4Backtrace``. Applications requiring/implenting their own signal
0433 #     handling should not enable this option.
0434 #
0435 if(UNIX)
0436   option(GEANT4_BUILD_BUILTIN_BACKTRACE
0437     "Enable automatic G4Backtrace signal handling in G4RunManager. Switch off for applications implementing their own signal handling"
0438     OFF)
0439   mark_as_advanced(GEANT4_BUILD_BUILTIN_BACKTRACE)
0440 endif()
0441 
0442 #.rst:
0443 # - ``GEANT4_BUILD_MSVC_MP`` (Windows only, Default: OFF)
0444 #
0445 #    - Provide optional file level parallelization with MSVC compiler.
0446 #
0447 
0448 # NB: This will only work if the build tool performs compilations
0449 # with multiple sources, e.g. "cl.exe a.cc b.cc c.cc"
0450 if(MSVC)
0451   option(GEANT4_BUILD_MSVC_MP "Use /MP option with MSVC for file level parallel builds" OFF)
0452   mark_as_advanced(GEANT4_BUILD_MSVC_MP)
0453 
0454   if(GEANT4_BUILD_MSVC_MP)
0455     set(CMAKE_CXX_FLAGS "/MP ${CMAKE_CXX_FLAGS}")
0456   endif()
0457 endif()
0458 
0459 
0460 #-----------------------------------------------------------------------
0461 #.rst:
0462 # Library Mode and Link Options
0463 # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0464 #
0465 # - ``BUILD_SHARED_LIBS`` (Default: ``ON``)
0466 #
0467 #   - Build shared libraries (`.so` on Unix, `.dylib` on macOS, `.dll/.lib` on Windows)
0468 #
0469 # - ``BUILD_STATIC_LIBS`` (Default: ``OFF``)
0470 #
0471 #   - Build static libraries (`.a` on Unices, `.lib` on Windows)
0472 #
0473 # Both options are adavanced and may be selected together to build
0474 # both types. If neither is selected, an error is emitted.
0475 #
0476 option(BUILD_SHARED_LIBS "Build Geant4 shared libraries" ON)
0477 option(BUILD_STATIC_LIBS "Build Geant4 static libraries" OFF)
0478 mark_as_advanced(BUILD_SHARED_LIBS BUILD_STATIC_LIBS)
0479 
0480 # Because both could be switched off accidently, FATAL_ERROR if neither
0481 # option has been selected.
0482 if(NOT BUILD_STATIC_LIBS AND NOT BUILD_SHARED_LIBS)
0483   message(FATAL_ERROR "Neither static nor shared libraries will be built")
0484 endif()
0485 
0486 # Always build global libraries - always FATAL_ERROR if old
0487 # granular library switch is set, e.g. from command line
0488 if(GEANT4_BUILD_GRANULAR_LIBS)
0489   message(FATAL_ERROR " Granular libraries are no longer supported!")
0490 endif()
0491 
0492 #------------------------------------------------------------------------
0493 #.rst:
0494 # Build Output Directories
0495 # ------------------------
0496 #
0497 # Because of the highly nested structure of Geant4, built libraries and
0498 # programs will be distributed throughout the build tree. To simplify
0499 # use and testing of the build, CMake is setup to output these products
0500 # to a directory hierarchy based on the configured install locations.
0501 
0502 # - Match output layout, so must have variables from GNUInstallDirs...
0503 if((NOT DEFINED CMAKE_INSTALL_BINDIR) OR (NOT DEFINED CMAKE_INSTALL_LIBDIR))
0504   message(FATAL_ERROR "Cannot configure build output dirs as install directories have not yet been defined")
0505 endif()
0506 
0507 #.rst
0508 # In all cases, build products are stored in a directory tree rooted
0509 # in a directory named ``BuildProducts`` under the ``PROJECT_BINARY_DIRECTORY``.
0510 #
0511 set(BASE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/BuildProducts")
0512 
0513 #.rst:
0514 # For single mode build generators (make, ninja), the following
0515 #  hierarchy is used:
0516 #
0517 #  .. code-block:: console
0518 #
0519 #    +- <PROJECT_BINARY_DIR>/
0520 #       +- BuildProducts/
0521 #          +- <CMAKE_INSTALL_BINDIR>/
0522 #             +- ... "runtime" targets ...
0523 #          +- <CMAKE_INSTALL_LIBDIR>/
0524 #             +- ... "library" and "archive" targets ...
0525 #
0526 
0527 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BASE_OUTPUT_DIRECTORY}/${CMAKE_INSTALL_BINDIR}")
0528 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${BASE_OUTPUT_DIRECTORY}/${CMAKE_INSTALL_LIBDIR}")
0529 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${BASE_OUTPUT_DIRECTORY}/${CMAKE_INSTALL_LIBDIR}")
0530 
0531 #.rst:
0532 #  For multimode build generators (Xcode, Visual Studio), each mode
0533 #  is separated using the hierarchy
0534 #
0535 #  .. code-block:: console
0536 #
0537 #    +- <PROJECT_BINARY_DIR>
0538 #       +- BuildProducts/
0539 #          +- <CONFIG>/
0540 #             +- <CMAKE_INSTALL_BINDIR>/
0541 #                +- ... "runtime" targets ...
0542 #             +- <CMAKE_INSTALL_LIBDIR>/
0543 #                +- ... "library" and "archive" targets ...
0544 #          +- ...
0545 #
0546 #  where ``<CONFIG>`` is repeated for each build configuration listed in
0547 #  :cmake:variable:`CMAKE_CONFIGURATION_TYPES <cmake:variable:CMAKE_CONFIGURATION_TYPES>`, e.g. Release, Debug, RelWithDebInfo etc.
0548 
0549 foreach(_conftype ${CMAKE_CONFIGURATION_TYPES})
0550   string(TOUPPER ${_conftype} _conftype_uppercase)
0551   set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${_conftype_uppercase}
0552     "${BASE_OUTPUT_DIRECTORY}/${_conftype}/${CMAKE_INSTALL_BINDIR}"
0553     )
0554   set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${_conftype_uppercase}
0555     "${BASE_OUTPUT_DIRECTORY}/${_conftype}/${CMAKE_INSTALL_LIBDIR}"
0556     )
0557   set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${_conftype_uppercase}
0558     "${BASE_OUTPUT_DIRECTORY}/${_conftype}/${CMAKE_INSTALL_LIBDIR}"
0559     )
0560 endforeach()