0

I am trying to make sense of previous help:

So I wrote a dead simple use-case of a project using gtest.

Currently I have:

% cat CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
project(p)

include(FetchContent)
FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG        58d77fa8070e8cec2dc1ed015d66b454c8d78850 # release-1.12.1
  OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(googletest)

if(TARGET GTest::gtest_main)
  message("Target was found")
endif()

find_package(GTest 1.12.1 REQUIRED)

# Now make sure hwy uses our gtest 1.12.1:
set(HWY_SYSTEM_GTEST ON CACHE BOOL "")
FetchContent_Declare(
  hwy
  GIT_REPOSITORY https://github.com/google/highway.git
  GIT_TAG        22e3d7276f4157d4a47586ba9fd91dd6303f441a # 1.0.1
)
FetchContent_MakeAvailable(hwy)

If I run it, here is what I get:

$ cmake ..
[...]
Target was found
-- Found GTest: /usr/lib/x86_64-linux-gnu/libgtest.a (Required is at least version "1.12.1")
[...]
-- Found GTest: /usr/lib/x86_64-linux-gnu/cmake/GTest/GTestConfig.cmake (found version "1.10.0")

The output is total non-sense to me. I'd would like to re-inject the build tree of googletest 1.12.1 during building of hwy 1.0.1. My question is as follow: how should I do that in the above example ?

The original upstream documentation is quite unclear about integrating FetchContent_Declare with find_package().


The important part of the question was the following two lines:

# Now make sure hwy uses our gtest 1.12.1:
set(HWY_SYSTEM_GTEST ON CACHE BOOL "")

The mechanism to re-inject gtest into hwy. My question was not about simple direct usage of gtest through fetchcontent.

malat
  • 12,152
  • 13
  • 89
  • 158

2 Answers2

1

Here is what works for me (using cmake 3.24.1)

cmake_minimum_required(VERSION 3.24)
project(p)

include(FetchContent)
FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG 58d77fa8070e8cec2dc1ed015d66b454c8d78850 # release-1.12.1
  OVERRIDE_FIND_PACKAGE)
FetchContent_MakeAvailable(googletest)

if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config.cmake
   AND NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/GTestConfig.cmake)
  file(
    WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config.cmake
    [=[
include(CMakeFindDependencyMacro)
find_dependency(googletest)
if(NOT TARGET GTest::GTest)
  add_library(GTest::GTest INTERFACE IMPORTED)
  target_link_libraries(GTest::GTest INTERFACE GTest::gtest)
endif()
if(NOT TARGET GTest::Main)
  add_library(GTest::Main INTERFACE IMPORTED)
  target_link_libraries(GTest::Main INTERFACE GTest::gtest_main)
endif()
]=])
endif()

if(NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config-version.cmake
   AND NOT EXISTS ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/GTestConfigVersion.cmake)
  file(
    WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/gtest-config-version.cmake
    [=[
include(${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletest-config-version.cmake OPTIONAL)
if(NOT PACKAGE_VERSION_COMPATIBLE)
  include(${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/googletestConfigVersion.cmake OPTIONAL)
endif()
]=])
endif()

FetchContent_Declare(
  hwy
  GIT_REPOSITORY https://github.com/google/highway.git
  GIT_TAG 22e3d7276f4157d4a47586ba9fd91dd6303f441a # 1.0.1
)
# Now make sure hwy uses our gtest 1.12.1:
set(HWY_SYSTEM_GTEST
    ON
    CACHE BOOL "")
FetchContent_MakeAvailable(hwy)

Just for reference, hwy is now supporting new style gtest since commit:

malat
  • 12,152
  • 13
  • 89
  • 158
0

The official documentation says:

When a FetchContent_Declare(<name> ...) call includes this option, subsequent calls to find_package(<name> ...) will ensure that FetchContent_MakeAvailable() has been called

You are using different names for FetchContent_Declare and find_package. Using find_package(googletest 1.12.1 REQUIRED) will do.

Unfortunately, there is a "bug" in the cmake scripts shipped with googletest, so that you can't use GTest with FetchContent_Declare. It uses GTest_FOUND and GMock_FOUND for the sub-projects, therefore you can't use GTest for the parent project (name conflict).

local-ninja
  • 1,198
  • 4
  • 11