|
1 | | -"""Per-target Xcode configuration for `//unified/swift-syntax-rs` on macOS. |
| 1 | +"""Per-target Xcode configuration for the `swift_library` under |
| 2 | +`//unified/swift-syntax-rs` on macOS. |
2 | 3 |
|
3 | 4 | On macOS, `rules_swift` auto-registers `xcode_swift_toolchain`, whose |
4 | 5 | analysis reads `cc_toolchain.target_gnu_system_name` and fails on Bazel's |
5 | | -built-in `local_config_cc` (triple is literally "local"). Working around |
6 | | -this needs two flags: |
| 6 | +built-in `local_config_cc` (whose triple is literally "local"). Working |
| 7 | +around this needs two flags: |
7 | 8 |
|
8 | 9 | - `--xcode_version_config=@local_config_xcode//:host_xcodes` — selects |
9 | 10 | `apple_support`'s xcode_config, whose version strings match the ones |
10 | 11 | `rules_swift`'s `system_sdk` selects are keyed on. |
11 | 12 | - `--extra_toolchains=@local_config_apple_cc_toolchains//:all` — forces |
12 | 13 | `apple_support`'s CC toolchain ahead of `local_config_cc`. |
13 | 14 |
|
14 | | -We apply them via an incoming-edge Starlark transition on the public |
15 | | -`//unified/swift-syntax-rs` binary/test wrappers rather than globally in |
16 | | -`.bazelrc`. That keeps every other target on macOS on Bazel's default CC |
17 | | -toolchain (`local_config_cc`) and avoids materializing the |
18 | | -`@local_config_xcode` / `@local_config_apple_cc_toolchains` repos unless |
19 | | -something under `//unified/swift-syntax-rs` is actually being built. |
| 15 | +We apply them via an incoming-edge Starlark transition on the |
| 16 | +`swift_library` target itself (through the `xcode_transition_swift_library` |
| 17 | +macro below), rather than globally in `.bazelrc`. That keeps every other |
| 18 | +target on macOS on Bazel's default CC toolchain (`local_config_cc`) and |
| 19 | +avoids materializing the `@local_config_xcode` / |
| 20 | +`@local_config_apple_cc_toolchains` repos unless something under |
| 21 | +`//unified/swift-syntax-rs` is actually being built. |
| 22 | +
|
| 23 | +The transition is placed on `swift_library` — not on the downstream Rust |
| 24 | +targets — so the Apple CC toolchain is only used for analyzing the Swift |
| 25 | +side. The Rust compilation and any other transitive C/C++ deps stay on |
| 26 | +Bazel's default CC toolchain, matching how they build elsewhere in the |
| 27 | +repository. |
20 | 28 |
|
21 | 29 | The transition is a no-op on non-macOS platforms. |
22 | 30 | """ |
23 | 31 |
|
24 | | -load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test") |
| 32 | +load("@rules_swift//swift:swift.bzl", "swift_library") |
25 | 33 | load("//misc/bazel:os.bzl", "os_select") |
26 | 34 |
|
27 | 35 | _XCODE_VERSION_CONFIG = "//command_line_option:xcode_version_config" |
@@ -50,97 +58,58 @@ _xcode_transition = transition( |
50 | 58 | ) |
51 | 59 |
|
52 | 60 | def _wrapper_impl(ctx): |
53 | | - # `ctx.attr.actual` is a list because of the incoming transition. |
54 | 61 | src = ctx.attr.actual[0] |
55 | | - src_default = src[DefaultInfo] |
56 | | - src_exe = src_default.files_to_run.executable |
57 | | - src_runfiles = src_default.default_runfiles |
58 | | - |
59 | | - # Copy (not symlink) the executable so that runfiles lookups via |
60 | | - # `argv[0]` resolve under this wrapper's runfiles tree. This mirrors |
61 | | - # the pattern used by `//swift:rules.bzl` `_cc_transition_impl`. |
62 | | - out = ctx.actions.declare_file(ctx.label.name) |
63 | | - ctx.actions.run_shell( |
64 | | - inputs = [src_exe], |
65 | | - outputs = [out], |
66 | | - command = "cp {src} {dst}".format(src = src_exe.path, dst = out.path), |
67 | | - ) |
68 | | - |
69 | | - # Rewrite runfiles so the wrapped executable is replaced by the copy. |
70 | | - files = src_runfiles.files.to_list() |
71 | | - if src_exe in files: |
72 | | - files.remove(src_exe) |
73 | | - files.append(out) |
74 | | - runfiles = ctx.runfiles(files = files) |
75 | | - |
76 | | - return [DefaultInfo( |
77 | | - executable = out, |
78 | | - files = depset([out]), |
79 | | - runfiles = runfiles, |
80 | | - )] |
81 | | - |
82 | | -_xcode_transition_binary_rule = rule( |
83 | | - implementation = _wrapper_impl, |
84 | | - attrs = { |
85 | | - "actual": attr.label( |
86 | | - mandatory = True, |
87 | | - cfg = _xcode_transition, |
88 | | - executable = True, |
89 | | - ), |
90 | | - "os": attr.string(mandatory = True), |
91 | | - "_allowlist_function_transition": attr.label( |
92 | | - default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
93 | | - ), |
94 | | - }, |
95 | | - executable = True, |
96 | | -) |
97 | | - |
98 | | -_xcode_transition_test_rule = rule( |
| 62 | + providers = [ |
| 63 | + # Only forward the providers a downstream `rust_*` target reads |
| 64 | + # from a `deps` entry. `CcInfo` carries the linking info; forward |
| 65 | + # `OutputGroupInfo` too for `bazel build --output_groups=...`. |
| 66 | + src[DefaultInfo], |
| 67 | + ] |
| 68 | + for p in (CcInfo, OutputGroupInfo): |
| 69 | + if p in src: |
| 70 | + providers.append(src[p]) |
| 71 | + return providers |
| 72 | + |
| 73 | +_xcode_transition_swift_library_rule = rule( |
99 | 74 | implementation = _wrapper_impl, |
100 | 75 | attrs = { |
101 | 76 | "actual": attr.label( |
102 | 77 | mandatory = True, |
103 | 78 | cfg = _xcode_transition, |
104 | | - executable = True, |
| 79 | + providers = [CcInfo], |
105 | 80 | ), |
106 | 81 | "os": attr.string(mandatory = True), |
107 | 82 | "_allowlist_function_transition": attr.label( |
108 | 83 | default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
109 | 84 | ), |
110 | 85 | }, |
111 | | - test = True, |
112 | 86 | ) |
113 | 87 |
|
114 | | -def _wrap(inner_rule, wrapper_rule, name, visibility = None, tags = None, target_compatible_with = None, **kwargs): |
115 | | - """Wrap a Rust target with the Xcode-config transition. |
116 | | -
|
117 | | - Splits `name` into a private inner target `_impl_<name>` (built with |
118 | | - `inner_rule` and hidden from wildcards via `tags = ["manual"]`) and a |
119 | | - public `name` (built with `wrapper_rule`, which applies the Xcode |
120 | | - transition on macOS). The `_impl_` prefix — rather than an `_impl` |
121 | | - suffix — preserves the caller's original suffix, which Bazel requires |
122 | | - for test rules (target names must end in `_test`).""" |
| 88 | +def xcode_transition_swift_library(name, visibility = None, tags = None, target_compatible_with = None, **kwargs): |
| 89 | + """`swift_library` wrapped in the macOS Xcode-config transition. |
| 90 | +
|
| 91 | + Splits `name` into a private inner target `_impl_<name>` (a plain |
| 92 | + `swift_library`, hidden from wildcards via `tags = ["manual"]`) and a |
| 93 | + public `name` that applies the incoming Xcode-config transition on |
| 94 | + macOS and forwards the inner target's `CcInfo` (and other relevant |
| 95 | + providers) to downstream consumers. Downstream `rust_*` targets can |
| 96 | + depend on `name` as an ordinary `deps` entry — they stay in the |
| 97 | + default configuration; only the `swift_library` sub-graph flips to |
| 98 | + the Apple CC toolchain, and only on macOS. |
| 99 | + """ |
123 | 100 | inner_name = "_impl_%s" % name |
124 | | - inner_rule( |
| 101 | + swift_library( |
125 | 102 | name = inner_name, |
126 | 103 | visibility = ["//visibility:private"], |
127 | 104 | tags = (tags or []) + ["manual"], |
128 | 105 | target_compatible_with = target_compatible_with, |
129 | 106 | **kwargs |
130 | 107 | ) |
131 | | - wrapper_rule( |
| 108 | + _xcode_transition_swift_library_rule( |
132 | 109 | name = name, |
133 | 110 | visibility = visibility, |
134 | 111 | tags = tags, |
135 | 112 | target_compatible_with = target_compatible_with, |
136 | 113 | actual = ":" + inner_name, |
137 | 114 | os = os_select(linux = "linux", macos = "macos", default = "other"), |
138 | 115 | ) |
139 | | - |
140 | | -def xcode_transition_rust_binary(name, **kwargs): |
141 | | - """`rust_binary` wrapped in the macOS Xcode-config transition.""" |
142 | | - _wrap(rust_binary, _xcode_transition_binary_rule, name = name, **kwargs) |
143 | | - |
144 | | -def xcode_transition_rust_test(name, **kwargs): |
145 | | - """`rust_test` wrapped in the macOS Xcode-config transition.""" |
146 | | - _wrap(rust_test, _xcode_transition_test_rule, name = name, **kwargs) |
0 commit comments