Skip to content

Commit cf53fd4

Browse files
committed
Unified: Bazel downloaded toolchain on macOS
1 parent 8ebd3bc commit cf53fd4

8 files changed

Lines changed: 215 additions & 126 deletions

File tree

MODULE.bazel

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,9 @@ bazel_dep(name = "gazelle", version = "0.50.0")
3131
bazel_dep(name = "rules_dotnet", version = "0.21.5-codeql.1")
3232
bazel_dep(name = "googletest", version = "1.17.0.bcr.2")
3333
bazel_dep(name = "rules_rust", version = "0.69.0")
34-
bazel_dep(name = "rules_swift", version = "4.0.0-rc5")
34+
bazel_dep(name = "rules_swift", version = "4.0.0-rc5-codeql.1")
3535
bazel_dep(name = "swift-syntax", version = "603.0.2")
3636

37-
# Needed so we can `use_repo` `local_config_xcode` and
38-
# `local_config_apple_cc_toolchains` below (referenced by the per-target
39-
# Xcode-config transition in `unified/swift-syntax-rs/xcode_transition.bzl`).
40-
bazel_dep(name = "apple_support", version = "2.8.0")
4137
bazel_dep(name = "zstd", version = "1.5.7.bcr.1")
4238

4339
bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True)
@@ -223,10 +219,7 @@ use_repo(
223219
"swift-resource-dir-macos",
224220
)
225221

226-
# Swift toolchain for building `unified/swift-syntax-rs`. On Linux we register
227-
# a hermetic swift.org toolchain as the exec toolchain; on macOS `rules_swift`
228-
# auto-registers `xcode_swift_toolchain` (host Xcode + OS-provided Swift
229-
# runtime), which is not hermetic.
222+
# Hermetic swift.org toolchain for building `unified/swift-syntax-rs`.
230223
#
231224
# The version is pinned as a literal rather than read from
232225
# `unified/swift-syntax-rs/.swift-version` via `swift_version_file`: the latter
@@ -244,20 +237,14 @@ use_repo(
244237
swift,
245238
"swift_toolchain",
246239
"swift_toolchain_ubuntu22.04",
240+
"swift_toolchain_xcode",
247241
)
248242

249243
register_toolchains(
250244
"@swift_toolchain//:swift_toolchain_exec_ubuntu22.04",
245+
"@swift_toolchain//:swift_toolchain_exec_xcode",
251246
)
252247

253-
# `apple_support`'s xcode_config and CC toolchains, needed by the Xcode
254-
# transition in `unified/swift-syntax-rs/xcode_transition.bzl`.
255-
xcode_configure = use_extension("@apple_support//xcode:xcode_configure.bzl", "xcode_configure_extension")
256-
use_repo(xcode_configure, "local_config_xcode")
257-
258-
apple_cc_configure = use_extension("@apple_support//crosstool:setup.bzl", "apple_cc_configure_extension")
259-
use_repo(apple_cc_configure, "local_config_apple_cc_toolchains")
260-
261248
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")
262249
node.toolchain(
263250
name = "nodejs",
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""rules_swift MODULE.bazel file"""
2+
3+
module(
4+
name = "rules_swift",
5+
version = "4.0.0-rc5-codeql.1",
6+
bazel_compatibility = [">=8.0.0"],
7+
compatibility_level = 3,
8+
)
9+
10+
bazel_dep(name = "abseil-cpp", version = "20250814.1")
11+
bazel_dep(name = "apple_support", version = "2.8.0")
12+
bazel_dep(name = "bazel_linux_packages", version = "0.4.1")
13+
bazel_dep(name = "bazel_features", version = "1.51.0")
14+
bazel_dep(name = "bazel_skylib", version = "1.7.1")
15+
bazel_dep(name = "nlohmann_json", version = "3.12.0.bcr.1")
16+
bazel_dep(name = "platforms", version = "0.0.11")
17+
bazel_dep(name = "protobuf", version = "34.0.bcr.1")
18+
bazel_dep(name = "rules_cc", version = "0.2.14")
19+
bazel_dep(name = "rules_shell", version = "0.3.0")
20+
bazel_dep(name = "swift_argument_parser", version = "1.7.0")
21+
22+
apt = use_extension("@bazel_linux_packages//apt:extensions.bzl", "apt")
23+
apt.ubuntu(
24+
name = "swift_ubuntu22.04_sysroot",
25+
architectures = ["amd64"],
26+
lockfile = "//swift/internal/extensions:ubuntu22.04_sysroot.lock.json",
27+
packages = [
28+
"libc6-dev",
29+
"libcurl4-openssl-dev",
30+
"libstdc++-11-dev",
31+
"libxml2-dev",
32+
"linux-libc-dev",
33+
"zlib1g-dev",
34+
],
35+
suites = ["jammy"],
36+
)
37+
apt.ubuntu(
38+
name = "swift_ubuntu22.04_aarch64_sysroot",
39+
architectures = ["arm64"],
40+
lockfile = "//swift/internal/extensions:ubuntu22.04_aarch64_sysroot.lock.json",
41+
packages = [
42+
"libc6-dev",
43+
"libcurl4-openssl-dev",
44+
"libstdc++-11-dev",
45+
"libxml2-dev",
46+
"linux-libc-dev",
47+
"zlib1g-dev",
48+
],
49+
suites = ["jammy"],
50+
)
51+
use_repo(
52+
apt,
53+
"swift_ubuntu22.04_aarch64_sysroot",
54+
"swift_ubuntu22.04_sysroot",
55+
)
56+
57+
swift_autoconfiguration = use_repo_rule("//swift/internal:swift_autoconfiguration.bzl", "swift_autoconfiguration")
58+
swift_autoconfiguration(name = "rules_swift_local_config")
59+
60+
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
61+
62+
http_archive(
63+
name = "rules_swift_index_import_5_8",
64+
build_file = "//third_party:rules_swift_index_import/BUILD.overlay",
65+
canonical_id = "index-import-5.8",
66+
sha256 = "28c1ffa39d99e74ed70623899b207b41f79214c498c603915aef55972a851a15",
67+
urls = ["https://github.com/MobileNativeFoundation/index-import/releases/download/5.8.0.1/index-import.tar.gz"],
68+
)
69+
70+
http_archive(
71+
name = "rules_swift_index_import_6_1",
72+
build_file = "//third_party:rules_swift_index_import/BUILD.overlay",
73+
canonical_id = "index-import-6.1",
74+
sha256 = "9a54fc1674af6031125a9884480a1e31e1bcf48b8f558b3e8bcc6b6fcd6e8b61",
75+
urls = ["https://github.com/MobileNativeFoundation/index-import/releases/download/6.1.0.1/index-import.tar.gz"],
76+
)
77+
78+
register_toolchains("//swift/toolchains:all")
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
diff --git a/swift/internal/extensions/toolchains.bzl b/swift/internal/extensions/toolchains.bzl
2+
--- a/swift/internal/extensions/toolchains.bzl
3+
+++ b/swift/internal/extensions/toolchains.bzl
4+
@@ -132,12 +132,11 @@ def toolchains_for_platform(platform, toolchain_repository):
5+
toolchain_repository = toolchain_repository,
6+
)
7+
8+
- if platform != "xcode":
9+
- content += _EXEC_TOOLCHAIN_PLATFORM.format(
10+
- exec_compatible_with = _exec_compatible_with_for_platform(platform),
11+
- platform = platform,
12+
- toolchain_repository = toolchain_repository,
13+
- )
14+
+ content += _EXEC_TOOLCHAIN_PLATFORM.format(
15+
+ exec_compatible_with = _exec_compatible_with_for_platform(platform),
16+
+ platform = platform,
17+
+ toolchain_repository = toolchain_repository,
18+
+ )
19+
20+
if platform in (
21+
"ubuntu22.04",
22+
diff --git a/swift/toolchains/swift_toolchain.bzl b/swift/toolchains/swift_toolchain.bzl
23+
--- a/swift/toolchains/swift_toolchain.bzl
24+
+++ b/swift/toolchains/swift_toolchain.bzl
25+
@@ -605,10 +605,14 @@ def _parse_target_system_name(*, arch, os, target_system_name):
26+
"""Returns the target system name set by the CC toolchain or attempts to create one based on the OS and arch."""
27+
28+
if target_system_name and target_system_name != "local":
29+
+ if os == "macos" and target_system_name.endswith(("-macos", "-macosx")):
30+
+ return target_system_name + "10.15"
31+
return target_system_name
32+
33+
if os == "linux":
34+
return "%s-unknown-linux-gnu" % arch
35+
+ elif os == "macos":
36+
+ return "%s-apple-macos10.15" % arch
37+
elif os == "windows":
38+
# The MSVC cc toolchain reports a `target_gnu_system_name` of "local",
39+
# so synthesize the triple.
40+
@@ -706,5 +710,16 @@ def _swift_toolchain_impl(ctx):
41+
elif ctx.attr.os == "none":
42+
swift_linkopts_cc_info = CcInfo()
43+
+ elif ctx.attr.os == "macos":
44+
+ macos_linkopts = ["-L{}".format(paths.join(toolchain_root, "lib/swift/macosx"))]
45+
+ if sdkroot:
46+
+ macos_linkopts.append("-L{}".format(paths.join(sdkroot, "usr/lib/swift")))
47+
+ swift_linkopts_cc_info = _swift_sdk_linkopts_cc_info(
48+
+ ctx.label,
49+
+ macos_linkopts,
50+
+ ctx.attr.swift_tools[SwiftToolsInfo].additional_inputs if ctx.attr.swift_tools else [],
51+
+ )
52+
+ dynamic_runtime_cc_info = None
53+
+ static_runtime_cc_info = None
54+
elif ctx.attr.os == "android":
55+
if not sdkroot:
56+
fail("Android toolchain requires a sysroot to be set, either via the `sdkroot` attribute or by using a CC toolchain that provides one.")
57+
diff --git a/swift/internal/extensions/toolchain.BUILD b/swift/internal/extensions/toolchain.BUILD
58+
--- a/swift/internal/extensions/toolchain.BUILD
59+
+++ b/swift/internal/extensions/toolchain.BUILD
60+
@@ -333,17 +333,22 @@ swift_toolchain(
61+
features = [
62+
"swift._supports_upcoming_features",
63+
"swift.no_embed_debug_module",
64+
- "swift.use_autolink_extract",
65+
- "swift.lld_gc_workaround",
66+
- "swift.use_module_wrap",
67+
# TODO: This should be removed so that private headers can be used with
68+
# explicit modules, but the build targets for CgRPC need to be cleaned up
69+
# first because they contain C++ code.
70+
"swift.module_map_no_private_headers",
71+
- ],
72+
+ ] + select({
73+
+ "@platforms//os:linux": [
74+
+ "swift.lld_gc_workaround",
75+
+ "swift.use_autolink_extract",
76+
+ "swift.use_module_wrap",
77+
+ ],
78+
+ "//conditions:default": [],
79+
+ }),
80+
os = select({
81+
"@platforms//os:linux": "linux",
82+
"@platforms//os:macos": "macos",
83+
}),
84+
parsed_version = "{swift_version}",
85+
+ sdkroot = "{macos_sdkroot}",
86+
static_runtime = select({
87+
diff --git a/swift/internal/extensions/standalone_toolchain.bzl b/swift/internal/extensions/standalone_toolchain.bzl
88+
--- a/swift/internal/extensions/standalone_toolchain.bzl
89+
+++ b/swift/internal/extensions/standalone_toolchain.bzl
90+
@@ -91,11 +91,13 @@ def _standalone_toolchain_impl(repository_ctx):
91+
strip_prefix = filename.removesuffix(".tar.gz"),
92+
)
93+
94+
+ macos_sdkroot = _run(repository_ctx, ["xcrun", "--sdk", "macosx", "--show-sdk-path"]) if repository_ctx.attr.platform == "xcode" else ""
95+
repository_ctx.file(".swift-version", repository_ctx.attr.swift_version)
96+
repository_ctx.template(
97+
"BUILD.bazel",
98+
repository_ctx.attr._build_template,
99+
substitutions = {
100+
+ "{macos_sdkroot}": macos_sdkroot,
101+
"{swift_version}": repository_ctx.attr.swift_version,
102+
},
103+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"integrity": "sha256-Ly2lS4AlZJMiyqAxKHyCB+2pGeP5aYkQ6zv8QheeSTI=",
3+
"url": "https://github.com/bazelbuild/rules_swift/releases/download/4.0.0-rc5/rules_swift.4.0.0-rc5.tar.gz",
4+
"patches": {
5+
"register_downloaded_macos_toolchain.patch": "sha256-kFzGyE1MhhiDxUfEzRPUTahvycyy+VNgPzQ5I44zRYc="
6+
},
7+
"patch_strip": 1
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"homepage": "https://github.com/bazelbuild/rules_swift",
3+
"maintainers": [],
4+
"repository": [
5+
"github:bazelbuild/rules_swift"
6+
],
7+
"versions": [
8+
"4.0.0-rc5-codeql.1"
9+
],
10+
"yanked_versions": {}
11+
}

unified/swift-syntax-rs/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
load("@rules_cc//cc:defs.bzl", "cc_library")
22
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
3+
load("@rules_swift//swift:swift.bzl", "swift_library")
34
load("//unified:platforms.bzl", "UNIFIED_SUPPORTED_PLATFORMS")
45
load(":swift_runtime.bzl", "swift_runtime_libs")
56
load(":swift_runtime_linking.bzl", "swift_runtime_linking")
6-
load(":xcode_transition.bzl", "xcode_transition_swift_library")
77

88
package(default_visibility = ["//visibility:public"])
99

@@ -34,7 +34,7 @@ cc_library(
3434
)
3535

3636
# Swift FFI shim: wraps swift-syntax and exposes a small C ABI.
37-
xcode_transition_swift_library(
37+
swift_library(
3838
name = "swift_syntax_ffi",
3939
srcs = ["swift/Sources/SwiftSyntaxFFI/SwiftSyntaxFFI.swift"],
4040
module_name = "SwiftSyntaxFFI",

unified/swift-syntax-rs/README.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,26 +174,20 @@ Requirements:
174174
- **`clang`** must be installed on the runner. `rules_swift` requires the Bazel
175175
CC toolchain to use clang; the repo's `.bazelrc` already sets
176176
`--repo_env=CC=clang`, so no extra flags are needed.
177-
- The registered Swift toolchains cover **ubuntu24.04 / x86_64** and
178-
**macOS / `xcode`** (Apple Silicon and Intel). Bazel selects the toolchain
179-
matching the host. Targets are marked `target_compatible_with` these two
180-
OSes, so on Windows Bazel skips them cleanly.
181-
- **macOS only:** the Swift toolchain comes from the host Xcode installation
182-
(`rules_swift` auto-registers `xcode_swift_toolchain`), which also needs
183-
Xcode's CC toolchain and xcode_config; these are applied to the Swift
184-
target via an incoming-edge Starlark transition (see
185-
[`xcode_transition.bzl`](xcode_transition.bzl)), so other targets on macOS
186-
keep using Bazel's default CC toolchain.
177+
- The registered Swift toolchains cover **ubuntu22.04 / x86_64** and
178+
**macOS** (Apple Silicon and Intel). Bazel selects the toolchain matching the
179+
host. Targets are marked `target_compatible_with` these two OSes, so on
180+
Windows Bazel skips them cleanly.
181+
- **macOS only:** `rules_swift` downloads the pinned Swift toolchain from
182+
swift.org. The Bazel C++ toolchain must still provide the macOS SDK, but a
183+
full Xcode installation is not required.
187184

188185
The Swift compiler version is kept in sync across three places: the
189186
[`.swift-version`](.swift-version) file (read by the local `cargo`/`swift build`
190187
and by [swiftly](https://www.swift.org/swiftly/)), the literal `swift_version`
191188
pinned on `swift.toolchain(...)` in the root `MODULE.bazel` (the hermetic
192-
swift.org **Linux** Bazel toolchain), and the `swift-syntax` release in
193-
`swift/Package.swift`. On **macOS** the version is *not* pinned by the Bazel
194-
build: `rules_swift` auto-registers the host `xcode_swift_toolchain`, which uses
195-
whichever Swift ships with the installed Xcode. So the pin governs Linux (and
196-
local) builds, while the macOS compiler version depends on the host Xcode.
189+
swift.org Bazel toolchain), and the `swift-syntax` release in
190+
`swift/Package.swift`.
197191

198192
(The Bazel toolchain pins a literal rather than reading `.swift-version` via
199193
`swift_version_file`, because the latter makes the module extension read a

0 commit comments

Comments
 (0)