Search code examples
rpmrpmbuildrpm-spec

How to create a conditional package using RPM packaging


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 ?


Solution

  • 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...