1 # It is recommended that projects build GTest/GMock themselves, rather
2 # than relying on system-provided libraries. This is to ensure that the
3 # same build configuration is used for both test cases and the
6 # This find module includes bundled sources to build the GTest and GMock
7 # libraries. Since including the provided CMake build system as a
8 # subproject would not allow for proper target installation,
9 # and linking GTest as a static library poses a special kind of hell,
10 # we rely on GTest build system internals to setup targets under our
13 # This means that this find module will only work properly as long as
14 # the layout and the internals of the provided GTest/GMock sources
15 # do not change in incompatible ways. Thus, if the bundled sources
16 # are updated, this find script may have to be adapted, too.
18 # This find module defines one alias target GMock::GMock to depend on.
19 ########################################################################
21 include(FindPackageHandleStandardArgs)
23 set(GTEST_VERSION "1.8.1")
25 set(GTEST_BASE_DIR "${CMAKE_SOURCE_DIR}/3rdparty/googletest-${GTEST_VERSION}")
26 set(GTEST_SRC_DIR "${GTEST_BASE_DIR}/googletest")
27 set(GTEST_INC_DIR "${GTEST_BASE_DIR}/googletest/include")
28 set(GMOCK_SRC_DIR "${GTEST_BASE_DIR}/googlemock")
29 set(GMOCK_INC_DIR "${GTEST_BASE_DIR}/googlemock/include")
31 # We don't install our tests
32 set(INSTALL_GTEST OFF)
34 # Link against the runtime dynamically on Windows
35 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
37 # Here's where it gets shaky... include GTest's CMake macros, which
38 # we'll use to setup the target with the expected compile flags.
39 include(${GTEST_SRC_DIR}/cmake/internal_utils.cmake)
41 # Provided by GTest, configure compile flags
42 config_compiler_and_linker()
44 # Create target using GTest's macro and compile flags
46 cxx_shared_library(${TARGET}
48 ${GTEST_SRC_DIR}/src/gtest-all.cc
49 ${GMOCK_SRC_DIR}/src/gmock-all.cc
52 # Alias to follow our convention of namespaced targets
53 add_library(GTest::GTest ALIAS gtest)
56 target_include_directories(${TARGET} SYSTEM BEFORE
65 # Ensure symbols are exported
66 target_compile_definitions(${TARGET} INTERFACE -DGTEST_LINKED_AS_SHARED_LIBRARY=1)
68 # Add support for find_package
69 find_package_handle_standard_args(GTest DEFAULT_MSG GTEST_INC_DIR)