• 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# Set BUILD_TESTING to OFF by default.
45# This must come before the project() and include(CTest) lines.
46OPTION(BUILD_TESTING "Build tests" OFF)
47
48project(absl LANGUAGES CXX VERSION 20210324)
49include(CTest)
50
51# Output directory is correct by default for most build setups. However, when
52# building Abseil as a DLL, it is important to have the DLL in the same
53# directory as the executable using it. Thus, we put all executables in a single
54# /bin directory.
55set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
56
57# when absl is included as subproject (i.e. using add_subdirectory(abseil-cpp))
58# in the source tree of a project that uses it, install rules are disabled.
59if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
60  option(ABSL_ENABLE_INSTALL "Enable install rule" OFF)
61else()
62  option(ABSL_ENABLE_INSTALL "Enable install rule" ON)
63endif()
64
65list(APPEND CMAKE_MODULE_PATH
66  ${CMAKE_CURRENT_LIST_DIR}/CMake
67  ${CMAKE_CURRENT_LIST_DIR}/absl/copts
68)
69
70include(CMakePackageConfigHelpers)
71include(GNUInstallDirs)
72include(AbseilDll)
73include(AbseilHelpers)
74
75
76##
77## Using absl targets
78##
79## all public absl targets are
80## exported with the absl:: prefix
81##
82## e.g absl::base absl::synchronization absl::strings ....
83##
84## DO NOT rely on the internal targets outside of the prefix
85
86
87# include current path
88list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
89
90if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
91  set(ABSL_USING_CLANG ON)
92else()
93  set(ABSL_USING_CLANG OFF)
94endif()
95
96# find dependencies
97## pthread
98find_package(Threads REQUIRED)
99
100option(ABSL_USE_EXTERNAL_GOOGLETEST
101  "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)
102
103option(ABSL_USE_GOOGLETEST_HEAD
104  "If ON, abseil will download HEAD from GoogleTest at config time." OFF)
105
106set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL")
107
108set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH
109  "If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout."
110  )
111
112if(BUILD_TESTING)
113  ## check targets
114  if (NOT ABSL_USE_EXTERNAL_GOOGLETEST)
115    set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build)
116    if(ABSL_USE_GOOGLETEST_HEAD AND ABSL_GOOGLETEST_DOWNLOAD_URL)
117      message(FATAL_ERROR "Do not set both ABSL_USE_GOOGLETEST_HEAD and ABSL_GOOGLETEST_DOWNLOAD_URL")
118    endif()
119    if(ABSL_USE_GOOGLETEST_HEAD)
120      set(absl_gtest_download_url "https://github.com/google/googletest/archive/master.zip")
121    elseif(ABSL_GOOGLETEST_DOWNLOAD_URL)
122      set(absl_gtest_download_url ${ABSL_GOOGLETEST_DOWNLOAD_URL})
123    endif()
124    if(absl_gtest_download_url)
125      set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src)
126    else()
127      set(absl_gtest_src_dir ${ABSL_LOCAL_GOOGLETEST_DIR})
128    endif()
129    include(CMake/Googletest/DownloadGTest.cmake)
130  endif()
131
132  check_target(gtest)
133  check_target(gtest_main)
134  check_target(gmock)
135
136  list(APPEND ABSL_TEST_COMMON_LIBRARIES
137    gtest_main
138    gtest
139    gmock
140    ${CMAKE_THREAD_LIBS_INIT}
141  )
142endif()
143
144add_subdirectory(absl)
145
146if(ABSL_ENABLE_INSTALL)
147
148
149  # install as a subdirectory only
150  install(EXPORT ${PROJECT_NAME}Targets
151    NAMESPACE absl::
152    DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
153  )
154
155  configure_package_config_file(
156    CMake/abslConfig.cmake.in
157    "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
158    INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
159  )
160  install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
161    DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
162  )
163
164  # Abseil only has a version in LTS releases.  This mechanism is accomplished
165  # Abseil's internal Copybara (https://github.com/google/copybara) workflows and
166  # isn't visible in the CMake buildsystem itself.
167  if(absl_VERSION)
168    write_basic_package_version_file(
169      "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
170      COMPATIBILITY ExactVersion
171    )
172
173    install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
174      DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
175    )
176  endif()  # absl_VERSION
177
178  install(DIRECTORY absl
179    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
180    FILES_MATCHING
181      PATTERN "*.inc"
182      PATTERN "*.h"
183      PATTERN "copts" EXCLUDE
184      PATTERN "testdata" EXCLUDE
185    )
186endif()  # ABSL_ENABLE_INSTALL
187