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
44project(absl CXX)
45
46# 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
52# 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}$")
55  option(ABSL_ENABLE_INSTALL "Enable install rule" OFF)
56else()
57  option(ABSL_ENABLE_INSTALL "Enable install rule" ON)
58endif()
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)
67include(AbseilDll)
68include(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
95option(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
98
99option(ABSL_USE_GOOGLETEST_HEAD
100  "If ON, abseil will download HEAD from googletest at config time." OFF)
101
102set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH
103  "If ABSL_USE_GOOGLETEST_HEAD is OFF, specifies the directory of a local googletest checkout."
104  )
105
106option(ABSL_RUN_TESTS "If ON, Abseil tests will be run." OFF)
107
108if(${ABSL_RUN_TESTS})
109  # enable CTest.  This will set BUILD_TESTING to ON unless otherwise specified
110  # on the command line
111  include(CTest)
112
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})
117      set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src)
118    else()
119      set(absl_gtest_src_dir ${ABSL_LOCAL_GOOGLETEST_DIR})
120    endif()
121    include(CMake/Googletest/DownloadGTest.cmake)
122  endif()
123
124  check_target(gtest)
125  check_target(gtest_main)
126  check_target(gmock)
127
128  list(APPEND ABSL_TEST_COMMON_LIBRARIES
129    gtest_main
130    gtest
131    gmock
132    ${CMAKE_THREAD_LIBS_INIT}
133  )
134endif()
135
136add_subdirectory(absl)
137
138if(ABSL_ENABLE_INSTALL)
139
140  # install as a subdirectory only
141  install(EXPORT ${PROJECT_NAME}Targets
142    NAMESPACE absl::
143    DESTINATION "${ABSL_INSTALL_CONFIGDIR}"
144  )
145
146  configure_package_config_file(
147    CMake/abslConfig.cmake.in
148    "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
149    INSTALL_DESTINATION "${ABSL_INSTALL_CONFIGDIR}"
150  )
151  install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
152    DESTINATION "${ABSL_INSTALL_CONFIGDIR}"
153  )
154
155  # Abseil only has a version in LTS releases.  This mechanism is accomplished
156  # Abseil's internal Copybara (https://github.com/google/copybara) workflows and
157  # isn't visible in the CMake buildsystem itself.
158  if(absl_VERSION)
159    write_basic_package_version_file(
160      "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
161      COMPATIBILITY ExactVersion
162    )
163
164    install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
165      DESTINATION ${ABSL_INSTALL_CONFIGDIR}
166    )
167  endif()  # absl_VERSION
168
169  install(DIRECTORY absl
170    DESTINATION ${ABSL_INSTALL_INCLUDEDIR}
171    FILES_MATCHING
172      PATTERN "*.inc"
173      PATTERN "*.h"
174      PATTERN "copts" EXCLUDE
175      PATTERN "testdata" EXCLUDE
176    )
177endif()  # ABSL_ENABLE_INSTALL
178