target_precompile_headers rebuild all project after adding/deleting .cpp files in target sources
Environment: MacOS 11.5.2, clang 12.0.5, CLion
I tryed to make title self-explained. Also, I created stackoverflow question with some actual examples.
I'm not sure is it an issue or not, but any answer might help.
Stackoverflow description
Problem: each time then I add or delete .cpp file in target source list, every translation unit gets recompiled
To demonstrate issue I created very basic project with this structure:
CMakeLists.txt
is very simple:
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 17)
project(SuperBasicProject)
set(TARGET_NAME ${PROJECT_NAME})
add_executable(${TARGET_NAME} main.cpp empty_cpp1.cpp)
target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_precompile_headers(${TARGET_NAME} PRIVATE my_pch.h)
Compiler output after running first time:
Scanning dependencies of target SuperBasicProject
[ 25%] Building CXX object CMakeFiles/SuperBasicProject.dir/cmake_pch.hxx.pch
[ 75%] Building CXX object CMakeFiles/SuperBasicProject.dir/empty_cpp1.cpp.o
[ 75%] Building CXX object CMakeFiles/SuperBasicProject.dir/main.cpp.o
[100%] Linking CXX executable SuperBasicProject
[100%] Built target SuperBasicProject
Then I'm adding another source file (empty_cpp2.cpp) to project by modifying add_executable
line:
add_executable(${TARGET_NAME} main.cpp empty_cpp1.cpp empty_cpp2.cpp)
And after that reload cmake and press build button.
Compiler output:
Scanning dependencies of target SuperBasicProject
[ 20%] Building CXX object CMakeFiles/SuperBasicProject.dir/cmake_pch.hxx.pch
[ 80%] Building CXX object CMakeFiles/SuperBasicProject.dir/empty_cpp2.cpp.o
[ 80%] Building CXX object CMakeFiles/SuperBasicProject.dir/main.cpp.o
[ 80%] Building CXX object CMakeFiles/SuperBasicProject.dir/empty_cpp1.cpp.o
[100%] Linking CXX executable SuperBasicProject
[100%] Built target SuperBasicProject
Why every translation unit gets recompiled and how to fix this? Then target is a project with hude codebase this behavior is unacceptable. Maybe I'm using target_precompile_headers wrong, or maybe there is another way, which not gonna recompile everything. I would appreciate any help.