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
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
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
| Summary | A summary of what the package provides |
| Name | Name of the package |
| Version | Package version |
| Release | Package release |
| Copyright | Copyright agreement under which the package is released |
| Group | The package group (Amusement, Documentation ...) |
| Source | Path to the archive containing source and files |
| BuildRoot | Path 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
| %define | Define a variable that can be referenced later in the SPEC file |
| %description | Paragraph type description for the package (usually longer than Summary |
| %prep | The preparation section, includes unpacking the source archive and patching |
| %setup | Unpack the source archive |
| %patch | Apply patches if needed |
| %build | The build section, includes commands to run in the BUILD directory and execute the next commands (make, ...) |
| %install | The install section, includes command to copy files from the BUILD
directory to the fake $RPM_BUILD_ROOT directory
|
| %clean | Delete all files in $RPM_BUILD_ROOT |
| %files | List of files in the package |
| %doc | List which files are part of the documentation |
| %config | List which files are configuration files |
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/