internal_utils.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. message(STATUS "hello mt")
  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 -Zi")
  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. # http://stackoverflow.com/questions/3232669 explains the issue.
  78. set(cxx_base_flags "${cxx_base_flags} -wd4702")
  79. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  80. set(cxx_base_flags "-Wall -Wshadow -Werror -Wconversion")
  81. set(cxx_exception_flags "-fexceptions")
  82. set(cxx_no_exception_flags "-fno-exceptions")
  83. set(cxx_strict_flags "-W -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wunused-parameter -Wcast-align -Wchar-subscripts -Winline -Wredundant-decls")
  84. set(cxx_no_rtti_flags "-fno-rtti")
  85. elseif (CMAKE_COMPILER_IS_GNUCXX)
  86. set(cxx_base_flags "-Wall -Wshadow -Werror")
  87. if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)
  88. set(cxx_base_flags "${cxx_base_flags} -Wno-error=dangling-else")
  89. endif()
  90. set(cxx_exception_flags "-fexceptions")
  91. set(cxx_no_exception_flags "-fno-exceptions")
  92. # Until version 4.3.2, GCC doesn't define a macro to indicate
  93. # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
  94. # explicitly.
  95. set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
  96. set(cxx_strict_flags
  97. "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
  98. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
  99. set(cxx_exception_flags "-features=except")
  100. # Sun Pro doesn't provide macros to indicate whether exceptions and
  101. # RTTI are enabled, so we define GTEST_HAS_* explicitly.
  102. set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
  103. set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
  104. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
  105. CMAKE_CXX_COMPILER_ID STREQUAL "XL")
  106. # CMake 2.8 changes Visual Age's compiler ID to "XL".
  107. set(cxx_exception_flags "-qeh")
  108. set(cxx_no_exception_flags "-qnoeh")
  109. # Until version 9.0, Visual Age doesn't define a macro to indicate
  110. # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
  111. # explicitly.
  112. set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
  113. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
  114. set(cxx_base_flags "-AA -mt")
  115. set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1")
  116. set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0")
  117. # RTTI can not be disabled in HP aCC compiler.
  118. set(cxx_no_rtti_flags "")
  119. endif()
  120. # The pthreads library is available and allowed?
  121. if (DEFINED GTEST_HAS_PTHREAD)
  122. set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=1")
  123. else()
  124. set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=0")
  125. endif()
  126. set(cxx_base_flags "${cxx_base_flags} ${GTEST_HAS_PTHREAD_MACRO}")
  127. # For building gtest's own tests and samples.
  128. set(cxx_exception "${cxx_base_flags} ${cxx_exception_flags}")
  129. set(cxx_no_exception
  130. "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
  131. set(cxx_default "${cxx_exception}")
  132. set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
  133. # For building the gtest libraries.
  134. set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
  135. endmacro()
  136. # Defines the gtest & gtest_main libraries. User tests should link
  137. # with one of them.
  138. function(cxx_library_with_type name type cxx_flags)
  139. # type can be either STATIC or SHARED to denote a static or shared library.
  140. # ARGN refers to additional arguments after 'cxx_flags'.
  141. add_library(${name} ${type} ${ARGN})
  142. set_target_properties(${name}
  143. PROPERTIES
  144. COMPILE_FLAGS "${cxx_flags}")
  145. # Generate debug library name with a postfix.
  146. set_target_properties(${name}
  147. PROPERTIES
  148. DEBUG_POSTFIX "d")
  149. # Set the output directory for build artifacts
  150. # rewrite it for RVC build procedure @Gifur
  151. set_target_properties(${name}
  152. PROPERTIES
  153. RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
  154. LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}"
  155. ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}"
  156. PDB_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
  157. # make PDBs match library name
  158. get_target_property(pdb_debug_postfix ${name} DEBUG_POSTFIX)
  159. set_target_properties(${name}
  160. PROPERTIES
  161. PDB_NAME "${name}"
  162. PDB_NAME_DEBUG "${name}${pdb_debug_postfix}"
  163. COMPILE_PDB_NAME "${name}"
  164. COMPILE_PDB_NAME_DEBUG "${name}${pdb_debug_postfix}")
  165. if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
  166. set_target_properties(${name}
  167. PROPERTIES
  168. COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
  169. if (NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
  170. target_compile_definitions(${name} INTERFACE
  171. $<INSTALL_INTERFACE:GTEST_LINKED_AS_SHARED_LIBRARY=1>)
  172. endif()
  173. endif()
  174. if (DEFINED GTEST_HAS_PTHREAD)
  175. if ("${CMAKE_VERSION}" VERSION_LESS "3.1.0")
  176. set(threads_spec ${CMAKE_THREAD_LIBS_INIT})
  177. else()
  178. set(threads_spec Threads::Threads)
  179. endif()
  180. target_link_libraries(${name} PUBLIC ${threads_spec})
  181. endif()
  182. endfunction()
  183. ########################################################################
  184. #
  185. # Helper functions for creating build targets.
  186. function(cxx_shared_library name cxx_flags)
  187. cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
  188. endfunction()
  189. function(cxx_library name cxx_flags)
  190. cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN})
  191. endfunction()
  192. # cxx_executable_with_flags(name cxx_flags libs srcs...)
  193. #
  194. # creates a named C++ executable that depends on the given libraries and
  195. # is built from the given source files with the given compiler flags.
  196. function(cxx_executable_with_flags name cxx_flags libs)
  197. add_executable(${name} ${ARGN})
  198. if (MSVC)
  199. # BigObj required for tests.
  200. set(cxx_flags "${cxx_flags} -bigobj")
  201. endif()
  202. if (cxx_flags)
  203. set_target_properties(${name}
  204. PROPERTIES
  205. COMPILE_FLAGS "${cxx_flags}")
  206. endif()
  207. if (BUILD_SHARED_LIBS)
  208. set_target_properties(${name}
  209. PROPERTIES
  210. COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
  211. endif()
  212. # To support mixing linking in static and dynamic libraries, link each
  213. # library in with an extra call to target_link_libraries.
  214. foreach (lib "${libs}")
  215. target_link_libraries(${name} ${lib})
  216. endforeach()
  217. endfunction()
  218. # cxx_executable(name dir lib srcs...)
  219. #
  220. # creates a named target that depends on the given libs and is built
  221. # from the given source files. dir/name.cc is implicitly included in
  222. # the source file list.
  223. function(cxx_executable name dir libs)
  224. cxx_executable_with_flags(
  225. ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
  226. endfunction()
  227. # Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
  228. find_package(PythonInterp)
  229. # cxx_test_with_flags(name cxx_flags libs srcs...)
  230. #
  231. # creates a named C++ test that depends on the given libs and is built
  232. # from the given source files with the given compiler flags.
  233. function(cxx_test_with_flags name cxx_flags libs)
  234. cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
  235. if (WIN32 OR MINGW)
  236. add_test(NAME ${name}
  237. COMMAND "powershell" "-Command" "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/RunTest.ps1" "$<TARGET_FILE:${name}>")
  238. else()
  239. add_test(NAME ${name}
  240. COMMAND "$<TARGET_FILE:${name}>")
  241. endif()
  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 (PYTHONINTERP_FOUND)
  258. if ("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_GREATER 3.1)
  259. if (CMAKE_CONFIGURATION_TYPES)
  260. # Multi-configuration build generators as for Visual Studio save
  261. # output in a subdirectory of CMAKE_CURRENT_BINARY_DIR (Debug,
  262. # Release etc.), so we have to provide it here.
  263. if (WIN32 OR MINGW)
  264. add_test(NAME ${name}
  265. COMMAND powershell -Command ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/RunTest.ps1
  266. ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  267. --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN})
  268. else()
  269. add_test(NAME ${name}
  270. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  271. --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN})
  272. endif()
  273. else (CMAKE_CONFIGURATION_TYPES)
  274. # Single-configuration build generators like Makefile generators
  275. # don't have subdirs below CMAKE_CURRENT_BINARY_DIR.
  276. if (WIN32 OR MINGW)
  277. add_test(NAME ${name}
  278. COMMAND powershell -Command ${CMAKE_CURRENT_BINARY_DIR}/RunTest.ps1
  279. ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  280. --build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN})
  281. else()
  282. add_test(NAME ${name}
  283. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  284. --build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN})
  285. endif()
  286. endif (CMAKE_CONFIGURATION_TYPES)
  287. else()
  288. # ${CMAKE_CURRENT_BINARY_DIR} is known at configuration time, so we can
  289. # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
  290. # only at ctest runtime (by calling ctest -c <Configuration>), so
  291. # we have to escape $ to delay variable substitution here.
  292. if (WIN32 OR MINGW)
  293. add_test(NAME ${name}
  294. COMMAND powershell -Command ${CMAKE_CURRENT_BINARY_DIR}/RunTest.ps1
  295. ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  296. --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN})
  297. else()
  298. add_test(NAME ${name}
  299. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  300. --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN})
  301. endif()
  302. endif()
  303. endif(PYTHONINTERP_FOUND)
  304. endfunction()
  305. # install_project(targets...)
  306. #
  307. # Installs the specified targets and configures the associated pkgconfig files.
  308. function(install_project)
  309. if(INSTALL_GTEST)
  310. install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/"
  311. DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
  312. # Install the project targets.
  313. install(TARGETS ${ARGN}
  314. EXPORT ${targets_export_name}
  315. RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
  316. ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  317. LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
  318. if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  319. # Install PDBs
  320. foreach(t ${ARGN})
  321. get_target_property(t_pdb_name ${t} COMPILE_PDB_NAME)
  322. get_target_property(t_pdb_name_debug ${t} COMPILE_PDB_NAME_DEBUG)
  323. get_target_property(t_pdb_output_directory ${t} PDB_OUTPUT_DIRECTORY)
  324. install(FILES
  325. "${t_pdb_output_directory}/\${CMAKE_INSTALL_CONFIG_NAME}/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb"
  326. DESTINATION ${CMAKE_INSTALL_LIBDIR}
  327. OPTIONAL)
  328. endforeach()
  329. endif()
  330. # Configure and install pkgconfig files.
  331. foreach(t ${ARGN})
  332. set(configured_pc "${generated_dir}/${t}.pc")
  333. configure_file("${PROJECT_SOURCE_DIR}/cmake/${t}.pc.in"
  334. "${configured_pc}" @ONLY)
  335. install(FILES "${configured_pc}"
  336. DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  337. endforeach()
  338. endif()
  339. endfunction()