Adrien Devresse | d5134a7 | 2017-09-26 20:37:27 -0700 | [diff] [blame] | 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 | # |
nik7273 | 38b7043 | 2019-03-08 10:27:53 -0500 | [diff] [blame] | 8 | # https://www.apache.org/licenses/LICENSE-2.0 |
Adrien Devresse | d5134a7 | 2017-09-26 20:37:27 -0700 | [diff] [blame] | 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 | # |
Abseil Team | 59ae4d5 | 2018-05-18 08:24:54 -0700 | [diff] [blame] | 16 | |
Abseil Team | 256be56 | 2019-03-12 11:39:15 -0700 | [diff] [blame] | 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. |
| 22 | cmake_minimum_required(VERSION 3.5) |
Abseil Team | 44b0faf | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 23 | |
| 24 | # Compiler id for Apple Clang is now AppleClang. |
Abseil Team | 3c2bed2 | 2020-07-20 09:08:19 -0700 | [diff] [blame] | 25 | if (POLICY CMP0025) |
| 26 | cmake_policy(SET CMP0025 NEW) |
| 27 | endif (POLICY CMP0025) |
Abseil Team | 44b0faf | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 28 | |
Abseil Team | bf29470 | 2019-03-19 11:14:01 -0700 | [diff] [blame] | 29 | # if command can use IN_LIST |
Abseil Team | 3c2bed2 | 2020-07-20 09:08:19 -0700 | [diff] [blame] | 30 | if (POLICY CMP0057) |
| 31 | cmake_policy(SET CMP0057 NEW) |
| 32 | endif (POLICY CMP0057) |
Abseil Team | bf29470 | 2019-03-19 11:14:01 -0700 | [diff] [blame] | 33 | |
Abseil Team | a877af1 | 2020-03-10 09:28:06 -0700 | [diff] [blame] | 34 | # Project version variables are the empty string if version is unspecified |
Abseil Team | 3c2bed2 | 2020-07-20 09:08:19 -0700 | [diff] [blame] | 35 | if (POLICY CMP0048) |
| 36 | cmake_policy(SET CMP0048 NEW) |
| 37 | endif (POLICY CMP0048) |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 38 | |
Abseil Team | f624790 | 2020-07-13 13:22:02 -0700 | [diff] [blame] | 39 | # option() honor variables |
Abseil Team | 3c2bed2 | 2020-07-20 09:08:19 -0700 | [diff] [blame] | 40 | if (POLICY CMP0077) |
| 41 | cmake_policy(SET CMP0077 NEW) |
| 42 | endif (POLICY CMP0077) |
Abseil Team | f624790 | 2020-07-13 13:22:02 -0700 | [diff] [blame] | 43 | |
WolverinDEV | 2d41897 | 2021-03-30 18:44:11 +0200 | [diff] [blame] | 44 | # Allow the user to specify the MSVC runtime |
| 45 | if (POLICY CMP0091) |
| 46 | cmake_policy(SET CMP0091 NEW) |
| 47 | endif (POLICY CMP0091) |
| 48 | |
Abseil Team | a50ae36 | 2021-02-22 13:18:10 -0800 | [diff] [blame] | 49 | # Set BUILD_TESTING to OFF by default. |
| 50 | # This must come before the project() and include(CTest) lines. |
| 51 | OPTION(BUILD_TESTING "Build tests" OFF) |
| 52 | |
Derek Mauro | 6a72279 | 2021-11-02 16:49:07 -0400 | [diff] [blame] | 53 | project(absl LANGUAGES CXX VERSION 20211102) |
Abseil Team | a50ae36 | 2021-02-22 13:18:10 -0800 | [diff] [blame] | 54 | include(CTest) |
Adrien Devresse | d5134a7 | 2017-09-26 20:37:27 -0700 | [diff] [blame] | 55 | |
Abseil Team | 37dd256 | 2020-01-28 11:50:11 -0800 | [diff] [blame] | 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. |
| 60 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 61 | |
Abseil Team | bf29470 | 2019-03-19 11:14:01 -0700 | [diff] [blame] | 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. |
Abseil Team | 22771d4 | 2021-01-20 12:39:22 -0800 | [diff] [blame] | 64 | if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) |
Abseil Team | f624790 | 2020-07-13 13:22:02 -0700 | [diff] [blame] | 65 | option(ABSL_ENABLE_INSTALL "Enable install rule" OFF) |
Abseil Team | bf29470 | 2019-03-19 11:14:01 -0700 | [diff] [blame] | 66 | else() |
Abseil Team | f624790 | 2020-07-13 13:22:02 -0700 | [diff] [blame] | 67 | option(ABSL_ENABLE_INSTALL "Enable install rule" ON) |
Abseil Team | bf29470 | 2019-03-19 11:14:01 -0700 | [diff] [blame] | 68 | endif() |
| 69 | |
Eric Barndollar | ee0ebda | 2021-07-29 21:33:51 -0400 | [diff] [blame] | 70 | option(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. |
| 73 | if((${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.") |
| 75 | endif() |
| 76 | |
Abseil Team | 284378a | 2018-12-05 12:37:41 -0800 | [diff] [blame] | 77 | list(APPEND CMAKE_MODULE_PATH |
| 78 | ${CMAKE_CURRENT_LIST_DIR}/CMake |
| 79 | ${CMAKE_CURRENT_LIST_DIR}/absl/copts |
| 80 | ) |
Adrien Devresse | d5134a7 | 2017-09-26 20:37:27 -0700 | [diff] [blame] | 81 | |
Abseil Team | bf29470 | 2019-03-19 11:14:01 -0700 | [diff] [blame] | 82 | include(CMakePackageConfigHelpers) |
Abseil Team | dcf4899 | 2021-03-17 20:08:11 -0700 | [diff] [blame] | 83 | include(GNUInstallDirs) |
Abseil Team | 37dd256 | 2020-01-28 11:50:11 -0800 | [diff] [blame] | 84 | include(AbseilDll) |
Adrien Devresse | d5134a7 | 2017-09-26 20:37:27 -0700 | [diff] [blame] | 85 | include(AbseilHelpers) |
| 86 | |
| 87 | |
Adrien Devresse | d5134a7 | 2017-09-26 20:37:27 -0700 | [diff] [blame] | 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 |
| 100 | list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) |
| 101 | |
Abseil Team | 44b0faf | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 102 | if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") |
| 103 | set(ABSL_USING_CLANG ON) |
| 104 | else() |
| 105 | set(ABSL_USING_CLANG OFF) |
| 106 | endif() |
| 107 | |
Adrien Devresse | d5134a7 | 2017-09-26 20:37:27 -0700 | [diff] [blame] | 108 | # find dependencies |
| 109 | ## pthread |
| 110 | find_package(Threads REQUIRED) |
| 111 | |
Florin Crișan | 8f92175 | 2021-06-10 02:26:40 +0300 | [diff] [blame] | 112 | include(CMakeDependentOption) |
| 113 | |
Adrian Imboden | c01b991 | 2020-04-07 02:50:57 +0200 | [diff] [blame] | 114 | option(ABSL_USE_EXTERNAL_GOOGLETEST |
Abseil Team | 73ea9a9 | 2020-04-06 20:53:47 -0700 | [diff] [blame] | 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 | |
Florin Crișan | 8f92175 | 2021-06-10 02:26:40 +0300 | [diff] [blame] | 117 | cmake_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 | |
Abseil Team | 26b789f | 2018-05-04 09:58:56 -0700 | [diff] [blame] | 124 | option(ABSL_USE_GOOGLETEST_HEAD |
Abseil Team | eb317a7 | 2020-10-21 00:01:29 -0700 | [diff] [blame] | 125 | "If ON, abseil will download HEAD from GoogleTest at config time." OFF) |
| 126 | |
| 127 | set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL") |
Abseil Team | 26b789f | 2018-05-04 09:58:56 -0700 | [diff] [blame] | 128 | |
Abseil Team | bcefbdc | 2020-02-28 12:18:28 -0800 | [diff] [blame] | 129 | set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH |
Abseil Team | eb317a7 | 2020-10-21 00:01:29 -0700 | [diff] [blame] | 130 | "If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout." |
Abseil Team | bcefbdc | 2020-02-28 12:18:28 -0800 | [diff] [blame] | 131 | ) |
| 132 | |
Abseil Team | a50ae36 | 2021-02-22 13:18:10 -0800 | [diff] [blame] | 133 | if(BUILD_TESTING) |
Abseil Team | 2069dc7 | 2020-05-28 12:55:24 -0700 | [diff] [blame] | 134 | ## check targets |
Florin Crișan | 8f92175 | 2021-06-10 02:26:40 +0300 | [diff] [blame] | 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() |
Adrian Imboden | c01b991 | 2020-04-07 02:50:57 +0200 | [diff] [blame] | 144 | set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build) |
Abseil Team | eb317a7 | 2020-10-21 00:01:29 -0700 | [diff] [blame] | 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) |
Adrian Imboden | c01b991 | 2020-04-07 02:50:57 +0200 | [diff] [blame] | 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) |
Abseil Team | 26b789f | 2018-05-04 09:58:56 -0700 | [diff] [blame] | 159 | endif() |
| 160 | |
Florin Crișan | 8f92175 | 2021-06-10 02:26:40 +0300 | [diff] [blame] | 161 | if (NOT ABSL_FIND_GOOGLETEST) |
| 162 | # When Google Test is included directly rather than through find_package, the aliases are missing. |
Florin Crișan | 8f92175 | 2021-06-10 02:26:40 +0300 | [diff] [blame] | 163 | add_library(GTest::gtest ALIAS gtest) |
Abseil Team | a2f52e1 | 2021-10-18 08:24:39 -0700 | [diff] [blame] | 164 | add_library(GTest::gtest_main ALIAS gtest_main) |
Florin Crișan | 8f92175 | 2021-06-10 02:26:40 +0300 | [diff] [blame] | 165 | add_library(GTest::gmock ALIAS gmock) |
Abseil Team | a2f52e1 | 2021-10-18 08:24:39 -0700 | [diff] [blame] | 166 | add_library(GTest::gmock_main ALIAS gmock_main) |
Florin Crișan | 8f92175 | 2021-06-10 02:26:40 +0300 | [diff] [blame] | 167 | endif() |
| 168 | |
| 169 | check_target(GTest::gtest) |
| 170 | check_target(GTest::gtest_main) |
| 171 | check_target(GTest::gmock) |
| 172 | check_target(GTest::gmock_main) |
Roman Gershman | 765541c | 2017-12-23 00:25:36 +0200 | [diff] [blame] | 173 | endif() |
Adrien Devresse | d5134a7 | 2017-09-26 20:37:27 -0700 | [diff] [blame] | 174 | |
| 175 | add_subdirectory(absl) |
Abseil Team | bf29470 | 2019-03-19 11:14:01 -0700 | [diff] [blame] | 176 | |
Abseil Team | 2c8421e | 2019-03-28 11:12:19 -0700 | [diff] [blame] | 177 | if(ABSL_ENABLE_INSTALL) |
Derek Mauro | 6a72279 | 2021-11-02 16:49:07 -0400 | [diff] [blame] | 178 | |
Abseil Team | 2c8421e | 2019-03-28 11:12:19 -0700 | [diff] [blame] | 179 | |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 180 | # install as a subdirectory only |
| 181 | install(EXPORT ${PROJECT_NAME}Targets |
| 182 | NAMESPACE absl:: |
Abseil Team | dcf4899 | 2021-03-17 20:08:11 -0700 | [diff] [blame] | 183 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 184 | ) |
Abseil Team | bf29470 | 2019-03-19 11:14:01 -0700 | [diff] [blame] | 185 | |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 186 | configure_package_config_file( |
| 187 | CMake/abslConfig.cmake.in |
| 188 | "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" |
Abseil Team | dcf4899 | 2021-03-17 20:08:11 -0700 | [diff] [blame] | 189 | INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 190 | ) |
| 191 | install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" |
Abseil Team | dcf4899 | 2021-03-17 20:08:11 -0700 | [diff] [blame] | 192 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 193 | ) |
Abseil Team | bf29470 | 2019-03-19 11:14:01 -0700 | [diff] [blame] | 194 | |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 195 | # Abseil only has a version in LTS releases. This mechanism is accomplished |
| 196 | # Abseil's internal Copybara (https://github.com/google/copybara) workflows and |
| 197 | # isn't visible in the CMake buildsystem itself. |
Abseil Team | 2c8421e | 2019-03-28 11:12:19 -0700 | [diff] [blame] | 198 | if(absl_VERSION) |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 199 | write_basic_package_version_file( |
| 200 | "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" |
| 201 | COMPATIBILITY ExactVersion |
| 202 | ) |
Abseil Team | bf29470 | 2019-03-19 11:14:01 -0700 | [diff] [blame] | 203 | |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 204 | install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" |
Abseil Team | dcf4899 | 2021-03-17 20:08:11 -0700 | [diff] [blame] | 205 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 206 | ) |
| 207 | endif() # absl_VERSION |
| 208 | |
| 209 | install(DIRECTORY absl |
Abseil Team | dcf4899 | 2021-03-17 20:08:11 -0700 | [diff] [blame] | 210 | DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 211 | FILES_MATCHING |
| 212 | PATTERN "*.inc" |
| 213 | PATTERN "*.h" |
Po-Chuan Hsieh | 8faf204 | 2020-06-03 02:18:31 +0800 | [diff] [blame] | 214 | PATTERN "copts" EXCLUDE |
| 215 | PATTERN "testdata" EXCLUDE |
Abseil Team | da3a876 | 2020-06-02 11:09:12 -0700 | [diff] [blame] | 216 | ) |
Abseil Team | 5b65c4a | 2019-03-27 08:05:41 -0700 | [diff] [blame] | 217 | endif() # ABSL_ENABLE_INSTALL |