Start the spec file with the following %define statements at the top.
%define name TEMPLATE
%define version TEMPLATE
%define buildnumber TEMPLATE
%define arch TEMPLATE
Continue the spec file as you would normally, using the variables you just defined in place of the real name, version, build, etc.
Summary: %{name} is the best application on the planet.
Name: %{name}
Version: %{version}
Release: %{buildnumber}
License: GPL
Group: Java
Source: %{name}-%{version}-%{release}.tgz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-ROOT%description
The best application on the planet.%prep
%setup -n %{name}-%{version}-%{release}%build
make%install
make install%clean
if [ -d $RPM_BUILD_DIR/%{name}-%{version}-%{release} ]; then
rm -rf $RPM_BUILD_DIR/%{name}-%{version}-%{release}
fi
…
Now create a script that replaces the %define variables in your spec file with current build information. You will call this script directly from the Makefile, so in the end you won’t have extra steps needed in order to build.
Mercurial, git, subversion, cvs - or whatever you have, you should be able to get the current build info from the metadata that comes along with your clone/checkout.
$ svn info
URL: http://www.rootninja.com/CoolApp/trunk
Repository Root: http://www.rootninja.com/CoolApp
Revision: 294
Last Changed Author: me
Use the data from the rpmbuild script you create to replace TEMPLATE items in the spec file. Your source control may be able to supply some of this information automatically.
#!/bin/sh
_APP=CoolApp
_REVISION=`svn info . | grep -m 1 “Revision:” | grep -o “[0-9]*$”`
_URL=`svn info . | grep URL:`
_ARCH=`uname -m`
_BASEDIR=`pwd`
_VERSION=`pwd | sed ’s/\// /g’ | awk ‘{print $NF}’`cd $_BASEDIR
sed “s/^%define name .*$/%define name $_APP/” $_APP.spec.template > $_APP.spec
sed -i “s/^%define version .*$/%define version $_VERSION/” $_APP.spec
sed -i “s/^%define svnrev .*$/%define buildnum $_REVISION/” $_APP.spec
sed -i “s/^%define arch .*$/%define arch $_ARCH/” $_APP.specrpmbuild -ba $_APP.spec
In this case, I’m starting with a spec file named CoolApp.spec.template and creating the CoolApp.spec that actually gets used.
Create a target called rpm in your Makefile that does nothing but call the rpmbuild script.
Now every time you make rpm, the Makefile will call the rpmbuild script which will compile the source and build an RPM from a spec file that includes the correct build information.
8:07 am
I think you have a typo. Your template shows:
%define buildnumber TEMPLATE
and your build script shows:
sed -i “s/^%define svnrev .*$/%define buildnum $_REVISION/” $_APP.spec
So, you’re replacing “%define svnrev” with “%define buildnum”, but “svnrev” is not defined anywhere.