From 928b1394f0ac82aa5bcb14e914bb94d17634b974 Mon Sep 17 00:00:00 2001 From: Luna Duclos Date: Thu, 15 Oct 2020 13:21:22 +0200 Subject: [PATCH] Implement a second more generic golink rule that can copy output from arbitrary rules --- .gitignore | 3 ++ BUILD | 4 ++ ...nto_workspace.sh => copy_into_workspace.sh | 0 golink.bzl | 53 +++++++++++++++++++ proto/BUILD | 5 -- proto/proto.bzl | 30 ++--------- 6 files changed, 65 insertions(+), 30 deletions(-) rename proto/copy_into_workspace.sh => copy_into_workspace.sh (100%) create mode 100644 golink.bzl diff --git a/.gitignore b/.gitignore index ac51a05..236f395 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ bazel-* + +# IntellIJ project file +golink.iml diff --git a/BUILD b/BUILD index e69de29..09d33e8 100644 --- a/BUILD +++ b/BUILD @@ -0,0 +1,4 @@ +exports_files( + ["copy_into_workspace.sh"], + visibility = ["//visibility:public"], +) diff --git a/proto/copy_into_workspace.sh b/copy_into_workspace.sh similarity index 100% rename from proto/copy_into_workspace.sh rename to copy_into_workspace.sh diff --git a/golink.bzl b/golink.bzl new file mode 100644 index 0000000..f1fc747 --- /dev/null +++ b/golink.bzl @@ -0,0 +1,53 @@ +load("@bazel_skylib//lib:shell.bzl", "shell") + +def gen_copy_files_script(ctx, files): + content = "" + for f in files: + print(files) + line = "cp -f %s %s/;\n" % (f.path, ctx.attr.dir) + content += line + substitutions = { + "@@CONTENT@@": shell.quote(content), + } + out = ctx.actions.declare_file(ctx.label.name + ".sh") + ctx.actions.expand_template( + template = ctx.file._template, + output = out, + substitutions = substitutions, + is_executable = True, + ) + runfiles = ctx.runfiles(files = [files[0]]) + return [ + DefaultInfo( + files = depset([out]), + runfiles = runfiles, + executable = out, + ), + ] + +def golink_impl(ctx, **kwargs): + print("Copying output files for rule %s" % ctx.attr.dep) + return gen_copy_files_script(ctx, ctx.files.dep) + + +_golink = rule( + implementation = golink_impl, + executable = True, + attrs = { + "dir": attr.string(), + "dep": attr.label(), + "_template": attr.label( + default = ":copy_into_workspace.sh", + allow_single_file = True, + ), + # It is not used, just used for versioning since this is experimental + "version": attr.string(), + }, +) + +def golink(name, **kwargs): + if not "dir" in kwargs: + dir = native.package_name() + kwargs["dir"] = dir + + _golink(name = name, **kwargs) diff --git a/proto/BUILD b/proto/BUILD index a979506..e8cb573 100644 --- a/proto/BUILD +++ b/proto/BUILD @@ -3,8 +3,3 @@ filegroup( srcs = glob(["*"]), visibility = ["//visibility:public"], ) - -exports_files( - ["copy_into_workspace.sh"], - visibility = ["//visibility:public"], -) diff --git a/proto/proto.bzl b/proto/proto.bzl index c12a22f..54a4a53 100644 --- a/proto/proto.bzl +++ b/proto/proto.bzl @@ -1,29 +1,9 @@ load("@bazel_skylib//lib:shell.bzl", "shell") +load("//:golink.bzl", "gen_copy_files_script") + def go_proto_link_impl(ctx, **kwargs): print("Copying generated files for proto library %s" % ctx.attr.dep) - generated = ctx.attr.dep[OutputGroupInfo].go_generated_srcs.to_list() - content = "" - for f in generated: - line = "cp -f %s %s/;\n" % (f.path, ctx.attr.dir) - content += line - substitutions = { - "@@CONTENT@@": shell.quote(content), - } - out = ctx.actions.declare_file(ctx.label.name + ".sh") - ctx.actions.expand_template( - template = ctx.file._template, - output = out, - substitutions = substitutions, - is_executable = True, - ) - runfiles = ctx.runfiles(files = [generated[0]]) - return [ - DefaultInfo( - files = depset([out]), - runfiles = runfiles, - executable = out, - ), - ] + return gen_copy_files_script(ctx, ctx.attr.dep[OutputGroupInfo].go_generated_srcs.to_list()) _go_proto_link = rule( implementation = go_proto_link_impl, @@ -32,7 +12,7 @@ _go_proto_link = rule( "dir": attr.string(), "dep": attr.label(), "_template": attr.label( - default = ":copy_into_workspace.sh", + default = "//:copy_into_workspace.sh", allow_single_file = True, ), # It is not used, just used for versioning since this is experimental @@ -45,4 +25,4 @@ def go_proto_link(name, **kwargs): dir = native.package_name() kwargs["dir"] = dir - _go_proto_link(name = name, **kwargs) \ No newline at end of file + _go_proto_link(name = name, **kwargs)