From 72baa6099ce1920c103dcb3a32cf77d8f1a73d08 Mon Sep 17 00:00:00 2001 From: Dillon Lees Date: Mon, 23 Jan 2023 13:47:56 -0500 Subject: [PATCH] Generate sha256 message digest for built binaries --- BUILD | 31 +++++++++++++++++++++++++++++++ def.bzl | 27 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 def.bzl diff --git a/BUILD b/BUILD index 9d98259d..72064f6d 100644 --- a/BUILD +++ b/BUILD @@ -1,6 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") load("@bazel_gazelle//:def.bzl", "gazelle") load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm") +load("//:def.bzl", "sha256sum") # gazelle:prefix github.com/bazelbuild/bazelisk gazelle(name = "gazelle") @@ -85,6 +86,11 @@ go_binary( visibility = ["//visibility:public"], ) +sha256sum( + name = "bazelisk-darwin-amd64-sha", + src = ":bazelisk-darwin-amd64", +) + go_binary( name = "bazelisk-darwin-arm64", out = "bazelisk-darwin_arm64", @@ -99,6 +105,11 @@ go_binary( visibility = ["//visibility:public"], ) +sha256sum( + name = "bazelisk-darwin-arm64-sha", + src = ":bazelisk-darwin-arm64", +) + genrule( name = "bazelisk-darwin-universal", srcs = [ @@ -113,6 +124,11 @@ genrule( ], ) +sha256sum( + name = "bazelisk-darwin-universal-sha", + src = ":bazelisk-darwin-universal", +) + go_binary( name = "bazelisk-linux-amd64", out = "bazelisk-linux_amd64", @@ -127,6 +143,11 @@ go_binary( visibility = ["//visibility:public"], ) +sha256sum( + name = "bazelisk-linux-amd64-sha", + src = ":bazelisk-linux-amd64", +) + go_binary( name = "bazelisk-linux-arm64", out = "bazelisk-linux_arm64", @@ -141,6 +162,11 @@ go_binary( visibility = ["//visibility:public"], ) +sha256sum( + name = "bazelisk-linux-arm64-sha", + src = ":bazelisk-linux-arm64", +) + go_binary( name = "bazelisk-windows-amd64", out = "bazelisk-windows_amd64.exe", @@ -151,6 +177,11 @@ go_binary( visibility = ["//visibility:public"], ) +sha256sum( + name = "bazelisk-windows-amd64-sha", + src = ":bazelisk-windows-amd64", +) + pkg_npm( name = "npm_package", package_name = "@bazel/bazelisk", diff --git a/def.bzl b/def.bzl new file mode 100644 index 00000000..8bcf3afb --- /dev/null +++ b/def.bzl @@ -0,0 +1,27 @@ +def _sha256sum_impl(ctx): + ctx.actions.run_shell( + outputs = [ctx.outputs.sha256], + inputs = [ctx.file.src], + command = "CWD=$PWD && cd `dirname {0}` && sha256sum `basename {0}` > $CWD/{1}".format(ctx.file.src.path, ctx.outputs.sha256.path), + ) + + return DefaultInfo(files = depset([ctx.outputs.sha256])) + +_sha256sum = rule( + implementation = _sha256sum_impl, + attrs = { + "src": attr.label(mandatory = True, allow_single_file = True), + "sha256": attr.output(), + } +) + +def sha256sum(name, src, **kwargs): + """ + Macro to create a sha256 message digest file + """ + _sha256sum( + name = name, + src = src, + sha256 = "%s.sha256" % src, + **kwargs, + )