include (ExternalProject)

set(googletest_URL https://github.com/google/googletest/archive/release-1.11.0.tar.gz)
use_mirror(VARIABLE googletest_URL URL ${googletest_URL})

set(GTEST_INSTALL_DIR ${THIRD_PARTY_DIR}/gtest)
set(GTEST_INSTALL_INCLUDEDIR include)
set(GTEST_INSTALL_LIBDIR lib)
set(GTEST_INSTALL_BINDIR bin)
set(GTEST_INCLUDE_DIR ${GTEST_INSTALL_DIR}/${GTEST_INSTALL_INCLUDEDIR})
set(GTEST_LIBRARY_DIR ${GTEST_INSTALL_DIR}/${GTEST_INSTALL_LIBDIR})
set(GTEST_BINARY_DIR ${GTEST_INSTALL_DIR}/${GTEST_INSTALL_BINDIR})

# gtest has a fix CMAKE_DEBUG_POSTFIX "d"
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  set(gtest_CMAKE_DEBUG_POSTFIX "d")
endif()

if(WIN32)
    set(GOOGLETEST_LIBRARY_NAMES gtest${gtest_CMAKE_DEBUG_POSTFIX}.lib gtest_main${gtest_CMAKE_DEBUG_POSTFIX}.lib)
    set(GOOGLEMOCK_LIBRARY_NAMES gmock${gtest_CMAKE_DEBUG_POSTFIX}.lib gmock_main${gtest_CMAKE_DEBUG_POSTFIX}.lib)
elseif(APPLE AND ("${CMAKE_GENERATOR}" STREQUAL "Xcode"))
    set(GOOGLETEST_LIBRARY_NAMES libgtest${gtest_CMAKE_DEBUG_POSTFIX}.a libgtest_main${gtest_CMAKE_DEBUG_POSTFIX}.a)
    set(GOOGLEMOCK_LIBRARY_NAMES libgmock${gtest_CMAKE_DEBUG_POSTFIX}.a libgmock_main${gtest_CMAKE_DEBUG_POSTFIX}.a)
else()
    set(GOOGLETEST_LIBRARY_NAMES libgtest${gtest_CMAKE_DEBUG_POSTFIX}.a libgtest_main${gtest_CMAKE_DEBUG_POSTFIX}.a)
    set(GOOGLEMOCK_LIBRARY_NAMES libgmock${gtest_CMAKE_DEBUG_POSTFIX}.a libgmock_main${gtest_CMAKE_DEBUG_POSTFIX}.a)
endif()

foreach(LIBRARY_NAME ${GOOGLETEST_LIBRARY_NAMES})
    list(APPEND GOOGLETEST_STATIC_LIBRARIES ${GTEST_LIBRARY_DIR}/${LIBRARY_NAME})
endforeach()

foreach(LIBRARY_NAME ${GOOGLEMOCK_LIBRARY_NAMES})
    list(APPEND GOOGLEMOCK_STATIC_LIBRARIES ${GTEST_LIBRARY_DIR}/${LIBRARY_NAME})
endforeach()

set(GOOGLETEST_INCLUDE_DIR ${GTEST_INCLUDE_DIR})
set(GOOGLEMOCK_INCLUDE_DIR ${GTEST_INCLUDE_DIR})

if(THIRD_PARTY)

ExternalProject_Add(googletest
    PREFIX googletest
    URL ${googletest_URL}
    URL_MD5 e8a8df240b6938bb6384155d4c37d937
    UPDATE_COMMAND ""
    BUILD_IN_SOURCE 1
    BUILD_BYPRODUCTS ${GOOGLETEST_STATIC_LIBRARIES} ${GOOGLEMOCK_STATIC_LIBRARIES}
    CMAKE_CACHE_ARGS
        -DCMAKE_C_COMPILER_LAUNCHER:STRING=${CMAKE_C_COMPILER_LAUNCHER}
        -DCMAKE_CXX_COMPILER_LAUNCHER:STRING=${CMAKE_CXX_COMPILER_LAUNCHER}
        -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
        -DCMAKE_CXX_FLAGS_DEBUG:STRING=${CMAKE_CXX_FLAGS_DEBUG}
        -DCMAKE_CXX_FLAGS_RELEASE:STRING=${CMAKE_CXX_FLAGS_RELEASE}
        -DCMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=${CMAKE_CXX_FLAGS_RELWITHDEBINFO}
        -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}
        -DBUILD_GMOCK:BOOL=ON
	      -DBUILD_GTEST:BOOL=OFF  # gmock includes gtest
        -DCMAKE_CXX_FLAGS_DEBUG:STRING=${CMAKE_CXX_FLAGS_DEBUG}
        -DCMAKE_INSTALL_PREFIX:STRING=${GTEST_INSTALL_DIR}
        -DCMAKE_INSTALL_INCLUDEDIR:STRING=${GTEST_INSTALL_INCLUDEDIR}
        -DCMAKE_INSTALL_LIBDIR:STRING=${GTEST_INSTALL_LIBDIR}
        -DCMAKE_INSTALL_BINDIR:STRING=${GTEST_INSTALL_BINDIR}
        -DCMAKE_INSTALL_MESSAGE:STRING=${CMAKE_INSTALL_MESSAGE}
        #-Dgtest_force_shared_crt:BOOL=ON  #default value is OFF
)

endif(THIRD_PARTY)
