internal_utils.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 -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")
  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. # Set the output directory for build artifacts
  148. set_target_properties(${name}
  149. PROPERTIES
  150. RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
  151. LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
  152. ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
  153. PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
  154. # make PDBs match library name
  155. get_target_property(pdb_debug_postfix ${name} DEBUG_POSTFIX)
  156. set_target_properties(${name}
  157. PROPERTIES
  158. PDB_NAME "${name}"
  159. PDB_NAME_DEBUG "${name}${pdb_debug_postfix}"
  160. COMPILE_PDB_NAME "${name}"
  161. COMPILE_PDB_NAME_DEBUG "${name}${pdb_debug_postfix}")
  162. if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
  163. set_target_properties(${name}
  164. PROPERTIES
  165. COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
  166. if (NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
  167. target_compile_definitions(${name} INTERFACE
  168. $<INSTALL_INTERFACE:GTEST_LINKED_AS_SHARED_LIBRARY=1>)
  169. endif()
  170. endif()
  171. if (DEFINED GTEST_HAS_PTHREAD)
  172. if ("${CMAKE_VERSION}" VERSION_LESS "3.1.0")
  173. set(threads_spec ${CMAKE_THREAD_LIBS_INIT})
  174. else()
  175. set(threads_spec Threads::Threads)
  176. endif()
  177. target_link_libraries(${name} PUBLIC ${threads_spec})
  178. endif()
  179. if (NOT "${CMAKE_VERSION}" VERSION_LESS "3.8")
  180. target_compile_features(${name} PUBLIC cxx_std_11)
  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. if ("${CMAKE_VERSION}" VERSION_LESS "3.12.0")
  229. find_package(PythonInterp)
  230. else()
  231. find_package(Python COMPONENTS Interpreter)
  232. set(PYTHONINTERP_FOUND ${Python_Interpreter_FOUND})
  233. set(PYTHON_EXECUTABLE ${Python_EXECUTABLE})
  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 (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. add_test(NAME ${name}
  264. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  265. --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN})
  266. else (CMAKE_CONFIGURATION_TYPES)
  267. # Single-configuration build generators like Makefile generators
  268. # don't have subdirs below CMAKE_CURRENT_BINARY_DIR.
  269. add_test(NAME ${name}
  270. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  271. --build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN})
  272. endif (CMAKE_CONFIGURATION_TYPES)
  273. else()
  274. # ${CMAKE_CURRENT_BINARY_DIR} is known at configuration time, so we can
  275. # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
  276. # only at ctest runtime (by calling ctest -c <Configuration>), so
  277. # we have to escape $ to delay variable substitution here.
  278. add_test(NAME ${name}
  279. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  280. --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN})
  281. endif()
  282. # Make the Python import path consistent between Bazel and CMake.
  283. set_tests_properties(${name} PROPERTIES ENVIRONMENT PYTHONPATH=${CMAKE_SOURCE_DIR})
  284. endif(PYTHONINTERP_FOUND)
  285. endfunction()
  286. # install_project(targets...)
  287. #
  288. # Installs the specified targets and configures the associated pkgconfig files.
  289. function(install_project)
  290. if(INSTALL_GTEST)
  291. install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/"
  292. DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
  293. # Install the project targets.
  294. install(TARGETS ${ARGN}
  295. EXPORT ${targets_export_name}
  296. RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
  297. ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  298. LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
  299. if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  300. # Install PDBs
  301. foreach(t ${ARGN})
  302. get_target_property(t_pdb_name ${t} COMPILE_PDB_NAME)
  303. get_target_property(t_pdb_name_debug ${t} COMPILE_PDB_NAME_DEBUG)
  304. get_target_property(t_pdb_output_directory ${t} PDB_OUTPUT_DIRECTORY)
  305. install(FILES
  306. "${t_pdb_output_directory}/\${CMAKE_INSTALL_CONFIG_NAME}/$<$<CONFIG:Debug>:${t_pdb_name_debug}>$<$<NOT:$<CONFIG:Debug>>:${t_pdb_name}>.pdb"
  307. DESTINATION ${CMAKE_INSTALL_LIBDIR}
  308. OPTIONAL)
  309. endforeach()
  310. endif()
  311. # Configure and install pkgconfig files.
  312. foreach(t ${ARGN})
  313. set(configured_pc "${generated_dir}/${t}.pc")
  314. configure_file("${PROJECT_SOURCE_DIR}/cmake/${t}.pc.in"
  315. "${configured_pc}" @ONLY)
  316. install(FILES "${configured_pc}"
  317. DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
  318. endforeach()
  319. endif()
  320. endfunction()