gtest-cpp.cmake 2.0 KB
Newer Older
1 2
find_package(Git REQUIRED)
message("${CMAKE_BUILD_TYPE}")
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
set(GTEST_PREFIX_DIR ${CMAKE_CURRENT_BINARY_DIR}/gtest)
set(GTEST_SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/gtest/src/extern_gtest)
set(GTEST_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/install/gtest)
set(GTEST_INCLUDE_DIR
    "${GTEST_INSTALL_DIR}/include"
    CACHE PATH "gtest include directory." FORCE)
set(GTEST_REPOSITORY https://github.com/google/googletest.git)
set(GTEST_TAG release-1.8.1)
include_directories(${GTEST_INCLUDE_DIR})
if(WIN32)
  # if use CMAKE_INSTALL_LIBDIR, the path of lib actually is install/gtest/lib/gtest.lib but GTEST_LIBRARIES
  # is install/gtest/gtest.lib
  set(GTEST_LIBRARIES
      "${GTEST_INSTALL_DIR}/lib/gtest.lib"
      CACHE FILEPATH "gtest libraries." FORCE)
  set(GTEST_MAIN_LIBRARIES
      "${GTEST_INSTALL_DIR}/lib/gtest_main.lib"
      CACHE FILEPATH "gtest main libraries." FORCE)
else()
  set(GTEST_LIBRARIES
      "${GTEST_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/libgtest.a"
      CACHE FILEPATH "gtest libraries." FORCE)
  set(GTEST_MAIN_LIBRARIES
      "${GTEST_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/libgtest_main.a"
      CACHE FILEPATH "gtest main libraries." FORCE)
endif(WIN32)
29
ExternalProject_Add(
30 31 32 33 34 35 36 37 38 39 40
  extern_gtest
  PREFIX gtest
  GIT_REPOSITORY ${GTEST_REPOSITORY}
  GIT_TAG ${GTEST_TAG}
  DOWNLOAD_DIR "${DOWNLOAD_LOCATION}"
  UPDATE_COMMAND ""
  CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${GTEST_INSTALL_DIR}
             -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
             -DCMAKE_BUILD_TYPE:STRING=Release
  BUILD_BYPRODUCTS ${GTEST_LIBRARIES}
  BUILD_BYPRODUCTS ${GTEST_MAIN_LIBRARIES})
41

42 43 44 45
add_library(thirdparty_gtest STATIC IMPORTED GLOBAL)
set_property(TARGET thirdparty_gtest PROPERTY IMPORTED_LOCATION
                                              ${GTEST_LIBRARIES})
add_dependencies(thirdparty_gtest extern_gtest)
46

47 48 49 50
add_library(thirdparty_gtest_main STATIC IMPORTED GLOBAL)
set_property(TARGET thirdparty_gtest_main PROPERTY IMPORTED_LOCATION
                                                   ${GTEST_MAIN_LIBRARIES})
add_dependencies(thirdparty_gtest_main extern_gtest)