Files
carbon-lang/examples/bazel/MODULE.bazel
T
b0ffed7c3e Make the Carbon toolchain a viable Bazel module exposing cc_toolchains (#6652)
This let's you point Bazel at an installed toolchain or download one of
our release archives. When you do, it will configure itself as a C++
Bazel toolchain. This toolchain works reasonably well, but doesn't cache
the C++ runtimes, and so linking is inefficient. The next step will be
to pivot the runtimes from the implicitly on-demand (which can't cache
when using a sandboxed build system like Bazel) to _explicit_ on-demand
runtimes directly with Bazel support.

I've included an example Bazel project that uses this and provides a
bunch of documentation and an example script that should let folks try
this out easily.

---------

Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: David Blaikie <dblaikie@gmail.com>
2026-01-28 02:45:19 +00:00

57 lines
2.1 KiB
Python

# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""Example Bazel module that builds C++ with the Carbon toolchain.
Note that this is at the root of an example Bazel project, not part of the
larger Carbon Bazel project. To interact with this example, you'll want to
`cd examples/bazel` so that you can operate Bazel entirely within this example
(sub-)project.
Once interacting with this subdirectory's project, you can use this and build
with the Carbon toolchain in a few different ways:
1) Override the module with a local installation on the command line:
`bazel build --override_module=carbon_toolchain=/path/to/carbon_toolchain/installation/lib/carbon`
2) Encode a local override into this file:
```
local_path_override(
module_name = "carbon_toolchain",
path = "/path/to/carbon_toolchain/installation/lib/carbon",
)
```
3) Encode a archive override into this file:
```
version = "0.0.0-0.nightly.YYYY.MM.DD"
archive_override(
module_name = "carbon_toolchain",
strip_prefix = "carbon_toolchain-{0}/lib/carbon".format(version),
urls = ["https://github.com/carbon-language/carbon-lang/releases/download/v{0}/carbon_toolchain-{0}.tar.gz".format(version)],
)
```
Note: Initially, Bazel will warn about the lack of an `integrity` field,
and will print the value it found by downloading the archive which you can
verify on GitHub before encoding.
You can use the provided script `update_module_to_nightly.py` to generate and
add an `archive_override` of the current nightly release, including integrity
from GitHub.
Once the Carbon toolchain has established releases in the Bazel central
registry, this file will work with an updated version out of the box.
"""
module(name = "example")
bazel_dep(name = "rules_cc", version = "0.2.14")
# Declare the `carbon_toolchain` module. This is needed even if it will be
# overridden with a local path or archive.
bazel_dep(name = "carbon_toolchain", version = "0.0.0")
register_toolchains("@carbon_toolchain//:all")