Allow substitution of user-defined variables in RPM preamble (#787)
* Allow substitution of user-defined variables in RPM preamble
It's desirable to be able to parameterize some variables in the
preamble such as architecture when RPM packages. This change enables
variable substitution in the preamble section so that the values may
be injected in this fashion in lieu of only using statically defined
values.
* Deal with mismatched variable definitions
Currently we don't handle things like $(foo or (bar) correctly.
Lacking regex matching, we can compensate for this somewhat by
attempting to find matching pairs of $( and ) and failing if we see
the start of a variable declaration but not its termination.
diff --git a/pkg/private/util.bzl b/pkg/private/util.bzl
index 7d36cf8..7225456 100644
--- a/pkg/private/util.bzl
+++ b/pkg/private/util.bzl
@@ -82,5 +82,14 @@
# Map $(var) to {x} and then use format for substitution.
# This is brittle and I hate it. We should have template substitution
- # in the Starlark runtime.
- return attribute_value.replace("$(", "{").replace(")", "}").format(**vars)
+ # in the Starlark runtime. This loop compensates for mismatched counts
+ # of $(foo) so that we don't try replace things like (bar) because we
+ # have no regex matching
+ for _ in range(attribute_value.count("$(")):
+ if attribute_value.find(")") == -1:
+ fail("mismatched variable declaration")
+
+ attribute_value = attribute_value.replace("$(", "{", 1)
+ attribute_value = attribute_value.replace(")", "}", 1)
+
+ return attribute_value.format(**vars)
\ No newline at end of file
diff --git a/pkg/rpm_pfg.bzl b/pkg/rpm_pfg.bzl
index 90fc800..0f2dd75 100644
--- a/pkg/rpm_pfg.bzl
+++ b/pkg/rpm_pfg.bzl
@@ -33,7 +33,7 @@
"PackageSymlinkInfo",
"PackageVariablesInfo",
)
-load("//pkg/private:util.bzl", "setup_output_files")
+load("//pkg/private:util.bzl", "setup_output_files", "substitute_package_variables")
rpm_filetype = [".rpm"]
@@ -349,7 +349,7 @@
)
ctx.actions.write(
output = preamble_file,
- content = "\n".join(preamble_pieces),
+ content = substitute_package_variables(ctx, "\n".join(preamble_pieces)),
)
files.append(preamble_file)
args.append("--preamble=" + preamble_file.path)