Skip to content
hermeticbuildPublic

About

Bazel build rules for Zig

Resources

Contributing

Stars

82 stars

Watchers

4 watching

Forks

Repository files navigation

Bazel build rules for Zig

Build Zig code with the Bazel build system.

Status

Take a look at the planned functionality tracking issue to get a picture of which functionality is already implemented and what is still missing.

Motivation

Why would you use Bazel to build Zig code, when Zig comes with its own perfectly fine build system?

These Bazel rules are not meant as competition to the Zig build system. If your project fits well within the scope of Zig's build system, in particular, it only uses Zig code and perhaps C/C++ code, then Zig's build system will probably work very well for your use-case.

Bazel is useful for polyglot, monorepo projects, especially if they are very large. If your project involves multiple programming languages and if some of them are not supported by Zig's build system, or if your project involves complex code-generation steps or requires a lot of custom build steps, then Bazel may be a good choice. If your project is already a Bazel project and you want to add Zig code to it then this rule set is for you.

Bazel has builtin support for cross-compilation and build configuration, meaning it can support Zig's cross-compilation and build configuration features well.

Installation

The instructions assume basic familiarity with the Bazel build system. Take a look at Bazel's documentation if you are unfamiliar.

Consider the Additional Setup section as well.

Bazel dependencies are managed using Bzlmod, you can read more about it in the Bazel documentation. Note, the old WORKSPACE mechanmism is no longer supported by rules_zig.

To install a release version of rules_zig follow the installation instructions given in the corresponding release notes.

To install a development version add the following to your MODULE.bazel file:

bazel_dep(name = "rules_zig")
archive_override(
    module_name = "rules_zig",
    integrity = "sha256-$SHA256",
    strip_prefix = "rules_zig-$COMMIT",
    urls = ["https://github.com/hermeticbuild/rules_zig/archive/$COMMIT.tar.gz"],
)

Note, $SHA256 and $COMMIT are placeholders that you need to fill in. Take a look at the Bazel documentation for further information.

Additional Setup

Zig Cache

The Zig compiler caches intermediate outputs on disk. This cache is shared and persisted between Bazel builds and build actions. You can configure the cache directory using the following set of environment variables, you can configure them through Bazel by using the --repo_env flag.

  • RULES_ZIG_CACHE_PREFIX_LINUX: Cache directory on Linux, default /tmp/zig-cache.
  • RULES_ZIG_CACHE_PREFIX_MACOS: Cache directory on MacOS, default /var/tmp/zig-cache.
  • RULES_ZIG_CACHE_PREFIX_WINDOWS: Cache directory on Windows, default C:\Temp\zig-cache.
  • RULES_ZIG_CACHE_PREFIX: Cache directory fall-back for all platforms, default /tmp/zig-cache.

Note

On Bazel 7 and above you need to explicitly allow persistence in the sandbox. You can use the following .bazelrc snippet, adjust the paths depending on your cache configuration.

common --enable_platform_specific_config
# You can configure `/tmp/zig-cache`, or similar, specifically,
# if you can ensure that the directory exists before the build.
build:linux --sandbox_add_mount_pair=/tmp
build:macos --sandbox_add_mount_pair=/var/tmp
build:windows --sandbox_add_mount_pair=C:\Temp

Private caches with a persistent worker

Use a persistent Bazel worker for ordinary target compilations with a hermetic Zig 0.17.x SDK on a POSIX execution platform:

build --@rules_zig//zig/settings:use_worker
build --strategy=ZigWorkerCompile=worker

The worker starts a fresh Zig process per action and keeps the local and global Zig caches in a private directory. Compilations using the same worker reuse those caches without a shared cache mount. Direct actions, including the worker's own build, use the cache configuration above. If you also enable incremental, it takes precedence for target compilations.

Execution Groups

Zig compiler actions use the zig execution group. When use_cc_common_link is enabled, the final C/C++ link uses cpp_link. This group resolves both the Zig SDK and the C/C++ toolchain, so the SDK's execution constraints also apply to the final link. Use Bazel's exec_group_compatible_with and group-prefixed exec_properties attributes to configure these groups. C preprocessing and other linking helper actions may still use the default execution context.

C and C++ Standard Libraries

Add @rules_zig//zig/lib:libc to a Zig target's deps to enable libc, or @rules_zig//zig/lib:libc++ to enable libc++ as well. These dependencies configure Zig compilation and carry the library requirements to the final link.

use_cc_common_link enables libc in the Zig compilation so the C runtime can call Zig's C ABI main. Use Zig's own linker for libc-free executables.

Usage Examples

Examples can be found among the end-to-end tests under ./e2e/workspace.

ZLS

ZLS toolchains are provided by a separate extension and selected by the active Zig SDK version.

See how the ZLS integration works for the 0.17 flow and the responsibilities of each component.

zls = use_extension("@rules_zig//zig/zls:extensions.bzl", "zls")
zls.index(file = "@rules_zig//zig/zls/private:versions.json")
zls.toolchain(
    zig_version = "0.16.0",
    zls_version = "0.16.0",
)
use_repo(zls, "zls_toolchains")
register_toolchains("@zls_toolchains//:all")

Use zig_version as the selector and zls_version as the artifact version. Patch versions may differ within a supported stable minor; arbitrary development builds are not necessarily compatible.

Zig 0.17.0 and 0.17.1 require the ZLS absolute-path correction from upstream commit bbd60390, which is absent from released ZLS 0.17.0. The catalog includes HermeticBuild's CI-built prebuilt backport, identified as 0.17.0+hermetic.1:

zls.toolchain(
    zig_version = "0.17.0",  # Use "0.17.1" for that SDK instead.
    zls_version = "0.17.0+hermetic.1",
)

This uses the same extension and registration shown above and downloads a checksum-pinned binary for x86_64/aarch64 Linux/macOS. Register one mapping per Zig SDK in use; keep the 0.16 mapping when supporting both minors.

Custom prebuilts already work through zls.index(file = "//:zls-versions.json"): provide a version-to-platform JSON mapping using tarball and shasum fields, then select that version with zls.toolchain. Archives must contain zls at their root (zls.exe on Windows). A consumer can alternatively declare its own zls_toolchain(bin = ...) and native Bazel toolchain registration.

Use the zls_completion macro to define a ZLS entry point for the Zig targets you want to expose to the language server. For example, in tools/BUILD.bazel:

load("@rules_zig//zig/zls:defs.bzl", "zls_completion")

zls_completion(
    name = "zls",
    deps = ["//src:app"],
)

Then point your editor's ZLS binary setting at a wrapper script that runs that target.

The launcher selects runtime Zig and ZLS tools for the editor's local host, even when build actions execute remotely. The entry point's target platform must match the editor host; the remote execution platform may differ.

#!/usr/bin/env bash
cd "$(dirname "${BASH_SOURCE[0]}")/.."
exec bazel run -- //tools:zls "${@}"

The integration exposes one selected Bazel configuration. An existing build.zig in the source ancestry serves as ZLS's discovery marker; the native adapter does not execute it. Configuration reload (for example, saving that marker) queries Bazel again, including changed build/module metadata and source producers. Bazel files are not watched automatically. Application compilation and Zig build-on-save are unsupported; use Bazel to build. Higher-priority editor settings enabling build-on-save produce an explicit failure.

Native protocol support currently targets Linux/macOS; Windows retains the legacy 0.16 route. Existing source-based module merging and external documents' transitive named-import association limits remain. This migration does not add conflicting configurations or full target/builtin semantics.

Reference Documentation

Generated API documentation for the provided rules is available in ./docs/rules.md.

Generated API documentation for the module extensions is available in ./docs/extensions.md.

Generated API documentation for the experimental zig_packages module extension, which imports Zig package dependencies, is available in ./docs/packages.md.

Generated API documentation for the toolchain rules is available in ./docs/toolchains.md.

Runfiles Library Documentation

Documentation for the Zig runfiles library can be found in ./zig/runfiles/guide.md.

About

Bazel build rules for Zig

Resources

Contributing

Stars

82 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages