|
22 | 22 |
|
23 | 23 | set(_TEST_RUNTIME_DIR ${PROJECT_BINARY_DIR}/tests)
|
24 | 24 |
|
| 25 | +# We include GMock without touching the compile flags. GMock can |
| 26 | +# handle that itself. It will also indirectly create targets for gmock |
| 27 | +# and gtest. |
| 28 | +# |
| 29 | +# Two alternatives for locating GMock *source code*: |
| 30 | +# 1. If WITH_GMOCK is given, this is expected to be the location of |
| 31 | +# the *source code*. |
| 32 | +# 2. If WITH_GMOCK is not given, it will look in the 'ext' directory |
| 33 | +# in the source root. |
| 34 | +if(ENABLE_TESTS) |
| 35 | + if(TARGET gmock) |
| 36 | + # don't build gmock, if the parent already built it |
| 37 | + |
| 38 | + # copying from unittest/gunit/CMakeFiles.txt |
| 39 | + # this should all be global-variables or a cmake/ file |
| 40 | + if(NOT DOWNLOAD_ROOT) |
| 41 | + set(DOWNLOAD_ROOT ${CMAKE_SOURCE_DIR}/source_downloads) |
| 42 | + endif() |
| 43 | + |
| 44 | + # We want googletest version 1.8, which also contains googlemock. |
| 45 | + set(GMOCK_PACKAGE_NAME "release-1.8.0") |
| 46 | + |
| 47 | + if(DEFINED ENV{WITH_GMOCK} AND NOT DEFINED WITH_GMOCK) |
| 48 | + file(TO_CMAKE_PATH "$ENV{WITH_GMOCK}" WITH_GMOCK) |
| 49 | + ENDIF() |
| 50 | + |
| 51 | + if(LOCAL_GMOCK_ZIP |
| 52 | + AND NOT ${LOCAL_GMOCK_ZIP} MATCHES ".*${GMOCK_PACKAGE_NAME}\\.zip") |
| 53 | + set(LOCAL_GMOCK_ZIP 0) |
| 54 | + endif() |
| 55 | + |
| 56 | + if(WITH_GMOCK) |
| 57 | + ## Did we get a full path name, including file name? |
| 58 | + if(${WITH_GMOCK} MATCHES ".*\\.zip") |
| 59 | + GET_FILENAME_COMPONENT(GMOCK_DIR ${WITH_GMOCK} PATH) |
| 60 | + GET_FILENAME_COMPONENT(GMOCK_ZIP ${WITH_GMOCK} NAME) |
| 61 | + FIND_FILE(LOCAL_GMOCK_ZIP |
| 62 | + NAMES ${GMOCK_ZIP} |
| 63 | + PATHS ${GMOCK_DIR} |
| 64 | + NO_DEFAULT_PATH |
| 65 | + ) |
| 66 | + else() |
| 67 | + ## Did we get a path name to the directory of the .zip file? |
| 68 | + ## Check for both release-x.y.z.zip and googletest-release-x.y.z.zip |
| 69 | + FIND_FILE(LOCAL_GMOCK_ZIP |
| 70 | + NAMES "${GMOCK_PACKAGE_NAME}.zip" "googletest-${GMOCK_PACKAGE_NAME}.zip" |
| 71 | + PATHS ${WITH_GMOCK} |
| 72 | + NO_DEFAULT_PATH |
| 73 | + ) |
| 74 | + ## If WITH_GMOCK is a directory, use it for download. |
| 75 | + set(DOWNLOAD_ROOT ${WITH_GMOCK}) |
| 76 | + endif() |
| 77 | + MESSAGE(STATUS "Local gmock zip ${LOCAL_GMOCK_ZIP}") |
| 78 | + endif() |
| 79 | + |
| 80 | + set(GMOCK_SOURCE_DIR ${DOWNLOAD_ROOT}/googletest-${GMOCK_PACKAGE_NAME}/googlemock) |
| 81 | + set(GTEST_SOURCE_DIR ${DOWNLOAD_ROOT}/googletest-${GMOCK_PACKAGE_NAME}/googletest) |
| 82 | + |
| 83 | + # introduce some compat |
| 84 | + set(GTEST_INCLUDE_DIRS ${GMOCK_INCLUDE_DIRS}) |
| 85 | + message("yyy seting GTEST_INCLUDE_DIRS to ${GTEST_INCLUDE_DIRS}") |
| 86 | + |
| 87 | + ADD_LIBRARY(gmock_main STATIC ${GMOCK_SOURCE_DIR}/src/gmock_main.cc) |
| 88 | + target_link_libraries(gmock_main gmock) |
| 89 | + target_include_directories(gmock_main |
| 90 | + PUBLIC ${GMOCK_INCLUDE_DIRS}) |
| 91 | + ADD_LIBRARY(gtest_main STATIC ${GTEST_SOURCE_DIR}/src/gtest_main.cc) |
| 92 | + target_include_directories(gtest_main |
| 93 | + PUBLIC ${GMOCK_INCLUDE_DIRS}) |
| 94 | + |
| 95 | + if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") |
| 96 | + set_target_properties(gtest_main gmock_main |
| 97 | + PROPERTIES |
| 98 | + COMPILE_FLAGS "-Wno-undef -Wno-conversion") |
| 99 | + endif() |
| 100 | + |
| 101 | + set(TEST_LIBRARIES gmock gtest gmock_main gtest_main) |
| 102 | + else() |
| 103 | + if(WITH_GMOCK) |
| 104 | + |
| 105 | + # There is a known gtest/gmock bug that surfaces with the gcc-6.x causing tests crashes: |
| 106 | + # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833450 |
| 107 | + # We have a patch for it in the gmock we bundle but if the user wants to use |
| 108 | + # it's own gtest/gmock we need to prevent it if the gcc-6.x is used |
| 109 | + if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 110 | + AND (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL "6.0" OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "6.0")) |
| 111 | + message(FATAL_ERROR "Parameter WITH_GMOCK is not supported for gcc-6 or greater." |
| 112 | + "You need to either disable the tests or use the bundled gmock (removing WITH_GMOCK parameter).") |
| 113 | + endif() |
| 114 | + |
| 115 | + set(_gmock_root ${WITH_GMOCK}) |
| 116 | + set(_gtest_root ${WITH_GMOCK}/gtest) |
| 117 | + elseif(EXISTS "${CMAKE_SOURCE_DIR}/ext/gmock/CMakeLists.txt") |
| 118 | + if(NOT EXISTS "${CMAKE_SOURCE_DIR}/ext/gtest/CMakeLists.txt") |
| 119 | + message(FATAL_ERROR "Cannot find GTest repository under ${CMAKE_SOURCE_DIR}/ext/gtest") |
| 120 | + endif() |
| 121 | + set(_gmock_root "${CMAKE_SOURCE_DIR}/ext/gmock") |
| 122 | + set(_gtest_root "${CMAKE_SOURCE_DIR}/ext/gtest") |
| 123 | + elseif(GMOCK_SOURCE_DIR) |
| 124 | + # means we are part of the server and GMOCK was downloaded |
| 125 | + set(_gmock_root ${GMOCK_SOURCE_DIR}) |
| 126 | + set(_gtest_root ${GMOCK_SOURCE_DIR}/gtest) |
| 127 | + else() |
| 128 | + # means we are part of the server and GMOCK is missing |
| 129 | + # act as other server components, disable the tests |
| 130 | + SET (ENABLE_TESTS 0) |
| 131 | + SET (ENABLE_TESTS 0 PARENT_SCOPE) |
| 132 | + endif() |
| 133 | + |
| 134 | + if (ENABLE_TESTS) |
| 135 | + if(NOT EXISTS "${_gmock_root}/CMakeLists.txt") |
| 136 | + message(WARNING |
| 137 | + "Unable to find GMock source, not possible to build tests. Either " |
| 138 | + "disable tests with ENABLE_TESTS=no or download the source code " |
| 139 | + "for GMock (available at https://github.com/google/googlemock) and " |
| 140 | + "set WITH_GMOCK to the directory of the unpacked source code.") |
| 141 | + endif() |
| 142 | + |
| 143 | + message(STATUS "Found GMock source under ${_gmock_root}") |
| 144 | + add_subdirectory(${_gmock_root} ext/gmock) |
| 145 | + |
| 146 | + # Setting variables that are normally discovered using FindXXX.cmake |
| 147 | + set(GTEST_INCLUDE_DIRS ${_gtest_root}/include) |
| 148 | + message("yyy seting GTEST_INCLUDE_DIRS to ${GTEST_INCLUDE_DIRS}") |
| 149 | + set(GTEST_LIBRARIES gtest) |
| 150 | + set(GTEST_MAIN_LIBRARIES gtest_main) |
| 151 | + set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES}) |
| 152 | + |
| 153 | + set(GMOCK_INCLUDE_DIRS ${_gmock_root}/include) |
| 154 | + set(GMOCK_LIBRARIES gmock) |
| 155 | + set(GMOCK_MAIN_LIBRARIES gmock_main) |
| 156 | + set(GMOCK_BOTH_LIBRARIES ${GMOCK_LIBRARIES} ${GMOCK_MAIN_LIBRARIES}) |
| 157 | + |
| 158 | + set(TEST_LIBRARIES ${GMOCK_BOTH_LIBRARIES} ${GTEST_BOTH_LIBRARIES}) |
| 159 | + |
| 160 | + # Since GMock and GTest do not set |
| 161 | + # INTERFACE_SYSTEM_INCLUDE_DIRECTORIES, we do that here. This means |
| 162 | + # that any targets that reference one of these libraries will |
| 163 | + # "automatically" have the include directories for these libraries |
| 164 | + # added to their build flags. We cannot use "SYSTEM" since that is |
| 165 | + # not available in 2.8.9 (it was introduced in 2.8.12). |
| 166 | + target_include_directories(gmock PUBLIC ${GMOCK_INCLUDE_DIRS}) |
| 167 | + target_include_directories(gmock_main PUBLIC ${GMOCK_INCLUDE_DIRS}) |
| 168 | + target_include_directories(gtest PUBLIC ${GTEST_INCLUDE_DIRS}) |
| 169 | + target_include_directories(gtest_main PUBLIC ${GTEST_INCLUDE_DIRS}) |
| 170 | + |
| 171 | + if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") |
| 172 | + |
| 173 | + set (comp_flags_ "-Wno-undef -Wno-missing-field-initializers") |
| 174 | + if(COMPILER_HAS_WARNING_MISSING_FORMAT_ATTRIBUTE) |
| 175 | + set(comp_flags_ "${comp_flags_} -Wno-missing-format-attribute") |
| 176 | + endif() |
| 177 | + |
| 178 | + set_target_properties(gtest gtest_main gmock gmock_main |
| 179 | + PROPERTIES |
| 180 | + COMPILE_FLAGS "${comp_flags_}") |
| 181 | + endif() |
| 182 | + endif() |
| 183 | + endif() |
| 184 | +endif() |
| 185 | + |
25 | 186 | # Set {RUNTIME,LIBRARY}_OUTPUT_DIRECTORY properties of a target to the stage dir.
|
26 | 187 | # On unix platforms this is just one directory, but on Windows it's per build-type,
|
27 | 188 | # e.g. build/stage/Debug/lib, build/stage/Release/lib, etc
|
|
0 commit comments