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# Most widely used distributions have cmake 3.5 or greater available as of March 18# 2019. A notable exception is RHEL-7 (CentOS7). You can install a current 19# version of CMake by first installing Extra Packages for Enterprise Linux 20# (https://fedoraproject.org/wiki/EPEL#Extra_Packages_for_Enterprise_Linux_.28EPEL.29) 21# and then issuing `yum install cmake3` on the command line. 22cmake_minimum_required(VERSION 3.5) 23 24# Compiler id for Apple Clang is now AppleClang. 25if (POLICY CMP0025) 26 cmake_policy(SET CMP0025 NEW) 27endif (POLICY CMP0025) 28 29# if command can use IN_LIST 30if (POLICY CMP0057) 31 cmake_policy(SET CMP0057 NEW) 32endif (POLICY CMP0057) 33 34# Project version variables are the empty string if version is unspecified 35if (POLICY CMP0048) 36 cmake_policy(SET CMP0048 NEW) 37endif (POLICY CMP0048) 38 39# option() honor variables 40if (POLICY CMP0077) 41 cmake_policy(SET CMP0077 NEW) 42endif (POLICY CMP0077) 43 44# Allow the user to specify the MSVC runtime 45if (POLICY CMP0091) 46 cmake_policy(SET CMP0091 NEW) 47endif (POLICY CMP0091) 48 49# Set BUILD_TESTING to OFF by default. 50# This must come before the project() and include(CTest) lines. 51OPTION(BUILD_TESTING "Build tests" OFF) 52 53project(absl LANGUAGES CXX) 54include(CTest) 55 56# Output directory is correct by default for most build setups. However, when 57# building Abseil as a DLL, it is important to have the DLL in the same 58# directory as the executable using it. Thus, we put all executables in a single 59# /bin directory. 60set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 61 62# when absl is included as subproject (i.e. using add_subdirectory(abseil-cpp)) 63# in the source tree of a project that uses it, install rules are disabled. 64if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) 65 option(ABSL_ENABLE_INSTALL "Enable install rule" OFF) 66else() 67 option(ABSL_ENABLE_INSTALL "Enable install rule" ON) 68endif() 69 70option(ABSL_PROPAGATE_CXX_STD 71 "Use CMake C++ standard meta features (e.g. cxx_std_11) that propagate to targets that link to Abseil" 72 OFF) # TODO: Default to ON for CMake 3.8 and greater. 73if((${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.8) AND (NOT ABSL_PROPAGATE_CXX_STD)) 74 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.") 75endif() 76 77list(APPEND CMAKE_MODULE_PATH 78 ${CMAKE_CURRENT_LIST_DIR}/CMake 79 ${CMAKE_CURRENT_LIST_DIR}/absl/copts 80) 81 82include(CMakePackageConfigHelpers) 83include(GNUInstallDirs) 84include(AbseilDll) 85include(AbseilHelpers) 86 87 88## 89## Using absl targets 90## 91## all public absl targets are 92## exported with the absl:: prefix 93## 94## e.g absl::base absl::synchronization absl::strings .... 95## 96## DO NOT rely on the internal targets outside of the prefix 97 98 99# include current path 100list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) 101 102if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 103 set(ABSL_USING_CLANG ON) 104else() 105 set(ABSL_USING_CLANG OFF) 106endif() 107 108# find dependencies 109## pthread 110find_package(Threads REQUIRED) 111 112include(CMakeDependentOption) 113 114option(ABSL_USE_EXTERNAL_GOOGLETEST 115 "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_subproject." OFF) 116 117cmake_dependent_option(ABSL_FIND_GOOGLETEST 118 "If ON, Abseil will use find_package(GTest) rather than assuming that GoogleTest is already provided by the including project." 119 ON 120 "ABSL_USE_EXTERNAL_GOOGLETEST" 121 OFF) 122 123 124option(ABSL_USE_GOOGLETEST_HEAD 125 "If ON, abseil will download HEAD from GoogleTest at config time." OFF) 126 127set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL") 128 129set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH 130 "If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout." 131 ) 132 133if(BUILD_TESTING) 134 ## check targets 135 if (ABSL_USE_EXTERNAL_GOOGLETEST) 136 if (ABSL_FIND_GOOGLETEST) 137 find_package(GTest REQUIRED) 138 else() 139 if (NOT TARGET gtest AND NOT TARGET GTest::gtest) 140 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.") 141 endif() 142 endif() 143 else() 144 set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build) 145 if(ABSL_USE_GOOGLETEST_HEAD AND ABSL_GOOGLETEST_DOWNLOAD_URL) 146 message(FATAL_ERROR "Do not set both ABSL_USE_GOOGLETEST_HEAD and ABSL_GOOGLETEST_DOWNLOAD_URL") 147 endif() 148 if(ABSL_USE_GOOGLETEST_HEAD) 149 set(absl_gtest_download_url "https://github.com/google/googletest/archive/master.zip") 150 elseif(ABSL_GOOGLETEST_DOWNLOAD_URL) 151 set(absl_gtest_download_url ${ABSL_GOOGLETEST_DOWNLOAD_URL}) 152 endif() 153 if(absl_gtest_download_url) 154 set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src) 155 else() 156 set(absl_gtest_src_dir ${ABSL_LOCAL_GOOGLETEST_DIR}) 157 endif() 158 include(CMake/Googletest/DownloadGTest.cmake) 159 endif() 160 161 if (NOT ABSL_FIND_GOOGLETEST) 162 # When Google Test is included directly rather than through find_package, the aliases are missing. 163 add_library(GTest::gtest_main ALIAS gtest_main) 164 add_library(GTest::gtest ALIAS gtest) 165 add_library(GTest::gmock ALIAS gmock) 166 endif() 167 168 check_target(GTest::gtest) 169 check_target(GTest::gtest_main) 170 check_target(GTest::gmock) 171 check_target(GTest::gmock_main) 172 173 list(APPEND ABSL_TEST_COMMON_LIBRARIES 174 GTest::gtest_main 175 GTest::gtest 176 GTest::gmock 177 ${CMAKE_THREAD_LIBS_INIT} 178 ) 179endif() 180 181add_subdirectory(absl) 182 183if(ABSL_ENABLE_INSTALL) 184 # absl:lts-remove-begin(system installation is supported for LTS releases) 185 # We don't support system-wide installation 186 list(APPEND SYSTEM_INSTALL_DIRS "/usr/local" "/usr" "/opt/" "/opt/local" "c:/Program Files/${PROJECT_NAME}") 187 if(NOT DEFINED CMAKE_INSTALL_PREFIX OR CMAKE_INSTALL_PREFIX IN_LIST SYSTEM_INSTALL_DIRS) 188 message(WARNING "\ 189 The default and system-level install directories are unsupported except in LTS \ 190 releases of Abseil. Please set CMAKE_INSTALL_PREFIX to install Abseil in your \ 191 source or build tree directly.\ 192 ") 193 endif() 194 # absl:lts-remove-end 195 196 # install as a subdirectory only 197 install(EXPORT ${PROJECT_NAME}Targets 198 NAMESPACE absl:: 199 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 200 ) 201 202 configure_package_config_file( 203 CMake/abslConfig.cmake.in 204 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" 205 INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 206 ) 207 install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" 208 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 209 ) 210 211 # Abseil only has a version in LTS releases. This mechanism is accomplished 212 # Abseil's internal Copybara (https://github.com/google/copybara) workflows and 213 # isn't visible in the CMake buildsystem itself. 214 if(absl_VERSION) 215 write_basic_package_version_file( 216 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" 217 COMPATIBILITY ExactVersion 218 ) 219 220 install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" 221 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 222 ) 223 endif() # absl_VERSION 224 225 install(DIRECTORY absl 226 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} 227 FILES_MATCHING 228 PATTERN "*.inc" 229 PATTERN "*.h" 230 PATTERN "copts" EXCLUDE 231 PATTERN "testdata" EXCLUDE 232 ) 233endif() # ABSL_ENABLE_INSTALL 234