You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
15 KiB

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