11load ("@rules_rust//rust:defs.bzl" , "rust_binary" , "rust_library" , "rust_test" )
2- load ("@rules_swift//swift:swift .bzl" , "swift_library " )
2+ load (":xcode_transition .bzl" , "xcode_transition_swift_library " )
33
44package (default_visibility = ["//visibility:public" ])
55
6- # The pinned Swift version, shared with the local `cargo` build and
7- # `swift/Package.swift`. Referenced by the `swift.toolchain` extension in
8- # //:MODULE.bazel via `swift_version_file`.
6+ # Pinned Swift version, shared with `cargo` and `swift/Package.swift`.
7+ # Referenced by `swift.toolchain(swift_version_file = ...)` in //:MODULE.bazel.
98exports_files ([".swift-version" ])
109
11- # The Swift FFI shim: wraps swift-syntax and exposes a small C ABI
12- # (`ssr_parse_json` / `ssr_string_free`). Built with the hermetic Swift
13- # toolchain registered in //:MODULE.bazel; provides a `CcInfo` that the Rust
14- # targets below link against.
15- swift_library (
10+ # Targets in this package require a Swift toolchain (Linux or macOS).
11+ # `select()` gives us OR-of-OSes; other platforms get marked incompatible
12+ # so `bazel build/test //...` skips them cleanly.
13+ _SWIFT_SUPPORTED_PLATFORMS = select ({
14+ "@platforms//os:linux" : [],
15+ "@platforms//os:macos" : [],
16+ "//conditions:default" : ["@platforms//:incompatible" ],
17+ })
18+
19+ # Swift FFI shim: wraps swift-syntax and exposes a small C ABI. The Rust
20+ # targets below link against its `CcInfo`.
21+ xcode_transition_swift_library (
1622 name = "swift_syntax_ffi" ,
1723 srcs = ["swift/Sources/SwiftSyntaxFFI/SwiftSyntaxFFI.swift" ],
1824 module_name = "SwiftSyntaxFFI" ,
25+ target_compatible_with = _SWIFT_SUPPORTED_PLATFORMS ,
1926 deps = [
2027 "@swift-syntax//:SwiftParser" ,
2128 "@swift-syntax//:SwiftSyntax" ,
2229 ],
2330)
2431
25- # Safe Rust bindings on top of the C ABI. `build.rs` is intentionally *not*
26- # wired up here (no `cargo_build_script`): under Bazel the Swift side is
27- # provided by `:swift_syntax_ffi`, whereas `build.rs` only exists to build the
28- # Swift shim in the local `cargo` workflow.
32+ # Safe Rust bindings on top of the C ABI. Under Bazel the Swift side comes
33+ # from `:swift_syntax_ffi`; `build.rs` is only used by the `cargo` workflow.
2934rust_library (
3035 name = "swift_syntax_rs" ,
3136 srcs = glob (
3237 ["src/**/*.rs" ],
3338 exclude = ["src/main.rs" ],
3439 ),
3540 edition = "2024" ,
41+ target_compatible_with = _SWIFT_SUPPORTED_PLATFORMS ,
3642 deps = [
3743 ":swift_syntax_ffi" ,
3844 ],
@@ -41,20 +47,27 @@ rust_library(
4147rust_binary (
4248 name = "swift-syntax-parse" ,
4349 srcs = ["src/main.rs" ],
44- # 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" ],
50+ # `rust_binary` doesn't copy the Swift runtime into runfiles the way
51+ # `swift_binary` does. On Linux, ship the standalone toolchain's runtime;
52+ # on macOS the OS provides it at `/usr/lib/swift` (rpath'd by
53+ # `xcode_swift_toolchain`).
54+ data = select ({
55+ "@platforms//os:macos" : [],
56+ "@platforms//os:linux" : ["@swift_toolchain_ubuntu24.04//:files" ],
57+ }),
5058 edition = "2024" ,
59+ target_compatible_with = _SWIFT_SUPPORTED_PLATFORMS ,
5160 deps = [":swift_syntax_rs" ],
5261)
5362
5463rust_test (
5564 name = "swift_syntax_rs_test" ,
5665 size = "small" ,
5766 crate = ":swift_syntax_rs" ,
58- data = ["@swift_toolchain_ubuntu24.04//:files" ],
67+ data = select ({
68+ "@platforms//os:macos" : [],
69+ "@platforms//os:linux" : ["@swift_toolchain_ubuntu24.04//:files" ],
70+ }),
5971 edition = "2024" ,
72+ target_compatible_with = _SWIFT_SUPPORTED_PLATFORMS ,
6073)
0 commit comments