Skip to content

Commit

Permalink
Merge pull request #4 from luna-duclos/ld/more-generic-golink-rule
Browse files Browse the repository at this point in the history
Implement a second more generic golink rule that can copy output from arbitrary rules
  • Loading branch information
nikunjy authored Oct 15, 2020
2 parents 2a0301f + 928b139 commit f3a9fee
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
bazel-*

# IntellIJ project file
golink.iml
4 changes: 4 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports_files(
["copy_into_workspace.sh"],
visibility = ["//visibility:public"],
)
File renamed without changes.
53 changes: 53 additions & 0 deletions golink.bzl
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 0 additions & 5 deletions proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@ filegroup(
srcs = glob(["*"]),
visibility = ["//visibility:public"],
)

exports_files(
["copy_into_workspace.sh"],
visibility = ["//visibility:public"],
)
30 changes: 5 additions & 25 deletions proto/proto.bzl
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -45,4 +25,4 @@ def go_proto_link(name, **kwargs):
dir = native.package_name()
kwargs["dir"] = dir

_go_proto_link(name = name, **kwargs)
_go_proto_link(name = name, **kwargs)

0 comments on commit f3a9fee

Please sign in to comment.