|
| 1 | +"Setup a diffutils toolchain repositories and rules" |
| 2 | + |
| 3 | +load(":repo_utils.bzl", "repo_utils") |
| 4 | + |
| 5 | +DIFFUTILS_PLATFORMS = { |
| 6 | + "darwin_amd64": struct( |
| 7 | + release_platform = "x86_64-apple-darwin", |
| 8 | + compatible_with = [ |
| 9 | + "@platforms//os:macos", |
| 10 | + "@platforms//cpu:x86_64", |
| 11 | + ], |
| 12 | + ), |
| 13 | + "darwin_arm64": struct( |
| 14 | + release_platform = "aarch64-apple-darwin", |
| 15 | + compatible_with = [ |
| 16 | + "@platforms//os:macos", |
| 17 | + "@platforms//cpu:aarch64", |
| 18 | + ], |
| 19 | + ), |
| 20 | + "linux_amd64": struct( |
| 21 | + release_platform = "x86_64-unknown-linux-gnu", |
| 22 | + compatible_with = [ |
| 23 | + "@platforms//os:linux", |
| 24 | + "@platforms//cpu:x86_64", |
| 25 | + ], |
| 26 | + ), |
| 27 | + "windows_amd64": struct( |
| 28 | + release_platform = "x86_64-pc-windows-msvc", |
| 29 | + archive_extension = "zip", |
| 30 | + compatible_with = [ |
| 31 | + "@platforms//os:windows", |
| 32 | + "@platforms//cpu:x86_64", |
| 33 | + ], |
| 34 | + ), |
| 35 | +} |
| 36 | + |
| 37 | +DIFFUTILS_VERSION = "0.4.2" |
| 38 | + |
| 39 | +# https://github.com/uutils/diffutils/releases |
| 40 | +# |
| 41 | +# The integrity hashes can be computed with |
| 42 | +# shasum -b -a 384 [downloaded file] | awk '{ print $1 }' | xxd -r -p | base64 |
| 43 | +DIFFUTILS_INTEGRITIES = { |
| 44 | + "aarch64-apple-darwin": "sha384-m5EzRSRFl5NE8bFw+9rV4n06OeMY/tpieZiQepbpDp7/prL2Zr6sw4LWnfhZC+15", |
| 45 | + "x86_64-apple-darwin": "sha384-tj3xGzlcdggMrIdrqnrK468cReGkKNQW1ZbKinOHMiMhhILuB1lzyzO2Jmfj9/GM", |
| 46 | + "x86_64-pc-windows-msvc": "sha384-kHUBxaHOZPqiXd0exGQsQEPDrDb0O0boAFu6KWSE3K2i1OO5bzwyz8VhmjHlvjGv", |
| 47 | + "x86_64-unknown-linux-gnu": "sha384-7qAQT0YR+zaGDPLYkHcBxLzVI+0lwHQx/VMkgEmvgVXkCntM3xaRjUov1Eyz1VBN", |
| 48 | +} |
| 49 | + |
| 50 | +DiffInfo = provider( |
| 51 | + doc = "Provide info for executing diff", |
| 52 | + fields = { |
| 53 | + "bin": "Executable diff binary", |
| 54 | + }, |
| 55 | +) |
| 56 | + |
| 57 | +def _diff_toolchain_impl(ctx): |
| 58 | + binary = ctx.file.bin |
| 59 | + |
| 60 | + # Make the $(DIFFUTILS_BIN) variable available in places like genrules. |
| 61 | + # See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables |
| 62 | + template_variables = platform_common.TemplateVariableInfo({ |
| 63 | + "DIFFUTILS_BIN": binary.path, |
| 64 | + }) |
| 65 | + default_info = DefaultInfo( |
| 66 | + files = depset([binary]), |
| 67 | + runfiles = ctx.runfiles(files = [binary]), |
| 68 | + ) |
| 69 | + diff_info = DiffInfo( |
| 70 | + bin = binary, |
| 71 | + ) |
| 72 | + |
| 73 | + # Export all the providers inside our ToolchainInfo |
| 74 | + # so the resolved_toolchain rule can grab and re-export them. |
| 75 | + toolchain_info = platform_common.ToolchainInfo( |
| 76 | + diffinfo = diff_info, |
| 77 | + template_variables = template_variables, |
| 78 | + default = default_info, |
| 79 | + ) |
| 80 | + |
| 81 | + return [default_info, toolchain_info, template_variables] |
| 82 | + |
| 83 | +diff_toolchain = rule( |
| 84 | + implementation = _diff_toolchain_impl, |
| 85 | + attrs = { |
| 86 | + "bin": attr.label( |
| 87 | + mandatory = True, |
| 88 | + allow_single_file = True, |
| 89 | + ), |
| 90 | + }, |
| 91 | +) |
| 92 | + |
| 93 | +def _diffutils_toolchains_repo_impl(rctx): |
| 94 | + # Expose a concrete toolchain which is the result of Bazel resolving the toolchain |
| 95 | + # for the execution or target platform. |
| 96 | + # Workaround for https://github.com/bazelbuild/bazel/issues/14009 |
| 97 | + starlark_content = """# @generated by @aspect_bazel_lib//lib/private:diff_test_toolchain.bzl |
| 98 | +
|
| 99 | +# Forward all the providers |
| 100 | +def _resolved_toolchain_impl(ctx): |
| 101 | + toolchain_info = ctx.toolchains["@aspect_bazel_lib//lib:diffutils_toolchain_type"] |
| 102 | + return [ |
| 103 | + toolchain_info, |
| 104 | + toolchain_info.default, |
| 105 | + toolchain_info.diffinfo, |
| 106 | + toolchain_info.template_variables, |
| 107 | + ] |
| 108 | +
|
| 109 | +# Copied from java_toolchain_alias |
| 110 | +# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl |
| 111 | +resolved_toolchain = rule( |
| 112 | + implementation = _resolved_toolchain_impl, |
| 113 | + toolchains = ["@aspect_bazel_lib//lib:diffutils_toolchain_type"], |
| 114 | + incompatible_use_toolchain_transition = True, |
| 115 | +) |
| 116 | +""" |
| 117 | + rctx.file("defs.bzl", starlark_content) |
| 118 | + |
| 119 | + build_content = """# @generated by @aspect_bazel_lib//lib/private:diff_test_toolchain.bzl |
| 120 | +# |
| 121 | +# These can be registered in the workspace file or passed to --extra_toolchains flag. |
| 122 | +# By default all these toolchains are registered by the diff_register_toolchains macro |
| 123 | +# so you don't normally need to interact with these targets. |
| 124 | +
|
| 125 | +load(":defs.bzl", "resolved_toolchain") |
| 126 | +
|
| 127 | +resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"]) |
| 128 | +
|
| 129 | +""" |
| 130 | + |
| 131 | + for [platform, meta] in DIFFUTILS_PLATFORMS.items(): |
| 132 | + build_content += """ |
| 133 | +toolchain( |
| 134 | + name = "{platform}_toolchain", |
| 135 | + exec_compatible_with = {compatible_with}, |
| 136 | + toolchain = "@{user_repository_name}_{platform}//:diff_toolchain", |
| 137 | + toolchain_type = "@aspect_bazel_lib//lib:diffutils_toolchain_type", |
| 138 | +) |
| 139 | +""".format( |
| 140 | + platform = platform, |
| 141 | + user_repository_name = rctx.attr.user_repository_name, |
| 142 | + compatible_with = meta.compatible_with, |
| 143 | + ) |
| 144 | + |
| 145 | + # Base BUILD file for this repository |
| 146 | + rctx.file("BUILD.bazel", build_content) |
| 147 | + |
| 148 | +diffutils_toolchains_repo = repository_rule( |
| 149 | + _diffutils_toolchains_repo_impl, |
| 150 | + doc = """Creates a repository with toolchain definitions for all known platforms |
| 151 | + which can be registered or selected.""", |
| 152 | + attrs = { |
| 153 | + "user_repository_name": attr.string(doc = "Base name for toolchains repository"), |
| 154 | + }, |
| 155 | +) |
| 156 | + |
| 157 | +def _diffutils_platform_repo_impl(rctx): |
| 158 | + is_windows = rctx.attr.platform.startswith("windows_") |
| 159 | + meta = DIFFUTILS_PLATFORMS[rctx.attr.platform] |
| 160 | + release_platform = meta.release_platform if hasattr(meta, "release_platform") else rctx.attr.platform |
| 161 | + archive_extension = meta.archive_extension if hasattr(meta, "archive_extension") else "tar.xz" |
| 162 | + |
| 163 | + url = "https://github.com/uutils/diffutils/releases/download/v{0}/diffutils-{1}.{2}".format( |
| 164 | + DIFFUTILS_VERSION, |
| 165 | + release_platform, |
| 166 | + archive_extension, |
| 167 | + ) |
| 168 | + |
| 169 | + rctx.download_and_extract( |
| 170 | + url = url, |
| 171 | + strip_prefix = "diffutils-{0}".format(release_platform), |
| 172 | + integrity = DIFFUTILS_INTEGRITIES[release_platform], |
| 173 | + ) |
| 174 | + build_content = """# @generated by @aspect_bazel_lib//lib/private:diff_test_toolchain.bzl |
| 175 | +load("@aspect_bazel_lib//lib/private:diff_test_toolchain.bzl", "diff_toolchain") |
| 176 | +exports_files(["{0}"]) |
| 177 | +diff_toolchain(name = "diff_toolchain", bin = "{0}", visibility = ["//visibility:public"]) |
| 178 | +""".format("diffutils.exe" if is_windows else "diffutils") |
| 179 | + |
| 180 | + # Base BUILD file for this repository |
| 181 | + rctx.file("BUILD.bazel", build_content) |
| 182 | + |
| 183 | +diffutils_platform_repo = repository_rule( |
| 184 | + implementation = _diffutils_platform_repo_impl, |
| 185 | + doc = "Fetch external tools needed for diff toolchain", |
| 186 | + attrs = { |
| 187 | + "platform": attr.string(mandatory = True, values = DIFFUTILS_PLATFORMS.keys()), |
| 188 | + }, |
| 189 | +) |
| 190 | + |
| 191 | +def _diffutils_host_alias_repo(rctx): |
| 192 | + ext = ".exe" if repo_utils.is_windows(rctx) else "" |
| 193 | + |
| 194 | + # Base BUILD file for this repository |
| 195 | + rctx.file("BUILD.bazel", """# @generated by @aspect_bazel_lib//lib/private:diff_test_toolchain.bzl |
| 196 | +package(default_visibility = ["//visibility:public"]) |
| 197 | +exports_files(["diff{ext}"]) |
| 198 | +""".format( |
| 199 | + ext = ext, |
| 200 | + )) |
| 201 | + |
| 202 | + rctx.symlink("../{name}_{platform}/diff{ext}".format( |
| 203 | + name = rctx.attr.name, |
| 204 | + platform = repo_utils.platform(rctx), |
| 205 | + ext = ext, |
| 206 | + ), "diff{ext}".format(ext = ext)) |
| 207 | + |
| 208 | +diffutils_host_alias_repo = repository_rule( |
| 209 | + _diffutils_host_alias_repo, |
| 210 | + doc = """Creates a repository with a shorter name meant for the host platform, which contains |
| 211 | + a BUILD.bazel file that exports symlinks to the host platform's binaries |
| 212 | + """, |
| 213 | +) |
0 commit comments