Support relocatable rpmbuild by adding data attribute to the toolchain (#1047)

* Support relocatable rpmbuild by adding data attribute to the toolchain

rpmbuild ships with runtime configuration files (lib/rpm/) that must be
present for it to successfully build RPMs. When running under remote
execution, these files were not being staged because the toolchain had
no way to declare them as dependencies.

Add a `data` attribute to `rpmbuild_toolchain` so that runtime files
can be specified alongside the rpmbuild binary. These files are
propagated through `RpmbuildInfo` and included as inputs to the MakeRpm
action in `pkg_rpm`.
diff --git a/examples/rpm/prebuilt_rpmbuild/local/BUILD b/examples/rpm/prebuilt_rpmbuild/local/BUILD
index 7c273bf..cfb147d 100644
--- a/examples/rpm/prebuilt_rpmbuild/local/BUILD
+++ b/examples/rpm/prebuilt_rpmbuild/local/BUILD
@@ -16,6 +16,7 @@
 
 rpmbuild_toolchain(
     name = "prebuilt_rpmbuild",
+    data = glob(["lib/rpm/**"]),
     # You could also point to a target that builds rpmbuild from source.
     label = ":rpmbuild_binary",
 )
diff --git a/examples/rpm/prebuilt_rpmbuild/local/lib/rpm/macros b/examples/rpm/prebuilt_rpmbuild/local/lib/rpm/macros
new file mode 100644
index 0000000..7f3e5be
--- /dev/null
+++ b/examples/rpm/prebuilt_rpmbuild/local/lib/rpm/macros
@@ -0,0 +1 @@
+# Replace me with real rpmbuild macros
diff --git a/examples/rpm/prebuilt_rpmbuild/local/lib/rpm/platform b/examples/rpm/prebuilt_rpmbuild/local/lib/rpm/platform
new file mode 100644
index 0000000..c2a8fee
--- /dev/null
+++ b/examples/rpm/prebuilt_rpmbuild/local/lib/rpm/platform
@@ -0,0 +1 @@
+# Replace me with real rpmbuild platform
diff --git a/examples/rpm/prebuilt_rpmbuild/local/lib/rpm/rpmrc b/examples/rpm/prebuilt_rpmbuild/local/lib/rpm/rpmrc
new file mode 100644
index 0000000..95b04ee
--- /dev/null
+++ b/examples/rpm/prebuilt_rpmbuild/local/lib/rpm/rpmrc
@@ -0,0 +1 @@
+# Replace me with real rpmbuild rpmrc
diff --git a/pkg/rpm_pfg.bzl b/pkg/rpm_pfg.bzl
index 941c4ed..205a620 100644
--- a/pkg/rpm_pfg.bzl
+++ b/pkg/rpm_pfg.bzl
@@ -475,6 +475,7 @@
 
     files = []
     tools = []
+    toolchain_data = []
     debuginfo_type = DEBUGINFO_TYPE_NONE
     name = ctx.attr.package_name if ctx.attr.package_name else ctx.label.name
     rpm_ctx.make_rpm_args.append("--name=" + name)
@@ -500,6 +501,8 @@
             tools.append(executable_files)
             rpm_ctx.make_rpm_args.append("--rpmbuild=%s" % executable_files.executable.path)
 
+        toolchain_data = toolchain.data
+
         if ctx.attr.debuginfo:
             debuginfo_type = toolchain.debuginfo_type
             rpm_ctx.make_rpm_args.append("--debuginfo_type=%s" % debuginfo_type)
@@ -885,7 +888,7 @@
         executable = ctx.executable._make_rpm,
         use_default_shell_env = True,
         arguments = rpm_ctx.make_rpm_args,
-        inputs = files + (ctx.files.data or []),
+        inputs = files + (ctx.files.data or []) + toolchain_data,
         outputs = rpm_ctx.output_rpm_files,
         env = {
             "LANG": "en_US.UTF-8",
diff --git a/tests/rpm/toolchain_tests.bzl b/tests/rpm/toolchain_tests.bzl
index 9fffa8e..d9c237e 100644
--- a/tests/rpm/toolchain_tests.bzl
+++ b/tests/rpm/toolchain_tests.bzl
@@ -53,6 +53,11 @@
         ctx.attr.expect_path,
         info.path,
     )
+    asserts.equals(
+        env,
+        ctx.attr.expect_data_count,
+        len(info.data),
+    )
     return analysistest.end(env)
 
 toolchain_contents_test = analysistest.make(
@@ -65,6 +70,7 @@
             allow_files = True,
         ),
         "expect_path": attr.string(),
+        "expect_data_count": attr.int(default = 0),
     },
 )
 
@@ -119,6 +125,21 @@
         expect_path = "/usr/bin/foo",
     )
 
+    rpmbuild_toolchain(
+        name = "tc_with_data",
+        path = "/usr/bin/foo",
+        data = [":toolchain_test.bzl"],
+        tags = ["manual"],
+    )
+    toolchain_contents_test(
+        name = "tc_with_data_test",
+        target_under_test = ":tc_with_data",
+        expect_valid = True,
+        expect_label = None,
+        expect_path = "/usr/bin/foo",
+        expect_data_count = 1,
+    )
+
 # buildifier: disable=unnamed-macro
 def create_toolchain_analysis_tests():
     _create_toolchain_creation_tests()
diff --git a/toolchains/rpm/rpmbuild.bzl b/toolchains/rpm/rpmbuild.bzl
index ebc1016..f07f819 100644
--- a/toolchains/rpm/rpmbuild.bzl
+++ b/toolchains/rpm/rpmbuild.bzl
@@ -22,6 +22,7 @@
         "path": "The path to a pre-built rpmbuild",
         "version": "The version string of rpmbuild",
         "debuginfo_type": "The variant of the underlying debuginfo config",
+        "data": "Additional files needed at runtime by rpmbuild",
     },
 )
 
@@ -37,6 +38,7 @@
             path = ctx.attr.path,
             version = ctx.attr.version,
             debuginfo_type = ctx.attr.debuginfo_type,
+            data = ctx.files.data,
         ),
     )
     return [toolchain_info]
@@ -64,6 +66,10 @@
             """,
             default = "none",
         ),
+        "data": attr.label_list(
+            doc = "Additional files needed at runtime by rpmbuild.",
+            allow_files = True,
+        ),
     },
 )