blob: 750a47557d201a3eafd5151dcd0b1cf1b4a94b83 [file] [log] [blame]
Adrien Devressed5134a72017-09-26 20:37:27 -07001#
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#
nik727338b70432019-03-08 10:27:53 -05008# https://www.apache.org/licenses/LICENSE-2.0
Adrien Devressed5134a72017-09-26 20:37:27 -07009#
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 Team59ae4d52018-05-18 08:24:54 -070016
Abseil Team256be562019-03-12 11:39:15 -070017# 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)
Abseil Team44b0faf2018-12-04 11:01:12 -080023
24# Compiler id for Apple Clang is now AppleClang.
Abseil Team3c2bed22020-07-20 09:08:19 -070025if (POLICY CMP0025)
26 cmake_policy(SET CMP0025 NEW)
27endif (POLICY CMP0025)
Abseil Team44b0faf2018-12-04 11:01:12 -080028
Abseil Teambf294702019-03-19 11:14:01 -070029# if command can use IN_LIST
Abseil Team3c2bed22020-07-20 09:08:19 -070030if (POLICY CMP0057)
31 cmake_policy(SET CMP0057 NEW)
32endif (POLICY CMP0057)
Abseil Teambf294702019-03-19 11:14:01 -070033
Abseil Teama877af12020-03-10 09:28:06 -070034# Project version variables are the empty string if version is unspecified
Abseil Team3c2bed22020-07-20 09:08:19 -070035if (POLICY CMP0048)
36 cmake_policy(SET CMP0048 NEW)
37endif (POLICY CMP0048)
Abseil Team5b65c4a2019-03-27 08:05:41 -070038
Abseil Teamf6247902020-07-13 13:22:02 -070039# option() honor variables
Abseil Team3c2bed22020-07-20 09:08:19 -070040if (POLICY CMP0077)
41 cmake_policy(SET CMP0077 NEW)
42endif (POLICY CMP0077)
Abseil Teamf6247902020-07-13 13:22:02 -070043
WolverinDEV2d418972021-03-30 18:44:11 +020044# Allow the user to specify the MSVC runtime
45if (POLICY CMP0091)
46 cmake_policy(SET CMP0091 NEW)
47endif (POLICY CMP0091)
48
Abseil Teama50ae362021-02-22 13:18:10 -080049# 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
Derek Mauro6a722792021-11-02 16:49:07 -040053project(absl LANGUAGES CXX VERSION 20211102)
Abseil Teama50ae362021-02-22 13:18:10 -080054include(CTest)
Adrien Devressed5134a72017-09-26 20:37:27 -070055
Abseil Team37dd2562020-01-28 11:50:11 -080056# 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
Abseil Teambf294702019-03-19 11:14:01 -070062# 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 Team22771d42021-01-20 12:39:22 -080064if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
Abseil Teamf6247902020-07-13 13:22:02 -070065 option(ABSL_ENABLE_INSTALL "Enable install rule" OFF)
Abseil Teambf294702019-03-19 11:14:01 -070066else()
Abseil Teamf6247902020-07-13 13:22:02 -070067 option(ABSL_ENABLE_INSTALL "Enable install rule" ON)
Abseil Teambf294702019-03-19 11:14:01 -070068endif()
69
Eric Barndollaree0ebda2021-07-29 21:33:51 -040070option(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
Abseil Team284378a2018-12-05 12:37:41 -080077list(APPEND CMAKE_MODULE_PATH
78 ${CMAKE_CURRENT_LIST_DIR}/CMake
79 ${CMAKE_CURRENT_LIST_DIR}/absl/copts
80)
Adrien Devressed5134a72017-09-26 20:37:27 -070081
Abseil Teambf294702019-03-19 11:14:01 -070082include(CMakePackageConfigHelpers)
Abseil Teamdcf48992021-03-17 20:08:11 -070083include(GNUInstallDirs)
Abseil Team37dd2562020-01-28 11:50:11 -080084include(AbseilDll)
Adrien Devressed5134a72017-09-26 20:37:27 -070085include(AbseilHelpers)
86
87
Adrien Devressed5134a72017-09-26 20:37:27 -070088##
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
Abseil Team44b0faf2018-12-04 11:01:12 -0800102if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
103 set(ABSL_USING_CLANG ON)
104else()
105 set(ABSL_USING_CLANG OFF)
106endif()
107
Adrien Devressed5134a72017-09-26 20:37:27 -0700108# find dependencies
109## pthread
110find_package(Threads REQUIRED)
111
Florin Crișan8f921752021-06-10 02:26:40 +0300112include(CMakeDependentOption)
113
Adrian Imbodenc01b9912020-04-07 02:50:57 +0200114option(ABSL_USE_EXTERNAL_GOOGLETEST
Abseil Team73ea9a92020-04-06 20:53:47 -0700115 "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șan8f921752021-06-10 02:26:40 +0300117cmake_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 Team26b789f2018-05-04 09:58:56 -0700124option(ABSL_USE_GOOGLETEST_HEAD
Abseil Teameb317a72020-10-21 00:01:29 -0700125 "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")
Abseil Team26b789f2018-05-04 09:58:56 -0700128
Abseil Teambcefbdc2020-02-28 12:18:28 -0800129set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH
Abseil Teameb317a72020-10-21 00:01:29 -0700130 "If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout."
Abseil Teambcefbdc2020-02-28 12:18:28 -0800131 )
132
Abseil Teama50ae362021-02-22 13:18:10 -0800133if(BUILD_TESTING)
Abseil Team2069dc72020-05-28 12:55:24 -0700134 ## check targets
Florin Crișan8f921752021-06-10 02:26:40 +0300135 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 Imbodenc01b9912020-04-07 02:50:57 +0200144 set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build)
Abseil Teameb317a72020-10-21 00:01:29 -0700145 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 Imbodenc01b9912020-04-07 02:50:57 +0200154 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 Team26b789f2018-05-04 09:58:56 -0700159 endif()
160
Florin Crișan8f921752021-06-10 02:26:40 +0300161 if (NOT ABSL_FIND_GOOGLETEST)
162 # When Google Test is included directly rather than through find_package, the aliases are missing.
Florin Crișan8f921752021-06-10 02:26:40 +0300163 add_library(GTest::gtest ALIAS gtest)
Abseil Teama2f52e12021-10-18 08:24:39 -0700164 add_library(GTest::gtest_main ALIAS gtest_main)
Florin Crișan8f921752021-06-10 02:26:40 +0300165 add_library(GTest::gmock ALIAS gmock)
Abseil Teama2f52e12021-10-18 08:24:39 -0700166 add_library(GTest::gmock_main ALIAS gmock_main)
Florin Crișan8f921752021-06-10 02:26:40 +0300167 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 Gershman765541c2017-12-23 00:25:36 +0200173endif()
Adrien Devressed5134a72017-09-26 20:37:27 -0700174
175add_subdirectory(absl)
Abseil Teambf294702019-03-19 11:14:01 -0700176
Abseil Team2c8421e2019-03-28 11:12:19 -0700177if(ABSL_ENABLE_INSTALL)
Derek Mauro6a722792021-11-02 16:49:07 -0400178
Abseil Team2c8421e2019-03-28 11:12:19 -0700179
Abseil Team5b65c4a2019-03-27 08:05:41 -0700180 # install as a subdirectory only
181 install(EXPORT ${PROJECT_NAME}Targets
182 NAMESPACE absl::
Abseil Teamdcf48992021-03-17 20:08:11 -0700183 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
Abseil Team5b65c4a2019-03-27 08:05:41 -0700184 )
Abseil Teambf294702019-03-19 11:14:01 -0700185
Abseil Team5b65c4a2019-03-27 08:05:41 -0700186 configure_package_config_file(
187 CMake/abslConfig.cmake.in
188 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
Abseil Teamdcf48992021-03-17 20:08:11 -0700189 INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
Abseil Team5b65c4a2019-03-27 08:05:41 -0700190 )
191 install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
Abseil Teamdcf48992021-03-17 20:08:11 -0700192 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
Abseil Team5b65c4a2019-03-27 08:05:41 -0700193 )
Abseil Teambf294702019-03-19 11:14:01 -0700194
Abseil Team5b65c4a2019-03-27 08:05:41 -0700195 # 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 Team2c8421e2019-03-28 11:12:19 -0700198 if(absl_VERSION)
Abseil Team5b65c4a2019-03-27 08:05:41 -0700199 write_basic_package_version_file(
200 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
201 COMPATIBILITY ExactVersion
202 )
Abseil Teambf294702019-03-19 11:14:01 -0700203
Abseil Team5b65c4a2019-03-27 08:05:41 -0700204 install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
Abseil Teamdcf48992021-03-17 20:08:11 -0700205 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
Abseil Team5b65c4a2019-03-27 08:05:41 -0700206 )
207 endif() # absl_VERSION
208
209 install(DIRECTORY absl
Abseil Teamdcf48992021-03-17 20:08:11 -0700210 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
Abseil Team5b65c4a2019-03-27 08:05:41 -0700211 FILES_MATCHING
212 PATTERN "*.inc"
213 PATTERN "*.h"
Po-Chuan Hsieh8faf2042020-06-03 02:18:31 +0800214 PATTERN "copts" EXCLUDE
215 PATTERN "testdata" EXCLUDE
Abseil Teamda3a8762020-06-02 11:09:12 -0700216 )
Abseil Team5b65c4a2019-03-27 08:05:41 -0700217endif() # ABSL_ENABLE_INSTALL