CMakeLists.txt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. # Most widely used distributions have cmake 3.5 or greater available as of March
  17. # 2019. A notable exception is RHEL-7 (CentOS7). You can install a current
  18. # version of CMake by first installing Extra Packages for Enterprise Linux
  19. # (https://fedoraproject.org/wiki/EPEL#Extra_Packages_for_Enterprise_Linux_.28EPEL.29)
  20. # and then issuing `yum install cmake3` on the command line.
  21. cmake_minimum_required(VERSION 3.5)
  22. # Compiler id for Apple Clang is now AppleClang.
  23. if (POLICY CMP0025)
  24. cmake_policy(SET CMP0025 NEW)
  25. endif (POLICY CMP0025)
  26. # if command can use IN_LIST
  27. if (POLICY CMP0057)
  28. cmake_policy(SET CMP0057 NEW)
  29. endif (POLICY CMP0057)
  30. # Project version variables are the empty string if version is unspecified
  31. if (POLICY CMP0048)
  32. cmake_policy(SET CMP0048 NEW)
  33. endif (POLICY CMP0048)
  34. # option() honor variables
  35. if (POLICY CMP0077)
  36. cmake_policy(SET CMP0077 NEW)
  37. endif (POLICY CMP0077)
  38. # Allow the user to specify the MSVC runtime
  39. if (POLICY CMP0091)
  40. cmake_policy(SET CMP0091 NEW)
  41. endif (POLICY CMP0091)
  42. project(absl LANGUAGES CXX)
  43. include(CTest)
  44. # Output directory is correct by default for most build setups. However, when
  45. # building Abseil as a DLL, it is important to have the DLL in the same
  46. # directory as the executable using it. Thus, we put all executables in a single
  47. # /bin directory.
  48. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  49. # when absl is included as subproject (i.e. using add_subdirectory(abseil-cpp))
  50. # in the source tree of a project that uses it, install rules are disabled.
  51. if(NOT CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
  52. option(ABSL_ENABLE_INSTALL "Enable install rule" OFF)
  53. else()
  54. option(ABSL_ENABLE_INSTALL "Enable install rule" ON)
  55. endif()
  56. option(ABSL_PROPAGATE_CXX_STD
  57. "Use CMake C++ standard meta features (e.g. cxx_std_14) that propagate to targets that link to Abseil"
  58. OFF) # TODO: Default to ON for CMake 3.8 and greater.
  59. if((${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.8) AND (NOT ABSL_PROPAGATE_CXX_STD))
  60. 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.")
  61. endif()
  62. option(ABSL_USE_SYSTEM_INCLUDES
  63. "Silence warnings in Abseil headers by marking them as SYSTEM includes"
  64. OFF)
  65. list(APPEND CMAKE_MODULE_PATH
  66. ${CMAKE_CURRENT_LIST_DIR}/CMake
  67. ${CMAKE_CURRENT_LIST_DIR}/absl/copts
  68. )
  69. include(CMakePackageConfigHelpers)
  70. include(GNUInstallDirs)
  71. include(AbseilDll)
  72. include(AbseilHelpers)
  73. ##
  74. ## Using absl targets
  75. ##
  76. ## all public absl targets are
  77. ## exported with the absl:: prefix
  78. ##
  79. ## e.g absl::base absl::synchronization absl::strings ....
  80. ##
  81. ## DO NOT rely on the internal targets outside of the prefix
  82. # include current path
  83. list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
  84. if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  85. set(ABSL_USING_CLANG ON)
  86. else()
  87. set(ABSL_USING_CLANG OFF)
  88. endif()
  89. # find dependencies
  90. ## pthread
  91. find_package(Threads REQUIRED)
  92. include(CMakeDependentOption)
  93. option(ABSL_BUILD_TESTING
  94. "If ON, Abseil will build all of Abseil's own tests." OFF)
  95. option(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_subdirectory." OFF)
  97. cmake_dependent_option(ABSL_FIND_GOOGLETEST
  98. "If ON, Abseil will use find_package(GTest) rather than assuming that GoogleTest is already provided by the including project."
  99. ON
  100. "ABSL_USE_EXTERNAL_GOOGLETEST"
  101. OFF)
  102. option(ABSL_USE_GOOGLETEST_HEAD
  103. "If ON, abseil will download HEAD from GoogleTest at config time." OFF)
  104. set(ABSL_GOOGLETEST_DOWNLOAD_URL "" CACHE STRING "If set, download GoogleTest from this URL")
  105. set(ABSL_LOCAL_GOOGLETEST_DIR "/usr/src/googletest" CACHE PATH
  106. "If ABSL_USE_GOOGLETEST_HEAD is OFF and ABSL_GOOGLETEST_URL is not set, specifies the directory of a local GoogleTest checkout."
  107. )
  108. if(BUILD_TESTING AND ABSL_BUILD_TESTING)
  109. ## check targets
  110. if (ABSL_USE_EXTERNAL_GOOGLETEST)
  111. if (ABSL_FIND_GOOGLETEST)
  112. find_package(GTest REQUIRED)
  113. elseif(NOT TARGET GTest::gtest)
  114. if(TARGET gtest)
  115. # When Google Test is included directly rather than through find_package, the aliases are missing.
  116. add_library(GTest::gtest ALIAS gtest)
  117. add_library(GTest::gtest_main ALIAS gtest_main)
  118. add_library(GTest::gmock ALIAS gmock)
  119. add_library(GTest::gmock_main ALIAS gmock_main)
  120. else()
  121. 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.")
  122. endif()
  123. endif()
  124. else()
  125. set(absl_gtest_build_dir ${CMAKE_BINARY_DIR}/googletest-build)
  126. if(ABSL_USE_GOOGLETEST_HEAD AND ABSL_GOOGLETEST_DOWNLOAD_URL)
  127. message(FATAL_ERROR "Do not set both ABSL_USE_GOOGLETEST_HEAD and ABSL_GOOGLETEST_DOWNLOAD_URL")
  128. endif()
  129. if(ABSL_USE_GOOGLETEST_HEAD)
  130. set(absl_gtest_download_url "https://github.com/google/googletest/archive/main.zip")
  131. elseif(ABSL_GOOGLETEST_DOWNLOAD_URL)
  132. set(absl_gtest_download_url ${ABSL_GOOGLETEST_DOWNLOAD_URL})
  133. endif()
  134. if(absl_gtest_download_url)
  135. set(absl_gtest_src_dir ${CMAKE_BINARY_DIR}/googletest-src)
  136. else()
  137. set(absl_gtest_src_dir ${ABSL_LOCAL_GOOGLETEST_DIR})
  138. endif()
  139. include(CMake/Googletest/DownloadGTest.cmake)
  140. endif()
  141. check_target(GTest::gtest)
  142. check_target(GTest::gtest_main)
  143. check_target(GTest::gmock)
  144. check_target(GTest::gmock_main)
  145. endif()
  146. add_subdirectory(absl)
  147. if(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. # install as a subdirectory only
  160. install(EXPORT ${PROJECT_NAME}Targets
  161. NAMESPACE absl::
  162. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  163. )
  164. configure_package_config_file(
  165. CMake/abslConfig.cmake.in
  166. "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  167. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  168. )
  169. install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  170. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  171. )
  172. # Abseil only has a version in LTS releases. This mechanism is accomplished
  173. # Abseil's internal Copybara (https://github.com/google/copybara) workflows and
  174. # isn't visible in the CMake buildsystem itself.
  175. if(absl_VERSION)
  176. write_basic_package_version_file(
  177. "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
  178. COMPATIBILITY ExactVersion
  179. )
  180. install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
  181. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  182. )
  183. endif() # absl_VERSION
  184. install(DIRECTORY absl
  185. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
  186. FILES_MATCHING
  187. PATTERN "*.inc"
  188. PATTERN "*.h"
  189. PATTERN "copts" EXCLUDE
  190. PATTERN "testdata" EXCLUDE
  191. )
  192. endif() # ABSL_ENABLE_INSTALL