spec and RPMS

SPEC file

The “Recipe” for creating an RPM package is a spec file. Spec files end in the “.spec” suffix and contain the package name, version, RPM revision number, steps to build, install, and clean a package, and a changelog. Multiple packages can be built from a single RPM spec file, if desired. RPM packages are created from RPM spec files using the rpmbuild tool.

Spec files are usually distributed within SRPM files, which contain the spec file packaged along with the source code.

SRPM

A typical RPM is pre-compiled software ready for direct installation. The corresponding source code can also be distributed. This is done in an SRPM, which also includes the “SPEC” file describing the software and how it is built. The SRPM also allows the user to compile, and perhaps modify, the code itself.

A software package may contain only scripts that are architecture-independent. In such a case only an SRPM may be available; this is still an installable RPM.

 

 

Rebuild the SRPM in One Step

The quickest way to rebuild the SRPM is to use the rpmbuild --rebuild ... command. This command will unpack the SRPM file into the specfile and the source files, and then it will build the RPM from the instructions on the specfile.

For example, to rebuild the /tmp/mypackage-1.0.0-1.src.rpm package, run this command:

 

[user@host ~]$ rpmbuild --rebuild /tmp/mypackage-1.0.0-1.src.rpm

If everything goes right, you will end up with a mypackage-1.0.0-1.i386.rpm file under the ~/rpmbuild/RPMS/i386 directory (if your architecture is i386, otherwise the name of the file and the directory will change accordingly).

 

For later Fedora RPMS, or any that produce an error like “error: unpacking of archive failed on file /builddir/build/SOURCES/mypackage-1.0.0.tar.gz;4dc983a7: cpio: MD5 sum mismatch”:

 

[user@host ~]$ rpm --nomd5 -i /tmp/mypackage-1.0.0-1.src.rpm

Once you unpacked the SRPM, you will notice that now there is a specfile (in this case, it will typically be called mypackage.spec) in the ~/rpmbuild/SPECS directory. That is the file you will use to build the RPM. To do that, use the following command:

 

[user@host ~]$ cd ~/rpmbuild/SPECS
[user@host SPECS]$ rpmbuild -ba mypackage.spec

The rpmbuild -ba command will run through all the steps of the RPM building process, and at the end it will create an RPM package file (which will be saved under~/rpmbuild/RPMS/i386, or the directory appropriate for your architecture), and also a new SRPM file (which will be saved under ~/rpmbuild/SRPMS).

The advantage of unpacking the SRPM first and then using rpmbuild -ba to rebuild it from the specfile is that you can modify the specfile (and maybe add some patches or even upgrade the source tarball) to suit your needs. This is a more complex situation than just rebuilding the SRPM, though, and if you are going down this route you should probably read more on the subject, as explained below, but the process goes like this:

  1. cd ~/rpmbuild/SPECS/
  2. rpmbuild -bp mypackage.spec
  3. cd ~/rpmbuild/BUILD/
  4. cp existing_directory existing_directory.orig
  5. cd existing_directory
  6. find the file you wish to change, modify it.
  7. cd ~/rpmbuild/BUILD/
  8. diff -Npru existing_directory.orig exiting_directory > name_of_your_patch_file.patch
  9. cp name_of_your_patch_file.patch ~/rpmbuild/SOURCES/
  10. cd ~/rpmbuild/SPECS/
  11. edit the mypackage.spec file to add the definition of name_of_your_patch_file.patch and the application of your_patch_file — please look in the file to see how that is done.
  12. rpmbuild -ba mypackage.spec

These are excellent resources to help you if you decide to customize a specfile to your needs: