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.

216 lines
8.8 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. if (NOT gtest_disable_pthreads)
  47. # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
  48. find_package(Threads)
  49. endif()
  50. fix_default_compiler_settings_()
  51. if (MSVC)
  52. # Newlines inside flags variables break CMake's NMake generator.
  53. # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds.
  54. set(cxx_base_flags "-GS -W4 -WX -wd4127 -wd4251 -wd4275 -nologo -J -Zi")
  55. set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
  56. set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
  57. set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
  58. set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0")
  59. set(cxx_no_rtti_flags "-GR-")
  60. elseif (CMAKE_COMPILER_IS_GNUCXX)
  61. set(cxx_base_flags "-Wall -Wshadow")
  62. set(cxx_exception_flags "-fexceptions")
  63. set(cxx_no_exception_flags "-fno-exceptions")
  64. # Until version 4.3.2, GCC doesn't define a macro to indicate
  65. # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
  66. # explicitly.
  67. set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
  68. set(cxx_strict_flags "-Wextra")
  69. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
  70. set(cxx_exception_flags "-features=except")
  71. # Sun Pro doesn't provide macros to indicate whether exceptions and
  72. # RTTI are enabled, so we define GTEST_HAS_* explicitly.
  73. set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0")
  74. set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0")
  75. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR
  76. CMAKE_CXX_COMPILER_ID STREQUAL "XL")
  77. # CMake 2.8 changes Visual Age's compiler ID to "XL".
  78. set(cxx_exception_flags "-qeh")
  79. set(cxx_no_exception_flags "-qnoeh")
  80. # Until version 9.0, Visual Age doesn't define a macro to indicate
  81. # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
  82. # explicitly.
  83. set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0")
  84. elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP")
  85. set(cxx_base_flags "-AA -mt")
  86. set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1")
  87. set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0")
  88. # RTTI can not be disabled in HP aCC compiler.
  89. set(cxx_no_rtti_flags "")
  90. endif()
  91. if (CMAKE_USE_PTHREADS_INIT) # The pthreads library is available and allowed.
  92. set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1")
  93. else()
  94. set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=0")
  95. endif()
  96. # For building gtest's own tests and samples.
  97. set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}")
  98. set(cxx_no_exception
  99. "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}")
  100. set(cxx_default "${cxx_exception}")
  101. set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}")
  102. set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1")
  103. # For building the gtest libraries.
  104. set(cxx_strict "${cxx_default} ${cxx_strict_flags}")
  105. endmacro()
  106. # Defines the gtest & gtest_main libraries. User tests should link
  107. # with one of them.
  108. function(cxx_library_with_type name type cxx_flags)
  109. # type can be either STATIC or SHARED to denote a static or shared library.
  110. # ARGN refers to additional arguments after 'cxx_flags'.
  111. add_library(${name} ${type} ${ARGN})
  112. set_target_properties(${name}
  113. PROPERTIES
  114. COMPILE_FLAGS "${cxx_flags}")
  115. if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
  116. set_target_properties(${name}
  117. PROPERTIES
  118. COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
  119. endif()
  120. if (CMAKE_USE_PTHREADS_INIT)
  121. target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
  122. endif()
  123. endfunction()
  124. ########################################################################
  125. #
  126. # Helper functions for creating build targets.
  127. function(cxx_shared_library name cxx_flags)
  128. cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN})
  129. endfunction()
  130. function(cxx_library name cxx_flags)
  131. cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN})
  132. endfunction()
  133. # cxx_executable_with_flags(name cxx_flags libs srcs...)
  134. #
  135. # creates a named C++ executable that depends on the given libraries and
  136. # is built from the given source files with the given compiler flags.
  137. function(cxx_executable_with_flags name cxx_flags libs)
  138. add_executable(${name} ${ARGN})
  139. if (cxx_flags)
  140. set_target_properties(${name}
  141. PROPERTIES
  142. COMPILE_FLAGS "${cxx_flags}")
  143. endif()
  144. if (BUILD_SHARED_LIBS)
  145. set_target_properties(${name}
  146. PROPERTIES
  147. COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1")
  148. endif()
  149. # To support mixing linking in static and dynamic libraries, link each
  150. # library in with an extra call to target_link_libraries.
  151. foreach (lib "${libs}")
  152. target_link_libraries(${name} ${lib})
  153. endforeach()
  154. endfunction()
  155. # cxx_executable(name dir lib srcs...)
  156. #
  157. # creates a named target that depends on the given libs and is built
  158. # from the given source files. dir/name.cc is implicitly included in
  159. # the source file list.
  160. function(cxx_executable name dir libs)
  161. cxx_executable_with_flags(
  162. ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN})
  163. endfunction()
  164. # Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE.
  165. find_package(PythonInterp)
  166. # cxx_test_with_flags(name cxx_flags libs srcs...)
  167. #
  168. # creates a named C++ test that depends on the given libs and is built
  169. # from the given source files with the given compiler flags.
  170. function(cxx_test_with_flags name cxx_flags libs)
  171. cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN})
  172. add_test(${name} ${name})
  173. endfunction()
  174. # cxx_test(name libs srcs...)
  175. #
  176. # creates a named test target that depends on the given libs and is
  177. # built from the given source files. Unlike cxx_test_with_flags,
  178. # test/name.cc is already implicitly included in the source file list.
  179. function(cxx_test name libs)
  180. cxx_test_with_flags("${name}" "${cxx_default}" "${libs}"
  181. "test/${name}.cc" ${ARGN})
  182. endfunction()
  183. # py_test(name)
  184. #
  185. # creates a Python test with the given name whose main module is in
  186. # test/name.py. It does nothing if Python is not installed.
  187. function(py_test name)
  188. # We are not supporting Python tests on Linux yet as they consider
  189. # all Linux environments to be google3 and try to use google3 features.
  190. if (PYTHONINTERP_FOUND)
  191. # ${CMAKE_BINARY_DIR} is known at configuration time, so we can
  192. # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
  193. # only at ctest runtime (by calling ctest -c <Configuration>), so
  194. # we have to escape $ to delay variable substitution here.
  195. add_test(${name}
  196. ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
  197. --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE})
  198. endif()
  199. endfunction()