googletest.cmake (2543B)
1 function(download_googltest) 2 if(CMAKE_VERSION VERSION_LESS 3.11) 3 list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules) 4 endif() 5 6 include(FetchContent) 7 8 set(googletest_URL "https://github.com/google/googletest/archive/refs/tags/v1.13.0.tar.gz") 9 set(googletest_URL2 "https://huggingface.co/csukuangfj/k2-cmake-deps/resolve/main/googletest-1.13.0.tar.gz") 10 set(googletest_HASH "SHA256=ad7fdba11ea011c1d925b3289cf4af2c66a352e18d4c7264392fead75e919363") 11 12 # If you don't have access to the Internet, 13 # please pre-download googletest 14 set(possible_file_locations 15 $ENV{HOME}/Downloads/googletest-1.13.0.tar.gz 16 ${PROJECT_SOURCE_DIR}/googletest-1.13.0.tar.gz 17 ${PROJECT_BINARY_DIR}/googletest-1.13.0.tar.gz 18 /tmp/googletest-1.13.0.tar.gz 19 /star-fj/fangjun/download/github/googletest-1.13.0.tar.gz 20 ) 21 22 foreach(f IN LISTS possible_file_locations) 23 if(EXISTS ${f}) 24 set(googletest_URL "${f}") 25 file(TO_CMAKE_PATH "${googletest_URL}" googletest_URL) 26 set(googletest_URL2) 27 break() 28 endif() 29 endforeach() 30 31 set(BUILD_GMOCK ON CACHE BOOL "" FORCE) 32 set(INSTALL_GTEST OFF CACHE BOOL "" FORCE) 33 set(gtest_disable_pthreads ON CACHE BOOL "" FORCE) 34 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) 35 36 FetchContent_Declare(googletest 37 URL 38 ${googletest_URL} 39 ${googletest_URL2} 40 URL_HASH ${googletest_HASH} 41 ) 42 43 FetchContent_GetProperties(googletest) 44 if(NOT googletest_POPULATED) 45 message(STATUS "Downloading googletest from ${googletest_URL}") 46 FetchContent_Populate(googletest) 47 endif() 48 message(STATUS "googletest is downloaded to ${googletest_SOURCE_DIR}") 49 message(STATUS "googletest's binary dir is ${googletest_BINARY_DIR}") 50 51 if(APPLE) 52 set(CMAKE_MACOSX_RPATH ON) # to solve the following warning on macOS 53 endif() 54 #[==[ 55 -- Generating done 56 Policy CMP0042 is not set: MACOSX_RPATH is enabled by default. Run "cmake 57 --help-policy CMP0042" for policy details. Use the cmake_policy command to 58 set the policy and suppress this warning. 59 60 MACOSX_RPATH is not specified for the following targets: 61 62 gmock 63 gmock_main 64 gtest 65 gtest_main 66 67 This warning is for project developers. Use -Wno-dev to suppress it. 68 ]==] 69 70 add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL) 71 72 target_include_directories(gtest 73 INTERFACE 74 ${googletest_SOURCE_DIR}/googletest/include 75 ${googletest_SOURCE_DIR}/googlemock/include 76 ) 77 endfunction() 78 79 download_googltest()