blob: 86f56344e74ea7c2fe0edd0a47adfce2b9bda29d [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.
25cmake_policy(SET CMP0025 NEW)
26
27# if command can use IN_LIST
28cmake_policy(SET CMP0057 NEW)
29
30# Project version variables are the empty std::string if version is unspecified
31cmake_policy(SET CMP0048 NEW)
32
33project(absl CXX)
34
35# when absl is included as subproject (i.e. using add_subdirectory(abseil-cpp))
36# in the source tree of a project that uses it, install rules are disabled.
37if(NOT "^${CMAKE_SOURCE_DIR}$" STREQUAL "^${PROJECT_SOURCE_DIR}$")
38 set(ABSL_ENABLE_INSTALL FALSE)
39else()
40 set(ABSL_ENABLE_INSTALL TRUE)
41endif()
42
43list(APPEND CMAKE_MODULE_PATH
44 ${CMAKE_CURRENT_LIST_DIR}/CMake
45 ${CMAKE_CURRENT_LIST_DIR}/absl/copts
46)
47
48include(AbseilInstallDirs)
49include(CMakePackageConfigHelpers)
50include(AbseilHelpers)
51
52
53##
54## Using absl targets
55##
56## all public absl targets are
57## exported with the absl:: prefix
58##
59## e.g absl::base absl::synchronization absl::strings ....
60##
61## DO NOT rely on the internal targets outside of the prefix
62
63
64# include current path
65list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
66
67if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
68 set(ABSL_USING_CLANG ON)
69else()
70 set(ABSL_USING_CLANG OFF)
71endif()
72
73# find dependencies
74## pthread
75find_package(Threads REQUIRED)
76
77option(ABSL_USE_GOOGLETEST_HEAD
78 "If ON, abseil will download HEAD from googletest at config time." OFF)
79
80option(ABSL_RUN_TESTS "If ON, Abseil tests will be run." OFF)
81
82if(${ABSL_RUN_TESTS})
83 # enable CTest. This will set BUILD_TESTING to ON unless otherwise specified
84 # on the command line
85 include(CTest)
86 enable_testing()
87endif()
88
89## check targets
90if(BUILD_TESTING)
91
92 if(${ABSL_USE_GOOGLETEST_HEAD})
93 include(CMake/Googletest/DownloadGTest.cmake)
94 set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src)
95 set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build)
96 endif()
97
98 check_target(gtest)
99 check_target(gtest_main)
100 check_target(gmock)
101
102 list(APPEND ABSL_TEST_COMMON_LIBRARIES
103 gtest_main
104 gtest
105 gmock
106 ${CMAKE_THREAD_LIBS_INIT}
107 )
108endif()
109
110add_subdirectory(absl)
111
112if(ABSL_ENABLE_INSTALL)
113 # absl:lts-remove-begin(system installation is supported for LTS releases)
114 # We don't support system-wide installation
115 list(APPEND SYSTEM_INSTALL_DIRS "/usr/local" "/usr" "/opt/" "/opt/local" "c:/Program Files/${PROJECT_NAME}")
116 if(NOT DEFINED CMAKE_INSTALL_PREFIX OR CMAKE_INSTALL_PREFIX IN_LIST SYSTEM_INSTALL_DIRS)
117 message(WARNING "\
118 The default and system-level install directories are unsupported except in LTS \
119 releases of Abseil. Please set CMAKE_INSTALL_PREFIX to install Abseil in your \
120 source or build tree directly.\
121 ")
122 endif()
123 # absl:lts-remove-end
124
125 # install as a subdirectory only
126 install(EXPORT ${PROJECT_NAME}Targets
127 NAMESPACE absl::
128 DESTINATION "${ABSL_INSTALL_CONFIGDIR}"
129 )
130
131 configure_package_config_file(
132 CMake/abslConfig.cmake.in
133 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
134 INSTALL_DESTINATION "${ABSL_INSTALL_CONFIGDIR}"
135 )
136 install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
137 DESTINATION "${ABSL_INSTALL_CONFIGDIR}"
138 )
139
140 # Abseil only has a version in LTS releases. This mechanism is accomplished
141 # Abseil's internal Copybara (https://github.com/google/copybara) workflows and
142 # isn't visible in the CMake buildsystem itself.
143 if(absl_VERSION)
144 write_basic_package_version_file(
145 "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
146 COMPATIBILITY ExactVersion
147 )
148
149 install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
150 DESTINATION ${ABSL_INSTALL_CONFIGDIR}
151 )
152 endif() # absl_VERSION
153
154 install(DIRECTORY absl
155 DESTINATION ${ABSL_INSTALL_INCLUDEDIR}
156 FILES_MATCHING
157 PATTERN "*.inc"
158 PATTERN "*.h"
159 )
160endif() # ABSL_ENABLE_INSTALL