Pull in CMake Files via FetchContent

Another variant would be to use FetchContent_MakeAvailable() (it’s shorter and makes this suggestion simpler) and call it in both places (dependencies and tests). The call in dependencies will do the actual download. The call in tests will do nothing because the download has already been done, but it will still set the output variables that give the locations of the source dir (and others). Then you use the source dir location to populate the CMAKE_MODULE_PATH variable just like you were before, but now you’re doing it in the directory scope where you actually want/need it. It’s pretty short and clear:

# In the tests directory
FetchContent_MakeAvailable(catch2)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/contrib)
1 Like