Created
March 23, 2018 15:47
-
-
Save mmorearty/486c496b22b3ec8dffcc17f8ad7101fa to your computer and use it in GitHub Desktop.
Bazel stamp for a custom rule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load(":myrules.bzl", "mybundle") | |
mybundle( | |
name = "main", | |
srcs = ["in.txt"], | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(this is the "srcs" file for our rule) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _mybundle_impl(ctx): | |
ctx.actions.run_shell( | |
inputs = ctx.files.srcs + [ctx.file.volatile_status], | |
outputs = [ctx.outputs.main], | |
command = "cat <(echo '=== the srcs contain:') {srcs} <(echo '=== and volatile-status.txt is:') {volatile} > {out}".format( | |
srcs = " ".join([src.path for src in ctx.files.srcs]), | |
volatile = ctx.file.volatile_status.path, | |
out = ctx.outputs.main.path | |
), | |
) | |
_mybundle = rule( | |
implementation = _mybundle_impl, | |
attrs = { | |
"srcs": attr.label_list(allow_files = True), | |
"volatile_status": attr.label(single_file = True, allow_files = True), | |
}, | |
outputs = { | |
"main": "%{name}.txt", | |
} | |
) | |
def mybundle(name, srcs): | |
# first, copy bazel-out/volatile-status.txt to a regular output file | |
volatile_name = name + "_volatile" | |
native.genrule( | |
name = volatile_name, | |
srcs = srcs, # important: it depends on all the inputs | |
stamp = 1, | |
outs = [name + "-volatile-status.txt"], | |
cmd = "cp bazel-out/volatile-status.txt $@", | |
) | |
# now run the interesting code, passing it a pointer to the copied volatile-status | |
_mybundle( | |
name = name, | |
srcs = srcs, | |
volatile_status = volatile_name | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment