Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -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 | # |
| 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. |
| 22 | cmake_minimum_required(VERSION 3.5) |
| 23 | |
| 24 | # Compiler id for Apple Clang is now AppleClang. |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 25 | if (POLICY CMP0025) |
| 26 | cmake_policy(SET CMP0025 NEW) |
| 27 | endif (POLICY CMP0025) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 28 | |
| 29 | # if command can use IN_LIST |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 30 | if (POLICY CMP0057) |
| 31 | cmake_policy(SET CMP0057 NEW) |
| 32 | endif (POLICY CMP0057) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 33 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 34 | # Project version variables are the empty string if version is unspecified |
| 35 | if (POLICY CMP0048) |
| 36 | cmake_policy(SET CMP0048 NEW) |
| 37 | endif (POLICY CMP0048) |
| 38 | |
| 39 | # option() honor variables |
| 40 | if (POLICY CMP0077) |
| 41 | cmake_policy(SET CMP0077 NEW) |
| 42 | endif (POLICY CMP0077) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 43 | |
| 44 | project(absl CXX) |
| 45 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 46 | # Output directory is correct by default for most build setups. However, when |
| 47 | # building Abseil as a DLL, it is important to have the DLL in the same |
| 48 | # directory as the executable using it. Thus, we put all executables in a single |
| 49 | # /bin directory. |
| 50 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 51 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 52 | # when absl is included as subproject (i.e. using add_subdirectory(abseil-cpp)) |
| 53 | # in the source tree of a project that uses it, install rules are disabled. |
| 54 | if(NOT "^${CMAKE_SOURCE_DIR}$" STREQUAL "^${PROJECT_SOURCE_DIR}$") |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 55 | option(ABSL_ENABLE_INSTALL "Enable install rule" OFF) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 56 | else() |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 57 | option(ABSL_ENABLE_INSTALL "Enable install rule" ON) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 58 | endif() |
| 59 | |
| 60 | list(APPEND CMAKE_MODULE_PATH |
| 61 | ${CMAKE_CURRENT_LIST_DIR}/CMake |
| 62 | ${CMAKE_CURRENT_LIST_DIR}/absl/copts |
| 63 | ) |
| 64 | |
| 65 | include(AbseilInstallDirs) |
| 66 | include(CMakePackageConfigHelpers) |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 67 | include(AbseilDll) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 68 | include(AbseilHelpers) |
| 69 | |
| 70 | |
| 71 | ## |
| 72 | ## Using absl targets |
| 73 | ## |
| 74 | ## all public absl targets are |
| 75 | ## exported with the absl:: prefix |
| 76 | ## |
| 77 | ## e.g absl::base absl::synchronization absl::strings .... |
| 78 | ## |
| 79 | ## DO NOT rely on the internal targets outside of the prefix |
| 80 | |
| 81 | |
| 82 | # include current path |
| 83 | list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) |
| 84 | |
| 85 | if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") |
| 86 | set(ABSL_USING_CLANG ON) |
| 87 | else() |
| 88 | set(ABSL_USING_CLANG OFF) |
| 89 | endif() |
| 90 | |
| 91 | # find dependencies |
| 92 | ## pthread |
| 93 | find_package(Threads REQUIRED) |
| 94 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 95 | option(ABSL_USE_EXTERNAL_GOOGLETEST |
| 96 | "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) |
| 97 | |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 98 | option(ABSL_USE_GOOGLETEST_HEAD |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 99 | "If ON, abseil will download HEAD from GoogleTest at config time." OFF) |
| 100 | |
| 101 | set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL") |
| 102 | |
| 103 | set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH |
| 104 | "If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout." |
| 105 | ) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 106 | |
| 107 | option(ABSL_RUN_TESTS "If ON, Abseil tests will be run." OFF) |
| 108 | |
| 109 | if(${ABSL_RUN_TESTS}) |
| 110 | # enable CTest. This will set BUILD_TESTING to ON unless otherwise specified |
| 111 | # on the command line |
| 112 | include(CTest) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 113 | |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 114 | ## check targets |
| 115 | if (NOT ABSL_USE_EXTERNAL_GOOGLETEST) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 116 | set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build) |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 117 | if(ABSL_USE_GOOGLETEST_HEAD AND ABSL_GOOGLETEST_DOWNLOAD_URL) |
| 118 | message(FATAL_ERROR "Do not set both ABSL_USE_GOOGLETEST_HEAD and ABSL_GOOGLETEST_DOWNLOAD_URL") |
| 119 | endif() |
| 120 | if(ABSL_USE_GOOGLETEST_HEAD) |
| 121 | set(absl_gtest_download_url "https://github.com/google/googletest/archive/master.zip") |
| 122 | elseif(ABSL_GOOGLETEST_DOWNLOAD_URL) |
| 123 | set(absl_gtest_download_url ${ABSL_GOOGLETEST_DOWNLOAD_URL}) |
| 124 | endif() |
| 125 | if(absl_gtest_download_url) |
| 126 | set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src) |
| 127 | else() |
| 128 | set(absl_gtest_src_dir ${ABSL_LOCAL_GOOGLETEST_DIR}) |
| 129 | endif() |
| 130 | include(CMake/Googletest/DownloadGTest.cmake) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 131 | endif() |
| 132 | |
| 133 | check_target(gtest) |
| 134 | check_target(gtest_main) |
| 135 | check_target(gmock) |
| 136 | |
| 137 | list(APPEND ABSL_TEST_COMMON_LIBRARIES |
| 138 | gtest_main |
| 139 | gtest |
| 140 | gmock |
| 141 | ${CMAKE_THREAD_LIBS_INIT} |
| 142 | ) |
| 143 | endif() |
| 144 | |
| 145 | add_subdirectory(absl) |
| 146 | |
| 147 | if(ABSL_ENABLE_INSTALL) |
| 148 | # absl:lts-remove-begin(system installation is supported for LTS releases) |
| 149 | # We don't support system-wide installation |
| 150 | list(APPEND SYSTEM_INSTALL_DIRS "/usr/local" "/usr" "/opt/" "/opt/local" "c:/Program Files/${PROJECT_NAME}") |
| 151 | if(NOT DEFINED CMAKE_INSTALL_PREFIX OR CMAKE_INSTALL_PREFIX IN_LIST SYSTEM_INSTALL_DIRS) |
| 152 | message(WARNING "\ |
| 153 | The default and system-level install directories are unsupported except in LTS \ |
| 154 | releases of Abseil. Please set CMAKE_INSTALL_PREFIX to install Abseil in your \ |
| 155 | source or build tree directly.\ |
| 156 | ") |
| 157 | endif() |
| 158 | # absl:lts-remove-end |
| 159 | |
| 160 | # install as a subdirectory only |
| 161 | install(EXPORT ${PROJECT_NAME}Targets |
| 162 | NAMESPACE absl:: |
| 163 | DESTINATION "${ABSL_INSTALL_CONFIGDIR}" |
| 164 | ) |
| 165 | |
| 166 | configure_package_config_file( |
| 167 | CMake/abslConfig.cmake.in |
| 168 | "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" |
| 169 | INSTALL_DESTINATION "${ABSL_INSTALL_CONFIGDIR}" |
| 170 | ) |
| 171 | install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" |
| 172 | DESTINATION "${ABSL_INSTALL_CONFIGDIR}" |
| 173 | ) |
| 174 | |
| 175 | # Abseil only has a version in LTS releases. This mechanism is accomplished |
| 176 | # Abseil's internal Copybara (https://github.com/google/copybara) workflows and |
| 177 | # isn't visible in the CMake buildsystem itself. |
| 178 | if(absl_VERSION) |
| 179 | write_basic_package_version_file( |
| 180 | "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" |
| 181 | COMPATIBILITY ExactVersion |
| 182 | ) |
| 183 | |
| 184 | install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" |
| 185 | DESTINATION ${ABSL_INSTALL_CONFIGDIR} |
| 186 | ) |
| 187 | endif() # absl_VERSION |
| 188 | |
| 189 | install(DIRECTORY absl |
| 190 | DESTINATION ${ABSL_INSTALL_INCLUDEDIR} |
| 191 | FILES_MATCHING |
| 192 | PATTERN "*.inc" |
| 193 | PATTERN "*.h" |
Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 194 | PATTERN "copts" EXCLUDE |
| 195 | PATTERN "testdata" EXCLUDE |
| 196 | ) |
Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame] | 197 | endif() # ABSL_ENABLE_INSTALL |