This project provides a minimal PHP script to be used as a server backend for the vcpkg binary caching HTTP module or vcpkg asset caching. The cache is meant to be public, e.g., no authentication to download files is implemented. However, writing to the cache requires a configured secret, e.g., to be used by specific trusted CI Jobs to fill the cache. Cache cleanup still needs to be implemented. Currently, all files are stored infinitely.
For full details, refer to the vcpkg binary caching documentation.
Set the environment variable VCPKG_BINARY_SOURCES
to
clear;http,https://example.com/{triplet}-{name}-{sha},read
.
Set the environment variable VCPKG_BINARY_SOURCES
to
clear;http,https://example.com/{triplet}-{name}-{sha},readwrite,Authorization: Token supersecrettoken
.
Vcpkg asset caching does currently not directly support an HTTP backend. However, it is possible to use custom asset provider scripts. So, we can implement an HTTP provider using a CMake script. For more details, refer to the vcpkg asset caching documentation.
Preconditions: CMake must be installed and available in PATH. At least version 3.18 is required.
X_VCPKG_ASSET_SOURCES="x-script,cmake -DURL={url} -DSHA512={sha512} -DDST={dst} -P /path/to/vcpkg_asset_cache_http.cmake;x-block-origin" ./vcpkg install zlib
Add to CMake:
set(ENV{X_VCPKG_ASSET_SOURCES} "x-script,${CMAKE_COMMAND} -DURL={url} -DSHA512={sha512} -DDST={dst} -P /path/to/vcpkg_asset_cache_http.cmake;x-block-origin")
By default, the script is in read-only mode.
To enable read and write, additionally set the environment variable ASSET_UPLOAD_TOKEN
to the authentication secret.
Store as vcpkg_asset_cache_http.cmake
:
cmake_minimum_required(VERSION 3.18...3.27 FATAL_ERROR)
message("=== Vcpkg HTTP Asset Cache ===")
message("URL: ${URL}")
message("SHA512: ${SHA512}")
message("DST: ${DST}")
set(CACHE_URL "https://example.com/asset_${SHA512}")
message("CACHE_URL: ${CACHE_URL}")
if (NOT DEFINED ENV{ASSET_UPLOAD_TOKEN})
# Only read asset cache
file(DOWNLOAD "${CACHE_URL}" "${DST}"
TLS_VERIFY ON
EXPECTED_HASH SHA512=${SHA512})
else()
# Read and write asset cache
set(has_cache FALSE)
# Try download
file(DOWNLOAD "${CACHE_URL}" "${DST}"
STATUS status
TLS_VERIFY ON)
list(GET status 0 status_code)
if ("${status_code}" EQUAL "0")
# Manually check hash, because using EXPECTED_HASH above will trigger error
file(SHA512 "${DST}" hash)
if ("${hash}" STREQUAL "${SHA512}")
set(has_cache TRUE)
endif()
endif()
if (has_cache)
# Asset already cached
return()
endif()
# Download from original source
file(DOWNLOAD "${URL}" "${DST}"
TLS_VERIFY ON
EXPECTED_HASH SHA512=${SHA512})
# Upload to cache
file(UPLOAD "${DST}" "${CACHE_URL}"
STATUS status
HTTPHEADER "Authorization: Token $ENV{ASSET_UPLOAD_TOKEN}"
TLS_VERIFY ON)
list(GET status 0 status_code)
if (NOT "${status_code}" EQUAL "0")
message(FATAL_ERROR "Error uploading to cache.")
endif()
endif()