I am using CMake's target_precompile_headers to generate a precompiled header from a PCH.hpp file in a project I'm contributing to.
I need to ensure that the resultant generated source file has the exact same compilation and warning flags as the other source files in my project, to avoid -Winvalid-pch issues.
At the moment, I am doing this:
target_precompile_headers(sfml-system PRIVATE "${CMAKE_SOURCE_DIR}/src/SFML/PCH.hpp")
set_property(SOURCE "${CMAKE_BINARY_DIR}/src/SFML/System/CMakeFiles/sfml-system.dir/cmake_pch.hxx.cxx"
APPEND_STRING PROPERTY COMPILE_FLAGS " ${WARNINGS}")
However, having to hardcode the path to the generated cmake_pch.hxx.cxx file seems brittle, potentially non-portable, and a maintenance burden.
Is there any way I can retrieve the path to the cmake_pch.hxx.cxx file without hardcoding it?
As far as I can tell, the flags associated with the target argument to
sfml-systemare already applied to the PCH. Here's my reproducer:Then I create empty
main.cppandPCH.hppfiles and do a dry-run of the build:As you can see, both the flags from
target_compile_optionsandCMAKE_CXX_FLAGS(at the command line) applied to the PCH.So to this point:
Any halfway reasonable way of adding flags should work automatically.