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 51project(absl LANGUAGES CXX) 52include(CTest) 53 54# Output directory is correct by default for most build setups. However, when 55# building Abseil as a DLL, it is important to have the DLL in the same 56# directory as the executable using it. Thus, we put all executables in a single 57# /bin directory. 58set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 59 60# when absl is included as subproject (i.e. using add_subdirectory(abseil-cpp)) 61# in the source tree of a project that uses it, install rules are disabled. 62if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) 63 option(ABSL_ENABLE_INSTALL "Enable install rule" OFF) 64else() 65 option(ABSL_ENABLE_INSTALL "Enable install rule" ON) 66endif() 67 68option(ABSL_PROPAGATE_CXX_STD 69 "Use CMake C++ standard meta features (e.g. cxx_std_14) that propagate to targets that link to Abseil" 70 OFF) # TODO: Default to ON for CMake 3.8 and greater. 71if((${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.8) AND (NOT ABSL_PROPAGATE_CXX_STD)) 72 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.") 73endif() 74 75option(ABSL_USE_SYSTEM_INCLUDES 76 "Silence warnings in Abseil headers by marking them as SYSTEM includes" 77 OFF) 78 79list(APPEND CMAKE_MODULE_PATH 80 ${CMAKE_CURRENT_LIST_DIR}/CMake 81 ${CMAKE_CURRENT_LIST_DIR}/absl/copts 82) 83 84include(CMakePackageConfigHelpers) 85include(GNUInstallDirs) 86include(AbseilDll) 87include(AbseilHelpers) 88 89 90## 91## Using absl targets 92## 93## all public absl targets are 94## exported with the absl:: prefix 95## 96## e.g absl::base absl::synchronization absl::strings .... 97## 98## DO NOT rely on the internal targets outside of the prefix 99 100 101# include current path 102list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) 103 104if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 105 set(ABSL_USING_CLANG ON) 106else() 107 set(ABSL_USING_CLANG OFF) 108endif() 109 110# find dependencies 111## pthread 112find_package(Threads REQUIRED) 113 114include(CMakeDependentOption) 115 116option(ABSL_BUILD_TESTING 117 "If ON, Abseil will build all of Abseil's own tests." OFF) 118 119option(ABSL_BUILD_TEST_HELPERS 120 "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." 121 OFF) 122 123option(ABSL_USE_EXTERNAL_GOOGLETEST 124 "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) 125 126cmake_dependent_option(ABSL_FIND_GOOGLETEST 127 "If ON, Abseil will use find_package(GTest) rather than assuming that GoogleTest is already provided by the including project." 128 ON 129 "ABSL_USE_EXTERNAL_GOOGLETEST" 130 OFF) 131 132 133option(ABSL_USE_GOOGLETEST_HEAD 134 "If ON, abseil will download HEAD from GoogleTest at config time." OFF) 135 136set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL") 137 138set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH 139 "If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout." 140 ) 141 142if((BUILD_TESTING AND ABSL_BUILD_TESTING) OR ABSL_BUILD_TEST_HELPERS) 143 if (ABSL_USE_EXTERNAL_GOOGLETEST) 144 if (ABSL_FIND_GOOGLETEST) 145 find_package(GTest REQUIRED) 146 elseif(NOT TARGET GTest::gtest) 147 if(TARGET gtest) 148 # When Google Test is included directly rather than through find_package, the aliases are missing. 149 add_library(GTest::gtest ALIAS gtest) 150 add_library(GTest::gtest_main ALIAS gtest_main) 151 add_library(GTest::gmock ALIAS gmock) 152 add_library(GTest::gmock_main ALIAS gmock_main) 153 else() 154 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.") 155 endif() 156 endif() 157 else() 158 set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build) 159 if(ABSL_USE_GOOGLETEST_HEAD AND ABSL_GOOGLETEST_DOWNLOAD_URL) 160 message(FATAL_ERROR "Do not set both ABSL_USE_GOOGLETEST_HEAD and ABSL_GOOGLETEST_DOWNLOAD_URL") 161 endif() 162 if(ABSL_USE_GOOGLETEST_HEAD) 163 set(absl_gtest_download_url "https://github.com/google/googletest/archive/main.zip") 164 elseif(ABSL_GOOGLETEST_DOWNLOAD_URL) 165 set(absl_gtest_download_url ${ABSL_GOOGLETEST_DOWNLOAD_URL}) 166 endif() 167 if(absl_gtest_download_url) 168 set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src) 169 else() 170 set(absl_gtest_src_dir ${ABSL_LOCAL_GOOGLETEST_DIR}) 171 endif() 172 include(CMake/Googletest/DownloadGTest.cmake) 173 endif() 174endif() 175 176add_subdirectory(absl) 177 178if(ABSL_ENABLE_INSTALL) 179 # absl:lts-remove-begin(system installation is supported for LTS releases) 180 # We don't support system-wide installation 181 list(APPEND SYSTEM_INSTALL_DIRS "/usr/local" "/usr" "/opt/" "/opt/local" "c:/Program Files/${PROJECT_NAME}") 182 if(NOT DEFINED CMAKE_INSTALL_PREFIX OR CMAKE_INSTALL_PREFIX IN_LIST SYSTEM_INSTALL_DIRS) 183 message(WARNING "\ 184 The default and system-level install directories are unsupported except in LTS \ 185 releases of Abseil. Please set CMAKE_INSTALL_PREFIX to install Abseil in your \ 186 source or build tree directly.\ 187 ") 188 endif() 189 # absl:lts-remove-end 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 file(READ "absl/base/options.h" ABSL_INTERNAL_OPTIONS_H_CONTENTS) 230 if (ABSL_INTERNAL_AT_LEAST_CXX17) 231 string(REGEX REPLACE 232 "#define ABSL_OPTION_USE_STD_([^ ]*) 2" 233 "#define ABSL_OPTION_USE_STD_\\1 1" 234 ABSL_INTERNAL_OPTIONS_H_PINNED 235 "${ABSL_INTERNAL_OPTIONS_H_CONTENTS}") 236 else() 237 string(REGEX REPLACE 238 "#define ABSL_OPTION_USE_STD_([^ ]*) 2" 239 "#define ABSL_OPTION_USE_STD_\\1 0" 240 ABSL_INTERNAL_OPTIONS_H_PINNED 241 "${ABSL_INTERNAL_OPTIONS_H_CONTENTS}") 242 endif() 243 file(WRITE "${CMAKE_BINARY_DIR}/options-pinned.h" "${ABSL_INTERNAL_OPTIONS_H_PINNED}") 244 245 install(FILES "${CMAKE_BINARY_DIR}/options-pinned.h" 246 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/absl/base 247 RENAME "options.h") 248 249endif() # ABSL_ENABLE_INSTALL 250