[users] spec file: conditional file inclusion

Dries Verachtert dries at ulyssis.org
Fri Mar 21 10:43:41 CET 2008


On Friday 21 March 2008 06:50:28 am Donald Raikes wrote:
> Hello,
> I have a package which will build a variable set of files depending on
> whether any of a combination of other packages are installed.
>
> For example, if package1 is installed when my package is installed, then
> package1_driver is compiled and installed.
>
> None of the other packages is required for successful compilation of my
> package.
>
> My problem occurs when I try to setup my %file section.
>
> If package1 is present when I build the package, then package1_driver needs
> to be included in the %file list, however if package1 is not installed then
> package1_driver is not compiled, and if it is in the %file list, my package
> doesn't build properly.
>
> Any suggestions would be appreciated.

You can add or remove buildrequirements and/or files in the %files section 
depending on variables defined by the user: you can add arguments 
like  --define 'with-something 1' for example. The following spec file uses 
this a lot :
http://svn.rpmforge.net/svn/trunk/rpms/mplayer/mplayer.spec
The %{!?_with_xxxxx} and %{!?_without_xxxx} macros can also be used in the 
files section.
You can check if something is installed with tricks like:
# from distcc.spec:
%define gccversion %(rpm -q gcc --qf '%{RPMTAG_VERSION}' | tail -1)
# from fail2ban.spec:
%define python_version %(%{__python} -c 'import sys; print 
sys.version.split(" ")[0]')
# from kernel-module-ibmasm.spec:
%{expand: %define 
_with_smp %(test -f /usr/src/linux-%{kernel}/configs/kernel-%{kversion}-%{_target_cpu}-smp.config 
&& echo 1 || echo 0)}

Everything is possible with some macro's but imho you should try to avoid 
using this. Always try to keep your spec file as simple as possible. In your 
case: i would suggest to make the spec file with a buildreq on package1 and 
always build it in a chroot. The spec file can contain a subpackage 
mypackage-package1 which contains the files which are only useful when 
package1 is installed and only this subpackage has a 'requires: package1'.

For example:
http://svn.rpmforge.net/svn/trunk/rpms/rrdtool/rrdtool.spec
=> perl is a buildrequirement but perl bindings are stored in a separate 
subpackage for people who succeed in installing their distro without perl..
http://svn.rpmforge.net/svn/trunk/rpms/mrtg/mrtg.spec
=> mrtg is built multiple times with different options and each binary is 
stored in another subpackage 

kind regards,
Dries






More information about the users mailing list