googletest.cmake 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright 2020 Fangjun Kuang (csukuangfj@gmail.com)
  2. # See ../LICENSE for clarification regarding multiple authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. function(download_googltest)
  16. if(CMAKE_VERSION VERSION_LESS 3.11)
  17. # FetchContent is available since 3.11,
  18. # we've copied it to ${CMAKE_SOURCE_DIR}/cmake/Modules
  19. # so that it can be used in lower CMake versions.
  20. message(STATUS "Use FetchContent provided by sherpa-ncnn")
  21. list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)
  22. endif()
  23. include(FetchContent)
  24. set(googletest_URL "https://github.com/google/googletest/archive/release-1.10.0.tar.gz")
  25. set(googletest_HASH "SHA256=9dc9157a9a1551ec7a7e43daea9a694a0bb5fb8bec81235d8a1e6ef64c716dcb")
  26. set(BUILD_GMOCK ON CACHE BOOL "" FORCE)
  27. set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
  28. set(gtest_disable_pthreads ON CACHE BOOL "" FORCE)
  29. set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  30. FetchContent_Declare(googletest
  31. URL ${googletest_URL}
  32. URL_HASH ${googletest_HASH}
  33. )
  34. FetchContent_GetProperties(googletest)
  35. if(NOT googletest_POPULATED)
  36. message(STATUS "Downloading googletest")
  37. FetchContent_Populate(googletest)
  38. endif()
  39. message(STATUS "googletest is downloaded to ${googletest_SOURCE_DIR}")
  40. message(STATUS "googletest's binary dir is ${googletest_BINARY_DIR}")
  41. if(APPLE)
  42. set(CMAKE_MACOSX_RPATH ON) # to solve the following warning on macOS
  43. endif()
  44. #[==[
  45. -- Generating done
  46. Policy CMP0042 is not set: MACOSX_RPATH is enabled by default. Run "cmake
  47. --help-policy CMP0042" for policy details. Use the cmake_policy command to
  48. set the policy and suppress this warning.
  49. MACOSX_RPATH is not specified for the following targets:
  50. gmock
  51. gmock_main
  52. gtest
  53. gtest_main
  54. This warning is for project developers. Use -Wno-dev to suppress it.
  55. ]==]
  56. add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)
  57. target_include_directories(gtest
  58. INTERFACE
  59. ${googletest_SOURCE_DIR}/googletest/include
  60. ${googletest_SOURCE_DIR}/googlemock/include
  61. )
  62. endfunction()
  63. download_googltest()