internal_utils.cmake 14 KB

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