TExcelTAD

Binary Caching

vcpkg builds ports from source, and big ones (Boost, Qt, LLVM) take a long time. Binary caching stores each built package as a zip, keyed by an ABI hash, and restores it the next time the same hash is needed, on your machine or any machine that shares the cache. Every vcpkg session in this course has worked offline because of it.

Where the cache is configured

vcpkg reads binary sources from VCPKG_BINARY_SOURCES (or --binarysource), a ;-separated list:

bash · try it yourself
echo "$VCPKG_BINARY_SOURCES"
find /opt/vcpkg-cache -name '*.zip' | wc -l
ls /opt/vcpkg-cache | head -3
clear;files,/opt/vcpkg-cache,readwrite
5
0b
1b
1f

The zips are spread over subfolders named after the first two characters of their hash, so no folder gets too large. clear removes the defaults, then files,<path>,readwrite adds a folder that vcpkg reads from and writes to. The default, if you set nothing, is a files cache in ~/.cache/vcpkg/archives (Linux/macOS) or %LOCALAPPDATA%\vcpkg\archives (Windows): even without configuration, a second project on the same machine reuses your builds.

Provider Example Use
files files,/mnt/cache,readwrite a local folder or a network share
http http,https://cache.example.com/{name}/{version}/{sha},readwrite,Authorization: Bearer … any HTTP server that supports GET/PUT
nuget nuget,https://nuget.pkg.github.com/ORG/index.json,readwrite NuGet feeds: GitHub Packages, Azure Artifacts, Artifactory, Nexus
x-azblob, x-gcs, x-aws, x-cos x-azblob,https://acct.blob.core.windows.net/cache,<SAS>,readwrite cloud object storage
default default,read the per-user folder

read, write or readwrite controls the direction; a common setup is "CI writes, developers read".

A hit, and a miss

bash · try it yourself
mkdir -p app && cd app
echo '{ "builtin-baseline": "5d4a3dc90c0b3773f3bac81bf41297b54328f471", "dependencies": [ "fmt" ] }' > vcpkg.json
echo "--- with the cache"
vcpkg install 2>&1 | grep -E 'Restored|package ABI'
rm -rf vcpkg_installed
echo "--- without it"
vcpkg install --binarysource=clear 2>&1 | grep -E 'Building|Downloading|error: [a-z]' | head -4
--- with the cache
Restored 3 package(s) from /opt/vcpkg-cache in 22.8 ms. Use --debug to see more details.
vcpkg-cmake-config:x64-linux@2026-07-21 package ABI: 1fe2b3e89bf936d3ca4a1da67308e95d33ba8aaa6c691fd9d56c2e6237b19a8e
vcpkg-cmake:x64-linux@2025-08-07 package ABI: 73813f8f6a46e99309077be37bc53ccb320a9ac1cb9848a543ef25143f3117ab
fmt:x64-linux@12.2.0#1 package ABI: 8321e6725b78fab9213891eeb6ef82c861a720f0fa145d876cfed6cd062d4e55
--- without it
Building vcpkg-cmake-config:x64-linux@2026-07-21...
Building vcpkg-cmake:x64-linux@2025-08-07...
Building fmt:x64-linux@12.2.0#1...
Downloading https://github.com/fmtlib/fmt/archive/12.2.0.tar.gz -> fmtlib-fmt-12.2.0.tar.gz

The first install restored three zips whose names are the package ABI hashes printed for each package. With the cache disabled, vcpkg had to build everything. The two helper ports are just scripts and built fine; fmt starts by downloading its source, and our offline container stops there. On a connected machine it would build, then write the result to every readwrite source.

What the ABI hash covers

Each installed package keeps the list of inputs that went into its hash, in share/<port>/vcpkg_abi_info.txt:

bash · try it yourself
mkdir -p app && cd app
echo '{ "builtin-baseline": "5d4a3dc90c0b3773f3bac81bf41297b54328f471", "dependencies": [ "spdlog" ] }' > vcpkg.json
vcpkg install > /dev/null 2>&1
cut -c1-60 vcpkg_installed/x64-linux/share/spdlog/vcpkg_abi_info.txt
cmake 0
features core;fmt;tz-offset
fmt 8321e6725b78fab9213891eeb6ef82c861a720f0fa145d876cfed6cd
portfile.cmake 1a62fecc62ccceeccefdba8920846afd5971906cb302d
ports.cmake ae49e729c370bed1954cdd64875854fe1bb2ae4b9ed0316f
post_build_checks 2
sbom_info 1
triplet x64-linux
triplet_abi 3600011444071beb27b81ff82b3c866e3d4054636e301160
usage bdc67ce7fb7be263e7fdf3c57815f8f9f6142aca5d8f4da5e5597f
vcpkg-cmake 73813f8f6a46e99309077be37bc53ccb320a9ac1cb9848a5
vcpkg-cmake-config 1fe2b3e89bf936d3ca4a1da67308e95d33ba8aaa6
vcpkg.json ec878fe44c677902d48354428d09fc6fd994c33a30e3752cd
vcpkg_check_features 943b217e0968d64cf2cb9c272608e6a0b497377
vcpkg_copy_pdbs d15c2f2822d93ecb9023af44803328e491c5374136e0
vcpkg_download_distfile a9c6f9943a6a066b39aee5614dcb819e93ba
vcpkg_fixup_pkgconfig 1a15f6c6d8e2b244d83a7514a0412d339127d2
vcpkg_from_git b2617361b8d8274195501c0d17fc179b27abf287fd32f
vcpkg_from_github fe7407019be299a707e7b271fec52a66c4159fa602
vcpkg_install_copyright b716d811257d950b92606d5910d8fae59acb
vcpkg_replace_string b450deb79207478b37119743e00808ebc42de06
Input Why it's there
portfile.cmake, vcpkg.json, patches the build recipe itself
fmt, vcpkg-cmake, … the ABI hashes of dependencies: rebuild spdlog when fmt changes
features core;fmt;tz-offset builds differently from core
triplet, triplet_abi the triplet file and a hash of the compiler detected for it
helper scripts (vcpkg_from_github, …) the vcpkg functions the portfile calls
cmake version, post_build_checks tool and policy versions

Any change gives a new hash and a cache miss. That's what makes caching safe: a binary is never reused for a configuration it wasn't built for. It's also why caches miss more often than people expect: a compiler update on the CI image changes triplet_abi and invalidates everything.

A runnable model: a content-addressed cache

python · try it yourself
import hashlib

def h(text):
    return hashlib.sha256(text.encode()).hexdigest()[:10]

def abi(port, ports, triplet, compiler, memo):
    if port in memo:
        return memo[port]
    p = ports[port]
    lines = [f"portfile {h(p['portfile'])}", f"triplet {triplet}", f"compiler {h(compiler)}"]
    lines += [f"{d} {abi(d, ports, triplet, compiler, memo)}" for d in sorted(p["deps"])]
    memo[port] = h("\n".join(lines))
    return memo[port]

ports = {
    "fmt":    {"portfile": "vcpkg_from_github(fmtlib/fmt 12.2.0)", "deps": []},
    "spdlog": {"portfile": "vcpkg_from_github(gabime/spdlog 1.17.0)", "deps": ["fmt"]},
}
cache = set()

def install(label, triplet="x64-linux", compiler="gcc 13.3.0"):
    memo, out = {}, []
    for port in ("fmt", "spdlog"):
        key = abi(port, ports, triplet, compiler, memo)
        out.append(f"{port}:{'restored' if key in cache else 'built'}")
        cache.add(key)
    print(f"{label:<22} {'  '.join(out)}")

install("first install")
install("same again")
ports["fmt"]["portfile"] += " + patch"
install("fmt port changed")
install("compiler upgraded", compiler="gcc 13.4.0")
install("other triplet", triplet="x64-linux-dynamic")
first install          fmt:built  spdlog:built
same again             fmt:restored  spdlog:restored
fmt port changed       fmt:built  spdlog:built
compiler upgraded      fmt:built  spdlog:built
other triplet          fmt:built  spdlog:built

Changing fmt's port rebuilt spdlog too, because spdlog's hash includes fmt's. That cascade is what keeps the cache correct.

Asset caching

Binary caching avoids rebuilds; asset caching avoids re-downloading sources and tools (X_VCPKG_ASSET_SOURCES, e.g. x-azurl,https://mirror.example.com/vcpkg-assets/,,readwrite). With both in place, and x-block-origin added to asset sources, a build never touches the public internet, which is what air-gapped and supply-chain-conscious environments require.

Test yourself

What is the key under which vcpkg stores a binary package?

  1. The port name
  2. The package ABI hash: port files, triplet, compiler, features and dependencies' hashes
  3. The version number
  4. A random UUID

fmt's port changes. Why is spdlog rebuilt as well?

  1. vcpkg always rebuilds everything
  2. spdlog's ABI hash includes fmt's ABI hash
  3. spdlog's version changed
  4. The cache was cleared

Which VCPKG_BINARY_SOURCES value lets CI publish binaries to a NuGet feed?

  1. files,/tmp,read
  2. nuget,<feed url>,readwrite
  3. clear
  4. default,read