CMake: Use PARSE_ARGV for argument parsing, LINKER: for linking

Addresses #104 and also fixes a TODO to use `target_link_options()`
and the `LINKER:` prefix in the SAPI build defs.

PiperOrigin-RevId: 426113536
Change-Id: I5322ab975986ceccedfab030cd4610287d297c35
This commit is contained in:
Christian Blichmann 2022-02-03 04:42:08 -08:00 committed by Copybara-Service
parent 9ee3a26e8b
commit b6d65ef244
2 changed files with 11 additions and 12 deletions

View File

@ -19,7 +19,8 @@
# SOURCES is a list of files that should be embedded. If a source names a # SOURCES is a list of files that should be embedded. If a source names a
# target the target binary is embedded instead. # target the target binary is embedded instead.
macro(sapi_cc_embed_data) macro(sapi_cc_embed_data)
cmake_parse_arguments(_sapi_embed "" "OUTPUT_NAME;NAME;NAMESPACE" "SOURCES" ${ARGN}) cmake_parse_arguments(_sapi_embed "" "OUTPUT_NAME;NAME;NAMESPACE" "SOURCES"
${ARGN})
foreach(src IN LISTS _sapi_embed_SOURCES) foreach(src IN LISTS _sapi_embed_SOURCES)
if(TARGET "${src}") if(TARGET "${src}")
get_target_property(_sapi_embed_src_OUTPUT_NAME ${src} OUTPUT_NAME) get_target_property(_sapi_embed_src_OUTPUT_NAME ${src} OUTPUT_NAME)
@ -90,20 +91,17 @@ function(add_sapi_library)
set(_sapi_opts NOEMBED) set(_sapi_opts NOEMBED)
set(_sapi_one_value HEADER LIBRARY LIBRARY_NAME NAMESPACE) set(_sapi_one_value HEADER LIBRARY LIBRARY_NAME NAMESPACE)
set(_sapi_multi_value SOURCES FUNCTIONS INPUTS) set(_sapi_multi_value SOURCES FUNCTIONS INPUTS)
cmake_parse_arguments(_sapi cmake_parse_arguments(PARSE_ARGV 0 _sapi "${_sapi_opts}"
"${_sapi_opts}" "${_sapi_one_value}" "${_sapi_multi_value}")
"${_sapi_one_value}"
"${_sapi_multi_value}"
${ARGN})
set(_sapi_NAME "${ARGV0}") set(_sapi_NAME "${ARGV0}")
set(_sapi_gen_header "${_sapi_NAME}.sapi.h") set(_sapi_gen_header "${_sapi_NAME}.sapi.h")
foreach(func IN LISTS _sapi_FUNCTIONS) foreach(func IN LISTS _sapi_FUNCTIONS)
list(APPEND _sapi_exported_funcs "-Wl,--export-dynamic-symbol,${func}") list(APPEND _sapi_exported_funcs "LINKER:--export-dynamic-symbol,${func}")
endforeach() endforeach()
if(NOT _sapi_exported_funcs) if(NOT _sapi_exported_funcs)
set(_sapi_exported_funcs -Wl,--whole-archive set(_sapi_exported_funcs LINKER:--whole-archive
-Wl,--allow-multiple-definition) LINKER:--allow-multiple-definition)
endif() endif()
# The sandboxed binary # The sandboxed binary
@ -112,13 +110,14 @@ function(add_sapi_library)
"${CMAKE_CURRENT_BINARY_DIR}/${_sapi_bin}_force_cxx_linkage.cc") "${CMAKE_CURRENT_BINARY_DIR}/${_sapi_bin}_force_cxx_linkage.cc")
file(WRITE "${_sapi_force_cxx_linkage}" "") file(WRITE "${_sapi_force_cxx_linkage}" "")
add_executable("${_sapi_bin}" "${_sapi_force_cxx_linkage}") add_executable("${_sapi_bin}" "${_sapi_force_cxx_linkage}")
# TODO(cblichmann): Use target_link_options on CMake >= 3.13
target_link_libraries("${_sapi_bin}" PRIVATE target_link_libraries("${_sapi_bin}" PRIVATE
-fuse-ld=gold -fuse-ld=gold
"${_sapi_LIBRARY}" "${_sapi_LIBRARY}"
sapi::client sapi::client
${CMAKE_DL_LIBS} ${CMAKE_DL_LIBS}
-Wl,-E )
target_link_options("${_sapi_bin}" PRIVATE
LINKER:-E
${_sapi_exported_funcs} ${_sapi_exported_funcs}
) )

View File

@ -39,7 +39,7 @@ endfunction()
# except that it strips import paths. This is necessary, because CMake's # except that it strips import paths. This is necessary, because CMake's
# protobuf rules don't work well with imports across different directories. # protobuf rules don't work well with imports across different directories.
function(sapi_protobuf_generate_cpp SRCS HDRS) function(sapi_protobuf_generate_cpp SRCS HDRS)
cmake_parse_arguments(_pb "" "EXPORT_MACRO" "" ${ARGN}) cmake_parse_arguments(PARSE_ARGV 2 _pb "" "EXPORT_MACRO" "")
if(NOT _pb_UNPARSED_ARGUMENTS) if(NOT _pb_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "sapi_protobuf_generate_cpp() missing proto files") message(FATAL_ERROR "sapi_protobuf_generate_cpp() missing proto files")
return() return()