CMake ExternalData custom fetch script

551 views Asked by At

Introduction

I have a project with a "tests" target that requires some extracted assets from a remote source to be present in the build tree at run-time. At present I have used ExternalData to, at build time, retrieve a zipped file from a remote url and place it into the build tree.

/tests/CMakeLists.txt :

include(ExternalData)
set(ExternalData_URL_TEMPLATES "https://data.com/path/samples.zip")

# [...]

ExternalData_Add_Test(MyData
   NAME MyTest
   COMMAND MyProjectTests DATA{samples.zip})
ExternalData_Add_Target(MyData)

(In the source tree I have a file /tests/samples.zip.md5 which contains the md5 of the archive.)

Question

I would like to extract this archive after it is retrieved. From reading the CMake documentation there appears to be a way to use a custom fetch script when retrieving from a remote.

The documentation does not show an example of a custom fetch script. It mentions that the file(DOWNLOAD) command is used normally, but it does not show explicitly how its default behaviour could be replicated by a custom fetch script.

I wish to implement and use a custom fetch script that replicates ExternalData's default behaviour, so that I can add a file(ARCHIVE_EXTRACT) command to extract the archive.

(I would also like to know whether extracting downloaded files should be a responsibility of a custom fetch script, and if not, where the best place is to put this is, given that the asset is only downloaded at build time).

1

There are 1 answers

0
geo On

I have not managed to replicate the default behaviour of ExternalData's fetch script. However, using add_custom_command(), I have extracted the test assets as a POST_BUILD step.

Appending to /tests/CMakeLists.txt :

set(ASSETS_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/assets)
file(MAKE_DIRECTORY ${ASSETS_DIRECTORY})
set(ExternalData_BINARY_ROOT ${ASSETS_DIRECTORY})

add_custom_command(TARGET waveioTests
                   POST_BUILD
                   COMMAND 
                   dir & 
                   echo "${CMAKE_COMMAND} -E copy samples.tar ${ASSETS_DIRECTORY}/samples.tar" & 
                   ${CMAKE_COMMAND} -E copy samples.tar ${ASSETS_DIRECTORY}/samples.tar & 
                   ${CMAKE_COMMAND} -E chdir ${ASSETS_DIRECTORY} tar vxf samples.tar
                   COMMENT "Extracting test assets...")