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