Skip to content

Commit 5898426

Browse files
sayrerclaude
andcommitted
Compute Swift toolchain root path dynamically for Bazel 9.
The canonical repo name for http_archive repos changed from +_repo_rules2+ to +http_archive+ in Bazel 9. Instead of hardcoding the path, use a repository rule to resolve it at fetch time via label resolution. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c6d9b01 commit 5898426

4 files changed

Lines changed: 60 additions & 41 deletions

File tree

MODULE.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ exports_files(glob(["usr/**/*"]))
199199
urls = ["https://download.swift.org/swift-6.0.3-release/ubuntu2204-aarch64/swift-6.0.3-RELEASE/swift-6.0.3-RELEASE-ubuntu22.04-aarch64.tar.gz"],
200200
)
201201

202-
register_toolchains("//tools/swift:hermetic_swift_linux_x86_64_toolchain")
202+
swift_toolchain_ext = use_extension("//tools/swift:extensions.bzl", "swift_toolchain_ext")
203+
use_repo(swift_toolchain_ext, "hermetic_swift_toolchain")
204+
205+
register_toolchains("@hermetic_swift_toolchain//:hermetic_swift_linux_x86_64_toolchain")
203206

204207
###############################################################################
205208
# Hermetic LLVM Toolchain

tools/swift/BUILD.bazel

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,8 @@
1-
"""Hermetic Swift toolchain configuration."""
1+
"""Hermetic Swift toolchain configuration.
22
3-
load("@rules_swift//swift/toolchains:swift_toolchain.bzl", "swift_toolchain")
3+
The actual swift_toolchain and toolchain rules are generated dynamically
4+
by the swift_toolchain_repo repository rule (swift_toolchain_wrapper.bzl)
5+
to avoid hardcoding the canonical repo path which changes between Bazel versions.
6+
"""
47

58
package(default_visibility = ["//visibility:public"])
6-
7-
# Hermetic Swift 6.0.3 toolchain for Linux x86_64
8-
swift_toolchain(
9-
name = "hermetic_swift_linux_x86_64",
10-
arch = "x86_64",
11-
# Enable module wrapping (swift-modulewrap) instead of -add_ast_path which is Darwin-only
12-
features = [
13-
"swift.use_module_wrap",
14-
],
15-
os = "linux",
16-
root = "external/+_repo_rules2+swift_linux_x86_64/usr",
17-
toolchain_files = "@swift_linux_x86_64//:usr",
18-
version_file = "@swift_linux_x86_64//:version_file",
19-
)
20-
21-
toolchain(
22-
name = "hermetic_swift_linux_x86_64_toolchain",
23-
exec_compatible_with = [
24-
"@platforms//os:linux",
25-
"@platforms//cpu:x86_64",
26-
],
27-
target_compatible_with = [
28-
"@platforms//os:linux",
29-
"@platforms//cpu:x86_64",
30-
],
31-
toolchain = ":hermetic_swift_linux_x86_64",
32-
toolchain_type = "@rules_swift//toolchains:toolchain_type",
33-
)

tools/swift/extensions.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Module extension for hermetic Swift toolchain."""
2+
3+
load("//tools/swift:swift_toolchain_wrapper.bzl", "swift_toolchain_repo")
4+
5+
def _swift_toolchain_impl(ctx):
6+
swift_toolchain_repo(name = "hermetic_swift_toolchain")
7+
8+
swift_toolchain_ext = module_extension(
9+
implementation = _swift_toolchain_impl,
10+
)
Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
1-
"""Repository rule to create a Swift toolchain with absolute paths."""
1+
"""Repository rule to create a Swift toolchain with the correct repo path."""
22

3-
def _swift_toolchain_path_impl(repository_ctx):
4-
"""Generate the absolute path to the Swift toolchain."""
5-
# Get the path to the Swift toolchain
6-
swift_path = str(repository_ctx.path(Label("@swift_linux_x86_64//:usr")).dirname.dirname)
3+
def _swift_toolchain_repo_impl(repository_ctx):
4+
"""Generate a swift_toolchain BUILD file with the correct root path."""
5+
# Resolve the canonical path to the Swift toolchain repo
6+
swift_repo_path = str(repository_ctx.path(Label("@swift_linux_x86_64//:BUILD.bazel")).dirname)
7+
8+
# Extract the canonical repo directory name (e.g., "+http_archive+swift_linux_x86_64")
9+
# by finding the "external/" segment in the absolute path
10+
parts = swift_repo_path.split("/external/")
11+
repo_dir = parts[-1]
12+
13+
root = "external/{}/usr".format(repo_dir)
714

815
repository_ctx.file("BUILD.bazel", """
16+
load("@rules_swift//swift/toolchains:swift_toolchain.bzl", "swift_toolchain")
17+
918
package(default_visibility = ["//visibility:public"])
1019
11-
exports_files(["swift_root.txt"])
12-
""")
20+
swift_toolchain(
21+
name = "hermetic_swift_linux_x86_64",
22+
arch = "x86_64",
23+
features = [
24+
"swift.use_module_wrap",
25+
],
26+
os = "linux",
27+
root = "{root}",
28+
toolchain_files = "@swift_linux_x86_64//:usr",
29+
version_file = "@swift_linux_x86_64//:version_file",
30+
)
1331
14-
repository_ctx.file("swift_root.txt", swift_path + "/usr")
32+
toolchain(
33+
name = "hermetic_swift_linux_x86_64_toolchain",
34+
exec_compatible_with = [
35+
"@platforms//os:linux",
36+
"@platforms//cpu:x86_64",
37+
],
38+
target_compatible_with = [
39+
"@platforms//os:linux",
40+
"@platforms//cpu:x86_64",
41+
],
42+
toolchain = ":hermetic_swift_linux_x86_64",
43+
toolchain_type = "@rules_swift//toolchains:toolchain_type",
44+
)
45+
""".format(root = root))
1546

16-
swift_toolchain_path = repository_rule(
17-
implementation = _swift_toolchain_path_impl,
47+
swift_toolchain_repo = repository_rule(
48+
implementation = _swift_toolchain_repo_impl,
1849
)

0 commit comments

Comments
 (0)