RPM has a great little facility that is seriously under-documented. It allows one to issue commands such as:

rpmbuild -ba --with foo bar.spec

This sets a variable called _with_foo within the build process. It's possible to use this facility to create single .spec files that will work across a variety of distributions, making it possible to do (for example)

rpmbuild -ba --with suse bar.spec

(and also mandriva, fedora, etc etc etc)

Once that value is set, you can utilise it within the .spec to set appropriate prefix/sysconfdir etc values, as well as Requires, BuildRequires etc for where distros name their packages differently.

What follows is an example from a mock-up .spec I did for a generic GNOME application allowing SuSE to be specified with --with

It assumes that on SuSE the package is called gfoo, whereas on other distros it is called gnome-foo, and sets the prefix and sysconfdir. (Note that other values such as mandir, libdir can also be set, but normally these are set automatically as subdirectories of prefix)

#
# spec file for package gfoo (Version 0.0.1)
#
# Copyright (c) 2005 James Ogley
# This file and all modifications and additions to the pristine
# package are under the same license as the package itself.
#
# Please submit bugfixes or comments to feedback@usr-local-bin.org
#

%{?_with_suse:%define suse 1}
%{!?_with_suse:%define suse 0}
%if %{suse}
%define name        gfoo
%define prefix      /opt/gnome
%define sysconfdir  /etc%{prefix}
%else
%define name        gnome-foo
%define prefix      %{_prefix}
%define sysconfdir  %{_sysconfdir}
%endif
Name:         %{name}
License:      X11/MIT
Group:        Applications/Internet
Autoreqprov:  on
Summary:      A fake internet related application
Version:      0.0.1
Release:      0
Source:       gfoo-%{version}.tar.bz2
PreReq:       /usr/bin/install

%description
Blah blah blah...

[snip]

%prep
%setup -q -n gfoo-%{version}

%build
autoreconf --force --install
CFLAGS="$RPM_OPT_FLAGS -fno-strict-aliasing" \
  ./configure --prefix=%prefix \
  --sysconfdir=%sysconfdir
make

[snip]

%fioles
%defattr(-, root, root)
%doc NEWS README ChangeLog COPYING TODO
%{prefix}/bin/gfoo
%dir %{prefix}/share/gfoo
%{prefix}/share/gfoo/*.txt

%changelog -n %{name}
* Sun Jun 12 2005 - james@usr-local-bin.org
- Create example .spec for live.gnome.org

Update: There was some discussion on the openSUSE build service list recently about this (the build service will in theory be able to build across distros, with some limitations).

It could be possible to have something like this:

%define is_suse %(test -e /etc/SuSE-release && echo 1 || echo 0)
%define is_fedora %(test -e /etc/fedora-release && echo 1 || echo 0)

in a .spec file.

You can then even check for the version of the different distro

%define fedora_release %(rpm -q --queryformat '%{VERSION}' fedora-release)
%define fedora_version %(echo "%fedora_release" | tr -d '.')

SUSE already provides a suse_version macro, perhaps other distros too.

Note from AaronBockover: In the openSUSE build service, it appears that suse_version (as expected), fedora_version, and mandriva_version are all set appropriately and need not be defined manually as above.

%if 0%{?suse_version} > 0
BuildRequires: suse-specific-devel
%endif
%if 0%{?fedora_version} > 0
BuildRequires: fedora-specific-devel
%endif
%if 0%{?mandriva_version} > 0
BuildRequires: mandriva-specific-devel
%endif

Attic/CrossDistroSpecFiles (last edited 2013-11-22 22:23:19 by WilliamJonMcCann)