bazel-contrib / bazel-contrib/bazel-lib

[FR]: Add `//command_line_option:extra_toolchains` in transitions rules

Open
#398 0 comments 0 reactions 0 assignees View on GitHub
enhancement need: discussion
Dominant language
Starlark
Stars
182
Forks
134
Avg merge
1d 46m
Merged PRs (30d)
1

Description

### What is the current behavior?

Currently rules like `platform_transition_binary` only support transitioning the `target_platform`:

https://github.com/aspect-build/bazel-lib/blob/f2bb1d1519390f04b7974b44c1c6879b1ffd33bd/lib/transitions.bzl#L10-L14

### Describe the feature

Add `extra_toolchains` to the transitions rules so that the toolchains can be directly controlled while minimizing `.bazelrc` additions for `--extra_toolchains`. Perhaps this is scope-creep, but I'd imagine that this could be commonly used when building language-specific multi-platform OCI images for cloud servers, especially when various lang rules like one may prefer a different toolchain for various language rules like `rules_rust` or `rules_go`. Probably also helps with Bazel IDE-integrations as the `config` isn't switching often.

Implementing it seemed straightforward, but I don't know if the example I got inspiration from was a proper patch to `bazel-lib`'s `platform_transition_binary` or not.

A `.bzl` file could look like:

```starlark
CPP_TARGETS = [
struct(
os = "linux", arch = "amd64", variant = "gcc",
target_platform = "//platforms:linux_x86_64",
extra_toolchains = ["@gcc_toolchain_x86_64//:cc_toolchain"],
),
struct(
os = "linux", arch = "amd64", variant = "llvm",
target_platform = "//platforms:linux_x86_64",
extra_toolchains = ["@llvm_toolchain_with_sysroot//:cc-toolchain-x86_64-linux"],
),
struct(
os = "linux", arch = "arm64", variant = "gcc",
target_platform = "//platforms:linux_aarch64",
extra_toolchains = ["@gcc_toolchain_aarch64//:cc_toolchain"],
),
struct(
os = "linux", arch = "arm64", variant = "llvm",
target_platform = "//platforms:linux_aarch64",
extra_toolchains = ["@llvm_toolchain_with_sysroot//:cc-toolchain-aarch64-linux"],
),
]
```

BUILD:
```starlark
cc_binary(
name = "hello",
srcs = ["hello.cpp"],
)

[transition_binary(
name = "{}_{}_{}".format(target.os, target.arch, target.variant),
binary = ":hello",
target_platform = target.target_platform,
extra_toolchains = target.extra_toolchains,
) for target in CPP_TARGETS]
```

Can yield something like:

```shell
$ bazel run //examples/cpp/hello:linux_amd64_gcc
INFO: Invocation ID: 8ff884fd-66d5-431d-95d8-b71d1787e548
INFO: Build option --toolchain_resolution_debug has changed, discarding analysis cache.
INFO: Analyzed target //examples/cpp/hello:linux_amd64_gcc (0 packages loaded, 8171 targets configured).
INFO: Found 1 target...
Target //examples/cpp/hello:linux_amd64_gcc up-to-date:
bazel-out/k8-fastbuild-ST-3dde90589ae2/bin/examples/cpp/hello/hello
bazel-bin/examples/cpp/hello/linux_amd64_gcc/hello
INFO: Elapsed time: 0.553s, Critical Path: 0.03s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/examples/cpp/hello/linux_amd64_gcc/hello
Hello, world!
compiler: gcc 10.3.0
$ bazel run //examples/cpp/hello:linux_amd64_llvm
INFO: Invocation ID: 8c287c3d-65ac-48dc-a4a2-635d4515c1f9
INFO: Analyzed target //examples/cpp/hello:linux_amd64_llvm (0 packages loaded, 8833 targets configured).
INFO: Found 1 target...
Target //examples/cpp/hello:linux_amd64_llvm up-to-date:
bazel-out/k8-fastbuild-ST-8ba7b2ee70c2/bin/examples/cpp/hello/hello
bazel-bin/examples/cpp/hello/linux_amd64_llvm/hello
INFO: Elapsed time: 0.521s, Critical Path: 0.05s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/examples/cpp/hello/linux_amd64_llvm/hello
Hello, world!
compiler: clang 15.0.6
$ bazel build //examples/cpp/hello:linux_arm64_llvm
INFO: Invocation ID: 5b52b8b5-ff2a-4ee4-abf9-e86cff15af65
INFO: Analyzed target //examples/cpp/hello:linux_arm64_llvm (1 packages loaded, 7114 targets configured).
INFO: Found 1 target...
Target //examples/cpp/hello:linux_arm64_llvm up-to-date:
bazel-out/k8-fastbuild-ST-ebaa31fb65be/bin/examples/cpp/hello/hello
bazel-bin/examples/cpp/hello/linux_arm64_llvm/hello
INFO: Elapsed time: 2.200s, Critical Path: 0.96s
INFO: 7 processes: 5 internal, 2 linux-sandbox.
INFO: Build completed successfully, 7 total actions
$ file -L bazel-bin/examples/cpp/hello/linux_arm64_llvm/hello
bazel-bin/examples/cpp/hello/linux_arm64_llvm/hello: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, BuildID[md5/uuid]=6624fd7e00d6c6a4cd4cf50c8fa1b6b7, not stripped
$ bazel run //examples/cpp/hello:linux_arm64_llvm_push
INFO: Invocation ID: 29f94c8d-6094-487e-9b9e-5654f746cae7
INFO: Analyzed target //examples/cpp/hello:linux_arm64_llvm_push (32 packages loaded, 10418 targets configured).
INFO: Found 1 target...
Target //examples/cpp/hello:linux_arm64_llvm_push up-to-date:
bazel-bin/examples/cpp/hello/push_linux_arm64_llvm_push.sh
INFO: Elapsed time: 14.468s, Critical Path: 13.38s
INFO: 35 processes: 2 remote cache hit, 19 internal, 11 linux-sandbox, 3 local.
INFO: Build completed successfully, 35 total actions
INFO: Running command line: bazel-bin/examples/cpp/hello/push_linux_arm64_llvm_push.sh
2023/03/25 13:49:22 pushed blob: sha256:3ca2f80360ec222c4c2dfd136f47c2f15ecb7b63b847836826c7d7c1fa6786b8
2023/03/25 13:49:22 pushed blob: sha256:4d704a88b82ecd694220e6bc895f01d7af15faf7bf6fa40bee96535a595aab6d
2023/03/25 13:49:22 pushed blob: sha256:4f70717a8bb3a9fc3400b1282d4ac4885bce11e92f53c2b9e236d774f1c54264
2023/03/25 13:49:22 pushed blob: sha256:5f80a38cb015768c1f273987f6ac784632f0aac64ddad5bd8a1d2fe9e751df0c
2023/03/25 13:49:22 pushed blob: sha256:9fb3436fe5063ba67a833910986f9c53a49b01b7203ca584b0b2ccf61007e3de
2023/03/25 13:49:22 pushed blob: sha256:28f3b53db0b05e8bd2542448dfcb1182089d3ca2fe4d5c189daf298211e5befb
2023/03/25 13:49:22 localhost:5000/examples/cpp/hello@sha256:11bd8c24f399c77ee4d76c4b3250a0cc2f11424f3d15aae0605af76cb6e4b186: digest: sha256:11bd8c24f399c77ee4d76c4b3250a0cc2f11424f3d15aae0605af76cb6e4b186 size: 1080
2023/03/25 13:49:22 localhost:5000/examples/cpp/hello:latest_linux_arm64_llvm: digest: sha256:11bd8c24f399c77ee4d76c4b3250a0cc2f11424f3d15aae0605af76cb6e4b186 size: 1080
$ docker pull localhost:5000/examples/cpp/hello:latest_linux_arm64_llvm
latest_linux_arm64_llvm: Pulling from examples/cpp/hello
5f80a38cb015: Already exists
9fb3436fe506: Already exists
4f70717a8bb3: Already exists
4d704a88b82e: Already exists
28f3b53db0b0: Pull complete
Digest: sha256:11bd8c24f399c77ee4d76c4b3250a0cc2f11424f3d15aae0605af76cb6e4b186
Status: Downloaded newer image for localhost:5000/examples/cpp/hello:latest_linux_arm64_llvm
localhost:5000/examples/cpp/hello:latest_linux_arm64_llvm
$ docker run --rm --platform linux/arm64 localhost:5000/examples/cpp/hello:latest_linux_arm64_llvm
Hello, world!
compiler: clang 15.0.6
$ docker run --rm --platform linux/arm64 localhost:5000/examples/cpp/hello:latest_linux_arm64_gcc
Hello, world!
compiler: gcc 10.3.0
```

This something open to adding?

Contributor guide

Open the contributing guide

Research direction

Start by reading lib/transitions.bzl at the linked transition rules and compare their current target_platform handling with the requested BUILD usage. Verify the intended extra_toolchains attribute semantics in Bazel transition documentation, then confirm that the shown C++ targets select their requested toolchains while existing transition behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Domain
build-system
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.