cmake: Use configure_file() to implement forced C++ linkage

This finally prevents Ninja from rebuilding everything needlessly each time a
CMake reconfiguration was triggered. The root cause is that we used
`file(WRITE ...)`, which always unconditionally overwrites, so Ninja sees
those files as "dirty".

PiperOrigin-RevId: 453849514
Change-Id: Ib878df21371387baa7bf791a0a054e1ea5d6b6ae
This commit is contained in:
Christian Blichmann 2022-06-08 23:38:05 -07:00 committed by Copybara-Service
parent 598b00103a
commit de836031d4
4 changed files with 10 additions and 12 deletions

View File

@ -90,9 +90,10 @@ configure_file(cmake/libcap_capability.h.in
# Library with basic project settings. The empty file is there to be able to
# define header-only libraries without cumbersome target_sources() hacks.
file(WRITE "${SAPI_BINARY_DIR}/sapi_base_force_cxx_linkage.cc" "")
configure_file(cmake/sapi_force_cxx_linkage.cc.in
"${SAPI_BINARY_DIR}/sapi_force_cxx_linkage.cc" COPYONLY)
add_library(sapi_base STATIC
"${SAPI_BINARY_DIR}/sapi_base_force_cxx_linkage.cc"
"${SAPI_BINARY_DIR}/sapi_force_cxx_linkage.cc"
)
add_library(sapi::base ALIAS sapi_base)
target_compile_features(sapi_base PUBLIC

View File

@ -111,10 +111,9 @@ function(add_sapi_library)
# The sandboxed binary
set(_sapi_bin "${_sapi_NAME}.bin")
set(_sapi_force_cxx_linkage
"${CMAKE_CURRENT_BINARY_DIR}/${_sapi_bin}_force_cxx_linkage.cc")
file(WRITE "${_sapi_force_cxx_linkage}" "")
add_executable("${_sapi_bin}" "${_sapi_force_cxx_linkage}")
add_executable("${_sapi_bin}"
"${SAPI_BINARY_DIR}/sapi_force_cxx_linkage.cc"
)
target_link_libraries("${_sapi_bin}" PRIVATE
-fuse-ld=gold
-Wl,--whole-archive "${_sapi_LIBRARY}" -Wl,--no-whole-archive
@ -192,10 +191,9 @@ function(add_sapi_library)
# Library with the interface
if(NOT _sapi_SOURCES)
set(_sapi_force_cxx_linkage
"${CMAKE_CURRENT_BINARY_DIR}/${_sapi_NAME}_force_cxx_linkage.cc")
file(WRITE "${_sapi_force_cxx_linkage}" "")
list(APPEND _sapi_SOURCES "${_sapi_force_cxx_linkage}")
list(APPEND _sapi_SOURCES
"${SAPI_BINARY_DIR}/sapi_force_cxx_linkage.cc"
)
endif()
add_library("${_sapi_NAME}" STATIC
"${_sapi_gen_header}"

View File

View File

@ -48,10 +48,9 @@ target_link_libraries(sapi_util_fileops PRIVATE
)
# sandboxed_api/util:flag
file(WRITE "${SAPI_BINARY_DIR}/sapi_util_flags_force_cxx_linkage.cc" "")
add_library(sapi_util_flags ${SAPI_LIB_TYPE}
flag.h
"${SAPI_BINARY_DIR}/sapi_util_flags_force_cxx_linkage.cc"
"${SAPI_BINARY_DIR}/sapi_force_cxx_linkage.cc"
)
add_library(sapi::flags ALIAS sapi_util_flags)
target_link_libraries(sapi_util_flags PUBLIC