CMake: Force inclusion of exported functions in add_sapi_library()

Newer versions of the GNU gold linkers sometimes omit the symbols for
sandboxed functions, leading to errors like `function not found` when
Sandboxed API tries to call them at runtime.

The fix is simple: Do what Bazel does and wrap the static library in
a `-Wl,--whole-archive`/`-Wl,--no-whole-archive` pair.

PiperOrigin-RevId: 426910732
Change-Id: Id4183f2964a2dea4fc1dfd8c409bc0b1af9db170
This commit is contained in:
Christian Blichmann 2022-02-07 07:22:46 -08:00 committed by Copybara-Service
parent 0f78eb4c59
commit 5c98e30ef2

View File

@ -100,8 +100,7 @@ function(add_sapi_library)
list(APPEND _sapi_exported_funcs "LINKER:--export-dynamic-symbol,${func}")
endforeach()
if(NOT _sapi_exported_funcs)
set(_sapi_exported_funcs LINKER:--whole-archive
LINKER:--allow-multiple-definition)
set(_sapi_exported_funcs LINKER:--allow-multiple-definition)
endif()
# The sandboxed binary
@ -112,7 +111,7 @@ function(add_sapi_library)
add_executable("${_sapi_bin}" "${_sapi_force_cxx_linkage}")
target_link_libraries("${_sapi_bin}" PRIVATE
-fuse-ld=gold
"${_sapi_LIBRARY}"
-Wl,--whole-archive "${_sapi_LIBRARY}" -Wl,--no-whole-archive
sapi::client
${CMAKE_DL_LIBS}
)