internal_utils.cmake 13 KB

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