HPCW 3.0
Loading...
Searching...
No Matches
fetchFromEP.cmake
Go to the documentation of this file.
1# Copyright (c) 2020 - 2024 David Guibert
2# Copyright (c) 2024 Niclas Schroeter
3# All rights reserved.
4#
5# SPDX-License-Identifier: Apache-2.0
6
7# https://cmake.org/cmake/help/latest/module/ExternalData.html#custom-fetch-scripts
8#ExternalData_CUSTOM_LOCATION
9# 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#
11#ExternalData_CUSTOM_FILE
12# 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.
13# The custom fetch script is expected to store fetched content in the file or set a variable:
14# ExternalData_CUSTOM_ERROR
15# 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.
16
17cmake_path(GET ExternalData_CUSTOM_SCRIPT_fetchURL PARENT_PATH SOURCE_DIR)
18include(${SOURCE_DIR}/../versions.cmake)
19
20list(FIND fetchfromep_list ${ExternalData_CUSTOM_LOCATION} hash_pos)
21
22math(EXPR next_pos "${hash_pos} + 1")
23
24if(next_pos GREATER "0")
25 list(GET fetchfromep_list ${next_pos} name)
26 list(GET fetchfromep_list ${hash_pos} hash)
27 string(REGEX REPLACE "/" "=" hash "${hash}")
28 string(REGEX REPLACE ".*${ExternalData_CUSTOM_LOCATION};(.*)(MD5|SHA256)*.*" "\\1" ep_args "${fetchfromep_list}")
29 # remove trailing checksums
30 string(REGEX REPLACE "(,*);(MD5|SHAR256|$).*" "\\1" ep_args "${ep_args}")
31
32 message(STATUS "fetchFromEP: ${ep_args}...")
33
34 include(FetchContent)
35 FetchContent_Populate(
36 ${ep_args}
37 SOURCE_DIR ${name}
38 )
39 # https://www.gnu.org/software/tar/manual/html_node/Reproducibility.html
40 execute_process(COMMAND bash -xc "env LC_ALL=C tar --sort=name --format=posix --pax-option=exthdr.name=%d/PaxHeaders/%f --pax-option=delete=atime,delete=ctime --clamp-mtime --mtime='1971-01-01T00:00:00Z' --numeric-owner --owner=0 --group=0 --mode=go+u,go-w --exclude-vcs -cvf - ${name} | gzip --no-name --best > ${ExternalData_CUSTOM_FILE}")
41
42 file(MD5 "${ExternalData_CUSTOM_FILE}" md5hash)
43 file(SHA256 "${ExternalData_CUSTOM_FILE}" sha256hash)
44 string(REGEX REPLACE "^.*=" "" hash "${hash}")
45
46 if(NOT (md5hash STREQUAL hash OR sha256hash STREQUAL hash))
47 set(ExternalData_CUSTOM_ERROR "Computed hashes ${md5hash} and ${sha256hash}, expected ${hash}")
48 endif()
49else()
50 message(DEBUG "FetchFromEP: unknown hash ${ExternalData_CUSTOM_LOCATION}")
51
52 # touch the file to allow other custom scripts (if any)
53 cmake_path(GET ExternalData_CUSTOM_FILE PARENT_PATH STORE_DIR)
54 file(MAKE_DIRECTORY ${STORE_DIR})
55 file(TOUCH ${ExternalData_CUSTOM_FILE})
56endif()