From 29dcff8ff38fb0ce33c809abb39a85cef17f4b23 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 13 Jul 2026 14:18:38 +0000 Subject: [PATCH] Add macOS (xcode) support to swift-syntax-rs build --- MODULE.bazel | 17 ++++++++++++----- unified/swift-syntax-rs/BUILD.bazel | 20 ++++++++++++++------ unified/swift-syntax-rs/README.md | 10 ++++++---- unified/swift-syntax-rs/build.rs | 2 +- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index aa1f6555e0c..babe3803e2c 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -227,12 +227,17 @@ use_repo( # patched prebuilt toolchain wired up via `swift_deps` above. # # Bazel cannot auto-select between Linux distributions, so the toolchain is -# registered explicitly for the distribution our CI runs on (ubuntu24.04, -# x86_64). Add further platforms here if CI grows to cover them. +# registered explicitly for the distributions/platforms our CI runs on: +# - ubuntu24.04 / x86_64 (Linux CI) +# - xcode (macOS CI — covers both Apple Silicon and Intel) # -# We register the `exec` toolchain (normal host compilation, targeting -# linux/x86_64) rather than the `embedded` one, which targets `os:none` -# (Embedded Swift) and would not match a normal host build. +# NOTE: The macOS toolchain is distributed as a `.pkg` archive that can only +# be extracted on a macOS host. Do not build the `xcode` toolchain on Linux. +# +# We register the `exec` toolchains (normal host compilation) rather than the +# `embedded` ones, which target `os:none` (Embedded Swift) and would not +# match a normal host build. Bazel selects the toolchain matching the host +# platform automatically. # # The Swift version is read from `unified/swift-syntax-rs/.swift-version`, which # is the single source of truth shared with the local `cargo` build (via @@ -246,10 +251,12 @@ use_repo( swift, "swift_toolchain", "swift_toolchain_ubuntu24.04", + "swift_toolchain_xcode", ) register_toolchains( "@swift_toolchain//:swift_toolchain_exec_ubuntu24.04", + "@swift_toolchain//:swift_toolchain_exec_xcode", ) node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node") diff --git a/unified/swift-syntax-rs/BUILD.bazel b/unified/swift-syntax-rs/BUILD.bazel index 9db40d4db30..c187cc19faf 100644 --- a/unified/swift-syntax-rs/BUILD.bazel +++ b/unified/swift-syntax-rs/BUILD.bazel @@ -42,11 +42,16 @@ rust_binary( name = "swift-syntax-parse", srcs = ["src/main.rs"], # The Swift toolchain propagates a runfiles-relative RPATH to its runtime - # `.so`s via `swift_syntax_ffi`'s `CcInfo`, but (unlike `swift_binary`) - # `rust_binary` does not copy those libraries into runfiles. Add the - # toolchain files as `data` so the runtime is found at execution time. - # (rules_swift does not yet support statically linking the runtime.) - data = ["@swift_toolchain_ubuntu24.04//:files"], + # `.so`s/`.dylib`s via `swift_syntax_ffi`'s `CcInfo`, but (unlike + # `swift_binary`) `rust_binary` does not copy those libraries into runfiles. + # Add the toolchain files as `data` so the runtime is found at execution + # time. (rules_swift does not yet support statically linking the runtime.) + # Select the correct per-platform toolchain repo: `xcode` on macOS, + # `ubuntu24.04` on Linux. + data = select({ + "@platforms//os:macos": ["@swift_toolchain_xcode//:files"], + "//conditions:default": ["@swift_toolchain_ubuntu24.04//:files"], + }), edition = "2024", deps = [":swift_syntax_rs"], ) @@ -55,6 +60,9 @@ rust_test( name = "swift_syntax_rs_test", size = "small", crate = ":swift_syntax_rs", - data = ["@swift_toolchain_ubuntu24.04//:files"], + data = select({ + "@platforms//os:macos": ["@swift_toolchain_xcode//:files"], + "//conditions:default": ["@swift_toolchain_ubuntu24.04//:files"], + }), edition = "2024", ) diff --git a/unified/swift-syntax-rs/README.md b/unified/swift-syntax-rs/README.md index a2d66495a54..85b8cf8186e 100644 --- a/unified/swift-syntax-rs/README.md +++ b/unified/swift-syntax-rs/README.md @@ -144,10 +144,12 @@ Requirements: - **`clang`** must be installed on the runner. `rules_swift` requires the Bazel CC toolchain to use clang; the repo's `.bazelrc` already sets `--repo_env=CC=clang`, so no extra flags are needed. -- The registered Swift toolchain currently targets **ubuntu24.04 / x86_64** - only (Bazel cannot auto-select between Linux distributions). Add more - platforms in `MODULE.bazel` (`swift.toolchain` + `register_toolchains`) if CI - grows to cover them. +- The registered Swift toolchains cover **ubuntu24.04 / x86_64** and + **macOS / `xcode`** (both Apple Silicon and Intel). Bazel automatically + selects the toolchain matching the host platform. +- **macOS only:** the macOS toolchain is distributed as a `.pkg` archive that + can only be fetched and extracted on a macOS host. Building the `xcode` + toolchain on Linux is not supported. The Swift compiler version is read from [`.swift-version`](.swift-version) by both the Bazel toolchain (`swift.toolchain(swift_version_file = …)`) and the diff --git a/unified/swift-syntax-rs/build.rs b/unified/swift-syntax-rs/build.rs index 0adb140140d..3cd2e285874 100644 --- a/unified/swift-syntax-rs/build.rs +++ b/unified/swift-syntax-rs/build.rs @@ -73,7 +73,7 @@ fn swift_runtime_dir() -> Option { let close = value_start.find('"')?; let resource_path = &value_start[..close]; - Some(PathBuf::from(resource_path).join("linux")) + Some(PathBuf::from(resource_path).join(if cfg!(target_os = "macos") { "macosx" } else { "linux" })) } /// The `swift` driver to invoke: `$SWIFT` if set, otherwise `swift` from `PATH`.