AbseilHelpers.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. include(CMakeParseArguments)
  17. include(AbseilConfigureCopts)
  18. include(AbseilDll)
  19. # The IDE folder for Abseil that will be used if Abseil is included in a CMake
  20. # project that sets
  21. # set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  22. # For example, Visual Studio supports folders.
  23. if(NOT DEFINED ABSL_IDE_FOLDER)
  24. set(ABSL_IDE_FOLDER Abseil)
  25. endif()
  26. # absl_cc_library()
  27. #
  28. # CMake function to imitate Bazel's cc_library rule.
  29. #
  30. # Parameters:
  31. # NAME: name of target (see Note)
  32. # HDRS: List of public header files for the library
  33. # SRCS: List of source files for the library
  34. # DEPS: List of other libraries to be linked in to the binary targets
  35. # COPTS: List of private compile options
  36. # DEFINES: List of public defines
  37. # LINKOPTS: List of link options
  38. # PUBLIC: Add this so that this library will be exported under absl::
  39. # Also in IDE, target will appear in Abseil folder while non PUBLIC will be in Abseil/internal.
  40. # TESTONLY: When added, this target will only be built if BUILD_TESTING=ON.
  41. #
  42. # Note:
  43. # By default, absl_cc_library will always create a library named absl_${NAME},
  44. # and alias target absl::${NAME}. The absl:: form should always be used.
  45. # This is to reduce namespace pollution.
  46. #
  47. # absl_cc_library(
  48. # NAME
  49. # awesome
  50. # HDRS
  51. # "a.h"
  52. # SRCS
  53. # "a.cc"
  54. # )
  55. # absl_cc_library(
  56. # NAME
  57. # fantastic_lib
  58. # SRCS
  59. # "b.cc"
  60. # DEPS
  61. # absl::awesome # not "awesome" !
  62. # PUBLIC
  63. # )
  64. #
  65. # absl_cc_library(
  66. # NAME
  67. # main_lib
  68. # ...
  69. # DEPS
  70. # absl::fantastic_lib
  71. # )
  72. #
  73. # TODO: Implement "ALWAYSLINK"
  74. function(absl_cc_library)
  75. cmake_parse_arguments(ABSL_CC_LIB
  76. "DISABLE_INSTALL;PUBLIC;TESTONLY"
  77. "NAME"
  78. "HDRS;SRCS;COPTS;DEFINES;LINKOPTS;DEPS"
  79. ${ARGN}
  80. )
  81. if(ABSL_CC_LIB_TESTONLY AND NOT BUILD_TESTING)
  82. return()
  83. endif()
  84. if(ABSL_ENABLE_INSTALL)
  85. set(_NAME "${ABSL_CC_LIB_NAME}")
  86. else()
  87. set(_NAME "absl_${ABSL_CC_LIB_NAME}")
  88. endif()
  89. # Check if this is a header-only library
  90. # Note that as of February 2019, many popular OS's (for example, Ubuntu
  91. # 16.04 LTS) only come with cmake 3.5 by default. For this reason, we can't
  92. # use list(FILTER...)
  93. set(ABSL_CC_SRCS "${ABSL_CC_LIB_SRCS}")
  94. foreach(src_file IN LISTS ABSL_CC_SRCS)
  95. if(${src_file} MATCHES ".*\\.(h|inc)")
  96. list(REMOVE_ITEM ABSL_CC_SRCS "${src_file}")
  97. endif()
  98. endforeach()
  99. if(ABSL_CC_SRCS STREQUAL "")
  100. set(ABSL_CC_LIB_IS_INTERFACE 1)
  101. else()
  102. set(ABSL_CC_LIB_IS_INTERFACE 0)
  103. endif()
  104. # Determine this build target's relationship to the DLL. It's one of four things:
  105. # 1. "dll" -- This target is part of the DLL
  106. # 2. "dll_dep" -- This target is not part of the DLL, but depends on the DLL.
  107. # Note that we assume any target not in the DLL depends on the
  108. # DLL. This is not a technical necessity but a convenience
  109. # which happens to be true, because nearly every target is
  110. # part of the DLL.
  111. # 3. "shared" -- This is a shared library, perhaps on a non-windows platform
  112. # where DLL doesn't make sense.
  113. # 4. "static" -- This target does not depend on the DLL and should be built
  114. # statically.
  115. if (${ABSL_BUILD_DLL})
  116. if(ABSL_ENABLE_INSTALL)
  117. absl_internal_dll_contains(TARGET ${_NAME} OUTPUT _in_dll)
  118. else()
  119. absl_internal_dll_contains(TARGET ${ABSL_CC_LIB_NAME} OUTPUT _in_dll)
  120. endif()
  121. if (${_in_dll})
  122. # This target should be replaced by the DLL
  123. set(_build_type "dll")
  124. set(ABSL_CC_LIB_IS_INTERFACE 1)
  125. else()
  126. # Building a DLL, but this target is not part of the DLL
  127. set(_build_type "dll_dep")
  128. endif()
  129. elseif(BUILD_SHARED_LIBS)
  130. set(_build_type "shared")
  131. else()
  132. set(_build_type "static")
  133. endif()
  134. # Generate a pkg-config file for every library:
  135. if((_build_type STREQUAL "static" OR _build_type STREQUAL "shared")
  136. AND ABSL_ENABLE_INSTALL)
  137. if(NOT ABSL_CC_LIB_TESTONLY)
  138. if(absl_VERSION)
  139. set(PC_VERSION "${absl_VERSION}")
  140. else()
  141. set(PC_VERSION "head")
  142. endif()
  143. foreach(dep ${ABSL_CC_LIB_DEPS})
  144. if(${dep} MATCHES "^absl::(.*)")
  145. # Join deps with commas.
  146. if(PC_DEPS)
  147. set(PC_DEPS "${PC_DEPS},")
  148. endif()
  149. set(PC_DEPS "${PC_DEPS} absl_${CMAKE_MATCH_1} = ${PC_VERSION}")
  150. endif()
  151. endforeach()
  152. foreach(cflag ${ABSL_CC_LIB_COPTS})
  153. if(${cflag} MATCHES "^(-Wno|/wd)")
  154. # These flags are needed to suppress warnings that might fire in our headers.
  155. set(PC_CFLAGS "${PC_CFLAGS} ${cflag}")
  156. elseif(${cflag} MATCHES "^(-W|/w[1234eo])")
  157. # Don't impose our warnings on others.
  158. else()
  159. set(PC_CFLAGS "${PC_CFLAGS} ${cflag}")
  160. endif()
  161. endforeach()
  162. FILE(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/lib/pkgconfig/absl_${_NAME}.pc" CONTENT "\
  163. prefix=${CMAKE_INSTALL_PREFIX}\n\
  164. exec_prefix=\${prefix}\n\
  165. libdir=${CMAKE_INSTALL_FULL_LIBDIR}\n\
  166. includedir=${CMAKE_INSTALL_FULL_INCLUDEDIR}\n\
  167. \n\
  168. Name: absl_${_NAME}\n\
  169. Description: Abseil ${_NAME} library\n\
  170. URL: https://abseil.io/\n\
  171. Version: ${PC_VERSION}\n\
  172. Requires:${PC_DEPS}\n\
  173. Libs: -L\${libdir} $<JOIN:${ABSL_CC_LIB_LINKOPTS}, > $<$<NOT:$<BOOL:${ABSL_CC_LIB_IS_INTERFACE}>>:-labsl_${_NAME}>\n\
  174. Cflags: -I\${includedir}${PC_CFLAGS}\n")
  175. INSTALL(FILES "${CMAKE_BINARY_DIR}/lib/pkgconfig/absl_${_NAME}.pc"
  176. DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  177. endif()
  178. endif()
  179. if(NOT ABSL_CC_LIB_IS_INTERFACE)
  180. if(_build_type STREQUAL "dll_dep")
  181. # This target depends on the DLL. When adding dependencies to this target,
  182. # any depended-on-target which is contained inside the DLL is replaced
  183. # with a dependency on the DLL.
  184. add_library(${_NAME} STATIC "")
  185. target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS})
  186. absl_internal_dll_targets(
  187. DEPS ${ABSL_CC_LIB_DEPS}
  188. OUTPUT _dll_deps
  189. )
  190. target_link_libraries(${_NAME}
  191. PUBLIC ${_dll_deps}
  192. PRIVATE
  193. ${ABSL_CC_LIB_LINKOPTS}
  194. ${ABSL_DEFAULT_LINKOPTS}
  195. )
  196. if (ABSL_CC_LIB_TESTONLY)
  197. set(_gtest_link_define "GTEST_LINKED_AS_SHARED_LIBRARY=1")
  198. else()
  199. set(_gtest_link_define)
  200. endif()
  201. target_compile_definitions(${_NAME}
  202. PUBLIC
  203. ABSL_CONSUME_DLL
  204. "${_gtest_link_define}"
  205. )
  206. elseif(_build_type STREQUAL "static" OR _build_type STREQUAL "shared")
  207. add_library(${_NAME} "")
  208. target_sources(${_NAME} PRIVATE ${ABSL_CC_LIB_SRCS} ${ABSL_CC_LIB_HDRS})
  209. target_link_libraries(${_NAME}
  210. PUBLIC ${ABSL_CC_LIB_DEPS}
  211. PRIVATE
  212. ${ABSL_CC_LIB_LINKOPTS}
  213. ${ABSL_DEFAULT_LINKOPTS}
  214. )
  215. else()
  216. message(FATAL_ERROR "Invalid build type: ${_build_type}")
  217. endif()
  218. # Linker language can be inferred from sources, but in the case of DLLs we
  219. # don't have any .cc files so it would be ambiguous. We could set it
  220. # explicitly only in the case of DLLs but, because "CXX" is always the
  221. # correct linker language for static or for shared libraries, we set it
  222. # unconditionally.
  223. set_property(TARGET ${_NAME} PROPERTY LINKER_LANGUAGE "CXX")
  224. target_include_directories(${_NAME}
  225. PUBLIC
  226. "$<BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS}>"
  227. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  228. )
  229. target_compile_options(${_NAME}
  230. PRIVATE ${ABSL_CC_LIB_COPTS})
  231. target_compile_definitions(${_NAME} PUBLIC ${ABSL_CC_LIB_DEFINES})
  232. # Add all Abseil targets to a a folder in the IDE for organization.
  233. if(ABSL_CC_LIB_PUBLIC)
  234. set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER})
  235. elseif(ABSL_CC_LIB_TESTONLY)
  236. set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test)
  237. else()
  238. set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/internal)
  239. endif()
  240. if(ABSL_PROPAGATE_CXX_STD)
  241. # Abseil libraries require C++11 as the current minimum standard.
  242. # Top-level application CMake projects should ensure a consistent C++
  243. # standard for all compiled sources by setting CMAKE_CXX_STANDARD.
  244. target_compile_features(${_NAME} PUBLIC cxx_std_11)
  245. else()
  246. # Note: This is legacy (before CMake 3.8) behavior. Setting the
  247. # target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is
  248. # initialized by CMAKE_CXX_STANDARD) should have no real effect, since
  249. # that is the default value anyway.
  250. #
  251. # CXX_STANDARD_REQUIRED does guard against the top-level CMake project
  252. # not having enabled CMAKE_CXX_STANDARD_REQUIRED (which prevents
  253. # "decaying" to an older standard if the requested one isn't available).
  254. set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD})
  255. set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
  256. endif()
  257. # When being installed, we lose the absl_ prefix. We want to put it back
  258. # to have properly named lib files. This is a no-op when we are not being
  259. # installed.
  260. if(ABSL_ENABLE_INSTALL)
  261. set_target_properties(${_NAME} PROPERTIES
  262. OUTPUT_NAME "absl_${_NAME}"
  263. SOVERSION "2111.0.0"
  264. )
  265. endif()
  266. else()
  267. # Generating header-only library
  268. add_library(${_NAME} INTERFACE)
  269. target_include_directories(${_NAME}
  270. INTERFACE
  271. "$<BUILD_INTERFACE:${ABSL_COMMON_INCLUDE_DIRS}>"
  272. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  273. )
  274. if (_build_type STREQUAL "dll")
  275. set(ABSL_CC_LIB_DEPS abseil_dll)
  276. endif()
  277. target_link_libraries(${_NAME}
  278. INTERFACE
  279. ${ABSL_CC_LIB_DEPS}
  280. ${ABSL_CC_LIB_LINKOPTS}
  281. ${ABSL_DEFAULT_LINKOPTS}
  282. )
  283. target_compile_definitions(${_NAME} INTERFACE ${ABSL_CC_LIB_DEFINES})
  284. if(ABSL_PROPAGATE_CXX_STD)
  285. # Abseil libraries require C++11 as the current minimum standard.
  286. # Top-level application CMake projects should ensure a consistent C++
  287. # standard for all compiled sources by setting CMAKE_CXX_STANDARD.
  288. target_compile_features(${_NAME} INTERFACE cxx_std_11)
  289. # (INTERFACE libraries can't have the CXX_STANDARD property set, so there
  290. # is no legacy behavior else case).
  291. endif()
  292. endif()
  293. # TODO currently we don't install googletest alongside abseil sources, so
  294. # installed abseil can't be tested.
  295. if(NOT ABSL_CC_LIB_TESTONLY AND ABSL_ENABLE_INSTALL)
  296. install(TARGETS ${_NAME} EXPORT ${PROJECT_NAME}Targets
  297. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  298. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  299. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  300. )
  301. endif()
  302. add_library(absl::${ABSL_CC_LIB_NAME} ALIAS ${_NAME})
  303. endfunction()
  304. # absl_cc_test()
  305. #
  306. # CMake function to imitate Bazel's cc_test rule.
  307. #
  308. # Parameters:
  309. # NAME: name of target (see Usage below)
  310. # SRCS: List of source files for the binary
  311. # DEPS: List of other libraries to be linked in to the binary targets
  312. # COPTS: List of private compile options
  313. # DEFINES: List of public defines
  314. # LINKOPTS: List of link options
  315. #
  316. # Note:
  317. # By default, absl_cc_test will always create a binary named absl_${NAME}.
  318. # This will also add it to ctest list as absl_${NAME}.
  319. #
  320. # Usage:
  321. # absl_cc_library(
  322. # NAME
  323. # awesome
  324. # HDRS
  325. # "a.h"
  326. # SRCS
  327. # "a.cc"
  328. # PUBLIC
  329. # )
  330. #
  331. # absl_cc_test(
  332. # NAME
  333. # awesome_test
  334. # SRCS
  335. # "awesome_test.cc"
  336. # DEPS
  337. # absl::awesome
  338. # GTest::gmock
  339. # GTest::gtest_main
  340. # )
  341. function(absl_cc_test)
  342. if(NOT BUILD_TESTING)
  343. return()
  344. endif()
  345. cmake_parse_arguments(ABSL_CC_TEST
  346. ""
  347. "NAME"
  348. "SRCS;COPTS;DEFINES;LINKOPTS;DEPS"
  349. ${ARGN}
  350. )
  351. set(_NAME "absl_${ABSL_CC_TEST_NAME}")
  352. add_executable(${_NAME} "")
  353. target_sources(${_NAME} PRIVATE ${ABSL_CC_TEST_SRCS})
  354. target_include_directories(${_NAME}
  355. PUBLIC ${ABSL_COMMON_INCLUDE_DIRS}
  356. PRIVATE ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS}
  357. )
  358. if (${ABSL_BUILD_DLL})
  359. target_compile_definitions(${_NAME}
  360. PUBLIC
  361. ${ABSL_CC_TEST_DEFINES}
  362. ABSL_CONSUME_DLL
  363. GTEST_LINKED_AS_SHARED_LIBRARY=1
  364. )
  365. # Replace dependencies on targets inside the DLL with abseil_dll itself.
  366. absl_internal_dll_targets(
  367. DEPS ${ABSL_CC_TEST_DEPS}
  368. OUTPUT ABSL_CC_TEST_DEPS
  369. )
  370. else()
  371. target_compile_definitions(${_NAME}
  372. PUBLIC
  373. ${ABSL_CC_TEST_DEFINES}
  374. )
  375. endif()
  376. target_compile_options(${_NAME}
  377. PRIVATE ${ABSL_CC_TEST_COPTS}
  378. )
  379. target_link_libraries(${_NAME}
  380. PUBLIC ${ABSL_CC_TEST_DEPS}
  381. PRIVATE ${ABSL_CC_TEST_LINKOPTS}
  382. )
  383. # Add all Abseil targets to a folder in the IDE for organization.
  384. set_property(TARGET ${_NAME} PROPERTY FOLDER ${ABSL_IDE_FOLDER}/test)
  385. if(ABSL_PROPAGATE_CXX_STD)
  386. # Abseil libraries require C++11 as the current minimum standard.
  387. # Top-level application CMake projects should ensure a consistent C++
  388. # standard for all compiled sources by setting CMAKE_CXX_STANDARD.
  389. target_compile_features(${_NAME} PUBLIC cxx_std_11)
  390. else()
  391. # Note: This is legacy (before CMake 3.8) behavior. Setting the
  392. # target-level CXX_STANDARD property to ABSL_CXX_STANDARD (which is
  393. # initialized by CMAKE_CXX_STANDARD) should have no real effect, since
  394. # that is the default value anyway.
  395. #
  396. # CXX_STANDARD_REQUIRED does guard against the top-level CMake project
  397. # not having enabled CMAKE_CXX_STANDARD_REQUIRED (which prevents
  398. # "decaying" to an older standard if the requested one isn't available).
  399. set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD ${ABSL_CXX_STANDARD})
  400. set_property(TARGET ${_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
  401. endif()
  402. add_test(NAME ${_NAME} COMMAND ${_NAME})
  403. endfunction()
  404. function(check_target my_target)
  405. if(NOT TARGET ${my_target})
  406. message(FATAL_ERROR " ABSL: compiling absl requires a ${my_target} CMake target in your project,
  407. see CMake/README.md for more details")
  408. endif(NOT TARGET ${my_target})
  409. endfunction()