0

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?

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • Careful using `CMAKE_{SOURCE,BINARY}_DIR`... they won't work if your project is included into another via `add_subdirectory` since they always point to the top-level. You most likely want `PROJECT_SOURCE_DIR` or `CMAKE_CURRENT_SOURCE_DIR` (equiv. `BINARY`). – Alex Reinking Feb 28 '22 at 18:54
  • 2
    Is it really necessary to manually provide these settings? It looks like they can be automatically obtained from sfml-system target. – user7860670 Feb 28 '22 at 19:04
  • @user7860670: it is, it won't work otherwise due to this same issue with `-Wpedantic`: https://github.com/sakra/cotire/issues/151 – Vittorio Romeo Feb 28 '22 at 19:14

1 Answers1

1

As far as I can tell, the flags associated with the target argument to sfml-system are already applied to the PCH. Here's my reproducer:

cmake_minimum_required(VERSION 3.22)
project(test)

add_executable(test main.cpp)
target_precompile_headers(test PRIVATE "PCH.hpp")

# This is just to probe CMake's behavior. Don't actually
# add defines this way!!
target_compile_options(test PRIVATE "-DVIA_TCO")

Then I create empty main.cpp and PCH.hpp files and do a dry-run of the build:

$ cmake -G Ninja -S . -B build -DCMAKE_CXX_FLAGS="-DVIA_FLAGS"
[1/3] /usr/bin/c++   -DVIA_FLAGS -DVIA_TCO -Winvalid-pch -x c++-header -include /home/alex/test/build/CMakeFiles/test.dir/cmake_pch.hxx -MD -MT CMakeFiles/test.dir/cmake_pch.hxx.gch -MF CMakeFiles/test.dir/cmake_pch.hxx.gch.d -o CMakeFiles/test.dir/cmake_pch.hxx.gch -c /home/alex/test/build/CMakeFiles/test.dir/cmake_pch.hxx.cxx
[2/3] /usr/bin/c++   -DVIA_FLAGS -DVIA_TCO -Winvalid-pch -include /home/alex/test/build/CMakeFiles/test.dir/cmake_pch.hxx -MD -MT CMakeFiles/test.dir/main.cpp.o -MF CMakeFiles/test.dir/main.cpp.o.d -o CMakeFiles/test.dir/main.cpp.o -c /home/alex/test/main.cpp
[3/3] : && /usr/bin/c++ -DVIA_FLAGS  CMakeFiles/test.dir/main.cpp.o -o test   && :

As you can see, both the flags from target_compile_options and CMAKE_CXX_FLAGS (at the command line) applied to the PCH.

So to this point:

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.

Any halfway reasonable way of adding flags should work automatically.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • This doesn't work in my case, unfortunately having `-Wpedantic` seems to screw things up. I have this same issue unless I explicitly add `set_property`: https://github.com/sakra/cotire/issues/151 – Vittorio Romeo Feb 28 '22 at 19:14
  • @VittorioRomeo - can you post an MRE? That's what I tried to do for you, here. – Alex Reinking Feb 28 '22 at 23:00