Product SiteDocumentation Site

2.2. Creating a Basic Spec File

This section shows how to write a basic spec file for your RPM package.

2.2.1. Creating an Example Spec File for eject

These steps show an example of creating a package from the source code archive of the eject utility. The eject utility is a simple software that ejects removable media using software control so it is an ideal candidate for creating a basic spec file.
Procedure 2.2. Creating an example package: eject
  1. In a shell prompt, go into the buildroot and create a new spec file for your package.
    1. To create a spec file template with the rpmdev-newspec command, run the following commands:
       cd ~/rpmbuild/SPECS 
       rpmdev-newspec eject 
      This creates a new spec file called eject.spec in the ~/rpmbuild/SPECS directory.
    2. To specify a spec file template for a particular type of packages, refer to the contents of the /etc/rpmdevtools/ directory, which includes spec file templates called spectemplate-type.spec. For example, to create a new spec file for a Python module, run the following commands:
       cd ~/rpmbuild/SPECS 
       rpmdev-newspec python-antigravity 
    3. For more information on creating a new spec file with rpmdev-newspec, run the rpmdev-newspec --help command.
    4. Alternatively, you can use the vim editor to create a spec file template for you. Change to the buildroot and run vim with the name of the spec file you want to create:
       cd ~/rpmbuild/SPECS 
       vi eject.spec 
  2. Open the spec file in a text editor. The spec file should be similar to the following example:
    Name:           eject
    Version:        
    Release:        1%{?dist}
    Summary:        
    
    Group:          
    License:        
    URL:            
    Source0:        
    BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
    
    BuildRequires:  
    Requires:       
    
    %description
    
    
    %prep
    %setup -q
    
    
    %build
    %configure
    make %{?_smp_mflags}
    
    
    %install
    rm -rf $RPM_BUILD_ROOT
    make install DESTDIR=$RPM_BUILD_ROOT
    
    
    %clean
    rm -rf $RPM_BUILD_ROOT
    
    
    %files
    %defattr(-,root,root,-)
    %doc
    
    
    
    %changelog

    The Group and BuildRoot tags are deprecated

    Although the Group and BuildRoot tags are included in the spec file templates, RPM in Fedora 18 does not require the presence of these tags in the spec file and ignores them.

    The %clean and %defattr directives are deprecated

    Although the %clean and %defattr directives are included in the spec file templates, RPM in Fedora 18 does not require the presence of these directives in the spec file and ignores them.
  3. Edit the Release tag to set the release value of the package. For example, set the value to 1%{?dist} if you are creating the initial release of the package:
    Release:        1%{?dist}
    
    The {?dist} tag is used to mark the distribution revision of a package.
  4. Fill in the version and add a summary of the software:
    Version:        2.1.5
    Release:        1%{?dist}
    Summary:        A program that ejects removable media using software control
    
  5. For the License tag, fill in the appropriate license for the software. In this case, eject uses the GNU General Public License v2.0 or later. The short name for this license is GPLv2+:
    License: GPLv2+
    
  6. From the eject project website, get the URL of the website and fill it in the URL tag:
    URL:            http://www.pobox.com/~tranter
    
  7. In the Source tag, fill in the URL of the source archive for the package:
    Source0: http://www.ibiblio.org/pub/Linux/utils/disk-management/%{name}-%{version}.tar.gz
    
  8. Edit the BuildRequires tag with requirements that are needed to build the package. BuildRequires can contain either a list of required packages or files. For example, the eject package requires the gettext and libtool packages:
    BuildRequires: gettext
    BuildRequires: libtool
    
  9. It is recommended to add a list of requirements that this package depends on to the Requires tag. Requires can contain either a list of required packages or files.
    In many cases, however, rpmbuild is able to detect the dependencies automatically for you so that you do not need to add them manually. For example, the eject package does not need any explicit requirements in the Requires tag, so do not include the tag in the spec file.
  10. Add a description for the package. The lines of text in %description should be at most 79 characters long:
    %description
    The eject program allows the user to eject removable media (typically
    CD-ROMs, floppy disks or Iomega Jaz or Zip disks) using software
    control. Eject can also control some multi-disk CD changers and even
    some devices' auto-eject features.
    
    Install eject if you'd like to eject removable media using software
    control.
    
  11. Add the %check section between the sections %build and %install in the spec file. The %check section typically contains the make test or make check command that runs any self-tests distributed with the software:
    %check
    make check
    
  12. Edit the %install section by adding the following installation instructions that are specific to eject the to the spec file:
    install -m 755 -d $RPM_BUILD_ROOT/%{_sbindir}
    ln -s ../bin/eject $RPM_BUILD_ROOT/%{_sbindir}
    This calls the install program and creates a symbolic link, which is necessary to properly build and install the eject package.
  13. Because the eject utility includes translation files, you need to define a macro called %find_lang, which will locate all of the translation files that belong to the package, and put this list in a file called %{name}.lang.
    1. To define the %find_lang macro, add the following to the %install section:
      %find_lang %{name}
    2. To include the %{name}.lang file with a list of translation files, add the file name with the -f option to %files:
      %files -f %{name}.lang
  14. Add the list of documentation files that are included in eject to the %doc directive:
    %doc README TODO COPYING ChangeLog
    
  15. Edit %changelog to describe the last change you have made to the package. Fill it in with the date, your name and email address, the version and release of the package, and a short description of what has changed in the package in the following format:
    * date Packager's Name <packager's_email> version-revision
    - Summary of changes
    To get the changelog entry in the required format, you can use the rpmdev-bumpspec utility. Run the following command:
     rpmdev-bumpspec --comment=summary of changes --userstring=Packager's Name <packager's_email> spec file 
    Because this is the first release of the package, run the rpmdev-bumpspec command with the following options:
     rpmdev-bumpspec --comment="Initial RPM release" --userstring="John Doe <jdoe@example.com>" eject.spec 
    This will produce a changelog entry in the spec file similar to the following:
    %changelog
    * Wed Oct 20 2011 John Doe <jdoe@example.com> 2.1.5-0.1
    - Initial RPM release
    
  16. Now the spec file should look like in the following example:
    Name: eject
    Version: 2.1.5
    Release: 1%{?dist}
    Summary: A program that ejects removable media using software control
    
    License: GPLv2+
    URL: http://www.pobox.com/~tranter
    Source0: http://www.ibiblio.org/pub/Linux/utils/disk-management/%{name}-%{version}.tar.gz
    
    BuildRequires: gettext
    BuildRequires: libtool
    
    %description
    The eject program allows the user to eject removable media (typically
    CD-ROMs, floppy disks or Iomega Jaz or Zip disks) using software
    control. Eject can also control some multi-disk CD changers and even
    some devices' auto-eject features.
    
    Install eject if you'd like to eject removable media using software
    control.
    
    %prep
    %setup -q -n
    
    %build
    %configure
    make %{?_smp_mflags}
    
    %check
    make check
    
    %install
    rm -rf $RPM_BUILD_ROOT
    make install DESTDIR=$RPM_BUILD_ROOT
    
    install -m 755 -d $RPM_BUILD_ROOT/%{_sbindir}
    ln -s ../bin/eject $RPM_BUILD_ROOT/%{_sbindir}
    
    %find_lang %{name}
    
    %files -f %{name}.lang
    %doc README TODO COPYING ChangeLog
    %{_bindir}/*
    %{_sbindir}/*
    %{_mandir}/man1/*
    
    %changelog
    * Wed Oct 20 2011 John Doe <jdoe@example.com> 0.8.18.1-0.1
    - Initial RPM release