xref: /aosp_15_r20/external/cronet/third_party/abseil-cpp/CMakeLists.txt (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#
2# Copyright 2017 The Abseil Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17# https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md
18# As of 2022-09-06, CMake 3.10 is the minimum supported version.
19cmake_minimum_required(VERSION 3.10)
20
21# Compiler id for Apple Clang is now AppleClang.
22if (POLICY CMP0025)
23  cmake_policy(SET CMP0025 NEW)
24endif (POLICY CMP0025)
25
26# if command can use IN_LIST
27if (POLICY CMP0057)
28  cmake_policy(SET CMP0057 NEW)
29endif (POLICY CMP0057)
30
31# Project version variables are the empty string if version is unspecified
32if (POLICY CMP0048)
33  cmake_policy(SET CMP0048 NEW)
34endif (POLICY CMP0048)
35
36# Honor the GTest_ROOT variable if specified
37if (POLICY CMP0074)
38  cmake_policy(SET CMP0074 NEW)
39endif (POLICY CMP0074)
40
41# option() honor variables
42if (POLICY CMP0077)
43  cmake_policy(SET CMP0077 NEW)
44endif (POLICY CMP0077)
45
46# Allow the user to specify the MSVC runtime
47if (POLICY CMP0091)
48  cmake_policy(SET CMP0091 NEW)
49endif (POLICY CMP0091)
50
51# try_compile() honors the CMAKE_CXX_STANDARD value
52if (POLICY CMP0067)
53  cmake_policy(SET CMP0067 NEW)
54endif (POLICY CMP0067)
55
56# Allow the user to specify the CMAKE_MSVC_DEBUG_INFORMATION_FORMAT
57if (POLICY CMP0141)
58  cmake_policy(SET CMP0141 NEW)
59endif (POLICY CMP0141)
60
61project(absl LANGUAGES CXX)
62include(CTest)
63
64# Output directory is correct by default for most build setups. However, when
65# building Abseil as a DLL, it is important to have the DLL in the same
66# directory as the executable using it. Thus, we put all executables in a single
67# /bin directory.
68set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
69
70# when absl is included as subproject (i.e. using add_subdirectory(abseil-cpp))
71# in the source tree of a project that uses it, install rules are disabled.
72if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
73  option(ABSL_ENABLE_INSTALL "Enable install rule" OFF)
74else()
75  option(ABSL_ENABLE_INSTALL "Enable install rule" ON)
76endif()
77
78option(ABSL_PROPAGATE_CXX_STD
79  "Use CMake C++ standard meta features (e.g. cxx_std_14) that propagate to targets that link to Abseil"
80  OFF)  # TODO: Default to ON for CMake 3.8 and greater.
81if(NOT ABSL_PROPAGATE_CXX_STD)
82  message(WARNING "A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake 3.8 and up. We recommend enabling this option to ensure your project still builds correctly.")
83endif()
84
85option(ABSL_USE_SYSTEM_INCLUDES
86  "Silence warnings in Abseil headers by marking them as SYSTEM includes"
87  OFF)
88
89list(APPEND CMAKE_MODULE_PATH
90  ${CMAKE_CURRENT_LIST_DIR}/CMake
91  ${CMAKE_CURRENT_LIST_DIR}/absl/copts
92)
93
94include(CMakePackageConfigHelpers)
95include(GNUInstallDirs)
96include(AbseilDll)
97include(AbseilHelpers)
98
99
100##
101## Using absl targets
102##
103## all public absl targets are
104## exported with the absl:: prefix
105##
106## e.g absl::base absl::synchronization absl::strings ....
107##
108## DO NOT rely on the internal targets outside of the prefix
109
110
111# include current path
112list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
113
114if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
115  set(ABSL_USING_CLANG ON)
116else()
117  set(ABSL_USING_CLANG OFF)
118endif()
119
120# find dependencies
121## pthread
122find_package(Threads REQUIRED)
123
124include(CMakeDependentOption)
125
126option(ABSL_BUILD_TESTING
127  "If ON, Abseil will build all of Abseil's own tests." OFF)
128
129option(ABSL_BUILD_TEST_HELPERS
130  "If ON, Abseil will build libraries that you can use to write tests against Abseil code. This option requires that Abseil is configured to use GoogleTest."
131  OFF)
132
133option(ABSL_USE_EXTERNAL_GOOGLETEST
134  "If ON, Abseil will assume that the targets for GoogleTest are already provided by the including project. This makes sense when Abseil is used with add_subdirectory." OFF)
135
136cmake_dependent_option(ABSL_FIND_GOOGLETEST
137  "If ON, Abseil will use find_package(GTest) rather than assuming that GoogleTest is already provided by the including project."
138  ON
139  "ABSL_USE_EXTERNAL_GOOGLETEST"
140  OFF)
141
142
143option(ABSL_USE_GOOGLETEST_HEAD
144  "If ON, abseil will download HEAD from GoogleTest at config time." OFF)
145
146set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL")
147
148set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH
149  "If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout."
150  )
151
152if((BUILD_TESTING AND ABSL_BUILD_TESTING) OR ABSL_BUILD_TEST_HELPERS)
153  if (ABSL_USE_EXTERNAL_GOOGLETEST)
154    if (ABSL_FIND_GOOGLETEST)
155      find_package(GTest REQUIRED)
156    elseif(NOT TARGET GTest::gtest)
157      if(TARGET gtest)
158        # When Google Test is included directly rather than through find_package, the aliases are missing.
159        add_library(GTest::gtest ALIAS gtest)
160        add_library(GTest::gtest_main ALIAS gtest_main)
161        add_library(GTest::gmock ALIAS gmock)
162        add_library(GTest::gmock_main ALIAS gmock_main)
163      else()
164        message(FATAL_ERROR "ABSL_USE_EXTERNAL_GOOGLETEST is ON and ABSL_FIND_GOOGLETEST is OFF, which means that the top-level project must build the Google Test project. However, the target gtest was not found.")
165      endif()
166    endif()
167  else()
168    set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build)
169    if(ABSL_USE_GOOGLETEST_HEAD AND ABSL_GOOGLETEST_DOWNLOAD_URL)
170      message(FATAL_ERROR "Do not set both ABSL_USE_GOOGLETEST_HEAD and ABSL_GOOGLETEST_DOWNLOAD_URL")
171    endif()
172    if(ABSL_USE_GOOGLETEST_HEAD)
173      set(absl_gtest_download_url "https://github.com/google/googletest/archive/main.zip")
174    elseif(ABSL_GOOGLETEST_DOWNLOAD_URL)
175      set(absl_gtest_download_url ${ABSL_GOOGLETEST_DOWNLOAD_URL})
176    endif()
177    if(absl_gtest_download_url)
178      set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src)
179    else()
180      set(absl_gtest_src_dir ${ABSL_LOCAL_GOOGLETEST_DIR})
181    endif()
182    include(CMake/Googletest/DownloadGTest.cmake)
183  endif()
184endif()
185
186add_subdirectory(absl)
187
188if(ABSL_ENABLE_INSTALL)
189  # absl:lts-remove-begin(system installation is supported for LTS releases)
190  # We don't support system-wide installation
191  list(APPEND SYSTEM_INSTALL_DIRS "/usr/local" "/usr" "/opt/" "/opt/local" "c:/Program Files/${PROJECT_NAME}")
192  if(NOT DEFINED CMAKE_INSTALL_PREFIX OR CMAKE_INSTALL_PREFIX IN_LIST SYSTEM_INSTALL_DIRS)
193    message(WARNING "\
194  The default and system-level install directories are unsupported except in LTS \
195  releases of Abseil.  Please set CMAKE_INSTALL_PREFIX to install Abseil in your \
196  source or build tree directly.\
197    ")
198  endif()
199  # absl:lts-remove-end
200
201  # install as a subdirectory only
202  install(EXPORT ${PROJECT_NAME}Targets
203    NAMESPACE absl::
204    DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
205  )
206
207  configure_package_config_file(
208    CMake/abslConfig.cmake.in
209    "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
210    INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
211  )
212  install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
213    DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
214  )
215
216  # Abseil only has a version in LTS releases.  This mechanism is accomplished
217  # Abseil's internal Copybara (https://github.com/google/copybara) workflows and
218  # isn't visible in the CMake buildsystem itself.
219  if(absl_VERSION)
220    write_basic_package_version_file(
221      "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
222      COMPATIBILITY ExactVersion
223    )
224
225    install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
226      DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
227    )
228  endif()  # absl_VERSION
229
230  install(DIRECTORY absl
231    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
232    FILES_MATCHING
233      PATTERN "*.inc"
234      PATTERN "*.h"
235      PATTERN "copts" EXCLUDE
236      PATTERN "testdata" EXCLUDE
237    )
238
239  # Rewrite options.h to use the compiled ABI.
240  file(READ "absl/base/options.h" ABSL_INTERNAL_OPTIONS_H_CONTENTS)
241
242  # Handle features that require at least C++20.
243  if (ABSL_INTERNAL_AT_LEAST_CXX20)
244    foreach(FEATURE "ORDERING")
245      string(REPLACE
246      "#define ABSL_OPTION_USE_STD_${FEATURE} 2"
247      "#define ABSL_OPTION_USE_STD_${FEATURE} 1"
248      ABSL_INTERNAL_OPTIONS_H_PINNED
249      "${ABSL_INTERNAL_OPTIONS_H_CONTENTS}")
250      set(ABSL_INTERNAL_OPTIONS_H_CONTENTS "${ABSL_INTERNAL_OPTIONS_H_PINNED}")
251    endforeach()
252  endif()
253
254  # Handle features that require at least C++17.
255  if (ABSL_INTERNAL_AT_LEAST_CXX17)
256    foreach(FEATURE "ANY" "OPTIONAL" "STRING_VIEW" "VARIANT")
257      string(REPLACE
258      "#define ABSL_OPTION_USE_STD_${FEATURE} 2"
259      "#define ABSL_OPTION_USE_STD_${FEATURE} 1"
260      ABSL_INTERNAL_OPTIONS_H_PINNED
261      "${ABSL_INTERNAL_OPTIONS_H_CONTENTS}")
262      set(ABSL_INTERNAL_OPTIONS_H_CONTENTS "${ABSL_INTERNAL_OPTIONS_H_PINNED}")
263    endforeach()
264  endif()
265
266  # Any feature that still has the value of 2 (because it was not handled above)
267  # should be set to 0.
268  string(REGEX REPLACE
269    "#define ABSL_OPTION_USE_STD_([^ ]*) 2"
270    "#define ABSL_OPTION_USE_STD_\\1 0"
271    ABSL_INTERNAL_OPTIONS_H_PINNED
272    "${ABSL_INTERNAL_OPTIONS_H_CONTENTS}")
273
274  file(WRITE "${CMAKE_BINARY_DIR}/options-pinned.h" "${ABSL_INTERNAL_OPTIONS_H_PINNED}")
275
276  install(FILES "${CMAKE_BINARY_DIR}/options-pinned.h"
277         DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/absl/base
278         RENAME "options.h")
279
280endif()  # ABSL_ENABLE_INSTALL
281