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