internal_utils.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. # Defines functions and macros useful for building Google Test and
  2. # Google Mock.
  3. #
  4. # Note:
  5. #
  6. # - This file will be run twice when building Google Mock (once via
  7. # Google Test's CMakeLists.txt, and once via Google Mock's).
  8. # Therefore it shouldn't have any side effects other than defining
  9. # the functions and macros.
  10. #
  11. # - The functions/macros defined in this file may depend on Google
  12. # Test and Google Mock's option() definitions, and thus must be
  13. # called *after* the options have been defined.
  14. if (POLICY CMP0054)
  15. cmake_policy(SET CMP0054 NEW)
  16. endif (POLICY CMP0054)
  17. # Tweaks CMake's default compiler/linker settings to suit Google Test's needs.
  18. #
  19. # This must be a macro(), as inside a function string() can only
  20. # update variables in the function scope.
  21. macro(fix_default_compiler_settings_)
  22. if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC|Clang")
  23. # For MSVC and Clang, CMake sets certain flags to defaults we want to
  24. # override.
  25. # This replacement code is taken from sample in the CMake Wiki at
  26. # https://gitlab.kitware.com/cmake/community/wikis/FAQ#dynamic-replace.
  27. foreach (flag_var
  28. CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
  29. CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
  30. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  31. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  32. if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt)
  33. # When Google Test is built as a shared library, it should also use
  34. # shared runtime libraries. Otherwise, it may end up with multiple
  35. # copies of runtime library data in different modules, resulting in
  36. # hard-to-find crashes. When it is built as a static library, it is
  37. # preferable to use CRT as static libraries, as we don't have to rely
  38. # on CRT DLLs being available. CMake always defaults to using shared
  39. # CRT libraries, so we override that default here.
  40. string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
  41. # When using Ninja with Clang, static builds pass -D_DLL on Windows.
  42. # This is incorrect and should not happen, so we fix that here.
  43. string(REPLACE "-D_DLL" "" ${flag_var} "${${flag_var}}")
  44. endif()
  45. # We prefer more strict warning checking for building Google Test.
  46. # Replaces /W3 with /W4 in defaults.
  47. string(REPLACE "/W3" "/W4" ${flag_var} "${${flag_var}}")
  48. # Prevent D9025 warning for targets that have exception handling
  49. # turned off (/EHs-c- flag). Where required, exceptions are explicitly
  50. # re-enabled using the cxx_exception_flags variable.
  51. string(REPLACE "/EHsc" "" ${flag_var} "${${flag_var}}")
  52. endforeach()
  53. endif()
  54. endmacro()
  55. # Defines the compiler/linker flags used to build Google Test and
  56. # Google Mock. You can tweak these definitions to suit your need. A
  57. # variable's value is empty before it's explicitly assigned to.
  58. macro(config_compiler_and_linker)
  59. # Note: pthreads on MinGW is not supported, even if available
  60. # instead, we use windows threading primitives
  61. unset(GTEST_HAS_PTHREAD)
  62. if (NOT gtest_disable_pthreads AND NOT MINGW)
  63. # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
  64. find_package(Threads)
  65. if (CMAKE_USE_PTHREADS_INIT)
  66. set(GTEST_HAS_PTHREAD ON)
  67. endif()
  68. endif()
  69. fix_default_compiler_settings_()
  70. if (MSVC)
  71. # Newlines inside flags variables break CMake's NMake generator.
  72. # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds.
  73. set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J")
  74. set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
  75. set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
  76. set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
  77. set(cxx_no_exception_flags "-EHs-c- -D_HAS_EXCEPTIONS=0")
  78. set(cxx_no_rtti_flags "-GR-")
  79. # Suppress "unreachable code" warning
  80. # http://stackoverflow.com/questions/3232669 explains the issue.
  81. set(cxx_base_flags "${cxx_base_flags} -wd4702")
  82. # Ensure MSVC treats source files as UTF-8 encoded.
  83. if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
  84. set(cxx_base_flags "${cxx_base_flags} -utf-8")
  85. endif()
  86. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  87. set(cxx_base_flags "-Wall -Wshadow -Wconversion")
  88. set(cxx_exception_flags "-fexceptions")
  89. set(cxx_no_exception_flags "-fno-exceptions")
  90. set(cxx_strict_flags "-W -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wunused-parameter -Wcast-align -Wchar-subscripts -Winline -Wredundant-decls")
  91. set(cxx_no_rtti_flags "-fno-rtti")
  92. elseif (CMAKE_COMPILER_IS_GNUCXX)
  93. set(cxx_base_flags "-Wall -Wshadow")
  94. if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)
  95. set(cxx_base_flags "${cxx_base_flags} -Wno-error=dangling-else")
  96. endif()
  97. set(cxx_exception_flags "-fexceptions")
  98. set(cxx_no_exception_flags "-fno-exceptions")
  99. # Until version 4.3.2, GCC doesn't define a macro to indicate
  100. # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
  101. # explicitly.
  102. set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
  103. set(cxx_strict_flags
  104. "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
  105. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
  106. set(cxx_exception_flags "-features=except")
  107. # Sun Pro doesn't provide macros to indicate whether exceptions and
  108. # RTTI are enabled, so we define GTEST_HAS_* explicitly.
  109. set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
  110. set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
  111. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
  112. CMAKE_CXX_COMPILER_ID STREQUAL "XL")
  113. # CMake 2.8 changes Visual Age's compiler ID to "XL".
  114. set(cxx_exception_flags "-qeh")
  115. set(cxx_no_exception_flags "-qnoeh")
  116. # Until version 9.0, Visual Age doesn't define a macro to indicate
  117. # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
  118. # explicitly.
  119. set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
  120. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
  121. set(cxx_base_flags "-AA -mt")
  122. set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1")
  123. set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0")
  124. # RTTI can not be disabled in HP aCC compiler.
  125. set(cxx_no_rtti_flags "")
  126. endif()
  127. # The pthreads library is available and allowed?
  128. if (DEFINED GTEST_HAS_PTHREAD)
  129. set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=1")
  130. else()
  131. set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=0")
  132. endif()
  133. set(cxx_base_flags "${cxx_base_flags} ${GTEST_HAS_PTHREAD_MACRO}")
  134. # For building gtest's own tests and samples.
  135. set(cxx_exception "${cxx_base_flags} ${cxx_exception_flags}")
  136. set(cxx_no_exception
  137. "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
  138. set(cxx_default "${cxx_exception}")
  139. set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
  140. # For building the gtest libraries.
  141. set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
  142. endmacro()
  143. # Defines the gtest & gtest_main libraries. User tests should link
  144. # with one of them.
  145. function(cxx_library_with_type name type cxx_flags)
  146. # type can be either STATIC or SHARED to denote a static or shared library.
  147. # ARGN refers to additional arguments after 'cxx_flags'.
  148. add_library(${name} ${type} ${ARGN})
  149. add_library(${cmake_package_name}::${name} ALIAS ${name})
  150. set_target_properties(${name}
  151. PROPERTIES
  152. COMPILE_FLAGS "${cxx_flags}")
  153. # Set the output directory for build artifacts
  154. set_target_properties(${name}
  155. PROPERTIES
  156. RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
  157. LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
  158. ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
  159. PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
  160. COMPILE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
  161. # make PDBs match library name
  162. get_target_property(pdb_debug_postfix ${name} DEBUG_POSTFIX)
  163. set_target_properties(${name}
  164. PROPERTIES
  165. PDB_NAME "${name}"
  166. PDB_NAME_DEBUG "${name}${pdb_debug_postfix}"
  167. COMPILE_PDB_NAME "${name}"
  168. COMPILE_PDB_NAME_DEBUG "${name}${pdb_debug_postfix}")
  169. if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
  170. set_target_properties(${name}
  171. PROPERTIES
  172. COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
  173. if (NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
  174. target_compile_definitions(${name} INTERFACE
  175. $<INSTALL_INTERFACE:GTEST_LINKED_AS_SHARED_LIBRARY=1>)
  176. endif()
  177. endif()
  178. if (DEFINED GTEST_HAS_PTHREAD)
  179. if ("${CMAKE_VERSION}" VERSION_LESS "3.1.0")
  180. set(threads_spec ${CMAKE_THREAD_LIBS_INIT})
  181. else()
  182. set(threads_spec Threads::Threads)
  183. endif()
  184. target_link_libraries(${name} PUBLIC ${threads_spec})
  185. endif()
  186. if (NOT "${CMAKE_VERSION}" VERSION_LESS "3.8")
  187. target_compile_features(${name} PUBLIC cxx_std_14)
  188. endif()
  189. endfunction()
  190. ########################################################################
  191. #
  192. # Helper functions for creating build targets.
  193. function(cxx_shared_library name cxx_flags)
  194. cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
  195. endfunction()
  196. function(cxx_library name cxx_flags)
  197. cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN})
  198. endfunction()
  199. # cxx_executable_with_flags(name cxx_flags libs srcs...)
  200. #
  201. # creates a named C++ executable that depends on the given libraries and
  202. # is built from the given source files with the given compiler flags.
  203. function(cxx_executable_with_flags name cxx_flags libs)
  204. add_executable(${name} ${ARGN})
  205. if (MSVC)
  206. # BigObj required for tests.
  207. set(cxx_flags "${cxx_flags} -bigobj")
  208. endif()
  209. if (cxx_flags)
  210. set_target_properties(${name}
  211. PROPERTIES
  212. COMPILE_FLAGS "${cxx_flags}")
  213. endif()
  214. if (BUILD_SHARED_LIBS)
  215. set_target_properties(${name}
  216. PROPERTIES
  217. COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
  218. endif()
  219. # To support mixing linking in static and dynamic libraries, link each
  220. # library in with an extra call to target_link_libraries.
  221. foreach (lib "${libs}")
  222. target_link_libraries(${name} ${lib})
  223. endforeach()
  224. endfunction()
  225. # cxx_executable(name dir lib srcs...)
  226. #
  227. # creates a named target that depends on the given libs and is built
  228. # from the given source files. dir/name.cc is implicitly included in
  229. # the source file list.
  230. function(cxx_executable name dir libs)
  231. cxx_executable_with_flags(
  232. ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
  233. endfunction()
  234. # CMP0094 policy enables finding a Python executable in the LOCATION order, as
  235. # specified by the PATH environment variable.
  236. if (POLICY CMP0094)
  237. cmake_policy(SET CMP0094 NEW)
  238. endif()
  239. # Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
  240. if ("${CMAKE_VERSION}" VERSION_LESS "3.12.0")
  241. find_package(PythonInterp)
  242. else()
  243. find_package(Python COMPONENTS Interpreter)
  244. set(PYTHONINTERP_FOUND ${Python_Interpreter_FOUND})
  245. set(PYTHON_EXECUTABLE ${Python_EXECUTABLE})
  246. endif()
  247. # cxx_test_with_flags(name cxx_flags libs srcs...)
  248. #
  249. # creates a named C++ test that depends on the given libs and is built
  250. # from the given source files with the given compiler flags.
  251. function(cxx_test_with_flags name cxx_flags libs)
  252. cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
  253. add_test(NAME ${name} COMMAND "$<TARGET_FILE:${name}>")
  254. endfunction()
  255. # cxx_test(name libs srcs...)
  256. #
  257. # creates a named test target that depends on the given libs and is
  258. # built from the given source files. Unlike cxx_test_with_flags,
  259. # test/name.cc is already implicitly included in the source file list.
  260. function(cxx_test name libs)
  261. cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
  262. "test/${name}.cc" ${ARGN})
  263. endfunction()
  264. # py_test(name)
  265. #
  266. # creates a Python test with the given name whose main module is in
  267. # test/name.py. It does nothing if Python is not installed.
  268. function(py_test name)
  269. if (PYTHONINTERP_FOUND)
  270. if ("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_GREATER 3.1)
  271. if (CMAKE_CONFIGURATION_TYPES)
  272. # Multi-configuration build generators as for Visual Studio save
  273. # output in a subdirectory of CMAKE_CURRENT_BINARY_DIR (Debug,
  274. # Release etc.), so we have to provide it here.
  275. add_test(NAME ${name}
  276. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  277. --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN})
  278. else (CMAKE_CONFIGURATION_TYPES)
  279. # Single-configuration build generators like Makefile generators
  280. # don't have subdirs below CMAKE_CURRENT_BINARY_DIR.
  281. add_test(NAME ${name}
  282. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  283. --build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN})
  284. endif (CMAKE_CONFIGURATION_TYPES)
  285. else()
  286. # ${CMAKE_CURRENT_BINARY_DIR} is known at configuration time, so we can
  287. # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
  288. # only at ctest runtime (by calling ctest -c <Configuration>), so
  289. # we have to escape $ to delay variable substitution here.
  290. add_test(NAME ${name}
  291. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  292. --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN})
  293. endif()
  294. # Make the Python import path consistent between Bazel and CMake.
  295. set_tests_properties(${name} PROPERTIES ENVIRONMENT PYTHONPATH=${CMAKE_SOURCE_DIR})
  296. endif(PYTHONINTERP_FOUND)
  297. endfunction()
  298. # install_project(targets...)
  299. #
  300. # Installs the specified targets and configures the associated pkgconfig files.
  301. function(install_project)
  302. if(INSTALL_GTEST)
  303. install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/"
  304. DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
  305. # Install the project targets.
  306. install(TARGETS ${ARGN}
  307. EXPORT ${targets_export_name}
  308. RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
  309. ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  310. LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
  311. if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  312. # Install PDBs
  313. foreach(t ${ARGN})
  314. get_target_property(t_pdb_name ${t} COMPILE_PDB_NAME)
  315. get_target_property(t_pdb_name_debug ${t} COMPILE_PDB_NAME_DEBUG)
  316. get_target_property(t_pdb_output_directory ${t} PDB_OUTPUT_DIRECTORY)
  317. install(FILES
  318. "${t_pdb_output_directory}/\${CMAKE_INSTALL_CONFIG_NAME}/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb"
  319. DESTINATION ${CMAKE_INSTALL_LIBDIR}
  320. OPTIONAL)
  321. endforeach()
  322. endif()
  323. # Configure and install pkgconfig files.
  324. foreach(t ${ARGN})
  325. set(configured_pc "${generated_dir}/${t}.pc")
  326. configure_file("${PROJECT_SOURCE_DIR}/cmake/${t}.pc.in"
  327. "${configured_pc}" @ONLY)
  328. install(FILES "${configured_pc}"
  329. DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  330. endforeach()
  331. endif()
  332. endfunction()