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 absl_internal_test_dll_contains(TARGET ${_NAME} OUTPUT _in_test_dll) 136 else() 137 absl_internal_dll_contains(TARGET ${ABSL_CC_LIB_NAME} OUTPUT _in_dll) 138 absl_internal_test_dll_contains(TARGET ${ABSL_CC_LIB_NAME} OUTPUT _in_test_dll) 139 endif() 140 if (${_in_dll} OR ${_in_test_dll}) 141 # This target should be replaced by the DLL 142 set(_build_type "dll") 143 set(ABSL_CC_LIB_IS_INTERFACE 1) 144 else() 145 # Building a DLL, but this target is not part of the DLL 146 set(_build_type "dll_dep") 147 endif() 148 elseif(BUILD_SHARED_LIBS) 149 set(_build_type "shared") 150 else() 151 set(_build_type "static") 152 endif() 153 154 # Generate a pkg-config file for every library: 155 if(ABSL_ENABLE_INSTALL) 156 if(NOT ABSL_CC_LIB_TESTONLY) 157 if(absl_VERSION) 158 set(PC_VERSION "${absl_VERSION}") 159 else() 160 set(PC_VERSION "head") 161 endif() 162 if(NOT _build_type STREQUAL "dll") 163 set(LNK_LIB "${LNK_LIB} -labsl_${_NAME}") 164 endif() 165 foreach(dep ${ABSL_CC_LIB_DEPS}) 166 if(${dep} MATCHES "^absl::(.*)") 167 # for DLL builds many libs are not created, but add 168 # the pkgconfigs nevertheless, pointing to the dll. 169 if(_build_type STREQUAL "dll") 170 # hide this MATCHES in an if-clause so it doesn't overwrite 171 # the CMAKE_MATCH_1 from (${dep} MATCHES "^absl::(.*)") 172 if(NOT PC_DEPS MATCHES "abseil_dll") 173 # Join deps with commas. 174 if(PC_DEPS) 175 set(PC_DEPS "${PC_DEPS},") 176 endif() 177 # don't duplicate dll-dep if it exists already 178 set(PC_DEPS "${PC_DEPS} abseil_dll = ${PC_VERSION}") 179 set(LNK_LIB "${LNK_LIB} -labseil_dll") 180 endif() 181 else() 182 # Join deps with commas. 183 if(PC_DEPS) 184 set(PC_DEPS "${PC_DEPS},") 185 endif() 186 set(PC_DEPS "${PC_DEPS} absl_${CMAKE_MATCH_1} = ${PC_VERSION}") 187 endif() 188 endif() 189 endforeach() 190 foreach(cflag ${ABSL_CC_LIB_COPTS}) 191 if(${cflag} MATCHES "^(-Wno|/wd)") 192 # These flags are needed to suppress warnings that might fire in our headers. 193 set(PC_CFLAGS "${PC_CFLAGS} ${cflag}") 194 elseif(${cflag} MATCHES "^(-W|/w[1234eo])") 195 # Don't impose our warnings on others. 196 elseif(${cflag} MATCHES "^-m") 197 # Don't impose CPU instruction requirements on others, as 198 # the code performs feature detection on runtime. 199 else() 200 set(PC_CFLAGS "${PC_CFLAGS} ${cflag}") 201 endif() 202 endforeach() 203 string(REPLACE ";" " " PC_LINKOPTS "${ABSL_CC_LIB_LINKOPTS}") 204 FILE(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/lib/pkgconfig/absl_${_NAME}.pc" CONTENT "\ 205prefix=${CMAKE_INSTALL_PREFIX}\n\ 206exec_prefix=\${prefix}\n\ 207libdir=${CMAKE_INSTALL_FULL_LIBDIR}\n\ 208includedir=${CMAKE_INSTALL_FULL_INCLUDEDIR}\n\ 209\n\ 210Name: absl_${_NAME}\n\ 211Description: Abseil ${_NAME} library\n\ 212URL: https://abseil.io/\n\ 213Version: ${PC_VERSION}\n\ 214Requires:${PC_DEPS}\n\ 215Libs: -L\${libdir} ${PC_LINKOPTS} $<$<NOT:$<BOOL:${ABSL_CC_LIB_IS_INTERFACE}>>:${LNK_LIB}>\n\ 216Cflags: -I\${includedir}${PC_CFLAGS}\n") 217 INSTALL(FILES "${CMAKE_BINARY_DIR}/lib/pkgconfig/absl_${_NAME}.pc" 218 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 219 endif() 220 endif() 221 222 if(NOT ABSL_CC_LIB_IS_INTERFACE) 223 if(_build_type STREQUAL "dll_dep") 224 # This target depends on the DLL. When adding dependencies to this target, 225 # any depended-on-target which is contained inside the DLL is replaced 226 # with a dependency on the DLL. 227 add_library(${_NAME} STATIC "") 228 target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS}) 229 absl_internal_dll_targets( 230 DEPS ${ABSL_CC_LIB_DEPS} 231 OUTPUT _dll_deps 232 ) 233 target_link_libraries(${_NAME} 234 PUBLIC ${_dll_deps} 235 PRIVATE 236 ${ABSL_CC_LIB_LINKOPTS} 237 ${ABSL_DEFAULT_LINKOPTS} 238 ) 239 240 if (ABSL_CC_LIB_TESTONLY) 241 set(_gtest_link_define "GTEST_LINKED_AS_SHARED_LIBRARY=1") 242 else() 243 set(_gtest_link_define) 244 endif() 245 246 target_compile_definitions(${_NAME} 247 PUBLIC 248 ABSL_CONSUME_DLL 249 "${_gtest_link_define}" 250 ) 251 252 elseif(_build_type STREQUAL "static" OR _build_type STREQUAL "shared") 253 add_library(${_NAME} "") 254 target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS}) 255 target_link_libraries(${_NAME} 256 PUBLIC ${ABSL_CC_LIB_DEPS} 257 PRIVATE 258 ${ABSL_CC_LIB_LINKOPTS} 259 ${ABSL_DEFAULT_LINKOPTS} 260 ) 261 else() 262 message(FATAL_ERROR "Invalid build type: ${_build_type}") 263 endif() 264 265 # Linker language can be inferred from sources, but in the case of DLLs we 266 # don't have any .cc files so it would be ambiguous. We could set it 267 # explicitly only in the case of DLLs but, because "CXX" is always the 268 # correct linker language for static or for shared libraries, we set it 269 # unconditionally. 270 set_property(TARGET ${_NAME} PROPERTY LINKER_LANGUAGE "CXX") 271 272 target_include_directories(${_NAME} ${ABSL_INTERNAL_INCLUDE_WARNING_GUARD} 273 PUBLIC 274 "$<BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS}>" 275 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> 276 ) 277 target_compile_options(${_NAME} 278 PRIVATE ${ABSL_CC_LIB_COPTS}) 279 target_compile_definitions(${_NAME} PUBLIC ${ABSL_CC_LIB_DEFINES}) 280 281 # Add all Abseil targets to a a folder in the IDE for organization. 282 if(ABSL_CC_LIB_PUBLIC) 283 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}) 284 elseif(ABSL_CC_LIB_TESTONLY) 285 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test) 286 else() 287 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/internal) 288 endif() 289 290 if(ABSL_PROPAGATE_CXX_STD) 291 # Abseil libraries require C++14 as the current minimum standard. When 292 # compiled with C++17 (either because it is the compiler's default or 293 # explicitly requested), then Abseil requires C++17. 294 _absl_target_compile_features_if_available(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE}) 295 else() 296 # Note: This is legacy (before CMake 3.8) behavior. Setting the 297 # target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is 298 # initialized by CMAKE_CXX_STANDARD) should have no real effect, since 299 # that is the default value anyway. 300 # 301 # CXX_STANDARD_REQUIRED does guard against the top-level CMake project 302 # not having enabled CMAKE_CXX_STANDARD_REQUIRED (which prevents 303 # "decaying" to an older standard if the requested one isn't available). 304 set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD}) 305 set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) 306 endif() 307 308 # When being installed, we lose the absl_ prefix. We want to put it back 309 # to have properly named lib files. This is a no-op when we are not being 310 # installed. 311 if(ABSL_ENABLE_INSTALL) 312 set_target_properties(${_NAME} PROPERTIES 313 OUTPUT_NAME "absl_${_NAME}" 314 SOVERSION "2301.0.0" 315 ) 316 endif() 317 else() 318 # Generating header-only library 319 add_library(${_NAME} INTERFACE) 320 target_include_directories(${_NAME} ${ABSL_INTERNAL_INCLUDE_WARNING_GUARD} 321 INTERFACE 322 "$<BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS}>" 323 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> 324 ) 325 326 if (_build_type STREQUAL "dll") 327 set(ABSL_CC_LIB_DEPS abseil_dll) 328 endif() 329 330 target_link_libraries(${_NAME} 331 INTERFACE 332 ${ABSL_CC_LIB_DEPS} 333 ${ABSL_CC_LIB_LINKOPTS} 334 ${ABSL_DEFAULT_LINKOPTS} 335 ) 336 target_compile_definitions(${_NAME} INTERFACE ${ABSL_CC_LIB_DEFINES}) 337 338 if(ABSL_PROPAGATE_CXX_STD) 339 # Abseil libraries require C++14 as the current minimum standard. 340 # Top-level application CMake projects should ensure a consistent C++ 341 # standard for all compiled sources by setting CMAKE_CXX_STANDARD. 342 _absl_target_compile_features_if_available(${_NAME} INTERFACE ${ABSL_INTERNAL_CXX_STD_FEATURE}) 343 344 # (INTERFACE libraries can't have the CXX_STANDARD property set, so there 345 # is no legacy behavior else case). 346 endif() 347 endif() 348 349 # TODO currently we don't install googletest alongside abseil sources, so 350 # installed abseil can't be tested. 351 if(NOT ABSL_CC_LIB_TESTONLY AND ABSL_ENABLE_INSTALL) 352 install(TARGETS ${_NAME} EXPORT ${PROJECT_NAME}Targets 353 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} 354 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 355 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 356 ) 357 endif() 358 359 add_library(absl::${ABSL_CC_LIB_NAME} ALIAS ${_NAME}) 360endfunction() 361 362# absl_cc_test() 363# 364# CMake function to imitate Bazel's cc_test rule. 365# 366# Parameters: 367# NAME: name of target (see Usage below) 368# SRCS: List of source files for the binary 369# DEPS: List of other libraries to be linked in to the binary targets 370# COPTS: List of private compile options 371# DEFINES: List of public defines 372# LINKOPTS: List of link options 373# 374# Note: 375# By default, absl_cc_test will always create a binary named absl_${NAME}. 376# This will also add it to ctest list as absl_${NAME}. 377# 378# Usage: 379# absl_cc_library( 380# NAME 381# awesome 382# HDRS 383# "a.h" 384# SRCS 385# "a.cc" 386# PUBLIC 387# ) 388# 389# absl_cc_test( 390# NAME 391# awesome_test 392# SRCS 393# "awesome_test.cc" 394# DEPS 395# absl::awesome 396# GTest::gmock 397# GTest::gtest_main 398# ) 399function(absl_cc_test) 400 if(NOT (BUILD_TESTING AND ABSL_BUILD_TESTING)) 401 return() 402 endif() 403 404 cmake_parse_arguments(ABSL_CC_TEST 405 "" 406 "NAME" 407 "SRCS;COPTS;DEFINES;LINKOPTS;DEPS" 408 ${ARGN} 409 ) 410 411 set(_NAME "absl_${ABSL_CC_TEST_NAME}") 412 413 add_executable(${_NAME} "") 414 target_sources(${_NAME} PRIVATE ${ABSL_CC_TEST_SRCS}) 415 target_include_directories(${_NAME} 416 PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} 417 PRIVATE ${absl_gtest_src_dir}/googletest/include ${absl_gtest_src_dir}/googlemock/include 418 ) 419 420 if (${ABSL_BUILD_DLL}) 421 target_compile_definitions(${_NAME} 422 PUBLIC 423 ${ABSL_CC_TEST_DEFINES} 424 ABSL_CONSUME_DLL 425 ABSL_CONSUME_TEST_DLL 426 GTEST_LINKED_AS_SHARED_LIBRARY=1 427 ) 428 429 # Replace dependencies on targets inside the DLL with abseil_dll itself. 430 absl_internal_dll_targets( 431 DEPS ${ABSL_CC_TEST_DEPS} 432 OUTPUT ABSL_CC_TEST_DEPS 433 ) 434 else() 435 target_compile_definitions(${_NAME} 436 PUBLIC 437 ${ABSL_CC_TEST_DEFINES} 438 ) 439 endif() 440 target_compile_options(${_NAME} 441 PRIVATE ${ABSL_CC_TEST_COPTS} 442 ) 443 444 target_link_libraries(${_NAME} 445 PUBLIC ${ABSL_CC_TEST_DEPS} 446 PRIVATE ${ABSL_CC_TEST_LINKOPTS} 447 ) 448 # Add all Abseil targets to a folder in the IDE for organization. 449 set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test) 450 451 if(ABSL_PROPAGATE_CXX_STD) 452 # Abseil libraries require C++14 as the current minimum standard. 453 # Top-level application CMake projects should ensure a consistent C++ 454 # standard for all compiled sources by setting CMAKE_CXX_STANDARD. 455 _absl_target_compile_features_if_available(${_NAME} PUBLIC ${ABSL_INTERNAL_CXX_STD_FEATURE}) 456 else() 457 # Note: This is legacy (before CMake 3.8) behavior. Setting the 458 # target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is 459 # initialized by CMAKE_CXX_STANDARD) should have no real effect, since 460 # that is the default value anyway. 461 # 462 # CXX_STANDARD_REQUIRED does guard against the top-level CMake project 463 # not having enabled CMAKE_CXX_STANDARD_REQUIRED (which prevents 464 # "decaying" to an older standard if the requested one isn't available). 465 set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD}) 466 set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) 467 endif() 468 469 add_test(NAME ${_NAME} COMMAND ${_NAME}) 470endfunction() 471