|
| 1 | +# |
| 2 | +# Copyright 2017 The Abseil Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +include(CMakeParseArguments) |
| 18 | + |
| 19 | +set(_ABSL_HELPERS_PATH "${CMAKE_CURRENT_LIST_DIR}") |
| 20 | + |
| 21 | +# |
| 22 | +# create a static library absl_based on the following variable |
| 23 | +# |
| 24 | +# parameters |
| 25 | +# SOURCES : sources files for the library |
| 26 | +# PUBLIC_LIBRARIES: targets and flags for linking phase |
| 27 | +# PRIVATE_COMPILE_FLAGS: compile flags for the library. Will not be exported. |
| 28 | +# EXPORT_NAME: export name for the absl:: target export |
| 29 | +# TARGET: target name |
| 30 | +# |
| 31 | +# create a target associated to <NAME> |
| 32 | +# libraries are installed under CMAKE_INSTALL_FULL_LIBDIR by default |
| 33 | +# |
| 34 | +function(absl_library) |
| 35 | + cmake_parse_arguments(ABSL_LIB |
| 36 | + "DISABLE_INSTALL" # keep that in case we want to support installation one day |
| 37 | + "TARGET;EXPORT_NAME" |
| 38 | + "SOURCES;PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS;PRIVATE_INCLUDE_DIRS" |
| 39 | + ${ARGN} |
| 40 | + ) |
| 41 | + |
| 42 | + set(_NAME ${ABSL_LIB_TARGET}) |
| 43 | + string(TOUPPER ${_NAME} _UPPER_NAME) |
| 44 | + |
| 45 | + add_library(${_NAME} STATIC ${ABSL_LIB_SOURCES}) |
| 46 | + |
| 47 | + target_compile_options(${_NAME} PRIVATE ${ABSL_COMPILE_CXXFLAGS} ${ABSL_LIB_PRIVATE_COMPILE_FLAGS}) |
| 48 | + target_link_libraries(${_NAME} PUBLIC ${ABSL_LIB_PUBLIC_LIBRARIES}) |
| 49 | + target_include_directories(${_NAME} |
| 50 | + PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_LIB_PUBLIC_INCLUDE_DIRS} |
| 51 | + PRIVATE ${ABSL_LIB_PRIVATE_INCLUDE_DIRS} |
| 52 | + ) |
| 53 | + |
| 54 | + if(ABSL_LIB_EXPORT_NAME) |
| 55 | + add_library(absl::${ABSL_LIB_EXPORT_NAME} ALIAS ${_NAME}) |
| 56 | + endif() |
| 57 | +endfunction() |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +# |
| 62 | +# header only virtual target creation |
| 63 | +# |
| 64 | +function(absl_header_library) |
| 65 | + cmake_parse_arguments(ABSL_HO_LIB |
| 66 | + "DISABLE_INSTALL" |
| 67 | + "EXPORT_NAME;TARGET" |
| 68 | + "PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS;PRIVATE_INCLUDE_DIRS" |
| 69 | + ${ARGN} |
| 70 | + ) |
| 71 | + |
| 72 | + set(_NAME ${ABSL_HO_LIB_TARGET}) |
| 73 | + |
| 74 | + set(__dummy_header_only_lib_file "${CMAKE_CURRENT_BINARY_DIR}/${_NAME}_header_only_dummy.cc") |
| 75 | + |
| 76 | + if(NOT EXISTS ${__dummy_header_only_lib_file}) |
| 77 | + file(WRITE ${__dummy_header_only_lib_file} |
| 78 | + "\ |
| 79 | + /* generated file for header-only cmake target */ \ |
| 80 | + \ |
| 81 | + // single meaningless symbol \ |
| 82 | + void __${_NAME}__header_fakesym() {} \ |
| 83 | + \ |
| 84 | + " |
| 85 | + ) |
| 86 | + endif() |
| 87 | + |
| 88 | + |
| 89 | + add_library(${_NAME} ${__dummy_header_only_lib_file}) |
| 90 | + target_link_libraries(${_NAME} PUBLIC ${ABSL_HO_LIB_PUBLIC_LIBRARIES}) |
| 91 | + target_include_directories(${_NAME} |
| 92 | + PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_HO_LIB_PUBLIC_INCLUDE_DIRS} |
| 93 | + PRIVATE ${ABSL_HO_LIB_PRIVATE_INCLUDE_DIRS} |
| 94 | + ) |
| 95 | + |
| 96 | + if(ABSL_HO_LIB_EXPORT_NAME) |
| 97 | + add_library(absl::${ABSL_HO_LIB_EXPORT_NAME} ALIAS ${_NAME}) |
| 98 | + endif() |
| 99 | + |
| 100 | +endfunction() |
| 101 | + |
| 102 | + |
| 103 | +# |
| 104 | +# create an abseil unit_test and add it to the executed test list |
| 105 | +# |
| 106 | +# parameters |
| 107 | +# TARGET: target name prefix |
| 108 | +# SOURCES: sources files for the tests |
| 109 | +# PUBLIC_LIBRARIES: targets and flags for linking phase. |
| 110 | +# PRIVATE_COMPILE_FLAGS: compile flags for the test. Will not be exported. |
| 111 | +# |
| 112 | +# create a target associated to <NAME>_bin |
| 113 | +# |
| 114 | +# all tests will be register for execution with add_test() |
| 115 | +# |
| 116 | +# test compilation and execution is disable when BUILD_TESTING=OFF |
| 117 | +# |
| 118 | +function(absl_test) |
| 119 | + |
| 120 | + cmake_parse_arguments(ABSL_TEST |
| 121 | + "" |
| 122 | + "TARGET" |
| 123 | + "SOURCES;PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS" |
| 124 | + ${ARGN} |
| 125 | + ) |
| 126 | + |
| 127 | + |
| 128 | + if(BUILD_TESTING) |
| 129 | + |
| 130 | + set(_NAME ${ABSL_TEST_TARGET}) |
| 131 | + string(TOUPPER ${_NAME} _UPPER_NAME) |
| 132 | + |
| 133 | + add_executable(${_NAME}_bin ${ABSL_TEST_SOURCES}) |
| 134 | + |
| 135 | + target_compile_options(${_NAME}_bin PRIVATE ${ABSL_COMPILE_CXXFLAGS} ${ABSL_TEST_PRIVATE_COMPILE_FLAGS}) |
| 136 | + target_link_libraries(${_NAME}_bin PUBLIC ${ABSL_TEST_PUBLIC_LIBRARIES} ${ABSL_TEST_COMMON_LIBRARIES}) |
| 137 | + target_include_directories(${_NAME}_bin |
| 138 | + PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_TEST_PUBLIC_INCLUDE_DIRS} |
| 139 | + PRIVATE ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS} |
| 140 | + ) |
| 141 | + |
| 142 | + add_test(${_NAME}_test ${_NAME}_bin) |
| 143 | + endif(BUILD_TESTING) |
| 144 | + |
| 145 | +endfunction() |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +function(check_target my_target) |
| 151 | + |
| 152 | + if(NOT TARGET ${my_target}) |
| 153 | + message(FATAL_ERROR " ABSL: compiling absl requires a ${my_target} CMake target in your project, |
| 154 | + see CMake/README.md for more details") |
| 155 | + endif(NOT TARGET ${my_target}) |
| 156 | + |
| 157 | +endfunction() |
0 commit comments