cmake/msvc: Add ninja support for precompiled headers

Ninja was unable to see the dependency between the cpp
that generated the pch and the compile units that used
it. Explicitly managing this now makes precompiled headers
work with both msvc and clang, with both msbuild and ninja
based generators.
This commit is contained in:
Ray molenkamp 2019-06-06 08:26:15 -06:00
parent 87fde57b63
commit 3c231c3810
1 changed files with 18 additions and 3 deletions

View File

@ -1222,9 +1222,24 @@ macro(WINDOWS_SIGN_TARGET target)
endmacro()
macro(blender_precompile_headers target cpp header)
if (MSVC AND NOT ${CMAKE_GENERATOR} STREQUAL "Ninja")
if (MSVC)
# get the name for the pch output file
get_filename_component( pchbase ${cpp} NAME_WE )
set( pchfinal "${CMAKE_CURRENT_BINARY_DIR}/${pchbase}.pch" )
# mark the cpp as the one outputting the pch
set_property(SOURCE ${cpp} APPEND PROPERTY OBJECT_OUTPUTS "${pchfinal}")
# get all sources for the target
get_target_property(sources ${target} SOURCES)
# make all sources depend on the pch to enforce the build order
foreach(src ${sources})
set_property(SOURCE ${src} APPEND PROPERTY OBJECT_DEPENDS "${pchfinal}")
endforeach()
target_sources(${target} PRIVATE ${cpp} ${header})
set_target_properties(${target} PROPERTIES COMPILE_FLAGS "/Yu${header} /FI${header}")
set_source_files_properties(${cpp} PROPERTIES COMPILE_FLAGS "/Yc${header}")
set_target_properties(${target} PROPERTIES COMPILE_FLAGS "/Yu${header} /Fp${pchfinal} /FI${header}")
set_source_files_properties(${cpp} PROPERTIES COMPILE_FLAGS "/Yc${header} /Fp${pchfinal}")
endif()
endmacro()