You may want to consider the approach used in the DownloadProject github project which includes an example that does a download step for the GoogleTest repo. It invokes CMake's ExternalProject
module during the configure stage rather than the build stage, so the external sources become available at configure time, which seems to be what you want here too. In your case, you don't need the subsequent step of using add_subdirectory()
to pull anything from the external project into your main build, so just the download is enough. This approach is also referenced by this answer.
Using DownloadProject
looks something like this (taken from the example provided as part of the github project):
include(DownloadProject.cmake)
download_project(PROJ googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
)
The downloaded source would then be available under the directory stored in the variable ${googletest_SOURCE_DIR}
. See the fully general implementation in the DownloadProject
source for how it works. You can simply embed it in your own project's sources and re-use it to efficiently handle downloading any external dependencies you might need.