1# Copyright (c) 2020 - 2024 David Guibert
4# SPDX-License-Identifier: Apache-2.0
6# https://cmake.org/cmake/help/latest/module/ExternalData.html#custom-fetch-scripts
7#ExternalData_CUSTOM_LOCATION
8# When a custom fetch script is loaded, this variable is set to the location part of the URL, which will contain the substituted hash algorithm name and content hash value.
10#ExternalData_CUSTOM_FILE
11# When a custom fetch script is loaded, this variable is set to the full path to a file in which the script must store the fetched content. The name of the file is unspecified and should not be interpreted in any way.
12# The custom fetch script is expected to store fetched content in the file or set a variable:
13# ExternalData_CUSTOM_ERROR
14# When a custom fetch script fails to fetch the requested content, it must set this variable to a short one-line message describing the reason for failure.
16cmake_path(GET ExternalData_CUSTOM_SCRIPT_fetchURL PARENT_PATH SOURCE_DIR)
17include(${SOURCE_DIR}/../versions.cmake)
19list(FIND fetchurl_list ${ExternalData_CUSTOM_LOCATION} hash_pos)
21math(EXPR url_pos "${hash_pos} + 1")
23if(url_pos GREATER "0")
24 list(GET fetchurl_list ${url_pos} url)
25 list(GET fetchurl_list ${hash_pos} hash)
26 string(REGEX REPLACE "/" "=" hash "${hash}")
28 message(STATUS "fetchURL: ${url}...")
29 file(DOWNLOAD ${url} ${ExternalData_CUSTOM_FILE} STATUS status SHOW_PROGRESS TIMEOUT 0 EXPECTED_HASH ${hash})
31 list(GET status 0 code)
33 list(GET status 1 ExternalData_CUSTOM_ERROR)
36 message(DEBUG "FetchURL: unknown hash ${ExternalData_CUSTOM_LOCATION}")
38 # touch the file to allow other custom scripts (if any)
39 cmake_path(GET ExternalData_CUSTOM_FILE PARENT_PATH STORE_DIR)
40 file(MAKE_DIRECTORY ${STORE_DIR})
41 file(TOUCH ${ExternalData_CUSTOM_FILE})