Skip to content

Commit f4c41e4

Browse files
authored
bazel: give apple_support cc toolchains priority on macOS
1 parent 7c84693 commit f4c41e4

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎.bazelrc‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ build:macos --host_swiftcopt=-target --host_swiftcopt=arm64-apple-macosx10.15
5858
# Wired up in `MODULE.bazel` via the `xcode_configure` extension.
5959
build:macos --xcode_version_config=@local_config_xcode//:host_xcodes
6060

61+
# Give `apple_support`'s CC toolchains priority over Bazel's built-in
62+
# `@rules_cc//...:local_config_cc` on macOS.
63+
#
64+
# `apple_support` registers `@local_config_apple_cc_toolchains//:all` from
65+
# its own `MODULE.bazel`, but module-registered toolchains are ordered by
66+
# the module DAG and Bazel's built-in `local_config_cc` (pulled in by
67+
# `rules_cc`) wins resolution first. That toolchain reports a
68+
# `target_gnu_system_name` of literally `"local"`, and
69+
# `rules_swift`'s `xcode_swift_toolchain` blows up in
70+
# `target_triples.parse(ctx.var.get("CC_TARGET_TRIPLE") or
71+
# cc_toolchain.target_gnu_system_name)` with:
72+
#
73+
# Invalid target triple: local, this likely means you're using the wrong
74+
# CC toolchain, make sure you include apple_support in your project
75+
#
76+
# Root-level `--extra_toolchains` beats all module-registered toolchains,
77+
# so this forces `@local_config_apple_cc_toolchains//:cc-toolchain-darwin_*`
78+
# to be selected for both the target and exec platforms. The apple_cc
79+
# toolchains only match Apple platform constraints, so this is a no-op
80+
# for any non-Apple exec/target platform.
81+
build:macos --extra_toolchains=@local_config_apple_cc_toolchains//:all
82+
6183
# Disable Android SDK auto-detection (we don't use it, and rules_android has Bazel 9 compatibility issues)
6284
build --repo_env=ANDROID_HOME=
6385

‎MODULE.bazel‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,24 @@ bazel_dep(name = "rules_rust", version = "0.69.0")
3434
bazel_dep(name = "rules_swift", version = "4.0.0-rc4")
3535
bazel_dep(name = "swift-syntax", version = "602.0.0.bcr.2")
3636
# `apple_support` is a transitive dep of `rules_swift`, but we depend on it
37-
# directly so we can `use_repo` `local_config_xcode` (see below). This lets
38-
# `xcode_swift_toolchain`'s `system_sdk` selects — which are keyed on the
39-
# exact Xcode version string from `xcode_locator.m` (e.g.
40-
# `xcode_26_6_0_17F113`) — resolve on macOS.
37+
# directly for two reasons on macOS:
38+
#
39+
# 1. We `use_repo` `local_config_xcode` (see below) so
40+
# `xcode_swift_toolchain`'s `system_sdk` selects — which are keyed on
41+
# the exact Xcode version string from `xcode_locator.m` (e.g.
42+
# `xcode_26_6_0_17F113`) — resolve.
43+
#
44+
# 2. It registers `@local_config_apple_cc_toolchains//:all`, whose
45+
# `cc-toolchain-darwin_*` toolchains report a proper Apple target
46+
# triple (`arm64-apple-macosx` / `x86_64-apple-macosx`) via
47+
# `target_gnu_system_name`. Bazel's built-in `local_config_cc`
48+
# toolchain reports the literal string `"local"`, and
49+
# `xcode_swift_toolchain` fails analysis in
50+
# `target_triples.parse(ctx.var.get("CC_TARGET_TRIPLE") or
51+
# cc_toolchain.target_gnu_system_name)` with
52+
# "Invalid target triple: local". Since module-registered toolchains
53+
# are ordered by the module DAG and `local_config_cc` wins by default,
54+
# we override the priority via `--extra_toolchains` in `.bazelrc`.
4155
bazel_dep(name = "apple_support", version = "2.6.1")
4256

4357
bazel_dep(name = "zstd", version = "1.5.7.bcr.1")

0 commit comments

Comments
 (0)