Product SiteDocumentation Site

8.2.4. Building RPMs with the rpmbuild command

To build RPMs with the rpmbuild command, use the following basic syntax:
rpmbuild -bBuildStage spec_file
The -b option tells rpmbuild to build an RPM. The extra BuildStage option is a special code that tells the rpmbuild command how far to go when building. Table 9-2 lists these options:
Table 9-2 Options for building with rpmbuild
Build all, both a binary and source RPM
Build a binary RPM
Build (compile) the program but do not make the full RPM, stopping just after the %build section
Prepare for building a binary RPM, and stop just after the %prep section
Create a binary RPM and stop just after the %install section
Check the listing of files for the RPM and generate errors if the buildroot is missing any of the files to be installed
Build a source RPM only
See chapter 12 for advanced options you can use with rpmbuild.
For example, to set up all the necessary files and prepare for building, run the following command:
rpmbuild –bp specfile
This example runs through the %prep section, and stops immediately after this section. With the jikes package, for example, you’ll see a result like the following:
$ rpmbuild -bp /usr/src/redhat/SPECS/jikes.spec
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.72435
+ umask 022
+ cd /usr/src/redhat/BUILD
+ export LANG
+ cd /usr/src/redhat/BUILD
+ rm -rf jikes-1.17
+ /usr/bin/gzip -dc /usr/src/redhat/SOURCES/jikes-1.17.tar.gz
+ tar -xf -
+ '[' 0 -ne 0 ']'
+ cd jikes-1.17
++ /usr/bin/id -u
+ '[' 500 = 0 ']'
++ /usr/bin/id -u
+ '[' 500 = 0 ']'
+ /bin/chmod -Rf a+rX,g-w,o-w .
+ exit 0
After running this command, the source files are extracted into the /usr/src/redhat/BUILD directory, under the jikes-1.17 subdirectory. Using a subdirectory keeps the sources for this package from intermixing with the sources for other packages.
Running a directory listing on the /usr/src/redhat/BUILD/jikes-1.17 subdirectory shows what the spec file %prep section commands have done. For example:
$ ls -1 /usr/src/redhat/BUILD/jikes-1.17
From these sources, you see a configure script. The configure script gives a good indication of how the software needs to be built. This example also shows a README file. You know what to do with these files.
The actual source code is in the /usr/src/redhat/BUILD/jikes-1.17/src directory. The user documentation is stored in the /usr/src/redhat/BUILD/jikes-1.17/doc directory.
To build a binary RPM, use the –bb option to the rpmbuild command. For example:
$ rpmbuild -bb /usr/src/redhat/SPECS/jikes.spec
Don’t build packages when you are logged in as the root user. Log in as a normal user instead. This is to limit the damage caused to your system if the spec file or the Makefile contains errors that delete system files, for example. If you are logged in as the root user, you will have permission to perform these destructive acts. If you are logged in as a normal user, though, these RPM spec file and Makefile errors will fail to run, because you don’t have permission to modify system files.
This command results in a lot of output, most coming from the configure script. (This script examines the C programming environment on your system.) When the rpmbuild command completes, you’ll see the binary RPM in the proper subdirectory of the RPMS directory. You can see the RPM with a directory listing, for example:
$ls /usr/src/redhat/RPMS/i386:
To stop execution just after the %install section, use a command like the following:
rpmbuild –bi specfile
For example:
# rpmbuild -bi /usr/src/redhat/SPECS/jikes.spec
To build a source RPM out of the files you have (in this case a tar archive of the sources and the spec file), use a command like the following:
rpmbuild –bs specfile
For example:
$ rpmbuild -bs /usr/src/redhat/SPECS/jikes.spec
When done, you’ll see the source RPM in the /usr/src/redhat/SRPMS directory:
$ ls /usr/src/redhat/SRPMS
To clean out the files created by building these RPMs, use the --clean option to the rpmbuild command:
rpmbuild --clean specfile
For example:
$ rpmbuild --clean /usr/src/redhat/SPECS/jikes.spec
Executing(--clean): /bin/sh -e /var/tmp/rpm-tmp.21908
+ umask 022
+ cd /usr/src/redhat/BUILD
+ rm -rf jikes-1.17
+ exit 0
Cross Reference
Chapter 11, Controlling the Build with rpmbuild covers a number of addition options for the rpmbuild command that you can use to customize the build.