Skip to content

Commit

Permalink
Implement prepend and append modes for all our normal build scriptlets
Browse files Browse the repository at this point in the history
Allows %build, %install, %check, %clean and %generate_depends to be
augmented arbitrary number of times by appending or prepending to them.
The main use-case is to support automatic population of these sections
(declarative builds) while still allowing packagers to easily tweak them,
but could also be useful for complicated conditional specs and such.

%prep is missing from the list because it's way too special at the
moment, it'll gain this capability when rpm-software-management#2205 is fixed.

Related: rpm-software-management#1087
  • Loading branch information
pmatilai committed Oct 20, 2023
1 parent 3b07050 commit 39973be
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
59 changes: 53 additions & 6 deletions build/parseSimpleScript.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,64 @@
int parseSimpleScript(rpmSpec spec, const char * name, StringBuf *sbp)
{
int res = PART_ERROR;

if (*sbp != NULL) {
poptContext optCon = NULL;
int argc;
const char **argv = NULL;
StringBuf *target = sbp;
StringBuf buf = NULL;
int rc, append = 0, prepend = 0;
struct poptOption optionsTable[] = {
{ NULL, 'a', POPT_ARG_NONE, &append, 'a', NULL, NULL },
{ NULL, 'p', POPT_ARG_NONE, &prepend, 'p', NULL, NULL },
{ NULL, 0, 0, NULL, 0, NULL, NULL }
};


if ((rc = poptParseArgvString(spec->line, &argc, &argv))) {
rpmlog(RPMLOG_ERR, _("line %d: Error parsing script: %s: %s\n"),
spec->lineNum, spec->line, poptStrerror(rc));
goto exit;
}

optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
while ((rc = poptGetNextOpt(optCon)) > 0) {};
if (rc < -1) {
rpmlog(RPMLOG_ERR, _("line %d: Bad %s option %s: %s\n"),
spec->lineNum, argv[0],
poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
poptStrerror(rc));
goto exit;
}

if (*sbp != NULL && append == 0 && prepend == 0) {
rpmlog(RPMLOG_ERR, _("line %d: second %s\n"),
spec->lineNum, name);
goto exit;
}

/* There are no options to %build, %install, %check, or %clean */
res = parseLines(spec, STRIP_NOTHING, NULL, sbp);


if (append && prepend) {
rpmlog(RPMLOG_ERR, _("line %d: append and prepend specified: %s\n"),
spec->lineNum, spec->line);
goto exit;
}

/* Prepend is only special if the section already exists */
if (prepend && *sbp) {
buf = newStringBuf();
target = &buf;
}

res = parseLines(spec, STRIP_NOTHING, NULL, target);

if (buf) {
appendStringBuf(buf, getStringBuf(*sbp));
freeStringBuf(*sbp);
*sbp = buf;
}

exit:
free(argv);
poptFreeContext(optCon);

return res;
}
7 changes: 7 additions & 0 deletions docs/manual/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@ when name is omitted, the description refers to the main package.
Package build is divided into multiple separate steps, each executed
in a separate shell.

Only one of each section can be present in a spec, but all build scriptlets
except for `%prep` accept options `-a` and `-p`, for append and prepend mode.
Append and prepend append or prepend lines to the section in the order they
appear in the spec. Both append and prepend can be used multiple times and
without other restrictions, but a section without either mode can only
appear first (eg `%build` cannot follow `%build -p`).

### %prep

%prep prepares the sources for building. This is where sources are
Expand Down
33 changes: 33 additions & 0 deletions tests/data/SPECS/append.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Name: append
Version: 1.0
Release: 1
BuildArch: noarch
Summary: Testing scriptlet append/prepend
License: GPL

%description
%{summary}

%build
echo BBB >> out

%build -a
echo CCC >> out

%build -p
echo AAA >> out

%build -a
echo DDD >> out

%build -p
echo 000 >> out

%install -a
cp out ${RPM_BUILD_ROOT}/opt/

%install -p
mkdir -p ${RPM_BUILD_ROOT}/opt/

%files
/opt/out
18 changes: 18 additions & 0 deletions tests/rpmbuild.at
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ runroot rpmbuild -bs /data/SPECS/specstep.spec 2>&1|grep ^Executing|cut -d: -f1
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild script prepend/append])
AT_KEYWORDS([build])
RPMDB_INIT
RPMTEST_CHECK([
runroot rpmbuild -bb --quiet ${RPMDATA}/SPECS/append.spec
runroot_other rpm2cpio /build/RPMS/noarch/append-1.0-1.noarch.rpm | cpio -id 2> /dev/null
cat opt/out
],
[0],
[000
AAA
BBB
CCC
DDD
],
[])
RPMTEST_CLEANUP

AT_SETUP([rpmbuild -ba autosetup])
AT_KEYWORDS([build])
RPMDB_INIT
Expand Down

0 comments on commit 39973be

Please sign in to comment.