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# option() honor variables 37if (POLICY CMP0077) 38 cmake_policy(SET CMP0077 NEW) 39endif (POLICY CMP0077) 40 41# Allow the user to specify the MSVC runtime 42if (POLICY CMP0091) 43 cmake_policy(SET CMP0091 NEW) 44endif (POLICY CMP0091) 45 46# try_compile() honors the CMAKE_CXX_STANDARD value 47if (POLICY CMP0067) 48 cmake_policy(SET CMP0067 NEW) 49endif (POLICY CMP0067) 50 51# Allow the user to specify the CMAKE_MSVC_DEBUG_INFORMATION_FORMAT 52if (POLICY CMP0141) 53 cmake_policy(SET CMP0141 NEW) 54endif (POLICY CMP0141) 55 56project(absl LANGUAGES CXX VERSION 20230802) 57include(CTest) 58 59# Output directory is correct by default for most build setups. However, when 60# building Abseil as a DLL, it is important to have the DLL in the same 61# directory as the executable using it. Thus, we put all executables in a single 62# /bin directory. 63set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 64 65# when absl is included as subproject (i.e. using add_subdirectory(abseil-cpp)) 66# in the source tree of a project that uses it, install rules are disabled. 67if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) 68 option(ABSL_ENABLE_INSTALL "Enable install rule" OFF) 69else() 70 option(ABSL_ENABLE_INSTALL "Enable install rule" ON) 71endif() 72 73option(ABSL_PROPAGATE_CXX_STD 74 "Use CMake C++ standard meta features (e.g. cxx_std_14) that propagate to targets that link to Abseil" 75 OFF) # TODO: Default to ON for CMake 3.8 and greater. 76if(NOT ABSL_PROPAGATE_CXX_STD) 77 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.") 78endif() 79 80option(ABSL_USE_SYSTEM_INCLUDES 81 "Silence warnings in Abseil headers by marking them as SYSTEM includes" 82 OFF) 83 84list(APPEND CMAKE_MODULE_PATH 85 ${CMAKE_CURRENT_LIST_DIR}/CMake 86 ${CMAKE_CURRENT_LIST_DIR}/absl/copts 87) 88 89include(CMakePackageConfigHelpers) 90include(GNUInstallDirs) 91include(AbseilDll) 92include(AbseilHelpers) 93 94 95## 96## Using absl targets 97## 98## all public absl targets are 99## exported with the absl:: prefix 100## 101## e.g absl::base absl::synchronization absl::strings .... 102## 103## DO NOT rely on the internal targets outside of the prefix 104 105 106# include current path 107list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) 108 109if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 110 set(ABSL_USING_CLANG ON) 111else() 112 set(ABSL_USING_CLANG OFF) 113endif() 114 115# find dependencies 116## pthread 117find_package(Threads REQUIRED) 118 119include(CMakeDependentOption) 120 121option(ABSL_BUILD_TESTING 122 "If ON, Abseil will build all of Abseil's own tests." OFF) 123 124option(ABSL_BUILD_TEST_HELPERS 125 "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." 126 OFF) 127 128option(ABSL_USE_EXTERNAL_GOOGLETEST 129 "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) 130 131cmake_dependent_option(ABSL_FIND_GOOGLETEST 132 "If ON, Abseil will use find_package(GTest) rather than assuming that GoogleTest is already provided by the including project." 133 ON 134 "ABSL_USE_EXTERNAL_GOOGLETEST" 135 OFF) 136 137 138option(ABSL_USE_GOOGLETEST_HEAD 139 "If ON, abseil will download HEAD from GoogleTest at config time." OFF) 140 141set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL") 142 143set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH 144 "If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout." 145 ) 146 147if((BUILD_TESTING AND ABSL_BUILD_TESTING) OR ABSL_BUILD_TEST_HELPERS) 148 if (ABSL_USE_EXTERNAL_GOOGLETEST) 149 if (ABSL_FIND_GOOGLETEST) 150 find_package(GTest REQUIRED) 151 elseif(NOT TARGET GTest::gtest) 152 if(TARGET gtest) 153 # When Google Test is included directly rather than through find_package, the aliases are missing. 154 add_library(GTest::gtest ALIAS gtest) 155 add_library(GTest::gtest_main ALIAS gtest_main) 156 add_library(GTest::gmock ALIAS gmock) 157 add_library(GTest::gmock_main ALIAS gmock_main) 158 else() 159 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.") 160 endif() 161 endif() 162 else() 163 set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build) 164 if(ABSL_USE_GOOGLETEST_HEAD AND ABSL_GOOGLETEST_DOWNLOAD_URL) 165 message(FATAL_ERROR "Do not set both ABSL_USE_GOOGLETEST_HEAD and ABSL_GOOGLETEST_DOWNLOAD_URL") 166 endif() 167 if(ABSL_USE_GOOGLETEST_HEAD) 168 set(absl_gtest_download_url "https://github.com/google/googletest/archive/main.zip") 169 elseif(ABSL_GOOGLETEST_DOWNLOAD_URL) 170 set(absl_gtest_download_url ${ABSL_GOOGLETEST_DOWNLOAD_URL}) 171 endif() 172 if(absl_gtest_download_url) 173 set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src) 174 else() 175 set(absl_gtest_src_dir ${ABSL_LOCAL_GOOGLETEST_DIR}) 176 endif() 177 include(CMake/Googletest/DownloadGTest.cmake) 178 endif() 179endif() 180 181add_subdirectory(absl) 182 183if(ABSL_ENABLE_INSTALL) 184 185 186 # install as a subdirectory only 187 install(EXPORT ${PROJECT_NAME}Targets 188 NAMESPACE absl:: 189 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 190 ) 191 192 configure_package_config_file( 193 CMake/abslConfig.cmake.in 194 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" 195 INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 196 ) 197 install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" 198 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 199 ) 200 201 # Abseil only has a version in LTS releases. This mechanism is accomplished 202 # Abseil's internal Copybara (https://github.com/google/copybara) workflows and 203 # isn't visible in the CMake buildsystem itself. 204 if(absl_VERSION) 205 write_basic_package_version_file( 206 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" 207 COMPATIBILITY ExactVersion 208 ) 209 210 install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" 211 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 212 ) 213 endif() # absl_VERSION 214 215 install(DIRECTORY absl 216 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} 217 FILES_MATCHING 218 PATTERN "*.inc" 219 PATTERN "*.h" 220 PATTERN "copts" EXCLUDE 221 PATTERN "testdata" EXCLUDE 222 ) 223 224 file(READ "absl/base/options.h" ABSL_INTERNAL_OPTIONS_H_CONTENTS) 225 if (ABSL_INTERNAL_AT_LEAST_CXX17) 226 string(REGEX REPLACE 227 "#define ABSL_OPTION_USE_STD_([^ ]*) 2" 228 "#define ABSL_OPTION_USE_STD_\\1 1" 229 ABSL_INTERNAL_OPTIONS_H_PINNED 230 "${ABSL_INTERNAL_OPTIONS_H_CONTENTS}") 231 else() 232 string(REGEX REPLACE 233 "#define ABSL_OPTION_USE_STD_([^ ]*) 2" 234 "#define ABSL_OPTION_USE_STD_\\1 0" 235 ABSL_INTERNAL_OPTIONS_H_PINNED 236 "${ABSL_INTERNAL_OPTIONS_H_CONTENTS}") 237 endif() 238 file(WRITE "${CMAKE_BINARY_DIR}/options-pinned.h" "${ABSL_INTERNAL_OPTIONS_H_PINNED}") 239 240 install(FILES "${CMAKE_BINARY_DIR}/options-pinned.h" 241 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/absl/base 242 RENAME "options.h") 243 244endif() # ABSL_ENABLE_INSTALL 245