Skip to content

Commit

Permalink
Allow substitution of user-defined variables in RPM preamble (#787)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
kellyma2 authored Jan 2, 2024
1 parent 994a1f5 commit c3a1ffb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 11 additions & 2 deletions pkg/private/util.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,14 @@ def substitute_package_variables(ctx, attribute_value):

# 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)
4 changes: 2 additions & 2 deletions pkg/rpm_pfg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ load(
"PackageSymlinkInfo",
"PackageVariablesInfo",
)
load("//pkg/private:util.bzl", "setup_output_files")
load("//pkg/private:util.bzl", "setup_output_files", "substitute_package_variables")

rpm_filetype = [".rpm"]

Expand Down Expand Up @@ -349,7 +349,7 @@ def _pkg_rpm_impl(ctx):
)
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)
Expand Down

0 comments on commit c3a1ffb

Please sign in to comment.