From ae36f37e8372b238f5e51cd94b17d06b3d061f0e Mon Sep 17 00:00:00 2001 From: heinrich5991 Date: Fri, 8 Feb 2019 09:50:25 +0100 Subject: [PATCH] Automatically download GTest on Windows --- CMakeLists.txt | 65 +++++++++++++++++++++++++- cmake/Download_GTest_CMakeLists.txt.in | 16 +++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 cmake/Download_GTest_CMakeLists.txt.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 92c8c2ce4..20e46dd55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ endif() option(CLIENT "Compile client" ON) option(DOWNLOAD_DEPENDENCIES "Download dependencies (only available on Windows)" ${AUTO_DEPENDENCIES_DEFAULT}) +option(DOWNLOAD_GTEST "Download and compile GTest if not found" ${AUTO_DEPENDENCIES_DEFAULT}) option(PREFER_BUNDLED_LIBS "Prefer bundled libraries over system libraries" ${AUTO_DEPENDENCIES_DEFAULT}) option(DEV "Don't generate stuff necessary for packaging" OFF) @@ -348,7 +349,16 @@ if(CLIENT AND NOT(SDL2_FOUND)) message(SEND_ERROR "You must install SDL2 to compile the Teeworlds client") endif() if(NOT(GTEST_FOUND)) - message(STATUS "To run the tests, you have to install GTest") + if(DOWNLOAD_GTEST) + if(GIT_FOUND) + message(STATUS "Automatically downloading GTest to be able to run tests") + else() + set(DOWNLOAD_GTEST OFF) + message(WARNING "To automatically download GTest, you have to install Git") + endif() + else() + message(STATUS "To run the tests, you have to install GTest") + endif() endif() if(TARGET_OS STREQUAL "windows") @@ -375,6 +385,57 @@ else() endif() endif() +######################################################################## +# DOWNLOAD GTEST +######################################################################## + +if(NOT(GTEST_FOUND) AND DOWNLOAD_GTEST) + set(TEEWORLDS_GTEST_VERSION release-1.8.1) + configure_file(cmake/Download_GTest_CMakeLists.txt.in googletest-download/CMakeLists.txt) + execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE result + WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/googletest-download + ) + if(result) + message(WARNING "CMake step for googletest failed: ${result}") + set(DOWNLOAD_GTEST OFF) + else() + execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/googletest-download + ) + if(result) + message(WARNING "Build step for googletest failed: ${result}") + set(DOWNLOAD_GTEST OFF) + else() + # Prevent overriding the parent project's compiler/linker settings on Windows + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + + # Add googletest directly to our build. This defines the gtest target. + add_subdirectory( + ${PROJECT_BINARY_DIR}/googletest-src + ${PROJECT_BINARY_DIR}/googletest-build + EXCLUDE_FROM_ALL + ) + + if(MSVC) + foreach(target gtest) + # `/w` disables all warnings. This is needed because `gtest` enables + # `/WX` (equivalent of `-Werror`) for some reason, breaking builds + # when MSVS adds new warnings. + target_compile_options(${target} PRIVATE $<$:/MT> $<${DBG}:/MTd> /w) + endforeach() + endif() + + set(GTEST_LIBRARIES gtest) + set(GTEST_INCLUDE_DIRS) + if(CMAKE_VERSION VERSION_LESS 2.8.11) + set(GTEST_INCLUDE_DIRS "${gtest_SOURCE_DIR}/include") + endif() + endif() + endif() +endif() + ######################################################################## # DEPENDENCY COMPILATION ######################################################################## @@ -1486,7 +1547,7 @@ add_custom_target(everything DEPENDS ${TARGETS_OWN}) # TESTS ######################################################################## -if(GTEST_FOUND) +if(GTEST_FOUND OR DOWNLOAD_GTEST) set_src(TESTS GLOB src/test fs.cpp git_revision.cpp diff --git a/cmake/Download_GTest_CMakeLists.txt.in b/cmake/Download_GTest_CMakeLists.txt.in new file mode 100644 index 000000000..c1a176b02 --- /dev/null +++ b/cmake/Download_GTest_CMakeLists.txt.in @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 2.8) + +project(googletest-download NONE) + +include(ExternalProject) +ExternalProject_Add(googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG "${TEEWORLDS_GTEST_VERSION}" + SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src" + BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" + TLS_VERIFY ON +)