0

I am using a spec file to create an RPM package. I want to package only those files which are present on my system. In my %files section, I am writing the files which I want to include in my package. The conditional and non conditional packages are included in the following manner.

%files
    %if "%is_file1_present"
    %attr (-, root, root) /location/to/file1
    %attr (-, root, root) /location/to/file2
%endif
%attr (-, root, root) /location/to/file3
%attr (-, root, root) /location/to/file4

%is_file1_present is defined in the %build section like this.

%build
%define is_file1_present %( if [ -f /location/to/file1 ]; then echo "1" ; else echo "0"; fi )`

While trying to build RPM package, it seems to ignore the if condition. What am I doing wrong ?

Shounak
  • 136
  • 1
  • 12
  • 1
    There are a few possible ways to do this. See [If condition inside the %Files section on a SPEC file](https://stackoverflow.com/q/18701771/4154375) and [Conditionally include file in an RPM](https://stackoverflow.com/q/18775680/4154375). – pjh Oct 02 '18 at 19:09

1 Answers1

0

There is a easier solution:

In your install section; only copy the files that are present; and use wildcards in the files section:

%install
install -d -m 0755 "${RPM_BUILD_ROOT}/location/to"
cp file* ${RPM_BUILD_ROOT}/location/to/

%files
%defattr(-,root,root)
/location/to/file*

NOTE: this works, but that means that your spec file will not always create the same package, which isn't very nice...

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • I tried this method and it says "File not found by glob". I was just wondering why doesn't if else statement work in %files section – Shounak Oct 02 '18 at 13:25
  • try with the edits to my answer. You can debug a little; this is supposed to work; I use it very often. You can read the output of the rpmbuild command to see what goes wrong. – Chris Maes Oct 02 '18 at 13:27
  • After making a few adjustments, your solution worked. Thanks !! – Shounak Oct 03 '18 at 06:31