diff options
author | Gennadiy Civil <gennadiycivil@users.noreply.github.com> | 2017-11-06T20·56-0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-06T20·56-0800 |
commit | 78e1abca86f4294ab26ef36e534798b893c828bd (patch) | |
tree | b4cd4da6696aed4d103a087c56bf363c4abe707c /CMake | |
parent | da336a84e9c1f86409b21996164ae9602b37f9ca (diff) | |
parent | 2eedcf020483cdbfd49a796fb78a3edd0134b8e6 (diff) |
Merge pull request #8 from adevress/master
Add CMake support
Diffstat (limited to 'CMake')
-rw-r--r-- | CMake/AbseilHelpers.cmake | 156 | ||||
-rw-r--r-- | CMake/README.md | 73 |
2 files changed, 229 insertions, 0 deletions
diff --git a/CMake/AbseilHelpers.cmake b/CMake/AbseilHelpers.cmake new file mode 100644 index 000000000000..9e1772bb5f0e --- /dev/null +++ b/CMake/AbseilHelpers.cmake @@ -0,0 +1,156 @@ +# +# Copyright 2017 The Abseil Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +include(CMakeParseArguments) + + +# +# create a library in the absl namespace +# +# parameters +# SOURCES : sources files for the library +# PUBLIC_LIBRARIES: targets and flags for linking phase +# PRIVATE_COMPILE_FLAGS: compile flags for the library. Will not be exported. +# EXPORT_NAME: export name for the absl:: target export +# TARGET: target name +# +# create a target associated to <NAME> +# libraries are installed under CMAKE_INSTALL_FULL_LIBDIR by default +# +function(absl_library) + cmake_parse_arguments(ABSL_LIB + "DISABLE_INSTALL" # keep that in case we want to support installation one day + "TARGET;EXPORT_NAME" + "SOURCES;PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS;PRIVATE_INCLUDE_DIRS" + ${ARGN} + ) + + set(_NAME ${ABSL_LIB_TARGET}) + string(TOUPPER ${_NAME} _UPPER_NAME) + + add_library(${_NAME} STATIC ${ABSL_LIB_SOURCES}) + + target_compile_options(${_NAME} PRIVATE ${ABSL_COMPILE_CXXFLAGS} ${ABSL_LIB_PRIVATE_COMPILE_FLAGS}) + target_link_libraries(${_NAME} PUBLIC ${ABSL_LIB_PUBLIC_LIBRARIES}) + target_include_directories(${_NAME} + PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_LIB_PUBLIC_INCLUDE_DIRS} + PRIVATE ${ABSL_LIB_PRIVATE_INCLUDE_DIRS} + ) + + if(ABSL_LIB_EXPORT_NAME) + add_library(absl::${ABSL_LIB_EXPORT_NAME} ALIAS ${_NAME}) + endif() +endfunction() + + + +# +# header only virtual target creation +# +function(absl_header_library) + cmake_parse_arguments(ABSL_HO_LIB + "DISABLE_INSTALL" + "EXPORT_NAME;TARGET" + "PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS;PRIVATE_INCLUDE_DIRS" + ${ARGN} + ) + + set(_NAME ${ABSL_HO_LIB_TARGET}) + + set(__dummy_header_only_lib_file "${CMAKE_CURRENT_BINARY_DIR}/${_NAME}_header_only_dummy.cc") + + if(NOT EXISTS ${__dummy_header_only_lib_file}) + file(WRITE ${__dummy_header_only_lib_file} + "\ + /* generated file for header-only cmake target */ \ + \ + // single meaningless symbol \ + void __${_NAME}__header_fakesym() {} \ + \ + " + ) + endif() + + + add_library(${_NAME} ${__dummy_header_only_lib_file}) + target_link_libraries(${_NAME} PUBLIC ${ABSL_HO_LIB_PUBLIC_LIBRARIES}) + target_include_directories(${_NAME} + PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_HO_LIB_PUBLIC_INCLUDE_DIRS} + PRIVATE ${ABSL_HO_LIB_PRIVATE_INCLUDE_DIRS} + ) + + if(ABSL_HO_LIB_EXPORT_NAME) + add_library(absl::${ABSL_HO_LIB_EXPORT_NAME} ALIAS ${_NAME}) + endif() + +endfunction() + + +# +# create an abseil unit_test and add it to the executed test list +# +# parameters +# TARGET: target name prefix +# SOURCES: sources files for the tests +# PUBLIC_LIBRARIES: targets and flags for linking phase. +# PRIVATE_COMPILE_FLAGS: compile flags for the test. Will not be exported. +# +# create a target associated to <NAME>_bin +# +# all tests will be register for execution with add_test() +# +# test compilation and execution is disable when BUILD_TESTING=OFF +# +function(absl_test) + + cmake_parse_arguments(ABSL_TEST + "" + "TARGET" + "SOURCES;PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS" + ${ARGN} + ) + + + if(BUILD_TESTING) + + set(_NAME ${ABSL_TEST_TARGET}) + string(TOUPPER ${_NAME} _UPPER_NAME) + + add_executable(${_NAME}_bin ${ABSL_TEST_SOURCES}) + + target_compile_options(${_NAME}_bin PRIVATE ${ABSL_COMPILE_CXXFLAGS} ${ABSL_TEST_PRIVATE_COMPILE_FLAGS}) + target_link_libraries(${_NAME}_bin PUBLIC ${ABSL_TEST_PUBLIC_LIBRARIES} ${ABSL_TEST_COMMON_LIBRARIES}) + target_include_directories(${_NAME}_bin + PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_TEST_PUBLIC_INCLUDE_DIRS} + PRIVATE ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS} + ) + + add_test(${_NAME}_test ${_NAME}_bin) + endif(BUILD_TESTING) + +endfunction() + + + + +function(check_target my_target) + + if(NOT TARGET ${my_target}) + message(FATAL_ERROR " ABSL: compiling absl requires a ${my_target} CMake target in your project, + see CMake/README.md for more details") + endif(NOT TARGET ${my_target}) + +endfunction() diff --git a/CMake/README.md b/CMake/README.md new file mode 100644 index 000000000000..21633f14d517 --- /dev/null +++ b/CMake/README.md @@ -0,0 +1,73 @@ + +## Abseil CMake build instructions + + +### Recommended usage : incorporate Abseil into an CMake project + + For API / ABI compatibility reasons, it is recommended to build + and use abseil in a subdirectory of your project or as an embedded + dependency + + This is similar to the recommended usage of the googletest framework + ( https://github.com/google/googletest/blob/master/googletest/README.md ) + + Build and use step-by-step + + + 1- Download abseil and copy it in a sub-directory in your project. + or add abseil as a git-submodule in your project + + 2- If not done yet, download and copy in your project the two dependencies of + abseil `cctz` and `googletest` + + * cctz https://github.com/google/cctz + * googletest https://github.com/google/googletest + + 3- You can then use the cmake command `add_subdirectory()` to include + abseil directly and use the abseil targets in your project. + + Note: Abseil requires CCTZ and the googletest framework. Consequently, + the targets `gtest`, `gtest_main`, `gmock` and `cctz` need + to be declared in your project before including abseil with `add_subdirectory`. + + + 4- Add the absl:: target you wish to use to the `target_link_libraries()` + section of your executable or of your library + + + Here is a short CMakeLists.txt example of a possible project file + using abseil + + cmake_minimum_required(VERSION 2.8.12) + project(my_project) + + set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ ${CMAKE_CXX_FLAGS}") + + add_subdirectory(googletest) + add_subdirectory(cctz) + add_subdirectory(abseil-cpp) + + add_executable(my_exe source.cpp) + target_link_libraries(my_exe absl::base absl::synchronization absl::strings) + + +You will need to create your own CMake files for cctz until https://github.com/google/cctz/pull/54 lands. As of this writing, that pull request requires -DBUILD_TESTING=OFF as it doesn't correctly export cctz's dependency on Google Benchmark. + + You will find here a non exhaustive list of absl public targets + + absl::base + absl::algorithm + absl::container + absl::debugging + absl::memory + absl::meta + absl::numeric + absl::strings + absl::synchronization + absl::time + absl::utility + + + + + |