Building a Package
Use the -b option with rpm to build packages. You can use other options with this command to perform only certain stages of the build process. This is useful for debugging the build process for a package. Table 1 lists the build options and the stages that they refer to.
Table 1 RPM Build Options
Option |
Stage or Action |
p |
Prep |
c |
Build (compile) |
i |
Install |
b |
Packaging (create a binary package) |
a |
All (build binary and source packages) |
l |
Check file list |
--short-circuit |
Execute only this stage (not preceding ones) |
The Prep Stage
During the prep stage of the package build, the script in the %Prep section of the spec file is run. This script will extract the source files from the TAR file and apply patches in preparation for the actual compilation of the software.
During this stage, a directory is created under /usr/src/distribution/BUILD. This is where the files are placed and where other stages will operate.
You can use any commands that you need to prepare the source files in the BUILD directory. However, there are two special directives that handle most of the actions that need to be performed. The %setup directive is used to extract the source files listed in the source lines. The %patch directive is used to apply the patches listed in the Patch lines.
The Build Stage
The build stage is where rpm actually compiles the software for your system, using the script in the %Build section. Often this script includes running programs such as configure, to set up the software build environment, and make, to actually compile the software.
No special directives are available for this section of the spec file. However, rpm sets several shell variables that have values that might be useful for this script.
The following shell variables are available for your use throughout the scripts in the spec file:
-
RPM_SOURCE_DIRLocation of the SOURCES directory. This is /usr/src/distribution/SOURCES by default, but it can be overridden by configuration values in /etc/rpmrc or ~/.rpmrc.
-
RPM_BUILD_DIRLocation of the BUILD directory. This is /usr/src/distribution/BUILD by default. This can also be overridden.
-
RPM_DOC_DIRLocation at which the document directory for this package is placed. The default value is /usr/doc.
-
RPM_OPT_FLAGSString of compiler options for producing optimized code for the target architecture (such as i386). This is often used with make during the compilation of the software.
-
RPM_OSString indicating the OS (not just Linux!) used during the build process.
-
RPM_ARCHString indicating the architecture during the build process.
-
RPM_ROOT_DIRRoot directory used during all steps of the build process.
-
RPM_BUILD_ROOTRoot directory used for the install and packaging stages of the build process.
-
RPM_PACKAGE_NAMEThe name of the package.
-
RPM_PACKAGE_VERSIONThe version of the software in the package.
-
RPM_PACKAGE_RELEASEThe release of the package.
The Install Stage
During the install stage, the commands are run to actually install the software on your system. At the end of this stage, the software will be installed on your system, compiled, formatted, and ready to run, exactly as it needs to be on an end user's system. The shell script to perform this is found in the %Install section of the spec file.
The files put in a package during the packaging state (described in the next section) are taken from their regular positions in the working filesystem. Their placement, ownership, and permissions are recorded in the package so that they will have those same attributes on the user's system when the package is installed. Thus, it is critical that the install stage sets up the files completely and properly.
The Packaging Stage
During this stage, the package files (binary and source RPMs) are actually created. The files that were placed in your filesystem during the install stage are collected and placed in the binary package file, along with information from the spec file. Then all the materials used to produce the package are collected and bundled together into a source RPM package, which can be used (with a similar set of tools and libraries) to reproduce the package at a later date.
The %Files section of the spec file lists the files and directories on your filesystem that will be put into the package. This will correspond with the files that were placed on your filesystem during the install stage of the build process.
The %Files section supports the following directives and options:
-
%docMarks documentation files. These go in a directory under /usr/doc on the end user's system. Don't use this for man pages or other documents that have to reside in specific locations on the end user's system. Note that the paths listed with this directive are relative to the BUILD directory, not the regular filesystem. For example, in Listing 1, the NEWS file listed with the %doc directive refers to /usr/src/distribution/BUILD/ed-0.2/NEWS, which will be placed on the user's system as /usr/doc/ed-0.2/NEWS when this package is installed.
-
%configMarks configuration files. These files get special handling during remove and upgrade operations on the user's machine.
-
%dirIndicates a directory. This causes rpm to package all the files in the indicated directory and all its subdirectories.
-
-f filenameThe -f option used with the %Files section indicates to read the file list from a separate file. For example, the following line reads the file list from the file /usr/src/distribution/BUILD/ed-0.2/edlist instead of from the spec file:
%File -f edlist
CAUTION
Be careful with listing directories in the file list. If an item in the file list is a directory, rpm interprets it as a %dir item and reads all the files in it. This can end up placing a huge number of files (and not the right ones) in the package if the directory is not correct.
At the end of this stage, there will be a binary package file in the RPMS/i386 directory and a source package file in SRPMS.