Skip to content

gleyba/rules_vcpkg

Repository files navigation

VCPKG rules for Bazel

This is a pre-alpha state of how vcpkg dependency manager can be integrated to Bazel build system.

Vcpkg is the well curated list of C/C++ libraries with predefined configurations to build from source. It is easy to integrate if you use Cmake, but Bazel is a separate story.

You can cc_import libraries produced by vcpkg, but you have to manage two different systems in conjunction with each other.

The main goal of rules_vcpkg is to wrap vcpkg as a hermetic toolchain for Bazel, and provide an easy way to declare package with Bazel's bzlmod module system.

Initial vcpkg bootstrap with all declared packages implemented as a repository rule. But each library build process is a separate execution phase action wrapped in custom rule.

Such a setup must be compatible with Bazel's remote caching and remote execution.

Somewhat similar in nature to how rules_foreign_cc implemented, but here we don't need to define complex configurations, all is done by vcpkg itself.

All we need is to provide a buildtree and transitive dependencies collected with a directory structure compatible to vcpkg build <package_name> invocation.

Which is done by bunch of hacks as a proof-of-concept.

Getting started

There is examples directory, which I'm going to extend. But to try yourself this not yet polished version, add to MODULE.bazel:

git_override(
    module_name = "rules_vcpkg", 
    remote = "https://github.com/gleyba/rules_vcpkg",
    branch = "main",
)

vcpkg = use_extension("@rules_vcpkg//vcpkg:extensions.bzl", "vcpkg")
vcpkg.bootstrap(
    release = "2025.07.25",
    sha256 = "dff617c636a6519d4f083e658d404970c9da7d940a974e1d17f855f26a334e2f",
)

vcpkg.install(package = "fmt")

use_repo(
    vcpkg,
    "vcpkg",
    "vcpkg_bootstrap",
)

register_toolchains("@vcpkg_bootstrap//:vcpkg_toolchain")

Then, in BUILD.bazel:

cc_binary(
    name = "hello_fmt",
    srcs = ["hello_fmt.cpp"],
    deps = ["@vcpkg//fmt"],
)

And hello_fmt.cpp:

#include <fmt/core.h>

int main() {
    fmt::print("Hello FMT!\n");
}

Run it as:

bazel run //:hello_fmt
INFO: Analyzed target //:hello_fmt (89 packages loaded, 9927 targets configured).
INFO: Found 1 target...
Target //:hello_fmt up-to-date:
  bazel-bin/hello_fmt
INFO: Elapsed time: 1.630s, Critical Path: 0.05s
INFO: 1 process: 41 action cache hit, 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/hello_fmt
Hello FMT!

Debugging

  • --@rules_vcpkg//:debug=True If set, will print VCPKG output for packages build. Also will initialize build tree in /tmp/vcpkg/builtrees/<package_name> dir.

  • --@rules_vcpkg//:debug_reuse_outputs=True If set and build outputs exist in /tmp/vcpkg/builtrees/<package_name> dir, will reuse these outputs instead of running build.

Done and Upcoming work

  • Test more sophisticated packages with complex transitive dependencies structure:
  • Prepare additional toolchains done via rules_vcpkg_externals:
    • make
    • m4
    • bison
    • flex
    • pkgconfig
    • autoconf
    • autoconf-archive
    • automake
    • libtoolize
    • gsed
  • Support other platforms besides Mac OS X aarh64:
    • Compile everything for MacOS x86_64
    • Compile everything for Linux Arm64
    • Compile everything for Linux x86_64
  • Setup CI checks:
    • Release rules_vcpkg_externals artifacts to github packages
    • Setup simple build examples CI checks
  • Hermeticity and reproducibility:
    • Some work done and packages build are mostly cacheable from disk cache
    • Use of VCPKG assetcaching to integrate packages downloads with Bazel downloader
    • Hermetic Mac OS X SDK for QT example
    • Do more comprehensive cache hit/miss analysis
  • Support hermetic C/C++ Bazel toolchains by generating custom Overlay Triplet with VCPKG_CHAINLOAD_TOOLCHAIN_FILE:
    • Overlay tripplet used as entry point to propagate custom configuration to VCPKG/Cmake
    • Setup chainload file for c/c++ toolchain
  • Test with remote build execution
  • Announce this work in Bazel slack

About

Bazel rules for vcpkg package manager, it works for Mac OS X and Linux.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors