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 VERSION 20240116) 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 190 191 # install as a subdirectory only 192 install(EXPORT ${PROJECT_NAME}Targets 193 NAMESPACE absl:: 194 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 195 ) 196 197 configure_package_config_file( 198 CMake/abslConfig.cmake.in 199 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" 200 INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 201 ) 202 install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" 203 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 204 ) 205 206 # Abseil only has a version in LTS releases. This mechanism is accomplished 207 # Abseil's internal Copybara (https://github.com/google/copybara) workflows and 208 # isn't visible in the CMake buildsystem itself. 209 if(absl_VERSION) 210 write_basic_package_version_file( 211 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" 212 COMPATIBILITY ExactVersion 213 ) 214 215 install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" 216 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 217 ) 218 endif() # absl_VERSION 219 220 install(DIRECTORY absl 221 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} 222 FILES_MATCHING 223 PATTERN "*.inc" 224 PATTERN "*.h" 225 PATTERN "copts" EXCLUDE 226 PATTERN "testdata" EXCLUDE 227 ) 228 229 # Rewrite options.h to use the compiled ABI. 230 file(READ "absl/base/options.h" ABSL_INTERNAL_OPTIONS_H_CONTENTS) 231 232 # Handle features that require at least C++20. 233 if (ABSL_INTERNAL_AT_LEAST_CXX20) 234 foreach(FEATURE "ORDERING") 235 string(REPLACE 236 "#define ABSL_OPTION_USE_STD_${FEATURE} 2" 237 "#define ABSL_OPTION_USE_STD_${FEATURE} 1" 238 ABSL_INTERNAL_OPTIONS_H_PINNED 239 "${ABSL_INTERNAL_OPTIONS_H_CONTENTS}") 240 set(ABSL_INTERNAL_OPTIONS_H_CONTENTS "${ABSL_INTERNAL_OPTIONS_H_PINNED}") 241 endforeach() 242 endif() 243 244 # Handle features that require at least C++17. 245 if (ABSL_INTERNAL_AT_LEAST_CXX17) 246 foreach(FEATURE "ANY" "OPTIONAL" "STRING_VIEW" "VARIANT") 247 string(REPLACE 248 "#define ABSL_OPTION_USE_STD_${FEATURE} 2" 249 "#define ABSL_OPTION_USE_STD_${FEATURE} 1" 250 ABSL_INTERNAL_OPTIONS_H_PINNED 251 "${ABSL_INTERNAL_OPTIONS_H_CONTENTS}") 252 set(ABSL_INTERNAL_OPTIONS_H_CONTENTS "${ABSL_INTERNAL_OPTIONS_H_PINNED}") 253 endforeach() 254 endif() 255 256 # Any feature that still has the value of 2 (because it was not handled above) 257 # should be set to 0. 258 string(REGEX REPLACE 259 "#define ABSL_OPTION_USE_STD_([^ ]*) 2" 260 "#define ABSL_OPTION_USE_STD_\\1 0" 261 ABSL_INTERNAL_OPTIONS_H_PINNED 262 "${ABSL_INTERNAL_OPTIONS_H_CONTENTS}") 263 264 file(WRITE "${CMAKE_BINARY_DIR}/options-pinned.h" "${ABSL_INTERNAL_OPTIONS_H_PINNED}") 265 266 install(FILES "${CMAKE_BINARY_DIR}/options-pinned.h" 267 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/absl/base 268 RENAME "options.h") 269 270endif() # ABSL_ENABLE_INSTALL 271