Is there any syntax or trick to be able to create a multiline rpm spec file macro
Asked Answered
S

6

13

Background.

$ rpmbuild --version
RPM version 4.3.3

I am working on a spec file that needs to process a list of files in multiple scriptlets. DRY (don't repeat youself) has me defining the list once as a macro which is expanded into the various helper scripts. Maintaining the list is a pain since I haven't seen a way to avoid putting all the files on the same line.

%define LIST \
a \
b 

gives an error

%define LIST a\
b\

gives an error as well

%define LIST a
%define LIST %LIST b

Fails due to a recursion error

Stifle answered 2/12, 2010 at 20:16 Comment(0)
B
5

You cannot really make multiline macros in a spec file the way you can in your ~/.rpmmacros file. Kind of silly.

I create a subdirectory SPECS/inc/ with spec files which act as multi-line macros which are used by all packages created for my site. Instead of using macros as %foo, you can just do %include inc/foo.spec. You can also create shortcuts to avoid the include syntax %global foo %include inc/foo.spec. Then it works just like a multi-line macro.

Here is a complete example.

inc/env.spec

%global foo %include inc/foo.spec
%global bar %include inc/bar.spec
#Lots of other common things to all packages

inc/foo.spec

a
b
c
d

And then in my package spec file, mylib.spec:

%include inc/env.spec

Name: mylib
Version: X.Y
#etc...

%foo
Bessiebessy answered 8/5, 2014 at 15:43 Comment(1)
This is really smart solution! Thanks!!Undaunted
A
3

Since you explained your goal a bit more, let's try different approach. Try this in your spec file:

Source1:        flist

%{expand:%global LIST %(cat %{SOURCE1})}

And this in Source1 file (in this case flist):

a \
b \
c 

I have tested this with rpm 4.4.6 since that's the oldest version available to me at this time.

Andra answered 28/4, 2012 at 19:16 Comment(1)
This does not work on CentOS4, which is end of life and has an ancient RPM version. Unfortunately that is what the OP and yours truly are trying to work with. Though by far, this is the closest one to a solution.Underestimate
A
3

In macro definitions, an end-of-line backslash is removed and used to tell RPM that the macro definition continues. The newline character is NOT stripped (unlike a make file). Therefore, if you are constructing a multi-line script, semi-colons are not needed. This also means that if you are splitting a single command, you must use 3 backslashes. Two to yield a backslash that the shell will see and one to tell rpmbuild to keep parsing the macro. The shell will remove the backslash it sees, plus the newline and that will yield one command on two lines. Whew !!

Asarum answered 14/5, 2012 at 21:56 Comment(1)
%define MAKE make \\\ name=TODO \\\ help \\\Rollo
R
2

This example should work:

cat file.spec

%define MAKE make \\\
 status=success \\\
 #EOL

%build
${MAKE}


cat Makefile

status?=fail
default: 
    echo "status=${status}"
Rollo answered 22/11, 2017 at 21:29 Comment(0)
U
1

You have to have at least some part of the value on the first line, i.e.:

%define LIST a\
b\
c

This macro definition will work. If you plan to use these as commands, i.e. something like

%define DOSOMETHING rm file1\
rm file2\
rm file3
...
%build
%DOSOMETHING

this will not work. The lines in executable sections are split into separate commands first, THEN the macros are expanded. I.e. defining the macro will work, executing it as three separate commands will not.

If you want to execute 3 separate commands, it's easier to do it like

%define DOSOMETHING rm file1; rm file2; rm file3
Uird answered 4/12, 2010 at 3:4 Comment(1)
The above solution doesn't work for me, possible might work for other versions of rpmbuild. I get an error: error: line 31: Unknown tag: b\ Edited the question to reflect this.Stifle
A
1

If I was you, I'd maintain the list in a source file.

Contents of %{_sourcedir}/LIST:

a
b
c
d

In the spec file:

%define process_files() \
    for i in %(cat %{_sourcedir}/LIST)
    do \
        echo "process list of files here" \
    done
Abolition answered 15/10, 2013 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.