Skip to content

Commit 66071dd

Browse files
authored
Use xcode_swift_toolchain on macOS instead of patched Unix toolchain
1 parent 0122729 commit 66071dd

5 files changed

Lines changed: 27 additions & 178 deletions

File tree

‎.bazelrc‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,6 @@ common --flag_alias=host_swiftcopt=@rules_swift//swift:exec_copt
4444
build:macos --swiftcopt=-target --swiftcopt=arm64-apple-macosx10.15
4545
build:macos --host_swiftcopt=-target --host_swiftcopt=arm64-apple-macosx10.15
4646

47-
# Disable `swift-autolink-extract` on macOS.
48-
#
49-
# rules_swift 4.0.0-rc4's standalone toolchain template
50-
# (`swift/internal/extensions/BUILD.bazel.tpl`) unconditionally hardcodes
51-
# `swift.use_autolink_extract` into the exec `swift_toolchain(...)` features
52-
# list — even for macOS. That schedules a `SwiftAutolinkExtract` action for
53-
# every `swift_library` (e.g. `@swift-syntax//:SwiftSyntax601`). But
54-
# `swift-autolink-extract` is a Linux/ELF-only workflow: on macOS the Apple
55-
# linker handles autolinking natively via `LC_LINKER_OPTION`, and running the
56-
# tool from a swift.org standalone toolchain against Mach-O `.o` files fails
57-
# with an opaque "worker failed" error.
58-
#
59-
# Negating the feature on the command line flips
60-
# `SWIFT_FEATURE_USE_AUTOLINK_EXTRACT in ctx.features` to `False` in
61-
# `swift_toolchain.bzl`, so the autolink-extract tool config (and therefore
62-
# the action) is never registered on macOS. The Linux exec toolchain is
63-
# unaffected.
64-
build:macos --features=-swift.use_autolink_extract
6547
# Disable Android SDK auto-detection (we don't use it, and rules_android has Bazel 9 compatibility issues)
6648
build --repo_env=ANDROID_HOME=
6749

‎MODULE.bazel‎

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,6 @@ 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

37-
# rules_swift 4.0.0-rc4's standalone toolchain extension unconditionally
38-
# emits Linux/ELF-only linker flags (swiftrt.o, -lstdc++, -lrt, -ldl,
39-
# -static-libgcc) for its macOS toolchain, which breaks any binary linked
40-
# via `swift_library` on macOS. Apply a local patch that skips those flags
41-
# when the toolchain OS is macos. See the patch header and
42-
# `misc/bazel/patches/rules_swift/macos-linkopts.patch` for details.
43-
single_version_override(
44-
module_name = "rules_swift",
45-
patch_strip = 1,
46-
patches = ["//misc/bazel/patches/rules_swift:macos-linkopts.patch"],
47-
version = "4.0.0-rc4",
48-
)
4937
bazel_dep(name = "zstd", version = "1.5.7.bcr.1")
5038

5139
bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True)
@@ -239,16 +227,28 @@ use_repo(
239227
# `rules_swift` standalone-toolchain extension and is independent of the
240228
# patched prebuilt toolchain wired up via `swift_deps` above.
241229
#
242-
# Bazel cannot auto-select between Linux distributions, so the toolchain is
243-
# registered explicitly for the distributions/platforms our CI runs on:
244-
# - ubuntu24.04 / x86_64 (Linux CI)
245-
# - xcode (macOS CI — covers both Apple Silicon and Intel)
230+
# We register the `exec` toolchain for **Linux only**. On macOS we do NOT
231+
# use this standalone toolchain: it is the Unix-shaped `swift_toolchain`
232+
# rule (`swift/toolchains/swift_toolchain.bzl`) which emits Linux/ELF
233+
# linkopts (`swiftrt.o`, `-lstdc++`, `-lrt`, `-ldl`, `-static-libgcc`) and
234+
# a Linux-shaped `-L{root}/lib/swift/{os}` search path — none of which make
235+
# sense on macOS. Instead, on macOS we rely on rules_swift's auto-registered
236+
# `@rules_swift//swift/toolchains:xcode-toolchain-*` toolchains (backed by
237+
# `xcode_swift_toolchain.bzl`, which has its own macOS-aware
238+
# `_swift_linkopts_cc_info` that uses the Apple linker's autolinking,
239+
# points at `Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx`,
240+
# and rpaths the OS-provided `/usr/lib/swift`). Those toolchains are
241+
# auto-registered by rules_swift's own MODULE.bazel via
242+
# `register_toolchains("//swift/toolchains:all")` and use the Xcode
243+
# selected on the host, so macOS contributors need a working Xcode
244+
# (or Command Line Tools) install.
246245
#
247-
# NOTE: The macOS toolchain is distributed as a `.pkg` archive that can only
248-
# be extracted on a macOS host. Do not build the `xcode` toolchain on Linux.
246+
# NOTE: The macOS standalone toolchain is distributed as a `.pkg` archive
247+
# that can only be extracted on a macOS host. Do not build the `xcode`
248+
# standalone toolchain on Linux.
249249
#
250-
# We register the `exec` toolchains (normal host compilation) rather than the
251-
# `embedded` ones, which target `os:none` (Embedded Swift) and would not
250+
# We register the `exec` toolchain (normal host compilation) rather than the
251+
# `embedded` one, which targets `os:none` (Embedded Swift) and would not
252252
# match a normal host build. Bazel selects the toolchain matching the host
253253
# platform automatically.
254254
#
@@ -264,12 +264,10 @@ use_repo(
264264
swift,
265265
"swift_toolchain",
266266
"swift_toolchain_ubuntu24.04",
267-
"swift_toolchain_xcode",
268267
)
269268

270269
register_toolchains(
271270
"@swift_toolchain//:swift_toolchain_exec_ubuntu24.04",
272-
"@swift_toolchain//:swift_toolchain_exec_xcode",
273271
)
274272

275273
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")

‎misc/bazel/patches/rules_swift/BUILD.bazel‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎misc/bazel/patches/rules_swift/macos-linkopts.patch‎

Lines changed: 0 additions & 128 deletions
This file was deleted.

‎unified/swift-syntax-rs/BUILD.bazel‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ rust_binary(
5959
# The Swift toolchain propagates a runfiles-relative RPATH to its runtime
6060
# `.so`s/`.dylib`s via `swift_syntax_ffi`'s `CcInfo`, but (unlike
6161
# `swift_binary`) `rust_binary` does not copy those libraries into runfiles.
62-
# Add the toolchain files as `data` so the runtime is found at execution
63-
# time. (rules_swift does not yet support statically linking the runtime.)
64-
# Select the correct per-platform toolchain repo: `xcode` on macOS,
65-
# `ubuntu24.04` on Linux.
62+
# On Linux the standalone swift.org toolchain provides the runtime, so we
63+
# add its files as `data` so the runtime is found at execution time.
64+
# On macOS the runtime lives in the OS at `/usr/lib/swift` (which
65+
# `xcode_swift_toolchain` rpaths automatically), so no data is needed.
66+
# (rules_swift does not yet support statically linking the runtime.)
6667
data = select({
67-
"@platforms//os:macos": ["@swift_toolchain_xcode//:files"],
68+
"@platforms//os:macos": [],
6869
"@platforms//os:linux": ["@swift_toolchain_ubuntu24.04//:files"],
6970
}),
7071
edition = "2024",
@@ -77,7 +78,7 @@ rust_test(
7778
size = "small",
7879
crate = ":swift_syntax_rs",
7980
data = select({
80-
"@platforms//os:macos": ["@swift_toolchain_xcode//:files"],
81+
"@platforms//os:macos": [],
8182
"@platforms//os:linux": ["@swift_toolchain_ubuntu24.04//:files"],
8283
}),
8384
edition = "2024",

0 commit comments

Comments
 (0)