|
| 1 | +"Provide access to ZSTD" |
| 2 | + |
| 3 | +ZSTD_PLATFORMS = { |
| 4 | + "darwin_amd64": struct( |
| 5 | + compatible_with = [ |
| 6 | + "@platforms//os:osx", |
| 7 | + "@platforms//cpu:x86_64", |
| 8 | + ], |
| 9 | + ), |
| 10 | + "darwin_arm64": struct( |
| 11 | + compatible_with = [ |
| 12 | + "@platforms//os:osx", |
| 13 | + "@platforms//cpu:aarch64", |
| 14 | + ], |
| 15 | + ), |
| 16 | + "linux_amd64": struct( |
| 17 | + compatible_with = [ |
| 18 | + "@platforms//os:linux", |
| 19 | + "@platforms//cpu:x86_64", |
| 20 | + ], |
| 21 | + ), |
| 22 | + "linux_arm64": struct( |
| 23 | + compatible_with = [ |
| 24 | + "@platforms//os:linux", |
| 25 | + "@platforms//cpu:aarch64", |
| 26 | + ], |
| 27 | + ), |
| 28 | +} |
| 29 | + |
| 30 | +ZSTD_PREBUILT = { |
| 31 | + "darwin_amd64": ( |
| 32 | + "https://github.com/aspect-build/zstd-prebuilt/releases/download/v1.5.6/zstd_darwin_amd64", |
| 33 | + "e4d517212005cf26f8b8d657455d1380318b071cb52a3ffd9dfbdf4c2ba71a13", |
| 34 | + ), |
| 35 | + "darwin_arm64": ( |
| 36 | + "https://github.com/aspect-build/zstd-prebuilt/releases/download/v1.5.6/zstd_darwin_arm64", |
| 37 | + "6e210eeae08fb6ba38c3ac2d1857075c28113aef68296f7e396f1180f7e894b9", |
| 38 | + ), |
| 39 | + "linux_amd64": ( |
| 40 | + "https://github.com/aspect-build/zstd-prebuilt/releases/download/v1.5.6/zstd_linux_amd64", |
| 41 | + "0f0bd1193509a598629d7fa745c4b0b6d5fa6719e0c94c01ef0f20e466d801a7", |
| 42 | + ), |
| 43 | + "linux_arm64": ( |
| 44 | + "https://github.com/aspect-build/zstd-prebuilt/releases/download/v1.5.6/zstd_linux_arm64", |
| 45 | + "82aacf8f1c67ff3c94e04afb0721a848bbba70fbf8249ee4bc4c9085afb84548", |
| 46 | + ), |
| 47 | +} |
| 48 | + |
| 49 | +def _zstd_binary_repo(rctx): |
| 50 | + (url, sha256) = ZSTD_PREBUILT[rctx.attr.platform] |
| 51 | + rctx.download( |
| 52 | + url = url, |
| 53 | + output = "zstd", |
| 54 | + executable = True, |
| 55 | + sha256 = sha256, |
| 56 | + ) |
| 57 | + binary = "zstd" |
| 58 | + |
| 59 | + rctx.file("BUILD.bazel", """\ |
| 60 | +# @generated by @aspect_bazel_lib//lib/private:zstd_toolchain.bzl |
| 61 | +
|
| 62 | +load("@aspect_bazel_lib//lib/private:zstd_toolchain.bzl", "zstd_toolchain") |
| 63 | +
|
| 64 | +package(default_visibility = ["//visibility:public"]) |
| 65 | +
|
| 66 | +zstd_toolchain(name = "zstd_toolchain", binary = "{}") |
| 67 | +""".format(binary)) |
| 68 | + |
| 69 | +zstd_binary_repo = repository_rule( |
| 70 | + implementation = _zstd_binary_repo, |
| 71 | + attrs = { |
| 72 | + "platform": attr.string(mandatory = True, values = ZSTD_PLATFORMS.keys()), |
| 73 | + }, |
| 74 | +) |
| 75 | + |
| 76 | +ZstdInfo = provider( |
| 77 | + doc = "Provide info for executing zstd", |
| 78 | + fields = { |
| 79 | + "binary": "zstd executable", |
| 80 | + }, |
| 81 | +) |
| 82 | + |
| 83 | +def _zstd_toolchain_impl(ctx): |
| 84 | + binary = ctx.executable.binary |
| 85 | + |
| 86 | + # Make the $(ZSTD_BIN) variable available in places like genrules. |
| 87 | + # See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables |
| 88 | + template_variables = platform_common.TemplateVariableInfo({ |
| 89 | + "ZSTD_BIN": binary.path, |
| 90 | + }) |
| 91 | + |
| 92 | + default_info = DefaultInfo( |
| 93 | + files = depset(ctx.files.binary + ctx.files.files), |
| 94 | + ) |
| 95 | + zstdinfo = ZstdInfo( |
| 96 | + binary = binary, |
| 97 | + ) |
| 98 | + |
| 99 | + # Export all the providers inside our ToolchainInfo |
| 100 | + # so the resolved_toolchain rule can grab and re-export them. |
| 101 | + toolchain_info = platform_common.ToolchainInfo( |
| 102 | + zstdinfo = zstdinfo, |
| 103 | + template_variables = template_variables, |
| 104 | + default = default_info, |
| 105 | + ) |
| 106 | + |
| 107 | + return [toolchain_info, template_variables, default_info] |
| 108 | + |
| 109 | +zstd_toolchain = rule( |
| 110 | + implementation = _zstd_toolchain_impl, |
| 111 | + attrs = { |
| 112 | + "binary": attr.label( |
| 113 | + doc = "a command to find on the system path", |
| 114 | + allow_files = True, |
| 115 | + executable = True, |
| 116 | + cfg = "exec", |
| 117 | + ), |
| 118 | + "files": attr.label_list(allow_files = True), |
| 119 | + }, |
| 120 | +) |
| 121 | + |
| 122 | +def _zstd_toolchains_repo_impl(rctx): |
| 123 | + # Expose a concrete toolchain which is the result of Bazel resolving the toolchain |
| 124 | + # for the execution or zstdget platform. |
| 125 | + # Workaround for https://github.com/bazelbuild/bazel/issues/14009 |
| 126 | + starlark_content = """\ |
| 127 | +# @generated by @aspect_bazel_lib//lib/private:zstd_toolchain.bzl |
| 128 | +
|
| 129 | +# Forward all the providers |
| 130 | +def _resolved_toolchain_impl(ctx): |
| 131 | + toolchain_info = ctx.toolchains["@aspect_bazel_lib//lib:zstd_toolchain_type"] |
| 132 | + return [ |
| 133 | + toolchain_info, |
| 134 | + toolchain_info.default, |
| 135 | + toolchain_info.zstdinfo, |
| 136 | + toolchain_info.template_variables, |
| 137 | + ] |
| 138 | +
|
| 139 | +# Copied from java_toolchain_alias |
| 140 | +# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl |
| 141 | +resolved_toolchain = rule( |
| 142 | + implementation = _resolved_toolchain_impl, |
| 143 | + toolchains = ["@aspect_bazel_lib//lib:zstd_toolchain_type"], |
| 144 | + incompatible_use_toolchain_transition = True, |
| 145 | +) |
| 146 | +""" |
| 147 | + rctx.file("defs.bzl", starlark_content) |
| 148 | + |
| 149 | + build_content = """# @generated by @aspect_bazel_lib//lib/private:zstd_toolchain.bzl |
| 150 | +load(":defs.bzl", "resolved_toolchain") |
| 151 | +load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS") |
| 152 | +
|
| 153 | +resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"])""" |
| 154 | + |
| 155 | + for [platform, meta] in ZSTD_PLATFORMS.items(): |
| 156 | + build_content += """ |
| 157 | +toolchain( |
| 158 | + name = "{platform}_toolchain", |
| 159 | + exec_compatible_with = {compatible_with}, |
| 160 | + toolchain = "@{user_repository_name}_{platform}//:zstd_toolchain", |
| 161 | + toolchain_type = "@aspect_bazel_lib//lib:zstd_toolchain_type", |
| 162 | +) |
| 163 | +""".format( |
| 164 | + platform = platform, |
| 165 | + user_repository_name = rctx.attr.user_repository_name, |
| 166 | + compatible_with = meta.compatible_with, |
| 167 | + ) |
| 168 | + |
| 169 | + rctx.file("BUILD.bazel", build_content) |
| 170 | + |
| 171 | +zstd_toolchains_repo = repository_rule( |
| 172 | + _zstd_toolchains_repo_impl, |
| 173 | + doc = """Creates a repository that exposes a zstd_toolchain_type zstdget.""", |
| 174 | + attrs = { |
| 175 | + "user_repository_name": attr.string(doc = "Base name for toolchains repository"), |
| 176 | + }, |
| 177 | +) |
0 commit comments