Reputation: 85
CMake, through its ExternalProject
, makes it easy to download huge archives during a build, keeping repositories clean. As an example, I can have Eigen downloaded and unpacked like this:
include(ExternalProject)
ExternalProject_Add(
eigen
URL http://bitbucket.org/eigen/eigen/get/3.2.8.tar.bz2
URL_HASH SHA512=53c27ba530c985dfef52188e03273eeef33abbc67e3f150cacd3371c8b9ddbd399228730595821c4c56c061d109cf509266c1dab2b8a7c730902cbd6fb18c100
INSTALL_COMMAND ""
)
I would like to be able to do the same for arbitrary files, in my case to download additional data for demonstration purposes. CMake does offer file(DOWNLOAD...)
for this purpose, and it works well enough, e.g.:
file(DOWNLOAD
http://graphics.stanford.edu/pub/3Dscanrep/bunny.tar.gz
${CMAKE_BINARY_DIR}/demo/bunny.tar.gz
SHA512=59e7b43db838dbe6f02fda5e844d18e190d32d2ca1a83dc9f6b1aaed43e0340fc1e8ecabed6fffdac9f85bd34e7e93b5d8a17283d59ea3c14977a0372785d2bd
SHOW_PROGRESS
)
add_custom_target(demo tar -xzf ${CMAKE_BINARY_DIR}/demo/bunny.tar.gz -C ${CMAKE_BINARY_DIR}/demo)
This downloads an archive, validates it and -- with make demo
-- extracts it. However, the archive is downloaded every time CMake is run, not just once, and it does not depend on the target demo
being invoked.
Can this somehow be achieved with CMake? I don't think that ExternalData
will help, because I really just want to download arbitrary files from the internet.
Upvotes: 8
Views: 13861
Reputation: 8517
However, the archive is downloaded every time CMake is run, not just once, and it does not depend on the target demo being invoked.
No wonder why. You're not adding the target eigen
or whatever as a dependency for demo
.
So, something like following should work:
ExternalProject_Add(
bunny
PREFIX "demo"
URL http://graphics.stanford.edu/pub/3Dscanrep/bunny.tar.gz
URL_HASH SHA512=59e7b43db838dbe6f02fda5e844d18e190d32d2ca1a83dc9f6b1aaed43e0340fc1e8ecabed6fffdac9f85bd34e7e93b5d8a17283d59ea3c14977a0372785d2bd
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
NOTE:
Target produced by ExternalProject_Add
extracts archives by default. And You're downloading archive called bunny.tar.gz
which contains another src/bunny.tar.gz
bundled inside.
EDIT: If we're talking about newer-ish CMake (>=3.11) You should also take a look at FetchContent() (official docs), which may take some burdens away as well.
Upvotes: 4