Avoid FetchContent when conan has already downloaded targets from package

114 Views Asked by At

I have included a test application which tests the main project using gtest. I want these tests to be executable in both cases, using cmake alone or conan with cmake. So I defined a dependency on gtest in conanfile as follows:

conanfile.py

..
def requirements(self):
    self.test_requires("gtest/1.14.0")
..

As I want the app to also be testable when NOT using conan, I included a call to FetchContent in the tests CMakeLists.txt.

tests/CMakeLists.txt

## Dependencies

..
# googletest
include(FetchContent)
FetchContent_Declare(googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG v1.14.x
  FIND_PACKAGE_ARGS NAMES GTest
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
..

As you can see I defined FIND_PACKAGE_ARGS NAMES GTest so that CMake should check if a target with this name exists before it downloads the gtest git repo. However, it turns out the download is always performed, even when I'm building my project with conan. My initial thought was that conan would download the gtest package and export the targets, so that a subsequent build of the project would settle for the already found target instead of fetching the whole thing again. Why is it not working and how can I account for this case?

0

There are 0 best solutions below