internal_utils.cmake 11 KB

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