Skip to content

Commit

Permalink
Ensure compatibility with CMake 3.10
Browse files Browse the repository at this point in the history
This fixes some CMake-3.12+-isms, like `list(JOIN ...)` and setting link
properties on OBJECT libraries.

PiperOrigin-RevId: 265033725
Change-Id: I0ee1ec0e1b1097ea8226ad6fdeff794a97c2881b
  • Loading branch information
cblichmann authored and copybara-github committed Aug 23, 2019
1 parent c966f21 commit d91879f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
tasks:
ubuntu1804:
shell_commands:
- apt-get update && apt-get install libcap-dev
- pip3 install absl-py clang
build_targets:
- "..."
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
8 changes: 4 additions & 4 deletions cmake/SapiBuildDefs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}")
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions cmake/SapiUtil.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions cmake/protobuf/Download.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 2 additions & 3 deletions sandboxed_api/examples/stringop/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions sandboxed_api/examples/sum/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d91879f

Please sign in to comment.