Add tar rule

This commit is contained in:
Stuart Axelbrooke 2025-04-21 21:51:43 -07:00
parent 9e5782304e
commit 3fd337a155
No known key found for this signature in database
GPG key ID: 1B0A848C29D46A35

View file

@ -255,6 +255,11 @@ def databuild_graph(name, jobs, lookup, visibility = None):
exec = "%s.exec" % name,
visibility = visibility,
)
_databuild_graph_tar(
name = "%s.tar" % name,
build = "%s.build" % name,
visibility = visibility,
)
# TODO there feels like a lot of boilerplate around wrapping a target with a script - can this be simplified?
@ -505,6 +510,66 @@ _databuild_graph_build = rule(
executable = True,
)
def _databuild_graph_tar_impl(ctx):
"""Creates a tar archive of the build target and all its runfiles."""
# Get the build target
build_target = ctx.attr.build
# Create a tar file containing the build target executable and all its runfiles
tar_file = ctx.actions.declare_file(ctx.label.name)
# Get all runfiles from the build target
runfiles_list = []
# Add the build target executable
runfiles_list.append(build_target.files_to_run.executable)
# Add all runfiles from the build target
if hasattr(build_target, "default_runfiles"):
runfiles_list.extend(build_target.default_runfiles.files.to_list())
# Create the tar file
args = ctx.actions.args()
args.add("--output", tar_file.path)
args.add("--directory", "/")
for f in runfiles_list:
# Use the short path to preserve the directory structure
dest = f.short_path
args.add("--file", "%s=%s" % (f.path, dest))
ctx.actions.run(
inputs = runfiles_list,
executable = ctx.executable._build_tar,
arguments = [args],
outputs = [tar_file],
mnemonic = "DataBuildGraphTar",
use_default_shell_env = True,
)
return [
DefaultInfo(
files = depset([tar_file]),
),
]
_databuild_graph_tar = rule(
implementation = _databuild_graph_tar_impl,
attrs = {
"build": attr.label(
doc = "The build target to tar up",
mandatory = True,
executable = True,
cfg = "target",
),
"_build_tar": attr.label(
default = Label("@bazel_tools//tools/build_defs/pkg:build_tar"),
executable = True,
cfg = "exec",
),
},
)
#def _graph_impl(name):
# """
#