Skip to content

Commit 32dcc0f

Browse files
authored
bazel: scope Xcode-coupled flags to Swift builds via --config=swift_macos
1 parent a29be0b commit 32dcc0f

4 files changed

Lines changed: 37 additions & 17 deletions

File tree

.bazelrc

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@ common --override_module=semmle_code=%workspace%/misc/bazel/semmle_code_stub
1212

1313
build --repo_env=CC=clang --repo_env=CXX=clang++
1414

15-
# Use apple_support's xcode_config: its version strings match the ones
16-
# `rules_swift`'s `system_sdk` selects are keyed on.
17-
build:macos --xcode_version_config=@local_config_xcode//:host_xcodes
18-
19-
# Force apple_support's CC toolchains over Bazel's built-in `local_config_cc`
20-
# (whose `target_gnu_system_name` is literally "local", which breaks
21-
# `xcode_swift_toolchain`'s triple parsing). Root-level `--extra_toolchains`
22-
# beats module-registered toolchains; only matches Apple platforms.
23-
build:macos --extra_toolchains=@local_config_apple_cc_toolchains//:all
15+
# macOS Xcode-coupled flags, scoped to Swift builds only so that non-Swift
16+
# builds (C/C++/Rust/...) work with just the Command Line Tools. Enable
17+
# with `--config=swift_macos` (see `unified/swift-syntax-rs/README.md`; CI
18+
# for the `swift/` package passes it in `.github/workflows/swift.yml`).
19+
#
20+
# - `--xcode_version_config` selects `apple_support`'s xcode_config, whose
21+
# version strings match the ones `rules_swift`'s `system_sdk` selects are
22+
# keyed on (Bazel's built-in `host_xcodes` does not match).
23+
# - `--extra_toolchains` forces `apple_support`'s CC toolchain ahead of
24+
# Bazel's built-in `local_config_cc` (whose `target_gnu_system_name` is
25+
# literally "local", which breaks `xcode_swift_toolchain`'s triple
26+
# parsing). Root-level `--extra_toolchains` beats module-registered
27+
# toolchains; only matches Apple platforms.
28+
build:swift_macos --xcode_version_config=@local_config_xcode//:host_xcodes
29+
build:swift_macos --extra_toolchains=@local_config_apple_cc_toolchains//:all
2430

2531
# Disable Android SDK auto-detection (we don't use it, and rules_android has Bazel 9 compatibility issues)
2632
build --repo_env=ANDROID_HOME=

.github/workflows/swift.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@ jobs:
4444
sudo apt-get install -y uuid-dev zlib1g-dev
4545
- name: Build Swift extractor
4646
shell: bash
47+
env:
48+
# On macOS, opt into the Xcode-coupled toolchain wiring only for
49+
# Swift builds. Empty on Linux (hermetic swift.org toolchain).
50+
BAZEL_SWIFT_CONFIG: ${{ runner.os == 'macOS' && '--config=swift_macos' || '' }}
4751
run: |
48-
bazel run :install
52+
bazel run $BAZEL_SWIFT_CONFIG :install
4953
- name: Run Swift tests
5054
shell: bash
55+
env:
56+
BAZEL_SWIFT_CONFIG: ${{ runner.os == 'macOS' && '--config=swift_macos' || '' }}
5157
run: |
52-
bazel test ... --test_tag_filters=-override --test_output=errors
58+
bazel test $BAZEL_SWIFT_CONFIG ... --test_tag_filters=-override --test_output=errors
5359
clang-format:
5460
runs-on: ubuntu-latest
5561
steps:

MODULE.bazel

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ bazel_dep(name = "rules_swift", version = "4.0.0-rc4")
3535
bazel_dep(name = "swift-syntax", version = "602.0.0.bcr.2")
3636
# Direct dep on `apple_support` (transitively pulled in by `rules_swift`) so we
3737
# can `use_repo` `local_config_xcode` and `local_config_apple_cc_toolchains`
38-
# below, needed to make `xcode_swift_toolchain` work on macOS. See `.bazelrc`.
38+
# below, needed by the `swift_macos` bazelrc config that makes
39+
# `xcode_swift_toolchain` work on macOS. See `.bazelrc`.
3940
bazel_dep(name = "apple_support", version = "2.6.1")
4041

4142
bazel_dep(name = "zstd", version = "1.5.7.bcr.1")
@@ -248,15 +249,17 @@ register_toolchains(
248249
)
249250

250251
# `apple_support`'s xcode_config, selected via `--xcode_version_config` in
251-
# `.bazelrc`. Needed because rules_swift's `system_sdk` selects use Xcode
252-
# version strings from `apple_support`'s `xcode_locator.m`, which Bazel's
253-
# built-in `host_xcodes` does not match.
252+
# the `swift_macos` bazelrc config (see `.bazelrc`). Needed because
253+
# rules_swift's `system_sdk` selects use Xcode version strings from
254+
# `apple_support`'s `xcode_locator.m`, which Bazel's built-in `host_xcodes`
255+
# does not match.
254256
xcode_configure = use_extension("@apple_support//xcode:xcode_configure.bzl", "xcode_configure_extension")
255257
use_repo(xcode_configure, "local_config_xcode")
256258

257259
# `apple_support`'s CC toolchains, forced ahead of Bazel's built-in
258-
# `local_config_cc` via `--extra_toolchains` in `.bazelrc`. Must be visible
259-
# from the root module for the flag to resolve.
260+
# `local_config_cc` via `--extra_toolchains` in the `swift_macos` bazelrc
261+
# config (see `.bazelrc`). Must be visible from the root module for the flag
262+
# to resolve.
260263
apple_cc_configure = use_extension("@apple_support//crosstool:setup.bzl", "apple_cc_configure_extension")
261264
use_repo(apple_cc_configure, "local_config_apple_cc_toolchains")
262265

unified/swift-syntax-rs/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ bazel test //unified/swift-syntax-rs:swift_syntax_rs_test
139139
bazel run //unified/swift-syntax-rs:swift-syntax-parse < some.swift
140140
```
141141

142+
On **macOS**, add `--config=swift_macos` to any of the above so that
143+
`rules_swift`'s `xcode_swift_toolchain` gets the Xcode-coupled CC toolchain
144+
and xcode_config it needs. The flag is opt-in so that non-Swift builds on
145+
macOS don't require a full Xcode.app install.
146+
142147
Requirements:
143148

144149
- **`clang`** must be installed on the runner. `rules_swift` requires the Bazel

0 commit comments

Comments
 (0)