|
| 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 | +# https://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 | +include(AbseilConfigureCopts) |
| 19 | + |
| 20 | +# The IDE folder for Abseil that will be used if Abseil is included in a CMake |
| 21 | +# project that sets |
| 22 | +# set_property(GLOBAL PROPERTY USE_FOLDERS ON) |
| 23 | +# For example, Visual Studio supports folders. |
| 24 | +set(ABSL_IDE_FOLDER Abseil) |
| 25 | + |
| 26 | +# absl_cc_library() |
| 27 | +# |
| 28 | +# CMake function to imitate Bazel's cc_library rule. |
| 29 | +# |
| 30 | +# Parameters: |
| 31 | +# NAME: name of target (see Note) |
| 32 | +# HDRS: List of public header files for the library |
| 33 | +# SRCS: List of source files for the library |
| 34 | +# DEPS: List of other libraries to be linked in to the binary targets |
| 35 | +# COPTS: List of private compile options |
| 36 | +# DEFINES: List of public defines |
| 37 | +# LINKOPTS: List of link options |
| 38 | +# PUBLIC: Add this so that this library will be exported under absl:: (see Note). |
| 39 | +# Also in IDE, target will appear in Abseil folder while non PUBLIC will be in Abseil/internal. |
| 40 | +# TESTONLY: When added, this target will only be built if user passes -DABSL_RUN_TESTS=ON to CMake. |
| 41 | +# |
| 42 | +# Note: |
| 43 | +# By default, absl_cc_library will always create a library named absl_internal_${NAME}, |
| 44 | +# and alias target absl::${NAME}. |
| 45 | +# This is to reduce namespace pollution. |
| 46 | +# |
| 47 | +# absl_cc_library( |
| 48 | +# NAME |
| 49 | +# awesome |
| 50 | +# HDRS |
| 51 | +# "a.h" |
| 52 | +# SRCS |
| 53 | +# "a.cc" |
| 54 | +# ) |
| 55 | +# absl_cc_library( |
| 56 | +# NAME |
| 57 | +# fantastic_lib |
| 58 | +# SRCS |
| 59 | +# "b.cc" |
| 60 | +# DEPS |
| 61 | +# absl_internal_awesome # not "awesome"! |
| 62 | +# ) |
| 63 | +# |
| 64 | +# If PUBLIC is set, absl_cc_library will instead create a target named |
| 65 | +# absl_${NAME} and still an alias absl::${NAME}. |
| 66 | +# |
| 67 | +# absl_cc_library( |
| 68 | +# NAME |
| 69 | +# main_lib |
| 70 | +# ... |
| 71 | +# PUBLIC |
| 72 | +# ) |
| 73 | +# |
| 74 | +# User can then use the library as absl::main_lib (although absl_main_lib is defined too). |
| 75 | +# |
| 76 | +# TODO: Implement "ALWAYSLINK" |
| 77 | +function(absl_cc_library) |
| 78 | + cmake_parse_arguments(ABSL_CC_LIB |
| 79 | + "DISABLE_INSTALL;PUBLIC;TESTONLY" |
| 80 | + "NAME" |
| 81 | + "HDRS;SRCS;COPTS;DEFINES;LINKOPTS;DEPS" |
| 82 | + ${ARGN} |
| 83 | + ) |
| 84 | + |
| 85 | + if (NOT ABSL_CC_LIB_TESTONLY OR ABSL_RUN_TESTS) |
| 86 | + if (ABSL_CC_LIB_PUBLIC) |
| 87 | + set(_NAME "absl_${ABSL_CC_LIB_NAME}") |
| 88 | + else() |
| 89 | + set(_NAME "absl_internal_${ABSL_CC_LIB_NAME}") |
| 90 | + endif() |
| 91 | + |
| 92 | + # Check if this is a header-only library |
| 93 | + # Note that as of February 2019, many popular OS's (for example, Ubuntu |
| 94 | + # 16.04 LTS) only come with cmake 3.5 by default. For this reason, we can't |
| 95 | + # use list(FILTER...) |
| 96 | + set(ABSL_CC_SRCS "${ABSL_CC_LIB_SRCS}") |
| 97 | + foreach(src_file IN LISTS ABSL_CC_SRCS) |
| 98 | + if(${src_file} MATCHES ".*\\.(h|inc)") |
| 99 | + list(REMOVE_ITEM ABSL_CC_SRCS "${src_file}") |
| 100 | + endif() |
| 101 | + endforeach() |
| 102 | + if ("${ABSL_CC_SRCS}" STREQUAL "") |
| 103 | + set(ABSL_CC_LIB_IS_INTERFACE 1) |
| 104 | + else() |
| 105 | + set(ABSL_CC_LIB_IS_INTERFACE 0) |
| 106 | + endif() |
| 107 | + |
| 108 | + if(NOT ABSL_CC_LIB_IS_INTERFACE) |
| 109 | + add_library(${_NAME} STATIC "") |
| 110 | + target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS}) |
| 111 | + target_include_directories(${_NAME} |
| 112 | + PUBLIC ${ABSL_COMMON_INCLUDE_DIRS}) |
| 113 | + target_compile_options(${_NAME} |
| 114 | + PRIVATE ${ABSL_CC_LIB_COPTS}) |
| 115 | + target_link_libraries(${_NAME} |
| 116 | + PUBLIC ${ABSL_CC_LIB_DEPS} |
| 117 | + PRIVATE ${ABSL_CC_LIB_LINKOPTS} |
| 118 | + ) |
| 119 | + target_compile_definitions(${_NAME} PUBLIC ${ABSL_CC_LIB_DEFINES}) |
| 120 | + |
| 121 | + # Add all Abseil targets to a a folder in the IDE for organization. |
| 122 | + if(ABSL_CC_LIB_PUBLIC) |
| 123 | + set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}) |
| 124 | + elseif(ABSL_CC_LIB_TESTONLY) |
| 125 | + set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test) |
| 126 | + else() |
| 127 | + set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/internal) |
| 128 | + endif() |
| 129 | + |
| 130 | + # INTERFACE libraries can't have the CXX_STANDARD property set |
| 131 | + set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD}) |
| 132 | + set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) |
| 133 | + else() |
| 134 | + # Generating header-only library |
| 135 | + add_library(${_NAME} INTERFACE) |
| 136 | + target_include_directories(${_NAME} |
| 137 | + INTERFACE ${ABSL_COMMON_INCLUDE_DIRS}) |
| 138 | + target_link_libraries(${_NAME} |
| 139 | + INTERFACE ${ABSL_CC_LIB_DEPS} ${ABSL_CC_LIB_LINKOPTS} |
| 140 | + ) |
| 141 | + target_compile_definitions(${_NAME} INTERFACE ${ABSL_CC_LIB_DEFINES}) |
| 142 | + endif() |
| 143 | + |
| 144 | + add_library(absl::${ABSL_CC_LIB_NAME} ALIAS ${_NAME}) |
| 145 | + endif() |
| 146 | +endfunction() |
| 147 | + |
| 148 | +# absl_cc_test() |
| 149 | +# |
| 150 | +# CMake function to imitate Bazel's cc_test rule. |
| 151 | +# |
| 152 | +# Parameters: |
| 153 | +# NAME: name of target (see Usage below) |
| 154 | +# SRCS: List of source files for the binary |
| 155 | +# DEPS: List of other libraries to be linked in to the binary targets |
| 156 | +# COPTS: List of private compile options |
| 157 | +# DEFINES: List of public defines |
| 158 | +# LINKOPTS: List of link options |
| 159 | +# |
| 160 | +# Note: |
| 161 | +# By default, absl_cc_test will always create a binary named absl_${NAME}. |
| 162 | +# This will also add it to ctest list as absl_${NAME}. |
| 163 | +# |
| 164 | +# Usage: |
| 165 | +# absl_cc_library( |
| 166 | +# NAME |
| 167 | +# awesome |
| 168 | +# HDRS |
| 169 | +# "a.h" |
| 170 | +# SRCS |
| 171 | +# "a.cc" |
| 172 | +# PUBLIC |
| 173 | +# ) |
| 174 | +# |
| 175 | +# absl_cc_test( |
| 176 | +# NAME |
| 177 | +# awesome_test |
| 178 | +# SRCS |
| 179 | +# "awesome_test.cc" |
| 180 | +# DEPS |
| 181 | +# absl::awesome |
| 182 | +# gmock |
| 183 | +# gtest_main |
| 184 | +# ) |
| 185 | +function(absl_cc_test) |
| 186 | + if(NOT ABSL_RUN_TESTS) |
| 187 | + return() |
| 188 | + endif() |
| 189 | + |
| 190 | + cmake_parse_arguments(ABSL_CC_TEST |
| 191 | + "" |
| 192 | + "NAME" |
| 193 | + "SRCS;COPTS;DEFINES;LINKOPTS;DEPS" |
| 194 | + ${ARGN} |
| 195 | + ) |
| 196 | + |
| 197 | + set(_NAME "absl_${ABSL_CC_TEST_NAME}") |
| 198 | + add_executable(${_NAME} "") |
| 199 | + target_sources(${_NAME} PRIVATE ${ABSL_CC_TEST_SRCS}) |
| 200 | + target_include_directories(${_NAME} |
| 201 | + PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} |
| 202 | + PRIVATE ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS} |
| 203 | + ) |
| 204 | + target_compile_definitions(${_NAME} |
| 205 | + PUBLIC ${ABSL_CC_TEST_DEFINES} |
| 206 | + ) |
| 207 | + target_compile_options(${_NAME} |
| 208 | + PRIVATE ${ABSL_CC_TEST_COPTS} |
| 209 | + ) |
| 210 | + target_link_libraries(${_NAME} |
| 211 | + PUBLIC ${ABSL_CC_TEST_DEPS} |
| 212 | + PRIVATE ${ABSL_CC_TEST_LINKOPTS} |
| 213 | + ) |
| 214 | + # Add all Abseil targets to a a folder in the IDE for organization. |
| 215 | + set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test) |
| 216 | + |
| 217 | + set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD}) |
| 218 | + set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) |
| 219 | + |
| 220 | + add_test(NAME ${_NAME} COMMAND ${_NAME}) |
| 221 | +endfunction() |
| 222 | + |
| 223 | + |
| 224 | +function(check_target my_target) |
| 225 | + if(NOT TARGET ${my_target}) |
| 226 | + message(FATAL_ERROR " ABSL: compiling absl requires a ${my_target} CMake target in your project, |
| 227 | + see CMake/README.md for more details") |
| 228 | + endif(NOT TARGET ${my_target}) |
| 229 | +endfunction() |
0 commit comments