|
| 1 | +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file") |
| 2 | +load("//:local_bazel.bzl", "local_bazel_import") |
| 3 | + |
| 4 | +SHORT_VERSION_MAPPING = { |
| 5 | + "bazel_7": "7.7.1", |
| 6 | + "bazel_8": "8.7.0", |
| 7 | + "bazel_9": "9.1.0", |
| 8 | +} |
| 9 | + |
| 10 | +SHA_MATRIX = { |
| 11 | + "7.7.1": { |
| 12 | + "linux_x86_64": "115a1b62be95f29e5821d4dddffba1b058905a48019b499919c285e7f708d5e2", |
| 13 | + "linux_arm64": "71df04ec724f1b577f1f47ec9a6b81d13f39683f6c3215cacf45fdaf40b2c5c1", |
| 14 | + "darwin_x86_64": "8582aea5ee2d8d0448bbda10fd7034734db1a21cbe4ea351a10012b969aa5d31", |
| 15 | + "darwin_arm64": "fe8a1ee9064e94afae075c0dd4efb453db9c1373b9df12fecbff8479d408eb08", |
| 16 | + "windows_x86_64": "6d9fb21e806cf4f4e61bfa2bc865df4900ffdc1e9ea90ca1016ba70367ef0de4", |
| 17 | + }, |
| 18 | + "8.7.0": { |
| 19 | + "linux_x86_64": "d7606e679b78067c811096fb3d6cf135225b528835ca396e3a4dddf957859544", |
| 20 | + "linux_arm64": "bfe9558bd8a2ecfe4841ec46c0dbccb4b469fe22d81f2f859de0de222b3e7ce3", |
| 21 | + "darwin_x86_64": "76f3eb05782098e9f9ddd8247ec969b085195a3ae2978c81721a2235052ccf26", |
| 22 | + "darwin_arm64": "575f20fb23955e02f73519befd180df635b4ed0960c60f0e70fcc8d74014a713", |
| 23 | + "windows_x86_64": "29f1796f57379933340afa135f02703ffa21dd30135754bea695f8fd15103420", |
| 24 | + }, |
| 25 | + "9.1.0": { |
| 26 | + "linux_x86_64": "a667454f3f4f8878df8199136b82c199f6ada8477b337fae3b1ef854f01e4e2f", |
| 27 | + "linux_arm64": "ba933bfc943e4c44f0743a5823aa2312a34b39628532add5dd037e08d8ec27a4", |
| 28 | + "darwin_x86_64": "666c6c79eda285cada5f5c39c891c6dd7ee0971b20bff365ea87a4b897271433", |
| 29 | + "darwin_arm64": "084a1784fa8f0dcae77fb4e88faa15048d8149a36c947ce198508bffb060e1bb", |
| 30 | + "windows_x86_64": "b457dccd36a9bb9be01326cc1d069a201bb50b4b94562a652afb6f43c5148d42", |
| 31 | + }, |
| 32 | +} |
| 33 | + |
| 34 | +def _remote_bazel_extension_impl(module_ctx): |
| 35 | + os_name = module_ctx.os.name.lower() |
| 36 | + os_key = "linux" |
| 37 | + exe_extension = "" |
| 38 | + if "windows" in os_name: |
| 39 | + os_key = "windows" |
| 40 | + exe_extension = ".exe" |
| 41 | + elif "mac" in os_name or "darwin" in os_name: |
| 42 | + os_key = "darwin" |
| 43 | + |
| 44 | + cpu_arch = module_ctx.os.arch.lower() |
| 45 | + if "x86_64" in cpu_arch or "amd64" in cpu_arch: |
| 46 | + arch_key = "x86_64" |
| 47 | + elif "arm64" in cpu_arch: |
| 48 | + arch_key = "arm64" |
| 49 | + else: |
| 50 | + fail("Unsupported architecture '{}'.".format(cpu_arch)) |
| 51 | + |
| 52 | + platform_key = "{}_{}".format(os_key, arch_key) |
| 53 | + |
| 54 | + requested_versions = {} |
| 55 | + for mod in module_ctx.modules: |
| 56 | + for tag in mod.tags.download: |
| 57 | + requested_versions[tag.bazel_version] = True |
| 58 | + |
| 59 | + for bazel_version in requested_versions: |
| 60 | + if bazel_version not in SHORT_VERSION_MAPPING: |
| 61 | + fail("Unrecognised version '%s'" % bazel_version) |
| 62 | + version = SHORT_VERSION_MAPPING[bazel_version] |
| 63 | + filename = "bazel-%s-%s-%s%s" % (version, os_key, arch_key, exe_extension) |
| 64 | + url = "https://github.com/bazelbuild/bazel/releases/download/%s/%s" % (version, filename) |
| 65 | + |
| 66 | + http_file( |
| 67 | + name = bazel_version, |
| 68 | + downloaded_file_path = "bazel" + exe_extension, |
| 69 | + executable = True, |
| 70 | + url = url, |
| 71 | + sha256 = SHA_MATRIX[version][platform_key], |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +def _local_bazel_extension_impl(module_ctx): |
| 76 | + local_bazel_import(name = "local_bazel") |
| 77 | + |
| 78 | +download_tag = tag_class(attrs = {"bazel_version": attr.string(mandatory = True)}) |
| 79 | + |
| 80 | +remote_bazel_extension = module_extension( |
| 81 | + implementation = _remote_bazel_extension_impl, |
| 82 | + tag_classes = {"download": download_tag}, |
| 83 | +) |
| 84 | + |
| 85 | +local_bazel_extension = module_extension( |
| 86 | + implementation = _local_bazel_extension_impl, |
| 87 | +) |
0 commit comments