From d91879f752a11d8f1c66ef526eb48dcc28aef5cf Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Fri, 23 Aug 2019 05:08:29 -0700 Subject: [PATCH] Ensure compatibility with CMake 3.10 This fixes some CMake-3.12+-isms, like `list(JOIN ...)` and setting link properties on OBJECT libraries. PiperOrigin-RevId: 265033725 Change-Id: I0ee1ec0e1b1097ea8226ad6fdeff794a97c2881b --- .bazelci/presubmit.yml | 3 +++ CMakeLists.txt | 2 +- cmake/SapiBuildDefs.cmake | 8 ++++---- cmake/SapiUtil.cmake | 10 ++++++++++ cmake/protobuf/Download.cmake | 2 ++ sandboxed_api/examples/stringop/lib/CMakeLists.txt | 5 ++--- sandboxed_api/examples/sum/lib/CMakeLists.txt | 5 ++--- 7 files changed, 24 insertions(+), 11 deletions(-) diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 7bb67a10f..d537aff95 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -1,5 +1,8 @@ --- tasks: ubuntu1804: + shell_commands: + - apt-get update && apt-get install libcap-dev + - pip3 install absl-py clang build_targets: - "..." diff --git a/CMakeLists.txt b/CMakeLists.txt index c00040984..cdb480c93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,7 @@ 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(TOUCH ${SAPI_BINARY_DIR}/sapi_base_force_cxx_linkage.cc) +file(WRITE ${SAPI_BINARY_DIR}/sapi_base_force_cxx_linkage.cc "") add_library(sapi_base STATIC "${SAPI_BINARY_DIR}/sapi_base_force_cxx_linkage.cc" ) diff --git a/cmake/SapiBuildDefs.cmake b/cmake/SapiBuildDefs.cmake index f2ce1b8d3..af70f8b77 100644 --- a/cmake/SapiBuildDefs.cmake +++ b/cmake/SapiBuildDefs.cmake @@ -102,7 +102,7 @@ function(add_sapi_library) set(_sapi_bin "${_sapi_NAME}.bin") set(_sapi_force_cxx_linkage "${CMAKE_CURRENT_BINARY_DIR}/${_sapi_bin}_force_cxx_linkage.cc") - file(TOUCH "${_sapi_force_cxx_linkage}") + 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 @@ -123,12 +123,12 @@ function(add_sapi_library) endif() # Interface - list(JOIN _sapi_FUNCTIONS "," _sapi_funcs) + list_join(_sapi_FUNCTIONS "," _sapi_funcs) foreach(src IN LISTS _sapi_INPUTS) get_filename_component(src "${src}" ABSOLUTE) list(APPEND _sapi_full_inputs "${src}") endforeach() - list(JOIN _sapi_full_inputs "," _sapi_full_inputs) + list_join(_sapi_full_inputs "," _sapi_full_inputs) if(NOT _sapi_NOEMBED) set(_sapi_embed_dir "${CMAKE_CURRENT_BINARY_DIR}") set(_sapi_embed_name "${_sapi_NAME}") @@ -152,7 +152,7 @@ function(add_sapi_library) if(NOT _sapi_SOURCES) set(_sapi_force_cxx_linkage "${CMAKE_CURRENT_BINARY_DIR}/${_sapi_NAME}_force_cxx_linkage.cc") - file(TOUCH "${_sapi_force_cxx_linkage}") + file(WRITE "${_sapi_force_cxx_linkage}" "") list(APPEND _sapi_SOURCES "${_sapi_force_cxx_linkage}") endif() add_library("${_sapi_NAME}" STATIC diff --git a/cmake/SapiUtil.cmake b/cmake/SapiUtil.cmake index c4645357d..6e7e7d271 100644 --- a/cmake/SapiUtil.cmake +++ b/cmake/SapiUtil.cmake @@ -35,6 +35,16 @@ function(create_directory_symlink SOURCE DESTINATION) endif() endfunction() +# Implements list(JOIN ...) for CMake < 3.12. +function(list_join LIST SEP OUTPUT) + foreach(item IN LISTS ${LIST}) + set(_concat "${_concat}${SEP}${item}") + endforeach() + string(LENGTH "${SEP}" _len) + string(SUBSTRING "${_concat}" ${_len} -1 _concat) + set(${OUTPUT} "${_concat}" PARENT_SCOPE) +endfunction() + # Helper function that behaves just like Protobuf's protobuf_generate_cpp(), # except that it strips import paths. This is necessary, because CMake's # protobuf rules don't work well with imports across different directories. diff --git a/cmake/protobuf/Download.cmake b/cmake/protobuf/Download.cmake index bd435838d..4d67f4009 100644 --- a/cmake/protobuf/Download.cmake +++ b/cmake/protobuf/Download.cmake @@ -38,3 +38,5 @@ set(protobuf_WITH_ZLIB OFF CACHE BOOL "") add_subdirectory("${CMAKE_BINARY_DIR}/protobuf-src/cmake" "${CMAKE_BINARY_DIR}/protobuf-build" EXCLUDE_FROM_ALL) +get_property(Protobuf_INCLUDE_DIRS TARGET protobuf::libprotobuf + PROPERTY INCLUDE_DIRECTORIES) diff --git a/sandboxed_api/examples/stringop/lib/CMakeLists.txt b/sandboxed_api/examples/stringop/lib/CMakeLists.txt index df75374da..3df67effd 100644 --- a/sandboxed_api/examples/stringop/lib/CMakeLists.txt +++ b/sandboxed_api/examples/stringop/lib/CMakeLists.txt @@ -23,9 +23,8 @@ add_library(sapi_stringop_params_proto OBJECT ${_sapi_stringop_params_pb_h} ) add_library(sapi::stringop_params_proto ALIAS sapi_stringop_params_proto) -target_link_libraries(sapi_stringop_params_proto - PRIVATE sapi::base - PUBLIC protobuf::libprotobuf +target_include_directories(sapi_stringop_params_proto PUBLIC + ${Protobuf_INCLUDE_DIRS} ) # sandboxed_api/examples/stringop/lib:stringop diff --git a/sandboxed_api/examples/sum/lib/CMakeLists.txt b/sandboxed_api/examples/sum/lib/CMakeLists.txt index b632117ae..976c6fb59 100644 --- a/sandboxed_api/examples/sum/lib/CMakeLists.txt +++ b/sandboxed_api/examples/sum/lib/CMakeLists.txt @@ -23,9 +23,8 @@ add_library(sapi_sum_params_proto OBJECT ${_sapi_sum_params_pb_h} ) add_library(sapi::sum_params_proto ALIAS sapi_sum_params_proto) -target_link_libraries(sapi_sum_params_proto - PRIVATE sapi::base - PUBLIC protobuf::libprotobuf +target_include_directories(sapi_sum_params_proto PUBLIC + ${Protobuf_INCLUDE_DIRS} ) # sandboxed_api/examples/sum/lib:sum