Name Date Size #Lines LOC

..--

BUILDH A D25-Apr-20254.4 KiB119108

README.mdH A D25-Apr-20253.4 KiB7960

buildme.ccH A D25-Apr-202537 53

sample_compilerH A D25-Apr-2025823 225

sample_linkerH A D25-Apr-2025783 245

toolchain_config.bzlH A D25-Apr-20252.4 KiB7871

README.md

1# Writing a custom C++ toolchain
2
3This example shows how to define and use a simple custom C++ toolchain.
4
5Output is non-functional: simple scripts replace compilation and linking
6with `I compiled!` and `I linked!` messages.
7
8[BUILD](BUILD) provides detailed implementation walkthrough. The fundamental
9sequence is:
10
111. Define the toolchain
121. Define how to invoke the toolchain.
13
14`1` is C++-specific: the logic and structure depends specifically on C++'s
15language model. Other languages have their own models.
16
17`2` supports two variations. `--crosstool_top` / `--cpu`, the legacy version,
18is C++-specific. `--platforms`, the modern version, is much more generic and
19supports all languages and features like [incompatible target
20skipping](https://docs.bazel.build/versions/master/platforms.html#skipping-incompatible-targets). See
21[Building with
22Platforms](https://docs.bazel.build/versions/master/platforms-intro.html) and
23its [C++
24notes](https://docs.bazel.build/versions/master/platforms-intro.html#c) for
25full review.
26
27## Building with the default toolchain
28
29```
30$ bazel clean
31$ bazel build //examples/custom_toolchain:buildme
32$ file bazel-bin/examples/custom_toolchain/libbuildme.a
33bazel-bin/examples/custom_toolchain/libbuildme.a: current ar archive
34```
35
36## Custom toolchain with platforms
37
38This mode requires `--incompatible_enable_cc_toolchain_resolution`. Without this
39flag, `--platforms` and `--extra_toolchains` are ignored and the default
40toolchain triggers.
41
42```
43$ bazel clean
44$ bazel build //examples/custom_toolchain:buildme --platforms=//examples/custom_toolchain:x86_platform --extra_toolchains=//examples/custom_toolchain:platform_based_toolchain --incompatible_enable_cc_toolchain_resolution
45DEBUG: /usr/local/google/home/gregce/bazel/rules_cc/examples/custom_toolchain/toolchain_config.bzl:17:10: Invoking my custom toolchain!
46INFO: From Compiling examples/custom_toolchain/buildme.cc:
47examples/custom_toolchain/sample_compiler: running sample cc_library compiler (produces .o output).
48INFO: From Linking examples/custom_toolchain/libbuildme.a:
49examples/custom_toolchain/sample_linker: running sample cc_library linker (produces .a output).
50
51$ cat bazel-bin/examples/custom_toolchain/libbuildme.a
52examples/custom_toolchain/sample_linker: sample output
53```
54
55This example uses a long command line for demonstration purposes. A real project
56would [register toolchains](https://docs.bazel.build/versions/master/toolchains.html#registering-and-building-with-toolchains)
57in `WORKSPACE` and auto-set
58`--incompatible_enable_cc_toolchain_resolution`. That reduces the command to:
59
60```
61$ bazel build //examples/custom_toolchain:buildme --platforms=//examples/custom_toolchain:x86_platform
62```
63
64## Custom toolchain with legacy selection:
65
66```
67$ bazel clean
68$ bazel build //examples/custom_toolchain:buildme --crosstool_top=//examples/custom_toolchain:legacy_selector --cpu=x86
69DEBUG: /usr/local/google/home/gregce/bazel/rules_cc/examples/custom_toolchain/toolchain_config.bzl:17:10: Invoking my custom toolchain!
70INFO: From Compiling examples/custom_toolchain/buildme.cc:
71examples/custom_toolchain/sample_compiler: running sample cc_library compiler (produces .o output).
72INFO: From Linking examples/custom_toolchain/libbuildme.a:
73examples/custom_toolchain/sample_linker: running sample cc_library linker (produces .a output).
74
75$ cat bazel-bin/examples/custom_toolchain/libbuildme.a
76examples/custom_toolchain/sample_linker: sample output
77```
78
79