Skip to content

Commit 29dcff8

Browse files
authored
Add macOS (xcode) support to swift-syntax-rs build
1 parent a8edbcf commit 29dcff8

4 files changed

Lines changed: 33 additions & 16 deletions

File tree

MODULE.bazel

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,17 @@ use_repo(
227227
# patched prebuilt toolchain wired up via `swift_deps` above.
228228
#
229229
# Bazel cannot auto-select between Linux distributions, so the toolchain is
230-
# registered explicitly for the distribution our CI runs on (ubuntu24.04,
231-
# x86_64). Add further platforms here if CI grows to cover them.
230+
# registered explicitly for the distributions/platforms our CI runs on:
231+
# - ubuntu24.04 / x86_64 (Linux CI)
232+
# - xcode (macOS CI — covers both Apple Silicon and Intel)
232233
#
233-
# We register the `exec` toolchain (normal host compilation, targeting
234-
# linux/x86_64) rather than the `embedded` one, which targets `os:none`
235-
# (Embedded Swift) and would not match a normal host build.
234+
# NOTE: The macOS toolchain is distributed as a `.pkg` archive that can only
235+
# be extracted on a macOS host. Do not build the `xcode` toolchain on Linux.
236+
#
237+
# We register the `exec` toolchains (normal host compilation) rather than the
238+
# `embedded` ones, which target `os:none` (Embedded Swift) and would not
239+
# match a normal host build. Bazel selects the toolchain matching the host
240+
# platform automatically.
236241
#
237242
# The Swift version is read from `unified/swift-syntax-rs/.swift-version`, which
238243
# is the single source of truth shared with the local `cargo` build (via
@@ -246,10 +251,12 @@ use_repo(
246251
swift,
247252
"swift_toolchain",
248253
"swift_toolchain_ubuntu24.04",
254+
"swift_toolchain_xcode",
249255
)
250256

251257
register_toolchains(
252258
"@swift_toolchain//:swift_toolchain_exec_ubuntu24.04",
259+
"@swift_toolchain//:swift_toolchain_exec_xcode",
253260
)
254261

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

unified/swift-syntax-rs/BUILD.bazel

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ rust_binary(
4242
name = "swift-syntax-parse",
4343
srcs = ["src/main.rs"],
4444
# The Swift toolchain propagates a runfiles-relative RPATH to its runtime
45-
# `.so`s via `swift_syntax_ffi`'s `CcInfo`, but (unlike `swift_binary`)
46-
# `rust_binary` does not copy those libraries into runfiles. Add the
47-
# toolchain files as `data` so the runtime is found at execution time.
48-
# (rules_swift does not yet support statically linking the runtime.)
49-
data = ["@swift_toolchain_ubuntu24.04//:files"],
45+
# `.so`s/`.dylib`s via `swift_syntax_ffi`'s `CcInfo`, but (unlike
46+
# `swift_binary`) `rust_binary` does not copy those libraries into runfiles.
47+
# Add the toolchain files as `data` so the runtime is found at execution
48+
# time. (rules_swift does not yet support statically linking the runtime.)
49+
# Select the correct per-platform toolchain repo: `xcode` on macOS,
50+
# `ubuntu24.04` on Linux.
51+
data = select({
52+
"@platforms//os:macos": ["@swift_toolchain_xcode//:files"],
53+
"//conditions:default": ["@swift_toolchain_ubuntu24.04//:files"],
54+
}),
5055
edition = "2024",
5156
deps = [":swift_syntax_rs"],
5257
)
@@ -55,6 +60,9 @@ rust_test(
5560
name = "swift_syntax_rs_test",
5661
size = "small",
5762
crate = ":swift_syntax_rs",
58-
data = ["@swift_toolchain_ubuntu24.04//:files"],
63+
data = select({
64+
"@platforms//os:macos": ["@swift_toolchain_xcode//:files"],
65+
"//conditions:default": ["@swift_toolchain_ubuntu24.04//:files"],
66+
}),
5967
edition = "2024",
6068
)

unified/swift-syntax-rs/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,12 @@ Requirements:
144144
- **`clang`** must be installed on the runner. `rules_swift` requires the Bazel
145145
CC toolchain to use clang; the repo's `.bazelrc` already sets
146146
`--repo_env=CC=clang`, so no extra flags are needed.
147-
- The registered Swift toolchain currently targets **ubuntu24.04 / x86_64**
148-
only (Bazel cannot auto-select between Linux distributions). Add more
149-
platforms in `MODULE.bazel` (`swift.toolchain` + `register_toolchains`) if CI
150-
grows to cover them.
147+
- The registered Swift toolchains cover **ubuntu24.04 / x86_64** and
148+
**macOS / `xcode`** (both Apple Silicon and Intel). Bazel automatically
149+
selects the toolchain matching the host platform.
150+
- **macOS only:** the macOS toolchain is distributed as a `.pkg` archive that
151+
can only be fetched and extracted on a macOS host. Building the `xcode`
152+
toolchain on Linux is not supported.
151153

152154
The Swift compiler version is read from [`.swift-version`](.swift-version) by
153155
both the Bazel toolchain (`swift.toolchain(swift_version_file = …)`) and the

unified/swift-syntax-rs/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn swift_runtime_dir() -> Option<PathBuf> {
7373
let close = value_start.find('"')?;
7474
let resource_path = &value_start[..close];
7575

76-
Some(PathBuf::from(resource_path).join("linux"))
76+
Some(PathBuf::from(resource_path).join(if cfg!(target_os = "macos") { "macosx" } else { "linux" }))
7777
}
7878

7979
/// The `swift` driver to invoke: `$SWIFT` if set, otherwise `swift` from `PATH`.

0 commit comments

Comments
 (0)