6.2. RPM Builds

6.2.1. Creating a Build Directoty

It is recommanded to first create a build root directory as a user. This will avoid having to compile software as root, which is potentially dangerous. Since most documentation assume that this directory in $HOME/rpmbuild, we will use this as our build root directory.

An helpful starting point is to download the following archive ftp://people.redhat.com/mharris/hacks/rpmbuild-nonroot-1.0.tar.gz. It contains a README as well as the .rpmrc and .rpmmacros files that need to be copied in the user's home directory.

Here is a list of directories that need to be created in ~/rpmbuild

  • SRPMS - contains the src.rpms packages
  • RPMS - contains binary (i368,i486,i586, etc) and noarch rpm packages (
  • BUILD - this directory is used to decompress and build the software
  • SPECS - specfiles are saved here when installing an src.rpm package
  • SOURCES - source code is saved here when installing an src.rpm package

6.2.2. The rpmbuild command

The rpmbuild command is used to build or rebuild RPM packages.

  • Rebuilds: it is possible to rebuild an RPM package from a source RPM (extension .src.rpm) as follows:

    rpmbuild --rebuild package-name.src.rpm
    

    This will place an RPM package in ~/rpmbuild/RPMS/that can be installed with the rpm command.

  • Build: if a source RPM doesn't exist, it is possible to create a specfile (project.spec) and build RPM packages (RPM or SRPM) from the software source (project.tar.gz) as follows:

    • If the specfile is included in the tarball (project.tar.gz) then the RPM (binary) and SRPM (source) packages are built with:

              rpmbuild -ta project.tar.gz
              
    • If the source archive is in ~/rpmbuild/SOURCES and the specfile is called project.spec, the RPM and SRPM packages are built with:

                 rpmbuild -ba project.spec
                 

6.2.3. The specfile options

The building process will take a source archive (e.g project.tar.gz) together with a spec file (e.g project.spec) to create an RPM package (e.g project.i386.rpm).

The next table is an overview of the sections found in a spec file.

Table 6.1. Project Desciption

SummaryA summary of what the package provides
NameName of the package
VersionPackage version
ReleasePackage release
CopyrightCopyright agreement under which the package is released
GroupThe package group (Amusement, Documentation ...)
SourcePath to the archive containing source and files
BuildRootPath to the temporary (fake) root filesystem

A certain number of macros are used to determine each stage of the build process (e.g %install or %clean) or will run a sequence of instructions (e.g %setup or %patch). The next table is an overview of such macros.

Table 6.2. Predefined Macros and Section

%defineDefine a variable that can be referenced later in the SPEC file
%descriptionParagraph type description for the package (usually longer than Summary
%prepThe preparation section, includes unpacking the source archive and patching
%setupUnpack the source archive
%patchApply patches if needed
%buildThe build section, includes commands to run in the BUILD directory and execute the next commands (make, ...)
%installThe install section, includes command to copy files from the BUILD directory to the fake $RPM_BUILD_ROOT directory
%cleanDelete all files in $RPM_BUILD_ROOT
%filesList of files in the package
%docList which files are part of the documentation
%configList which files are configuration files

6.2.4. Example: Copy fstab to /tmp/etc/fstab

We can build a simple RPM package that installs an fstab file into /tmp/etc/. The spec file will look like this:

#This is the Header section
Summary: Installs a fstab file to /tmp/etc
%define name tmp-fstab
%define version 0.2
%define release 1
Name: %{name}
Version: %{version}
Release: %{release}
Copyright: Freely distributable
Group: Documentation
Source: %{name}-%{version}.tar.gz
Packager: Adrian Thomasset <adrian@linuxit.com>

#The BuildRoot directory is a temporary replacement for root (/) 
#while the package is being built.
BuildRoot: /var/tmp/rpm-%{name}/

 
%description
This package copies a file called fstab to /tmp/etc/


%prep
#The %setup macro simply opens the archived files from SOURCES into 
#BUILD and changes directory to it (/../../BUILD/%{name}-%{version}/ 
%setup


#All the work is done here: $RPM_BUILD_ROOT is a reference to the 
#variable defined using the %BuildRoot command earlier 
%install

rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/tmp/etc/
install -m 644 fstab $RPM_BUILD_ROOT/tmp/etc/fstab

%clean 
rm -rf $RPM_BUILD_ROOT
#Define which files must be copied to the binary RPM package. 
#The $RPM_BUILD_ROOT is taken as the root directory
%files 
/tmp/etc/fstab
%defattr(-,adrian,adrian)

All that is left to do is to prepare the source. In this case we need to create a directory called tmp-fstab-0.2 containing fstab. Notice that the name and the version correspond to the name and version defined in the SPEC file

mkdir tmp-fstab-0.2
cp /etc/fstab tmp-fstab-0.2/

Next we archive the directory and copy this to the SOURCES directory

tar cvzf tmp-fstab-0.2.tar.gz tmp-fstab/
cp tmp-fstab-0.2.tar.gz /path/to/SOURCES/