GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - mrom1/vcpkg-bootstrap: Automatically bootstrap vcpkg in your CMake project in just three lines using FetchContent. Prioritizes other dependency managers. No pre-installed vcpkg required for full functionality.

BranchesTags
Open more actions menu

Latest commit

 

History

16 Commits
16 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
.gitignore
.gitignore
 
 
CMakeLists.txt
CMakeLists.txt
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
vcpkg-bootstrap.cmake
vcpkg-bootstrap.cmake
 
 

Repository files navigation

vcpkg-bootstrap

Linux Windows macOS

A single CMake script that makes vcpkg available before project(): it uses an existing vcpkg or clones and bootstraps one, and sets vcpkg up as the toolchain. After that, packages come from plain find_package(), and dependencies are declared the way vcpkg documents it, in a vcpkg.json.

Requires CMake 3.24 or newer, git and a C++ compiler.

Usage

Add this to your top-level CMakeLists.txt, before the first project():

cmake_minimum_required(VERSION 3.24)

include(FetchContent)
FetchContent_Declare(vcpkg_bootstrap
  GIT_REPOSITORY https://github.com/mrom1/vcpkg-bootstrap.git
  GIT_TAG        main              # better: a release tag or commit hash
)
# Optional: isolated mode keeps vcpkg and all packages in the build folder.
option(VCPKG_BOOTSTRAP_ISOLATED "Virtual Environment Mode" OFF)
FetchContent_MakeAvailable(vcpkg_bootstrap)

project(myproject CXX)

find_package(fmt CONFIG REQUIRED)

and list your dependencies in a vcpkg.json next to it:

{
  "dependencies": [ "fmt" ]
}

That's all: configure as usual (cmake -S . -B build). A complete, working project lives on the example branch.

Without FetchContent: copy the file

vcpkg-bootstrap.cmake is self-contained. Copy it into your project and include it before project():

cmake_minimum_required(VERSION 3.24)

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/vcpkg-bootstrap.cmake)

project(myproject CXX)

Isolated mode ("virtual environment")

VCPKG_BOOTSTRAP_ISOLATED Where vcpkg comes from Where packages are installed
OFF (default) An existing vcpkg if there is one (see below), otherwise one shared clone in a per-user cache, used by all your projects vcpkg's defaults: <build>/vcpkg_installed with a vcpkg.json, otherwise <vcpkg>/installed
ON A private clone in <build>/vcpkg; VCPKG_ROOT is ignored Everything stays in the build folder

With ON, deleting the build folder removes vcpkg and every package it installed; nothing is written outside of it. That includes vcpkg's own caches, which otherwise live in your user profile:

Shared mode (vcpkg's defaults) Isolated mode
Binary cache (built packages, reused across build folders) %LOCALAPPDATA%\vcpkg\archives, ~/.cache/vcpkg/archives <build>/vcpkg-bootstrap/archives
Git registry cache %LOCALAPPDATA%\vcpkg\registries, ~/.cache/vcpkg/registries <build>/vcpkg-bootstrap/registries
Downloads (sources, tools) <vcpkg>/downloads <build>/vcpkg/downloads

In isolated mode, settings for these in the environment (VCPKG_BINARY_SOURCES, VCPKG_DEFAULT_BINARY_CACHE, X_VCPKG_REGISTRIES_CACHE, VCPKG_DOWNLOADS) are ignored, like VCPKG_ROOT; the configure says so. The price is a full vcpkg clone and bootstrap per build folder, and every package is built once per build folder.

Choose the mode on the first configure of a build folder (-DVCPKG_BOOTSTRAP_ISOLATED=ON). It is remembered; changing it later in the same build folder is refused with an error (see Troubleshooting).

Windows: path lengths

A build folder is usually deeper than vcpkg's own directory, and some ports exceed Windows' 260 character path limit there (see Troubleshooting). To give ports more room, the script has vcpkg build them with short intermediate directories on Windows: CMake 4.2+ (current vcpkg) puts objects into <build>/.o/<hash>/<hash>.obj instead of <build>/CMakeFiles/<target>.dir/<source>.obj (CMAKE_INTERMEDIATE_DIR_STRATEGY=SHORT). It is passed through vcpkg's build environment (VCPKG_KEEP_ENV_VARS), does not change the package ABI and does not affect your own project; set CMAKE_INTERMEDIATE_DIR_STRATEGY in your environment to override it. Ports whose own file names are too long for the location still fail.

Where vcpkg comes from

The first match wins:

# Source Modified by the script?
1 -DVCPKG_BOOTSTRAP_ROOT_DIR=<dir>: an existing vcpkg checkout never
2 Isolated mode: <build>/vcpkg cloned, updated, bootstrapped
3 The VCPKG_ROOT environment variable: your own vcpkg, a CI runner's vcpkg, ... never
4 A shared clone in <VCPKG_BOOTSTRAP_INSTALL_DIR>/vcpkg or the per-user cache cloned, updated, bootstrapped

Per-user cache: %LOCALAPPDATA%\vcpkg-bootstrap\vcpkg on Windows, $XDG_CACHE_HOME/vcpkg-bootstrap/vcpkg or ~/.cache/vcpkg-bootstrap/vcpkg on Linux and macOS. It is cloned and bootstrapped once and shared by all projects and build folders.

  • A vcpkg toolchain you pass yourself (-DCMAKE_TOOLCHAIN_FILE=<vcpkg>/scripts/buildsystems/vcpkg.cmake, a preset, an IDE integration) always wins: the script then only uses it.
  • Visual Studio sets VCPKG_ROOT to its bundled vcpkg, which has no ports of its own and only works with a vcpkg.json that has a builtin-baseline (vcpkg's own rule). Without such a manifest the script says so and uses the shared clone instead.
  • A VCPKG_ROOT that is not a vcpkg root (e.g. a leftover after moving or deleting a vcpkg) is ignored with a note.
  • vcpkg checkouts the script did not create are only read. If one is older than your builtin-baseline, you get a warning telling you to update it.
  • Clones made by the script are full clones (vcpkg's versioning needs the history). A file lock makes parallel configures safe. To start over, delete the directory.

Getting packages

  1. vcpkg.json (recommended). vcpkg's manifest mode: everything listed is installed during configure into <build>/vcpkg_installed.

  2. vcpkg_bootstrap_install(<port>...) after project(), anywhere in the tree, for projects without a manifest:

    project(myproject CXX)
    vcpkg_bootstrap_install(fmt nlohmann-json)
    find_package(fmt CONFIG REQUIRED)
    find_package(nlohmann_json CONFIG REQUIRED)

    Port names use vcpkg syntax (fmt, fmt[core]). This is vcpkg install in classic mode, so the packages go to <vcpkg>/installed. In manifest mode the call does nothing and warns about ports missing from vcpkg.json.

  3. Plain find_package(<Name> ... REQUIRED) without a manifest (an extra of this project, not vcpkg behaviour): if <Name> is not found anywhere else (system, other toolchains, prefix paths), the vcpkg port that provides it is installed. It is deliberately strict: never for find_package(... QUIET) without REQUIRED, never meta-ports like boost, never guessed names. If a package is not picked up, use vcpkg_bootstrap_install(<port>).

Versions

  • Ports: set builtin-baseline in vcpkg.json for reproducible builds (vcpkg x-update-baseline --add-initial-baseline). Without a baseline, versions come from whatever commit the vcpkg checkout is on — not "the latest".
  • vcpkg itself is put on the commit of your baseline: a new clone and the isolated clone (<build>/vcpkg) are checked out at exactly the baseline. vcpkg's own scripts and tools are part of every package's ABI hash, so this keeps builds reproducible and lets every build folder reuse packages from vcpkg's binary cache instead of rebuilding them.
  • The shared clone serves other projects too, so it only ever moves forward: if it does not contain your baseline yet, it is fetched once, moved to the baseline and bootstrapped again.
  • Otherwise configure does not go to the network for vcpkg itself.
  • vcpkg itself: -DVCPKG_BOOTSTRAP_REF=<tag or commit> checks out that vcpkg release in a clone made by the script. The shared clone is shared, so give projects that pin different refs their own VCPKG_BOOTSTRAP_INSTALL_DIR (or use isolated mode).
  • The script: pin GIT_TAG to a release tag or commit hash.

Cross-compiling and other toolchains

A toolchain you already use (-DCMAKE_TOOLCHAIN_FILE, a preset, or the CMAKE_TOOLCHAIN_FILE environment variable) is not replaced: it is passed to vcpkg as VCPKG_CHAINLOAD_TOOLCHAIN_FILE, so Android, Emscripten and embedded toolchains keep working. Select the vcpkg triplet as usual, e.g. -DVCPKG_TARGET_TRIPLET=arm64-android.

Conan and other dependency providers

  • conan_toolchain.cmake as toolchain is chainloaded; packages found through Conan are used from Conan, and the find_package() fallback only fills in what is missing.
  • If another dependency provider is configured (e.g. cmake-conan in CMAKE_PROJECT_TOP_LEVEL_INCLUDES), it takes precedence and the fallback is off (the script says so). vcpkg.json and vcpkg_bootstrap_install() keep working.

Offline and local development

Once vcpkg is cloned and contains your baseline, configuring only needs the network for ports that vcpkg has not built or cached yet. To use a local copy of this repository instead of downloading it:

cmake -S . -B build -DFETCHCONTENT_SOURCE_DIR_VCPKG_BOOTSTRAP=/path/to/vcpkg-bootstrap

Troubleshooting

  • "configured before without vcpkg's toolchain" / "The vcpkg location changed": CMake reads the toolchain file only on the first configure of a build folder. Delete the build folder (or its CMakeCache.txt) and configure again.
  • "The environment points to another vcpkg … keeping …": the same build folder was configured from a different shell, e.g. once from a plain terminal and once from a Visual Studio developer prompt (or an IDE that loads it, like VS Code's CMake Tools), which sets VCPKG_ROOT to its bundled vcpkg. When only the environment differs, the vcpkg the folder was configured with is kept. A location you choose with a setting (VCPKG_BOOTSTRAP_ROOT_DIR, VCPKG_BOOTSTRAP_ISOLATED, VCPKG_BOOTSTRAP_INSTALL_DIR) still fails with the error above.
  • "older than builtin-baseline": your own vcpkg (VCPKG_ROOT, VCPKG_BOOTSTRAP_ROOT_DIR) is outdated: git -C <vcpkg> pull, then run its bootstrap-vcpkg script.
  • "These ports will likely fail to build from source on Windows": some ports (Qt, Boost, ...) exceed Windows' 260 character path limit when vcpkg builds them in a deep directory; the limits are vcpkg's own (vcpkg_buildpath_length_warning). The script checks them before vcpkg runs, for ports not yet installed in the build folder (overlay ports included). The limit comes from MSVC: its compiler (cl.exe) cannot open longer paths, not even with Windows' long path support enabled. Packages restored from vcpkg's binary cache are fine. To build such ports, keep vcpkg in a short directory, as vcpkg recommends: shared mode with -DVCPKG_BOOTSTRAP_INSTALL_DIR=<short directory> (isolated mode keeps vcpkg in the build folder). Or build with a compiler that supports long paths, such as clang-cl, with Windows' long path support enabled, for the project and all its C++ dependencies (C++ libraries built with MSVC and clang-cl cannot always be mixed; Qt cannot).
  • Included after project(): the script only warns and does nothing; move it before the first project() call.
  • A package is not installed automatically: list it in vcpkg.json, or call vcpkg_bootstrap_install(<port>) before find_package().

Known limitations

  • Dependencies declared with FetchContent_Declare(... FIND_PACKAGE_ARGS ...) go through find_package() first, so without a manifest they may come from vcpkg instead of being fetched.
  • Tool ports whose usage declares a CMake package (e.g. Python3) are installed when that package is really missing.

Reference

Variable Default Meaning
VCPKG_BOOTSTRAP_ISOLATED OFF vcpkg and all packages in the build folder; VCPKG_ROOT ignored
VCPKG_BOOTSTRAP_ROOT_DIR — Use this existing vcpkg as-is (wins over everything)
VCPKG_BOOTSTRAP_INSTALL_DIR per-user cache Shared mode: manage vcpkg in <dir>/vcpkg
VCPKG_BOOTSTRAP_REF — vcpkg tag or commit for a clone made by the script
VCPKG_BOOTSTRAP_DISABLE OFF Do nothing
VCPKG_BOOTSTRAP_ROOT output The vcpkg root in use (cache)

Relative paths are relative to the top-level source directory. vcpkg's own variables (VCPKG_MANIFEST_DIR, VCPKG_MANIFEST_MODE, VCPKG_TARGET_TRIPLET, VCPKG_INSTALLED_DIR, VCPKG_OVERLAY_PORTS, VCPKG_OVERLAY_TRIPLETS, VCPKG_INSTALL_OPTIONS) work as documented by vcpkg.

Function: vcpkg_bootstrap_install(<port>...) — see Getting packages.

Branches

  • example: a minimal, copyable project.
  • testing: the integration tests (one self-contained folder per case) and the CI behind the badges.

License

MIT

About

Automatically bootstrap vcpkg in your CMake project in just three lines using FetchContent. Prioritizes other dependency managers. No pre-installed vcpkg required for full functionality.

Topics

cmakecmake-modulesfetchcontentpackage-managervcpkg

Resources

Readme
MIT license
Activity

Stars

4 stars

Watchers

1 watching

Forks

0 forks
Report repository

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.