# Distributed under the MIT License. # See LICENSE.txt for details. # Support for precompiled headers (PCH) # ===================================== # # Precompiled headers are a way to speed up compilation by precompiling # frequently used headers. # # We have the following requirements on the PCH: # - it must be rebuilt whenever anything it includes is changed. # - it must be removed when running `make clean` # - it must be generated before any object files are built that # use it # - it must be able to use flags from other targets (since in modern CMake # flags, etc. are propagated via targets) # - the correct flags to include the PCH must be propagated to any targets # that use the PCH. # # We use CMake's PCH support to accomplish this. It is documented here: # https://cmake.org/cmake/help/latest/command/target_precompile_headers.html. # # The PCH is generated by compiling a source file that includes all the headers # we want to precompile. These precompiled headers are listed in # `tools/SpectrePch.hpp`. # # Instructions for using the PCH: # ------------------------------- # # Targets can use the PCH generated for the `SpectrePch` library. They must also # link the `SpectrePchFlags` library so they compile with the same flags as the # PCH. Make sure to check the PCH exist before using them: # # if(TARGET SpectrePch) # target_precompile_headers(${TARGET_NAME} REUSE_FROM SpectrePch) # target_link_libraries(${TARGET_NAME} PRIVATE SpectrePchFlags) # endif() # This library tracks all compiler flags to build the PCH. Targets that use # the PCH must link this library so they compile with the same flags as the # PCH. add_library(SpectrePchFlags INTERFACE) target_link_libraries( SpectrePchFlags INTERFACE Blaze Brigand Charmxx::charmxx Charmxx::pup HDF5::HDF5 SpectreFlags SpectreKokkos ) # Targets can reuse the PCH generated for this library. They must also link # the `SpectrePchFlags` library. Notes: # - We store the PCH header in ${CMAKE_SOURCE_DIR}/tools so that it is not # accidentally included anywhere. # - We also store a source file that just includes the PCH header so the # library has something to compile. # - This is an object library because we don't need to actually link it, we # only need to build it so it generates the PCH. configure_file( ${CMAKE_SOURCE_DIR}/tools/SpectrePch.hpp ${CMAKE_BINARY_DIR} ) set(SPECTRE_PCH_HEADER ${CMAKE_BINARY_DIR}/SpectrePch.hpp) file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/SpectrePch.cpp CONTENT "#include \"${SPECTRE_PCH_HEADER}\"\n") add_library( SpectrePch OBJECT ${CMAKE_BINARY_DIR}/SpectrePch.cpp) target_precompile_headers( SpectrePch PRIVATE ${SPECTRE_PCH_HEADER} ) target_link_libraries( SpectrePch PRIVATE SpectrePchFlags )