blob: c1ae8d56f3388a36e08e31b111306590d6d4cf56 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -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#
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.
Austin Schuhb4691e92020-12-31 12:37:18 -080025if (POLICY CMP0025)
26 cmake_policy(SET CMP0025 NEW)
27endif (POLICY CMP0025)
Austin Schuh36244a12019-09-21 17:52:38 -070028
29# if command can use IN_LIST
Austin Schuhb4691e92020-12-31 12:37:18 -080030if (POLICY CMP0057)
31 cmake_policy(SET CMP0057 NEW)
32endif (POLICY CMP0057)
Austin Schuh36244a12019-09-21 17:52:38 -070033
Austin Schuhb4691e92020-12-31 12:37:18 -080034# 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)
Austin Schuh36244a12019-09-21 17:52:38 -070043
44project(absl CXX)
45
Austin Schuhb4691e92020-12-31 12:37:18 -080046# 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.
50set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
51
Austin Schuh36244a12019-09-21 17:52:38 -070052# 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.
54if(NOT "^${CMAKE_SOURCE_DIR}$" STREQUAL "^${PROJECT_SOURCE_DIR}$")
Austin Schuhb4691e92020-12-31 12:37:18 -080055 option(ABSL_ENABLE_INSTALL "Enable install rule" OFF)
Austin Schuh36244a12019-09-21 17:52:38 -070056else()
Austin Schuhb4691e92020-12-31 12:37:18 -080057 option(ABSL_ENABLE_INSTALL "Enable install rule" ON)
Austin Schuh36244a12019-09-21 17:52:38 -070058endif()
59
60list(APPEND CMAKE_MODULE_PATH
61 ${CMAKE_CURRENT_LIST_DIR}/CMake
62 ${CMAKE_CURRENT_LIST_DIR}/absl/copts
63)
64
65include(AbseilInstallDirs)
66include(CMakePackageConfigHelpers)
Austin Schuhb4691e92020-12-31 12:37:18 -080067include(AbseilDll)
Austin Schuh36244a12019-09-21 17:52:38 -070068include(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
83list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
84
85if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
86 set(ABSL_USING_CLANG ON)
87else()
88 set(ABSL_USING_CLANG OFF)
89endif()
90
91# find dependencies
92## pthread
93find_package(Threads REQUIRED)
94
Austin Schuhb4691e92020-12-31 12:37:18 -080095option(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 Schuh36244a12019-09-21 17:52:38 -070098option(ABSL_USE_GOOGLETEST_HEAD
Austin Schuhb4691e92020-12-31 12:37:18 -080099 "If ON, abseil will download HEAD from GoogleTest at config time." OFF)
100
101set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL")
102
103set(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 Schuh36244a12019-09-21 17:52:38 -0700106
107option(ABSL_RUN_TESTS "If ON, Abseil tests will be run." OFF)
108
109if(${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 Schuh36244a12019-09-21 17:52:38 -0700113
Austin Schuhb4691e92020-12-31 12:37:18 -0800114 ## check targets
115 if (NOT ABSL_USE_EXTERNAL_GOOGLETEST)
Austin Schuh36244a12019-09-21 17:52:38 -0700116 set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build)
Austin Schuhb4691e92020-12-31 12:37:18 -0800117 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 Schuh36244a12019-09-21 17:52:38 -0700131 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 )
143endif()
144
145add_subdirectory(absl)
146
147if(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 Schuhb4691e92020-12-31 12:37:18 -0800194 PATTERN "copts" EXCLUDE
195 PATTERN "testdata" EXCLUDE
196 )
Austin Schuh36244a12019-09-21 17:52:38 -0700197endif() # ABSL_ENABLE_INSTALL