target_precompile_headers REUSE_FROM does not play well with CMAKE_OPTIMIZE_DEPENDENCIES
If you have 2 C++ static libraries, Bar_Lib and Foo_Lib where you want to use Foo_Lib headers from Bar_Lib, you can create a target_link_libraries dependency:
target_link_libraries(Bar_Lib PUBLIC Foo_Lib)
but if you don't want them build-order sequenced you can specify:
set(CMAKE_OPTIMIZE_DEPENDENCIES 1)
So far so good.
However, if you also add a PCH to each static lib, i.e.: target_precompile_headers(Foo_Lib REUSE_FROM Precompiled_Lib)
and target_precompile_headers(Bar_Lib REUSE_FROM Precompiled_Lib)
it causes Bar_Lib to pick up a build-time dependency on Foo_Lib. (They get build-time dependencies on Precompiled_Lib as well, but that's obviously wanted. But Bar_Lib shouldn't depend on Foo_Lib to be built.)
When I look at GLOBAL_DEPENDS_DEBUG_MODE
I see that without the precompiled header, there is no dependency between Bar_Lib and Foo_Lib. When I specify the Precompiled_Lib it gains a (weak) dependency, which apparently is enough to add it to the project dependency list (Visual Studio).
target 62 is [Bar_Lib]
depends on target 60 [Foo_Lib] (weak)
depends on target 3 [Precompiled_Lib] (strong)
The following self-contained cmake repros this issue. Comment out the target_precompile_headers to see the effect in action.
cmake_minimum_required(VERSION 3.20)
set(CMAKE_OPTIMIZE_DEPENDENCIES 1)
project(ProjectName)
add_library(pch STATIC EXCLUDE_FROM_ALL)
target_sources(pch PRIVATE pch.c)
target_precompile_headers(pch PUBLIC pch.h)
add_library(foo STATIC EXCLUDE_FROM_ALL)
target_sources(foo PRIVATE foo.c)
target_precompile_headers(foo REUSE_FROM pch)
add_library(bar STATIC EXCLUDE_FROM_ALL)
target_sources(bar PRIVATE bar.c)
target_link_libraries(bar PRIVATE foo)
target_precompile_headers(bar REUSE_FROM pch)
add_executable(main)
target_sources(main PRIVATE main.c)
target_link_libraries(main PRIVATE foo bar)
target_precompile_headers(main REUSE_FROM pch)