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 17include(CMakeParseArguments) 18include(AbseilConfigureCopts) 19include(AbseilDll) 20 21# The IDE folder for Abseil that will be used if Abseil is included in a CMake 22# project that sets 23# set_property(GLOBAL PROPERTY USE_FOLDERS ON) 24# For example, Visual Studio supports folders. 25if(NOT DEFINED ABSL_IDE_FOLDER) 26 set(ABSL_IDE_FOLDER Abseil) 27endif() 28 29if(ABSL_USE_SYSTEM_INCLUDES) 30 set(ABSL_INTERNAL_INCLUDE_WARNING_GUARD SYSTEM) 31else() 32 set(ABSL_INTERNAL_INCLUDE_WARNING_GUARD "") 33endif() 34 35# absl_cc_library() 36# 37# CMake function to imitate Bazel's cc_library rule. 38# 39# Parameters: 40# NAME: name of target (see Note) 41# HDRS: List of public header files for the library 42# SRCS: List of source files for the library 43# DEPS: List of other libraries to be linked in to the binary targets 44# COPTS: List of private compile options 45# DEFINES: List of public defines 46# LINKOPTS: List of link options 47# PUBLIC: Add this so that this library will be exported under absl:: 48# Also in IDE, target will appear in Abseil folder while non PUBLIC will be in Abseil/internal. 49# TESTONLY: When added, this target will only be built if both 50# BUILD_TESTING=ON and ABSL_BUILD_TESTING=ON. 51# 52# Note: 53# By default, absl_cc_library will always create a library named absl_${NAME}, 54# and alias target absl::${NAME}. The absl:: form should always be used. 55# This is to reduce namespace pollution. 56# 57# absl_cc_library( 58# NAME 59# awesome 60# HDRS 61# "a.h" 62# SRCS 63# "a.cc" 64# ) 65# absl_cc_library( 66# NAME 67# fantastic_lib 68# SRCS 69# "b.cc" 70# DEPS 71# absl::awesome # not "awesome" ! 72# PUBLIC 73# ) 74# 75# absl_cc_library( 76# NAME 77# main_lib 78# ... 79# DEPS 80# absl::fantastic_lib 81# ) 82# 83# TODO: Implement "ALWAYSLINK" 84function(absl_cc_library) 85 cmake_parse_arguments(ABSL_CC_LIB 86 "DISABLE_INSTALL;PUBLIC;TESTONLY" 87 "NAME" 88 "HDRS;SRCS;COPTS;DEFINES;LINKOPTS;DEPS" 89 ${ARGN} 90 ) 91 92 if(ABSL_CC_LIB_TESTONLY AND 93 NOT ((BUILD_TESTING AND ABSL_BUILD_TESTING) OR 94 (ABSL_BUILD_TEST_HELPERS AND ABSL_CC_LIB_PUBLIC))) 95 return() 96 endif() 97 98 if(ABSL_ENABLE_INSTALL) 99 set(_NAME "${ABSL_CC_LIB_NAME}") 100 else() 101 set(_NAME "absl_${ABSL_CC_LIB_NAME}") 102 endif() 103 104 # Check if this is a header-only library 105 # Note that as of February 2019, many popular OS's (for example, Ubuntu 106 # 16.04 LTS) only come with cmake 3.5 by default. For this reason, we can't 107 # use list(FILTER...) 108 set(ABSL_CC_SRCS "${ABSL_CC_LIB_SRCS}") 109 foreach(src_file IN LISTS ABSL_CC_SRCS) 110 if(${src_file} MATCHES ".*\\.(h|inc)") 111 list(REMOVE_ITEM ABSL_CC_SRCS "${src_file}") 112 endif() 113 endforeach() 114 115 if(ABSL_CC_SRCS STREQUAL "") 116 set(ABSL_CC_LIB_IS_INTERFACE 1) 117 else() 118 set(ABSL_CC_LIB_IS_INTERFACE 0) 119 endif() 120 121 # Determine this build target's relationship to the DLL. It's one of four things: 122 # 1. "dll" -- This target is part of the DLL 123 # 2. "dll_dep" -- This target is not part of the DLL, but depends on the DLL. 124 # Note that we assume any target not in the DLL depends on the 125 # DLL. This is not a technical necessity but a convenience 126 # which happens to be true, because nearly every target is 127 # part of the DLL. 128 # 3. "shared" -- This is a shared library, perhaps on a non-windows platform 129 # where DLL doesn't make sense. 130 # 4. "static" -- This target does not depend on the DLL and should be built 131 # statically. 132 if (${ABSL_BUILD_DLL}) 133 if(ABSL_ENABLE_INSTALL) 134 absl_internal_dll_contains(TARGET ${_NAME} OUTPUT _in_dll) 135 else() 136 absl_internal_dll_contains(TARGET ${ABSL_CC_LIB_NAME} OUTPUT _in_dll) 137 endif() 138 if (${_in_dll}) 139 # This target should be replaced by the DLL 140 set(_build_type "dll") 141 set(ABSL_CC_LIB_IS_INTERFACE 1) 142 else() 143 # Building a DLL, but this target is not part of the DLL 144 set(_build_type "dll_dep") 145 endif() 146 elseif(BUILD_SHARED_LIBS) 147 set(_build_type "shared") 148 else() 149 set(_build_type "static") 150 endif() 151 152 # Generate a pkg-config file for every library: 153 if((_build_type STREQUAL "static" OR _build_type STREQUAL "shared") 154 AND ABSL_ENABLE_INSTALL) 155 if(NOT ABSL_CC_LIB_TESTONLY) 156 if(absl_VERSION) 157 set(PC_VERSION "${absl_VERSION}") 158 else() 159 set(PC_VERSION "head") 160 endif() 161 foreach(dep ${ABSL_CC_LIB_DEPS}) 162 if(${dep} MATCHES "^absl::(.*)") 163 # Join deps with commas. 164 if(PC_DEPS) 165 set(PC_DEPS "${PC_DEPS},") 166 endif() 167 set(PC_DEPS "${PC_DEPS} absl_${CMAKE_MATCH_1} = ${PC_VERSION}") 168 endif() 169 endforeach() 170 foreach(cflag ${ABSL_CC_LIB_COPTS}) 171 if(${cflag} MATCHES "^(-Wno|/wd)") 172 # These flags are needed to suppress warnings that might fire in our headers. 173 set(PC_CFLAGS "${PC_CFLAGS} ${cflag}") 174 elseif(${cflag} MATCHES "^(-W|/w[1234eo])") 175 # Don't impose our warnings on others. 176 elseif(${cflag} MATCHES "^-m") 177 # Don't impose CPU instruction requirements on others, as 178 # the code performs feature detection on runtime. 179 else() 180 set(PC_CFLAGS "${PC_CFLAGS} ${cflag}") 181 endif() 182 endforeach() 183 string(REPLACE ";" " " PC_LINKOPTS "${ABSL_CC_LIB_LINKOPTS}") 184 FILE(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/lib/pkgconfig/absl_${_NAME}.pc" CONTENT "\ 185prefix=${CMAKE_INSTALL_PREFIX}\n\ 186exec_prefix=\${prefix}\n\ 187libdir=${CMAKE_INSTALL_FULL_LIBDIR}\n\ 188includedir=${CMAKE_INSTALL_FULL_INCLUDEDIR}\n\ 189\n\ 190Name: absl_${_NAME}\n\ 191Description: Abseil ${_NAME} library\n\ 192URL: https://abseil.io/\n\ 193Version: ${PC_VERSION}\n\ 194Requires:${PC_DEPS}\n\ 195Libs: -L\${libdir} ${PC_LINKOPTS} $<$<NOT:$<BOOL:${ABSL_CC_LIB_IS_INTERFACE}>>:-labsl_${_NAME}>\n\ 196Cflags: -I\${includedir}${PC_CFLAGS}\n") 197 INSTALL(FILES "${CMAKE_BINARY_DIR}/lib/pkgconfig/absl_${_NAME}.pc" 198 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 199 endif() 200 endif() 201 202 if(NOT ABSL_CC_LIB_IS_INTERFACE) 203 if(_build_type STREQUAL "dll_dep") 204 # This target depends on the DLL. When adding dependencies to this target, 205 # any depended-on-target which is contained inside the DLL is replaced 206 # with a dependency on the DLL. 207 add_library(${_NAME} STATIC "") 208 target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS}) 209 absl_internal_dll_targets( 210 DEPS ${ABSL_CC_LIB_DEPS} 211 OUTPUT _dll_deps 212 ) 213 target_link_libraries(${_NAME} 214 PUBLIC ${_dll_deps} 215 PRIVATE 216 ${ABSL_CC_LIB_LINKOPTS} 217 ${ABSL_DEFAULT_LINKOPTS} 218 ) 219 220 if (ABSL_CC_LIB_TESTONLY) 221 set(_gtest_link_define "GTEST_LINKED_AS_SHARED_LIBRARY=1") 222 else() 223 set(_gtest_link_define) 224 endif() 225 226 target_compile_definitions(${_NAME} 227 PUBLIC 228 ABSL_CONSUME_DLL 229 "${_gtest_link_define}" 230 ) 231 232 elseif(_build_type STREQUAL "static" OR _build_type STREQUAL "shared") 233 add_library(${_NAME} "") 234 target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS}) 235 target_link_libraries(${_NAME} 236 PUBLIC ${ABSL_CC_LIB_DEPS} 237 PRIVATE 238 ${ABSL_CC_LIB_LINKOPTS} 239 ${ABSL_DEFAULT_LINKOPTS} 240 ) 241 else() 242 message(FATAL_ERROR "Invalid build type: ${_build_type}") 243 endif() 244 245 # Linker language can be inferred from sources, but in the case of DLLs we 246 # don't have any .cc files so it would be ambiguous. We could set it 247 # explicitly only in the case of DLLs but, because "CXX" is always the 248 # correct linker language for static or for shared libraries, we set it 249 # unconditionally. 250 set_property(TARGET ${_NAME} PROPERTY LINKER_LANGUAGE "CXX") 251 252 target_include_directories(${_NAME} ${ABSL_INTERNAL_INCLUDE_WARNING_GUARD} 253 PUBLIC 254 "$<BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS}>" 255 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> 256 ) 257 target_compile_options(${_NAME} 258 PRIVATE ${ABSL_CC_LIB_COPTS}) 259 target_compile_definitions(${_NAME} PUBLIC ${ABSL_CC_LIB_DEFINES}) 260 261 # Add all Abseil targets to a a folder in the IDE for organization. 262 if(ABSL_CC_LIB_PUBLIC) 263 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}) 264 elseif(ABSL_CC_LIB_TESTONLY) 265 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test) 266 else() 267 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/internal) 268 endif() 269 270 if(ABSL_PROPAGATE_CXX_STD) 271 # Abseil libraries require C++14 as the current minimum standard. When 272 # compiled with C++17 (either because it is the compiler's default or 273 # explicitly requested), then Abseil requires C++17. 274 _absl_target_compile_features_if_available(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE}) 275 else() 276 # Note: This is legacy (before CMake 3.8) behavior. Setting the 277 # target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is 278 # initialized by CMAKE_CXX_STANDARD) should have no real effect, since 279 # that is the default value anyway. 280 # 281 # CXX_STANDARD_REQUIRED does guard against the top-level CMake project 282 # not having enabled CMAKE_CXX_STANDARD_REQUIRED (which prevents 283 # "decaying" to an older standard if the requested one isn't available). 284 set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD}) 285 set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) 286 endif() 287 288 # When being installed, we lose the absl_ prefix. We want to put it back 289 # to have properly named lib files. This is a no-op when we are not being 290 # installed. 291 if(ABSL_ENABLE_INSTALL) 292 set_target_properties(${_NAME} PROPERTIES 293 OUTPUT_NAME "absl_${_NAME}" 294 SOVERSION 0 295 ) 296 endif() 297 else() 298 # Generating header-only library 299 add_library(${_NAME} INTERFACE) 300 target_include_directories(${_NAME} ${ABSL_INTERNAL_INCLUDE_WARNING_GUARD} 301 INTERFACE 302 "$<BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS}>" 303 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> 304 ) 305 306 if (_build_type STREQUAL "dll") 307 set(ABSL_CC_LIB_DEPS abseil_dll) 308 endif() 309 310 target_link_libraries(${_NAME} 311 INTERFACE 312 ${ABSL_CC_LIB_DEPS} 313 ${ABSL_CC_LIB_LINKOPTS} 314 ${ABSL_DEFAULT_LINKOPTS} 315 ) 316 target_compile_definitions(${_NAME} INTERFACE ${ABSL_CC_LIB_DEFINES}) 317 318 if(ABSL_PROPAGATE_CXX_STD) 319 # Abseil libraries require C++14 as the current minimum standard. 320 # Top-level application CMake projects should ensure a consistent C++ 321 # standard for all compiled sources by setting CMAKE_CXX_STANDARD. 322 _absl_target_compile_features_if_available(${_NAME} INTERFACE ${ABSL_INTERNAL_CXX_STD_FEATURE}) 323 324 # (INTERFACE libraries can't have the CXX_STANDARD property set, so there 325 # is no legacy behavior else case). 326 endif() 327 endif() 328 329 # TODO currently we don't install googletest alongside abseil sources, so 330 # installed abseil can't be tested. 331 if(NOT ABSL_CC_LIB_TESTONLY AND ABSL_ENABLE_INSTALL) 332 install(TARGETS ${_NAME} EXPORT ${PROJECT_NAME}Targets 333 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} 334 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 335 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 336 ) 337 endif() 338 339 add_library(absl::${ABSL_CC_LIB_NAME} ALIAS ${_NAME}) 340endfunction() 341 342# absl_cc_test() 343# 344# CMake function to imitate Bazel's cc_test rule. 345# 346# Parameters: 347# NAME: name of target (see Usage below) 348# SRCS: List of source files for the binary 349# DEPS: List of other libraries to be linked in to the binary targets 350# COPTS: List of private compile options 351# DEFINES: List of public defines 352# LINKOPTS: List of link options 353# 354# Note: 355# By default, absl_cc_test will always create a binary named absl_${NAME}. 356# This will also add it to ctest list as absl_${NAME}. 357# 358# Usage: 359# absl_cc_library( 360# NAME 361# awesome 362# HDRS 363# "a.h" 364# SRCS 365# "a.cc" 366# PUBLIC 367# ) 368# 369# absl_cc_test( 370# NAME 371# awesome_test 372# SRCS 373# "awesome_test.cc" 374# DEPS 375# absl::awesome 376# GTest::gmock 377# GTest::gtest_main 378# ) 379function(absl_cc_test) 380 if(NOT (BUILD_TESTING AND ABSL_BUILD_TESTING)) 381 return() 382 endif() 383 384 cmake_parse_arguments(ABSL_CC_TEST 385 "" 386 "NAME" 387 "SRCS;COPTS;DEFINES;LINKOPTS;DEPS" 388 ${ARGN} 389 ) 390 391 set(_NAME "absl_${ABSL_CC_TEST_NAME}") 392 393 add_executable(${_NAME} "") 394 target_sources(${_NAME} PRIVATE ${ABSL_CC_TEST_SRCS}) 395 target_include_directories(${_NAME} 396 PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} 397 PRIVATE ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS} 398 ) 399 400 if (${ABSL_BUILD_DLL}) 401 target_compile_definitions(${_NAME} 402 PUBLIC 403 ${ABSL_CC_TEST_DEFINES} 404 ABSL_CONSUME_DLL 405 GTEST_LINKED_AS_SHARED_LIBRARY=1 406 ) 407 408 # Replace dependencies on targets inside the DLL with abseil_dll itself. 409 absl_internal_dll_targets( 410 DEPS ${ABSL_CC_TEST_DEPS} 411 OUTPUT ABSL_CC_TEST_DEPS 412 ) 413 else() 414 target_compile_definitions(${_NAME} 415 PUBLIC 416 ${ABSL_CC_TEST_DEFINES} 417 ) 418 endif() 419 target_compile_options(${_NAME} 420 PRIVATE ${ABSL_CC_TEST_COPTS} 421 ) 422 423 target_link_libraries(${_NAME} 424 PUBLIC ${ABSL_CC_TEST_DEPS} 425 PRIVATE ${ABSL_CC_TEST_LINKOPTS} 426 ) 427 # Add all Abseil targets to a folder in the IDE for organization. 428 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test) 429 430 if(ABSL_PROPAGATE_CXX_STD) 431 # Abseil libraries require C++14 as the current minimum standard. 432 # Top-level application CMake projects should ensure a consistent C++ 433 # standard for all compiled sources by setting CMAKE_CXX_STANDARD. 434 _absl_target_compile_features_if_available(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE}) 435 else() 436 # Note: This is legacy (before CMake 3.8) behavior. Setting the 437 # target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is 438 # initialized by CMAKE_CXX_STANDARD) should have no real effect, since 439 # that is the default value anyway. 440 # 441 # CXX_STANDARD_REQUIRED does guard against the top-level CMake project 442 # not having enabled CMAKE_CXX_STANDARD_REQUIRED (which prevents 443 # "decaying" to an older standard if the requested one isn't available). 444 set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD}) 445 set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) 446 endif() 447 448 add_test(NAME ${_NAME} COMMAND ${_NAME}) 449endfunction() 450