• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
22cmake_minimum_required(VERSION 3.5)
23
24# Compiler id for Apple Clang is now AppleClang.
25if (POLICY CMP0025)
26  cmake_policy(SET CMP0025 NEW)
27endif (POLICY CMP0025)
28
29# if command can use IN_LIST
30if (POLICY CMP0057)
31  cmake_policy(SET CMP0057 NEW)
32endif (POLICY CMP0057)
33
34# 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)
43
44# Allow the user to specify the MSVC runtime
45if (POLICY CMP0091)
46  cmake_policy(SET CMP0091 NEW)
47endif (POLICY CMP0091)
48
49project(absl LANGUAGES CXX)
50include(CTest)
51
52# Output directory is correct by default for most build setups. However, when
53# building Abseil as a DLL, it is important to have the DLL in the same
54# directory as the executable using it. Thus, we put all executables in a single
55# /bin directory.
56set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
57
58# when absl is included as subproject (i.e. using add_subdirectory(abseil-cpp))
59# in the source tree of a project that uses it, install rules are disabled.
60if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
61  option(ABSL_ENABLE_INSTALL "Enable install rule" OFF)
62else()
63  option(ABSL_ENABLE_INSTALL "Enable install rule" ON)
64endif()
65
66option(ABSL_PROPAGATE_CXX_STD
67  "Use CMake C++ standard meta features (e.g. cxx_std_14) that propagate to targets that link to Abseil"
68  OFF)  # TODO: Default to ON for CMake 3.8 and greater.
69if((${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.8) AND (NOT ABSL_PROPAGATE_CXX_STD))
70  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.")
71endif()
72
73option(ABSL_USE_SYSTEM_INCLUDES
74  "Silence warnings in Abseil headers by marking them as SYSTEM includes"
75  OFF)
76
77list(APPEND CMAKE_MODULE_PATH
78  ${CMAKE_CURRENT_LIST_DIR}/CMake
79  ${CMAKE_CURRENT_LIST_DIR}/absl/copts
80)
81
82include(CMakePackageConfigHelpers)
83include(GNUInstallDirs)
84include(AbseilDll)
85include(AbseilHelpers)
86
87
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
100list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
101
102if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
103  set(ABSL_USING_CLANG ON)
104else()
105  set(ABSL_USING_CLANG OFF)
106endif()
107
108# find dependencies
109## pthread
110find_package(Threads REQUIRED)
111
112include(CMakeDependentOption)
113
114option(ABSL_BUILD_TESTING
115  "If ON, Abseil will build all of Abseil's own tests." OFF)
116
117option(ABSL_USE_EXTERNAL_GOOGLETEST
118  "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_subdirectory." OFF)
119
120cmake_dependent_option(ABSL_FIND_GOOGLETEST
121  "If ON, Abseil will use find_package(GTest) rather than assuming that GoogleTest is already provided by the including project."
122  ON
123  "ABSL_USE_EXTERNAL_GOOGLETEST"
124  OFF)
125
126
127option(ABSL_USE_GOOGLETEST_HEAD
128  "If ON, abseil will download HEAD from GoogleTest at config time." OFF)
129
130set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL")
131
132set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH
133  "If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout."
134  )
135
136if(BUILD_TESTING AND ABSL_BUILD_TESTING)
137  ## check targets
138  if (ABSL_USE_EXTERNAL_GOOGLETEST)
139    if (ABSL_FIND_GOOGLETEST)
140      find_package(GTest REQUIRED)
141    elseif(NOT TARGET GTest::gtest)
142      if(TARGET gtest)
143        # When Google Test is included directly rather than through find_package, the aliases are missing.
144        add_library(GTest::gtest ALIAS gtest)
145        add_library(GTest::gtest_main ALIAS gtest_main)
146        add_library(GTest::gmock ALIAS gmock)
147        add_library(GTest::gmock_main ALIAS gmock_main)
148      else()
149        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.")
150      endif()
151    endif()
152  else()
153    set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build)
154    if(ABSL_USE_GOOGLETEST_HEAD AND ABSL_GOOGLETEST_DOWNLOAD_URL)
155      message(FATAL_ERROR "Do not set both ABSL_USE_GOOGLETEST_HEAD and ABSL_GOOGLETEST_DOWNLOAD_URL")
156    endif()
157    if(ABSL_USE_GOOGLETEST_HEAD)
158      set(absl_gtest_download_url "https://github.com/google/googletest/archive/main.zip")
159    elseif(ABSL_GOOGLETEST_DOWNLOAD_URL)
160      set(absl_gtest_download_url ${ABSL_GOOGLETEST_DOWNLOAD_URL})
161    endif()
162    if(absl_gtest_download_url)
163      set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src)
164    else()
165      set(absl_gtest_src_dir ${ABSL_LOCAL_GOOGLETEST_DIR})
166    endif()
167    include(CMake/Googletest/DownloadGTest.cmake)
168  endif()
169
170  check_target(GTest::gtest)
171  check_target(GTest::gtest_main)
172  check_target(GTest::gmock)
173  check_target(GTest::gmock_main)
174endif()
175
176add_subdirectory(absl)
177
178if(ABSL_ENABLE_INSTALL)
179  # absl:lts-remove-begin(system installation is supported for LTS releases)
180  # We don't support system-wide installation
181  list(APPEND SYSTEM_INSTALL_DIRS "/usr/local" "/usr" "/opt/" "/opt/local" "c:/Program Files/${PROJECT_NAME}")
182  if(NOT DEFINED CMAKE_INSTALL_PREFIX OR CMAKE_INSTALL_PREFIX IN_LIST SYSTEM_INSTALL_DIRS)
183    message(WARNING "\
184  The default and system-level install directories are unsupported except in LTS \
185  releases of Abseil.  Please set CMAKE_INSTALL_PREFIX to install Abseil in your \
186  source or build tree directly.\
187    ")
188  endif()
189  # absl:lts-remove-end
190
191  # install as a subdirectory only
192  install(EXPORT ${PROJECT_NAME}Targets
193    NAMESPACE absl::
194    DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
195  )
196
197  configure_package_config_file(
198    CMake/abslConfig.cmake.in
199    "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
200    INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
201  )
202  install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
203    DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
204  )
205
206  # Abseil only has a version in LTS releases.  This mechanism is accomplished
207  # Abseil's internal Copybara (https://github.com/google/copybara) workflows and
208  # isn't visible in the CMake buildsystem itself.
209  if(absl_VERSION)
210    write_basic_package_version_file(
211      "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
212      COMPATIBILITY ExactVersion
213    )
214
215    install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
216      DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
217    )
218  endif()  # absl_VERSION
219
220  install(DIRECTORY absl
221    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
222    FILES_MATCHING
223      PATTERN "*.inc"
224      PATTERN "*.h"
225      PATTERN "copts" EXCLUDE
226      PATTERN "testdata" EXCLUDE
227    )
228endif()  # ABSL_ENABLE_INSTALL
229