From b6d65ef244b949daca4ca88d998d713d3aca92e0 Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Thu, 3 Feb 2022 04:42:08 -0800 Subject: [PATCH] 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 --- cmake/SapiBuildDefs.cmake | 21 ++++++++++----------- cmake/SapiUtil.cmake | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/cmake/SapiBuildDefs.cmake b/cmake/SapiBuildDefs.cmake index e283fc8..c7b25b2 100644 --- a/cmake/SapiBuildDefs.cmake +++ b/cmake/SapiBuildDefs.cmake @@ -19,7 +19,8 @@ # SOURCES is a list of files that should be embedded. If a source names a # target the target binary is embedded instead. 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) if(TARGET "${src}") 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_one_value HEADER LIBRARY LIBRARY_NAME NAMESPACE) set(_sapi_multi_value SOURCES FUNCTIONS INPUTS) - cmake_parse_arguments(_sapi - "${_sapi_opts}" - "${_sapi_one_value}" - "${_sapi_multi_value}" - ${ARGN}) + cmake_parse_arguments(PARSE_ARGV 0 _sapi "${_sapi_opts}" + "${_sapi_one_value}" "${_sapi_multi_value}") set(_sapi_NAME "${ARGV0}") set(_sapi_gen_header "${_sapi_NAME}.sapi.h") 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() if(NOT _sapi_exported_funcs) - set(_sapi_exported_funcs -Wl,--whole-archive - -Wl,--allow-multiple-definition) + set(_sapi_exported_funcs LINKER:--whole-archive + LINKER:--allow-multiple-definition) endif() # The sandboxed binary @@ -112,13 +110,14 @@ function(add_sapi_library) "${CMAKE_CURRENT_BINARY_DIR}/${_sapi_bin}_force_cxx_linkage.cc") file(WRITE "${_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 -fuse-ld=gold "${_sapi_LIBRARY}" sapi::client ${CMAKE_DL_LIBS} - -Wl,-E + ) + target_link_options("${_sapi_bin}" PRIVATE + LINKER:-E ${_sapi_exported_funcs} ) diff --git a/cmake/SapiUtil.cmake b/cmake/SapiUtil.cmake index e7849b5..2e6b11e 100644 --- a/cmake/SapiUtil.cmake +++ b/cmake/SapiUtil.cmake @@ -39,7 +39,7 @@ endfunction() # except that it strips import paths. This is necessary, because CMake's # protobuf rules don't work well with imports across different directories. 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) message(FATAL_ERROR "sapi_protobuf_generate_cpp() missing proto files") return()