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.
 
 
 
 
 
 

274 lines
12 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. # 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. # http://www.cmake.org/Wiki/CMake_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. endforeach()
  40. endif()
  41. endmacro()
  42. # Defines the compiler/linker flags used to build Google Test and
  43. # Google Mock. You can tweak these definitions to suit your need. A
  44. # variable's value is empty before it's explicitly assigned to.
  45. macro(config_compiler_and_linker)
  46. # Note: pthreads on MinGW is not supported, even if available
  47. # instead, we use windows threading primitives
  48. unset(GTEST_HAS_PTHREAD)
  49. if (NOT gtest_disable_pthreads AND NOT MINGW)
  50. # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
  51. set(THREADS_PREFER_PTHREAD_FLAG ON)
  52. find_package(Threads)
  53. if (CMAKE_USE_PTHREADS_INIT)
  54. set(GTEST_HAS_PTHREAD ON)
  55. endif()
  56. endif()
  57. fix_default_compiler_settings_()
  58. if (MSVC)
  59. # Newlines inside flags variables break CMake's NMake generator.
  60. # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds.
  61. set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J -Zi")
  62. if (MSVC_VERSION LESS 1400) # 1400 is Visual Studio 2005
  63. # Suppress spurious warnings MSVC 7.1 sometimes issues.
  64. # Forcing value to bool.
  65. set(cxx_base_flags "${cxx_base_flags} -wd4800")
  66. # Copy constructor and assignment operator could not be generated.
  67. set(cxx_base_flags "${cxx_base_flags} -wd4511 -wd4512")
  68. # Compatibility warnings not applicable to Google Test.
  69. # Resolved overload was found by argument-dependent lookup.
  70. set(cxx_base_flags "${cxx_base_flags} -wd4675")
  71. endif()
  72. if (MSVC_VERSION LESS 1500) # 1500 is Visual Studio 2008
  73. # Conditional expression is constant.
  74. # When compiling with /W4, we get several instances of C4127
  75. # (Conditional expression is constant). In our code, we disable that
  76. # warning on a case-by-case basis. However, on Visual Studio 2005,
  77. # the warning fires on std::list. Therefore on that compiler and earlier,
  78. # we disable the warning project-wide.
  79. set(cxx_base_flags "${cxx_base_flags} -wd4127")
  80. endif()
  81. if (NOT (MSVC_VERSION LESS 1700)) # 1700 is Visual Studio 2012.
  82. # Suppress "unreachable code" warning on VS 2012 and later.
  83. # http://stackoverflow.com/questions/3232669 explains the issue.
  84. set(cxx_base_flags "${cxx_base_flags} -wd4702")
  85. endif()
  86. set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
  87. set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
  88. set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
  89. set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0")
  90. set(cxx_no_rtti_flags "-GR-")
  91. elseif (CMAKE_COMPILER_IS_GNUCXX)
  92. set(cxx_base_flags "-Wall -Wshadow -Werror")
  93. set(cxx_exception_flags "-fexceptions")
  94. set(cxx_no_exception_flags "-fno-exceptions")
  95. # Until version 4.3.2, GCC doesn't define a macro to indicate
  96. # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
  97. # explicitly.
  98. set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
  99. set(cxx_strict_flags
  100. "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
  101. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
  102. set(cxx_exception_flags "-features=except")
  103. # Sun Pro doesn't provide macros to indicate whether exceptions and
  104. # RTTI are enabled, so we define GTEST_HAS_* explicitly.
  105. set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
  106. set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
  107. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
  108. CMAKE_CXX_COMPILER_ID STREQUAL "XL")
  109. # CMake 2.8 changes Visual Age's compiler ID to "XL".
  110. set(cxx_exception_flags "-qeh")
  111. set(cxx_no_exception_flags "-qnoeh")
  112. # Until version 9.0, Visual Age doesn't define a macro to indicate
  113. # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
  114. # explicitly.
  115. set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
  116. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
  117. set(cxx_base_flags "-AA -mt")
  118. set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1")
  119. set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0")
  120. # RTTI can not be disabled in HP aCC compiler.
  121. set(cxx_no_rtti_flags "")
  122. endif()
  123. # The pthreads library is available and allowed?
  124. if (DEFINED GTEST_HAS_PTHREAD)
  125. set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=1")
  126. else()
  127. set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=0")
  128. endif()
  129. set(cxx_base_flags "${cxx_base_flags} ${GTEST_HAS_PTHREAD_MACRO}")
  130. # For building gtest's own tests and samples.
  131. set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}")
  132. set(cxx_no_exception
  133. "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
  134. set(cxx_default "${cxx_exception}")
  135. set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
  136. set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1")
  137. # For building the gtest libraries.
  138. set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
  139. endmacro()
  140. # Defines the gtest & gtest_main libraries. User tests should link
  141. # with one of them.
  142. function(cxx_library_with_type name type cxx_flags)
  143. # type can be either STATIC or SHARED to denote a static or shared library.
  144. # ARGN refers to additional arguments after 'cxx_flags'.
  145. add_library(${name} ${type} ${ARGN})
  146. set_target_properties(${name}
  147. PROPERTIES
  148. COMPILE_FLAGS "${cxx_flags}")
  149. if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
  150. set_target_properties(${name}
  151. PROPERTIES
  152. COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
  153. endif()
  154. if (DEFINED GTEST_HAS_PTHREAD)
  155. target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
  156. endif()
  157. endfunction()
  158. ########################################################################
  159. #
  160. # Helper functions for creating build targets.
  161. function(cxx_shared_library name cxx_flags)
  162. cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
  163. endfunction()
  164. function(cxx_library name cxx_flags)
  165. cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN})
  166. endfunction()
  167. # cxx_executable_with_flags(name cxx_flags libs srcs...)
  168. #
  169. # creates a named C++ executable that depends on the given libraries and
  170. # is built from the given source files with the given compiler flags.
  171. function(cxx_executable_with_flags name cxx_flags libs)
  172. add_executable(${name} ${ARGN})
  173. if (MSVC AND (NOT (MSVC_VERSION LESS 1700))) # 1700 is Visual Studio 2012.
  174. # BigObj required for tests.
  175. set(cxx_flags "${cxx_flags} -bigobj")
  176. endif()
  177. if (cxx_flags)
  178. set_target_properties(${name}
  179. PROPERTIES
  180. COMPILE_FLAGS "${cxx_flags}")
  181. endif()
  182. if (BUILD_SHARED_LIBS)
  183. set_target_properties(${name}
  184. PROPERTIES
  185. COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
  186. endif()
  187. # To support mixing linking in static and dynamic libraries, link each
  188. # library in with an extra call to target_link_libraries.
  189. foreach (lib "${libs}")
  190. target_link_libraries(${name} ${lib})
  191. endforeach()
  192. endfunction()
  193. # cxx_executable(name dir lib srcs...)
  194. #
  195. # creates a named target that depends on the given libs and is built
  196. # from the given source files. dir/name.cc is implicitly included in
  197. # the source file list.
  198. function(cxx_executable name dir libs)
  199. cxx_executable_with_flags(
  200. ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
  201. endfunction()
  202. # Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
  203. find_package(PythonInterp)
  204. # cxx_test_with_flags(name cxx_flags libs srcs...)
  205. #
  206. # creates a named C++ test that depends on the given libs and is built
  207. # from the given source files with the given compiler flags.
  208. function(cxx_test_with_flags name cxx_flags libs)
  209. cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
  210. add_test(${name} ${name})
  211. endfunction()
  212. # cxx_test(name libs srcs...)
  213. #
  214. # creates a named test target that depends on the given libs and is
  215. # built from the given source files. Unlike cxx_test_with_flags,
  216. # test/name.cc is already implicitly included in the source file list.
  217. function(cxx_test name libs)
  218. cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
  219. "test/${name}.cc" ${ARGN})
  220. endfunction()
  221. # py_test(name)
  222. #
  223. # creates a Python test with the given name whose main module is in
  224. # test/name.py. It does nothing if Python is not installed.
  225. function(py_test name)
  226. if (PYTHONINTERP_FOUND)
  227. if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
  228. if (CMAKE_CONFIGURATION_TYPES)
  229. # Multi-configuration build generators as for Visual Studio save
  230. # output in a subdirectory of CMAKE_CURRENT_BINARY_DIR (Debug,
  231. # Release etc.), so we have to provide it here.
  232. add_test(
  233. NAME ${name}
  234. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  235. --build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>)
  236. else (CMAKE_CONFIGURATION_TYPES)
  237. # Single-configuration build generators like Makefile generators
  238. # don't have subdirs below CMAKE_CURRENT_BINARY_DIR.
  239. add_test(
  240. NAME ${name}
  241. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  242. --build_dir=${CMAKE_CURRENT_BINARY_DIR})
  243. endif (CMAKE_CONFIGURATION_TYPES)
  244. else (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
  245. # ${CMAKE_CURRENT_BINARY_DIR} is known at configuration time, so we can
  246. # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
  247. # only at ctest runtime (by calling ctest -c <Configuration>), so
  248. # we have to escape $ to delay variable substitution here.
  249. add_test(
  250. ${name}
  251. ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  252. --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE})
  253. endif (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
  254. endif(PYTHONINTERP_FOUND)
  255. endfunction()