From 3fd337a155a94d82f0a78ee14d3b706368fa272f Mon Sep 17 00:00:00 2001 From: Stuart Axelbrooke Date: Mon, 21 Apr 2025 21:51:43 -0700 Subject: [PATCH] Add tar rule --- rules.bzl | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/rules.bzl b/rules.bzl index eaba414..79c1957 100644 --- a/rules.bzl +++ b/rules.bzl @@ -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): # """ #