Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | # Defines functions and macros useful for building Google Test and |
michael@0 | 2 | # Google Mock. |
michael@0 | 3 | # |
michael@0 | 4 | # Note: |
michael@0 | 5 | # |
michael@0 | 6 | # - This file will be run twice when building Google Mock (once via |
michael@0 | 7 | # Google Test's CMakeLists.txt, and once via Google Mock's). |
michael@0 | 8 | # Therefore it shouldn't have any side effects other than defining |
michael@0 | 9 | # the functions and macros. |
michael@0 | 10 | # |
michael@0 | 11 | # - The functions/macros defined in this file may depend on Google |
michael@0 | 12 | # Test and Google Mock's option() definitions, and thus must be |
michael@0 | 13 | # called *after* the options have been defined. |
michael@0 | 14 | |
michael@0 | 15 | # Tweaks CMake's default compiler/linker settings to suit Google Test's needs. |
michael@0 | 16 | # |
michael@0 | 17 | # This must be a macro(), as inside a function string() can only |
michael@0 | 18 | # update variables in the function scope. |
michael@0 | 19 | macro(fix_default_compiler_settings_) |
michael@0 | 20 | if (MSVC) |
michael@0 | 21 | # For MSVC, CMake sets certain flags to defaults we want to override. |
michael@0 | 22 | # This replacement code is taken from sample in the CMake Wiki at |
michael@0 | 23 | # http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace. |
michael@0 | 24 | foreach (flag_var |
michael@0 | 25 | CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE |
michael@0 | 26 | CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) |
michael@0 | 27 | if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt) |
michael@0 | 28 | # When Google Test is built as a shared library, it should also use |
michael@0 | 29 | # shared runtime libraries. Otherwise, it may end up with multiple |
michael@0 | 30 | # copies of runtime library data in different modules, resulting in |
michael@0 | 31 | # hard-to-find crashes. When it is built as a static library, it is |
michael@0 | 32 | # preferable to use CRT as static libraries, as we don't have to rely |
michael@0 | 33 | # on CRT DLLs being available. CMake always defaults to using shared |
michael@0 | 34 | # CRT libraries, so we override that default here. |
michael@0 | 35 | string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}") |
michael@0 | 36 | endif() |
michael@0 | 37 | |
michael@0 | 38 | # We prefer more strict warning checking for building Google Test. |
michael@0 | 39 | # Replaces /W3 with /W4 in defaults. |
michael@0 | 40 | string(REPLACE "/W3" "-W4" ${flag_var} "${${flag_var}}") |
michael@0 | 41 | endforeach() |
michael@0 | 42 | endif() |
michael@0 | 43 | endmacro() |
michael@0 | 44 | |
michael@0 | 45 | # Defines the compiler/linker flags used to build Google Test and |
michael@0 | 46 | # Google Mock. You can tweak these definitions to suit your need. A |
michael@0 | 47 | # variable's value is empty before it's explicitly assigned to. |
michael@0 | 48 | macro(config_compiler_and_linker) |
michael@0 | 49 | if (NOT gtest_disable_pthreads) |
michael@0 | 50 | # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT. |
michael@0 | 51 | find_package(Threads) |
michael@0 | 52 | endif() |
michael@0 | 53 | |
michael@0 | 54 | fix_default_compiler_settings_() |
michael@0 | 55 | if (MSVC) |
michael@0 | 56 | # Newlines inside flags variables break CMake's NMake generator. |
michael@0 | 57 | # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds. |
michael@0 | 58 | set(cxx_base_flags "-GS -W4 -WX -wd4127 -wd4251 -wd4275 -nologo -J -Zi") |
michael@0 | 59 | if (MSVC_VERSION LESS 1400) |
michael@0 | 60 | # Suppress spurious warnings MSVC 7.1 sometimes issues. |
michael@0 | 61 | # Forcing value to bool. |
michael@0 | 62 | set(cxx_base_flags "${cxx_base_flags} -wd4800") |
michael@0 | 63 | # Copy constructor and assignment operator could not be generated. |
michael@0 | 64 | set(cxx_base_flags "${cxx_base_flags} -wd4511 -wd4512") |
michael@0 | 65 | # Compatibility warnings not applicable to Google Test. |
michael@0 | 66 | # Resolved overload was found by argument-dependent lookup. |
michael@0 | 67 | set(cxx_base_flags "${cxx_base_flags} -wd4675") |
michael@0 | 68 | endif() |
michael@0 | 69 | set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32") |
michael@0 | 70 | set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN") |
michael@0 | 71 | set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1") |
michael@0 | 72 | set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0") |
michael@0 | 73 | set(cxx_no_rtti_flags "-GR-") |
michael@0 | 74 | elseif (CMAKE_COMPILER_IS_GNUCXX) |
michael@0 | 75 | set(cxx_base_flags "-Wall -Wshadow") |
michael@0 | 76 | set(cxx_exception_flags "-fexceptions") |
michael@0 | 77 | set(cxx_no_exception_flags "-fno-exceptions") |
michael@0 | 78 | # Until version 4.3.2, GCC doesn't define a macro to indicate |
michael@0 | 79 | # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI |
michael@0 | 80 | # explicitly. |
michael@0 | 81 | set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0") |
michael@0 | 82 | set(cxx_strict_flags |
michael@0 | 83 | "-Wextra -Wno-unused-parameter -Wno-missing-field-initializers") |
michael@0 | 84 | elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro") |
michael@0 | 85 | set(cxx_exception_flags "-features=except") |
michael@0 | 86 | # Sun Pro doesn't provide macros to indicate whether exceptions and |
michael@0 | 87 | # RTTI are enabled, so we define GTEST_HAS_* explicitly. |
michael@0 | 88 | set(cxx_no_exception_flags "-features=no%except -DGTEST_HAS_EXCEPTIONS=0") |
michael@0 | 89 | set(cxx_no_rtti_flags "-features=no%rtti -DGTEST_HAS_RTTI=0") |
michael@0 | 90 | elseif (CMAKE_CXX_COMPILER_ID STREQUAL "VisualAge" OR |
michael@0 | 91 | CMAKE_CXX_COMPILER_ID STREQUAL "XL") |
michael@0 | 92 | # CMake 2.8 changes Visual Age's compiler ID to "XL". |
michael@0 | 93 | set(cxx_exception_flags "-qeh") |
michael@0 | 94 | set(cxx_no_exception_flags "-qnoeh") |
michael@0 | 95 | # Until version 9.0, Visual Age doesn't define a macro to indicate |
michael@0 | 96 | # whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI |
michael@0 | 97 | # explicitly. |
michael@0 | 98 | set(cxx_no_rtti_flags "-qnortti -DGTEST_HAS_RTTI=0") |
michael@0 | 99 | elseif (CMAKE_CXX_COMPILER_ID STREQUAL "HP") |
michael@0 | 100 | set(cxx_base_flags "-AA -mt") |
michael@0 | 101 | set(cxx_exception_flags "-DGTEST_HAS_EXCEPTIONS=1") |
michael@0 | 102 | set(cxx_no_exception_flags "+noeh -DGTEST_HAS_EXCEPTIONS=0") |
michael@0 | 103 | # RTTI can not be disabled in HP aCC compiler. |
michael@0 | 104 | set(cxx_no_rtti_flags "") |
michael@0 | 105 | endif() |
michael@0 | 106 | |
michael@0 | 107 | if (CMAKE_USE_PTHREADS_INIT) # The pthreads library is available and allowed. |
michael@0 | 108 | set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1") |
michael@0 | 109 | else() |
michael@0 | 110 | set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=0") |
michael@0 | 111 | endif() |
michael@0 | 112 | |
michael@0 | 113 | # For building gtest's own tests and samples. |
michael@0 | 114 | set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}") |
michael@0 | 115 | set(cxx_no_exception |
michael@0 | 116 | "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_no_exception_flags}") |
michael@0 | 117 | set(cxx_default "${cxx_exception}") |
michael@0 | 118 | set(cxx_no_rtti "${cxx_default} ${cxx_no_rtti_flags}") |
michael@0 | 119 | set(cxx_use_own_tuple "${cxx_default} -DGTEST_USE_OWN_TR1_TUPLE=1") |
michael@0 | 120 | |
michael@0 | 121 | # For building the gtest libraries. |
michael@0 | 122 | set(cxx_strict "${cxx_default} ${cxx_strict_flags}") |
michael@0 | 123 | endmacro() |
michael@0 | 124 | |
michael@0 | 125 | # Defines the gtest & gtest_main libraries. User tests should link |
michael@0 | 126 | # with one of them. |
michael@0 | 127 | function(cxx_library_with_type name type cxx_flags) |
michael@0 | 128 | # type can be either STATIC or SHARED to denote a static or shared library. |
michael@0 | 129 | # ARGN refers to additional arguments after 'cxx_flags'. |
michael@0 | 130 | add_library(${name} ${type} ${ARGN}) |
michael@0 | 131 | set_target_properties(${name} |
michael@0 | 132 | PROPERTIES |
michael@0 | 133 | COMPILE_FLAGS "${cxx_flags}") |
michael@0 | 134 | if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED") |
michael@0 | 135 | set_target_properties(${name} |
michael@0 | 136 | PROPERTIES |
michael@0 | 137 | COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1") |
michael@0 | 138 | endif() |
michael@0 | 139 | if (CMAKE_USE_PTHREADS_INIT) |
michael@0 | 140 | target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT}) |
michael@0 | 141 | endif() |
michael@0 | 142 | endfunction() |
michael@0 | 143 | |
michael@0 | 144 | ######################################################################## |
michael@0 | 145 | # |
michael@0 | 146 | # Helper functions for creating build targets. |
michael@0 | 147 | |
michael@0 | 148 | function(cxx_shared_library name cxx_flags) |
michael@0 | 149 | cxx_library_with_type(${name} SHARED "${cxx_flags}" ${ARGN}) |
michael@0 | 150 | endfunction() |
michael@0 | 151 | |
michael@0 | 152 | function(cxx_library name cxx_flags) |
michael@0 | 153 | cxx_library_with_type(${name} "" "${cxx_flags}" ${ARGN}) |
michael@0 | 154 | endfunction() |
michael@0 | 155 | |
michael@0 | 156 | # cxx_executable_with_flags(name cxx_flags libs srcs...) |
michael@0 | 157 | # |
michael@0 | 158 | # creates a named C++ executable that depends on the given libraries and |
michael@0 | 159 | # is built from the given source files with the given compiler flags. |
michael@0 | 160 | function(cxx_executable_with_flags name cxx_flags libs) |
michael@0 | 161 | add_executable(${name} ${ARGN}) |
michael@0 | 162 | if (cxx_flags) |
michael@0 | 163 | set_target_properties(${name} |
michael@0 | 164 | PROPERTIES |
michael@0 | 165 | COMPILE_FLAGS "${cxx_flags}") |
michael@0 | 166 | endif() |
michael@0 | 167 | if (BUILD_SHARED_LIBS) |
michael@0 | 168 | set_target_properties(${name} |
michael@0 | 169 | PROPERTIES |
michael@0 | 170 | COMPILE_DEFINITIONS "GTEST_LINKED_AS_SHARED_LIBRARY=1") |
michael@0 | 171 | endif() |
michael@0 | 172 | # To support mixing linking in static and dynamic libraries, link each |
michael@0 | 173 | # library in with an extra call to target_link_libraries. |
michael@0 | 174 | foreach (lib "${libs}") |
michael@0 | 175 | target_link_libraries(${name} ${lib}) |
michael@0 | 176 | endforeach() |
michael@0 | 177 | endfunction() |
michael@0 | 178 | |
michael@0 | 179 | # cxx_executable(name dir lib srcs...) |
michael@0 | 180 | # |
michael@0 | 181 | # creates a named target that depends on the given libs and is built |
michael@0 | 182 | # from the given source files. dir/name.cc is implicitly included in |
michael@0 | 183 | # the source file list. |
michael@0 | 184 | function(cxx_executable name dir libs) |
michael@0 | 185 | cxx_executable_with_flags( |
michael@0 | 186 | ${name} "${cxx_default}" "${libs}" "${dir}/${name}.cc" ${ARGN}) |
michael@0 | 187 | endfunction() |
michael@0 | 188 | |
michael@0 | 189 | # Sets PYTHONINTERP_FOUND and PYTHON_EXECUTABLE. |
michael@0 | 190 | find_package(PythonInterp) |
michael@0 | 191 | |
michael@0 | 192 | # cxx_test_with_flags(name cxx_flags libs srcs...) |
michael@0 | 193 | # |
michael@0 | 194 | # creates a named C++ test that depends on the given libs and is built |
michael@0 | 195 | # from the given source files with the given compiler flags. |
michael@0 | 196 | function(cxx_test_with_flags name cxx_flags libs) |
michael@0 | 197 | cxx_executable_with_flags(${name} "${cxx_flags}" "${libs}" ${ARGN}) |
michael@0 | 198 | add_test(${name} ${name}) |
michael@0 | 199 | endfunction() |
michael@0 | 200 | |
michael@0 | 201 | # cxx_test(name libs srcs...) |
michael@0 | 202 | # |
michael@0 | 203 | # creates a named test target that depends on the given libs and is |
michael@0 | 204 | # built from the given source files. Unlike cxx_test_with_flags, |
michael@0 | 205 | # test/name.cc is already implicitly included in the source file list. |
michael@0 | 206 | function(cxx_test name libs) |
michael@0 | 207 | cxx_test_with_flags("${name}" "${cxx_default}" "${libs}" |
michael@0 | 208 | "test/${name}.cc" ${ARGN}) |
michael@0 | 209 | endfunction() |
michael@0 | 210 | |
michael@0 | 211 | # py_test(name) |
michael@0 | 212 | # |
michael@0 | 213 | # creates a Python test with the given name whose main module is in |
michael@0 | 214 | # test/name.py. It does nothing if Python is not installed. |
michael@0 | 215 | function(py_test name) |
michael@0 | 216 | # We are not supporting Python tests on Linux yet as they consider |
michael@0 | 217 | # all Linux environments to be google3 and try to use google3 features. |
michael@0 | 218 | if (PYTHONINTERP_FOUND) |
michael@0 | 219 | # ${CMAKE_BINARY_DIR} is known at configuration time, so we can |
michael@0 | 220 | # directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known |
michael@0 | 221 | # only at ctest runtime (by calling ctest -c <Configuration>), so |
michael@0 | 222 | # we have to escape $ to delay variable substitution here. |
michael@0 | 223 | add_test(${name} |
michael@0 | 224 | ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py |
michael@0 | 225 | --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE}) |
michael@0 | 226 | endif() |
michael@0 | 227 | endfunction() |