Findgoogletest.cmake 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. # GoogleTest and GoogleMock used to be two different repositories, but they are now combined into one repository.
  9. # This script fetches the combined repository and builds both libraries.
  10. if (TARGET 3rdParty::googletest::GTest)
  11. return()
  12. endif()
  13. if (TARGET 3rdParty::googletest::GMock)
  14. return()
  15. endif()
  16. # You should not be generating dependencies on googletest (via AzTest)
  17. # if you are on a platform that cannot actually compile googletest (See cmake/Platform/platformname/PAL_platformname.cmake)
  18. if (NOT PAL_TRAIT_TEST_GOOGLE_TEST_SUPPORTED)
  19. return()
  20. endif()
  21. set(GOOGLETEST_GIT_REPOSITORY "https://github.com/google/googletest.git")
  22. set(GOOGLETEST_GIT_TAG b514bdc898e2951020cbdca1304b75f5950d1f59) # tag name is v1.15.2
  23. set(GOOGLETEST_VERSION_STRING "v1.15.2")
  24. message(STATUS "AzTest uses googletest ${GOOGLETEST_VERSION_STRING} (BSD-3-Clause) from ${GOOGLETEST_GIT_REPOSITORY}")
  25. # there are optional flags you can pass FetchContent that make the project cleaner but require newer versions of cmake
  26. set(ADDITIONAL_FETCHCONTENT_FLAGS "")
  27. if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.25")
  28. list(APPEND ADDITIONAL_FETCHCONTENT_FLAGS "SYSTEM") # treat it as a 3p library, that is, do not issue warnings using the same warning level
  29. endif()
  30. set(CMAKE_POLICY_DEFAULT_CMP0148 OLD)
  31. set(OLD_CMAKE_WARN_DEPRECATED ${CMAKE_WARN_DEPRECATED})
  32. set(CMAKE_WARN_DEPRECATED FALSE CACHE BOOL "" FORCE)
  33. include(FetchContent)
  34. FetchContent_Declare(
  35. googletest
  36. GIT_REPOSITORY ${GOOGLETEST_GIT_REPOSITORY}
  37. GIT_TAG ${GOOGLETEST_GIT_TAG}
  38. ${ADDITIONAL_FETCHCONTENT_FLAGS}
  39. GIT_SHALLOW TRUE
  40. )
  41. # CMAKE_ARGS don't actually affect FetchContent (currently by design) https://gitlab.kitware.com/cmake/cmake/-/issues/20799
  42. set(PYTHONINTERP_FOUND ON)
  43. set(PYTHON_EXECUTABLE ${Python_EXECUTABLE})
  44. set(PYTHON_VERSION_STRING ${Python_VERSION})
  45. set(gtest_force_shared_crt ON)
  46. set(BUILD_GMOCK ON)
  47. set(BUILD_GTEST ON)
  48. set(INSTALL_GTEST OFF)
  49. set(gmock_build_tests OFF)
  50. set(gtest_build_tests OFF)
  51. set(gtest_build_samples OFF)
  52. set(gtest_hide_internal_symbols ON)
  53. # Save values that apply globally that the 3rd party library may mess with
  54. # Some of these are null, hence the set(xxx "quoted value") so that if it isn't set it becomes the empty string.
  55. set(CMAKE_WARN_DEPRECATED OFF CACHE BOOL "" FORCE)
  56. set(OLD_CMAKE_CXX_VISIBILITY_PRESET "${CMAKE_CXX_VISIBILITY_PRESET}")
  57. set(OLD_CMAKE_VISIBILITY_INLINES_HIDDEN "${CMAKE_VISIBILITY_INLINES_HIDDEN}")
  58. FetchContent_MakeAvailable(googletest)
  59. set(CMAKE_WARN_DEPRECATED ON CACHE BOOL "" FORCE)
  60. if (NOT "${OLD_CMAKE_CXX_VISIBILITY_PRESET}" STREQUAL "")
  61. set(CMAKE_CXX_VISIBILITY_PRESET "${OLD_CMAKE_CXX_VISIBILITY_PRESET}")
  62. else()
  63. unset(CMAKE_CXX_VISIBILITY_PRESET)
  64. endif()
  65. if (NOT "${OLD_CMAKE_VISIBILITY_INLINES_HIDDEN}" STREQUAL "")
  66. set(CMAKE_VISIBILITY_INLINES_HIDDEN "${OLD_CMAKE_VISIBILITY_INLINES_HIDDEN}")
  67. else()
  68. unset(CMAKE_VISIBILITY_INLINES_HIDDEN)
  69. endif()
  70. # Let's not clutter the root of any IDE folder structure with 3rd party dependencies
  71. # Setting the FOLDER makes it show up there in the solution build in VS and similarly
  72. # any other IDEs that organize in folders.
  73. set_target_properties(
  74. gtest
  75. gmock
  76. gtest_main
  77. gmock_main
  78. PROPERTIES
  79. FOLDER "3rdParty Dependencies")
  80. unset(CMAKE_POLICY_DEFAULT_CMP0148)
  81. set(CMAKE_WARN_DEPRECATED ${OLD_CMAKE_WARN_DEPRECATED} CACHE BOOL "" FORCE)
  82. FetchContent_GetProperties(
  83. googletest
  84. SOURCE_DIR googletest_source_dir)
  85. # GoogleTest and GoogleMock are considered public API - accessed by adding a dependency to AZ::AzTest.
  86. # This means we DO need to ship the include folders, in the installer and we do need the static libs, in case you link to AzTest.
  87. # By Default, no headers or includes are actually shipped, so we need to explicitly add them using ly_install.
  88. # doing so also helps us split them up between debug and non-debug versions
  89. # install the headers
  90. # note that source files include these headers using the name of the library, for example, they do
  91. # #include <gtest/gtest.h> and #include <gmock/gmock.h>, not just <gmock.h> or <gtest.h>
  92. # One option would be to just put these folders in (root)/include/gtest and (root)/include/gmock
  93. # and then add (root)/include as the include path for targets using these libraries.
  94. # However, that would cause any OTHER files in (root)/include to be visible to the compiler when all
  95. # you asked for is gmock/gtest. So to avoid this, we instead copy the include files into
  96. # install/gmock_gtest/gmock
  97. # install/gmock_gtest/gtest
  98. # and have install/gmock_gtest be the include path that is added to your compiler settings when you use these libraries,
  99. # so that #include <gtest/gtest> still works (since its relative to that path) BUT it adds no additional other
  100. # files or libraries to your include path that would otherwise leak in.
  101. ly_install(DIRECTORY ${googletest_source_dir}/googletest/include/gtest DESTINATION include/gmock_gtest COMPONENT CORE)
  102. ly_install(DIRECTORY ${googletest_source_dir}/googlemock/include/gmock DESTINATION include/gmock_gtest COMPONENT CORE)
  103. # include the license files just for good measure (Although using the library will disclose the
  104. # source git repository and version anyway.
  105. ly_install(FILES ${googletest_source_dir}/LICENSE COMPONENT CORE DESTINATION include/gmock_gtest/gtest)
  106. ly_install(FILES ${googletest_source_dir}/LICENSE COMPONENT CORE DESTINATION include/gmock_gtest/gmock)
  107. # Make the installer find-files be used when in an installer pre-built mode
  108. ly_install(FILES ${CMAKE_CURRENT_LIST_DIR}/Installer/Findgoogletest.cmake DESTINATION cmake/3rdParty)
  109. # install the libraries making sure to use different directories for debug/release/etc
  110. set(BASE_LIBRARY_FOLDER "lib/${PAL_PLATFORM_NAME}")
  111. foreach(conf IN LISTS CMAKE_CONFIGURATION_TYPES)
  112. string(TOUPPER ${conf} UCONF)
  113. ly_install(TARGETS gtest gmock
  114. ARCHIVE
  115. DESTINATION "${BASE_LIBRARY_FOLDER}/${conf}"
  116. COMPONENT ${LY_INSTALL_PERMUTATION_COMPONENT}_${UCONF}
  117. CONFIGURATIONS ${conf}
  118. )
  119. endforeach()
  120. # do not use ly_create_alias as that will autogenerate duplicate calls inside the installer.
  121. # ly_create_alias would create "GTest", "GMock" and "GTest::GTest" and "GMock::GMock" aliases, as well
  122. # as add them to an internal list of targets to autogenerate imported targets for.
  123. # That's okay if you're going to let the installer do its own thing, but in our case, we're including
  124. # a FindXXXX in the installer, so create them directly, without calling the LY macro
  125. add_library(3rdParty::googletest::GTest ALIAS gtest)
  126. add_library(3rdParty::googletest::GMock ALIAS gmock)
  127. set(googletest_FOUND TRUE)