internal_utils.cmake 14 KB

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