|
1 | | -"""Repository rule to create a Swift toolchain with absolute paths.""" |
| 1 | +"""Repository rule to create a Swift toolchain with the correct repo path.""" |
2 | 2 |
|
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) |
7 | 14 |
|
8 | 15 | repository_ctx.file("BUILD.bazel", """ |
| 16 | +load("@rules_swift//swift/toolchains:swift_toolchain.bzl", "swift_toolchain") |
| 17 | +
|
9 | 18 | package(default_visibility = ["//visibility:public"]) |
10 | 19 |
|
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 | +) |
13 | 31 |
|
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)) |
15 | 46 |
|
16 | | -swift_toolchain_path = repository_rule( |
17 | | - implementation = _swift_toolchain_path_impl, |
| 47 | +swift_toolchain_repo = repository_rule( |
| 48 | + implementation = _swift_toolchain_repo_impl, |
18 | 49 | ) |
0 commit comments