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