4

How can I conditionally include a file in an .rpm based on a define _foobar being set or not? The define _foobar would contain the absolute path inside the build root. The file is there.

According to the documentation, I expected the following to work (note: there are more files in this %files section, but this is the gist):

%files
%if %{_foobar}
%{_foobar}
%endif

which, however, gives me the error:

error: parse error in expression
error: /path/to/specfile:LINENO: parseExpressionBoolean returns -1

where /path/to/specfile:LINENO is the path to the .spec file and the line number of the line with the %if.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152

2 Answers2

6

Thanks to this blog post I found a version that works for me:

%files
%if %{?_foobar:1}%{!?_foobar:0}
%{_foobar}
%endif

What this does is to expand to %if 1 if the define is set and %if 0 otherwise.

If someone else has a better solution, please answer. I'll certainly prefer accepting someone else's answer over my own.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
  • Thanks for this solution. This really works. Although there are some nice things here : http://stackoverflow.com/questions/18701771/if-condition-inside-the-files-section-on-a-spec-file , but that didn't solve my problem – AnotherDeveloper Feb 04 '15 at 14:18
4

This is how I solved my problem

step 1 :

   In Build section .. somewhere I wrote :
 %build
  .....
  #check my condition here & if true define some macro
  %define is_valid %( if [ -f /usr/bin/myfile ]; then echo "1" ; else echo "0"; fi )
 #after his normal continuation
 .....
 ...

Step 2: in install section

  %install
  ......
  #do something in that condition
  if %is_valid
  install -m 0644 <file>
  %endif
  #rest all your stuff
  ................

Step 3:in files section

   %files
   if %is_valid 
   %{_dir}/<file>
   %endif

That's it

It works.

PS : I cannot give you full code hence giving all useful snippet

AnotherDeveloper
  • 2,161
  • 2
  • 23
  • 27