Work with spec files

To package new software with RPM, you must create a spec file.

You can create the spec file in one of the following ways:

  • Write the new spec file manually from scratch.
  • Use the rpmdev-newspec utility. This utility creates an unpopulated spec file, where you fill the necessary directives and fields.
Note
Some programmer-focused text editors pre-populate a new spec file with their own spec template. The rpmdev-newspec utility provides an editor-agnostic method.

Create a new spec file for sample Bash, Python, and C programs

Create a spec file for each of the three sample implementations of the Hello World! program by using the rpmdev-newspec utility.

Before you begin

Procedure

  1. Navigate to the ~/rpmbuild/SPECS directory:
    $ cd ~/rpmbuild/SPECS
  2. Create a spec file for each of the three implementations of the Hello World! program:
    $ rpmdev-newspec bello
    bello.spec created; type minimal, rpm version >= 4.11.
    $ rpmdev-newspec cello
    cello.spec created; type minimal, rpm version >= 4.11.
    $ rpmdev-newspec pello
    pello.spec created; type minimal, rpm version >= 4.11.

    The ~/rpmbuild/SPECS/ directory now contains three spec files named bello.spec, cello.spec, and pello.spec.

  3. Examine the created files.

    The directives in the files represent those described in About spec files. In the following sections, you will populate a particular section in the output files of rpmdev-newspec.

Modify an original spec file

Provide necessary instructions for the rpmbuild utility by modifying the original output spec file generated by the rpmdev-newspec utility. rpmbuild then uses these instructions to build an RPM package.

Before you begin

Procedure

  1. Open the ~/rpmbuild/SPECS/<name>.spec file provided by the rpmdev-newspec utility.
  2. Populate the following directives of the spec file Preamble section:
    • Name: Name was already specified as an argument to rpmdev-newspec.
    • Version: Set Version to match the upstream release version of the source code.
    • Release: Release is automatically set to 1%{?dist}, which is initially 1.
    • Summary: Enter a one-line explanation of the package.
    • License: Enter the software license associated with the source code.
    • URL: Enter the URL to the upstream software website. For consistency, use the %{name} RPM macro variable and the https://example.com/%{name} format.
    • Source: Enter the URL to the upstream software source code. Link directly to the software version being packaged. Note
      The example URLs in this documentation include hard-coded values that could possibly change in the future. Similarly, the release version can change as well. To simplify these potential future changes, use the %{name} and %{version} macros. By using these macros, you need to update only one field in the spec file.
    • BuildRequires: Specify build-time dependencies for the package.
    • Requires: Specify run-time dependencies for the package.
    • BuildArch: Specify the software architecture.
  3. Populate the following directives of the spec file Body section. You can think of these directives as section headings, because these directives can define multi-line, multi-instruction, or scripted tasks to occur.
    • %description: Enter the full description of the software.
    • %prep: Enter a command or series of commands to prepare software for building.
    • %conf: Enter a command or series of commands to configure software for building.
    • %build: Enter a command or series of commands for building software.
    • %install: Enter a command or series of commands that instruct the rpmbuild command on how to install the software into the BUILDROOT directory.
    • %files: Specify the list of files, provided by the RPM package, to be installed on your system.
    • %changelog: Enter the list of datestamped entries for each Version-Release of the package.

      Start the first line of the %changelog section with an asterisk (*) character followed by Day-of-Week Month Day Year Name Surname <email> - Version-Release.

      For the actual change entry, follow these rules:

      • Each change entry can contain multiple items, one for each change.
      • Each item starts on a new line.
      • Each item begins with a hyphen (-) character.

An example spec file for a sample Bash program

Review the following annotated example spec file for a sample program written in the Bash programming language (bello).

To install bello, you must create the destination directory and install the executable bash script file there. Therefore, you can use the install command in the %install section. You can use RPM macros to do this without hardcoding paths.

An example spec file for the bello program

Name:           bello
Version:        0.1
Release:        1%{?dist}
Summary:        Hello World example implemented in bash script

License:        GPLv3+
URL:            https://www.example.com/%{name}
Source0:        https://www.example.com/%{name}/releases/%{name}-%{version}.tar.gz

Requires:       bash

BuildArch:      noarch

%description
The long-tail description for our Hello World Example implemented in
bash script.

%prep
%setup -q

%build

%install

mkdir -p %{buildroot}/%{_bindir}

install -m 0755 %{name} %{buildroot}/%{_bindir}/%{name}

%files
%license LICENSE
%{_bindir}/%{name}

%changelog
* Tue May 31 2016 Adam Miller <maxamillion@fedoraproject.org> - 0.1-1
- First bello package
- Example second item in the changelog for version-release 0.1-1
  • The BuildRequires directive, which specifies build-time dependencies for the package, was deleted because there is no building step for bello. Bash is a raw interpreted programming language, and the files are just installed to their location on the system.
  • The Requires directive, which specifies run-time dependencies for the package, includes only bash, because the bello script requires only the bash shell environment to run.
  • The %build section, which specifies how to build the software, is blank, because the bash script does not need to be built.

An example spec file for a sample Python program

Review the following annotated example spec file for a sample program written in the Python programming language (pello).

This example of creating a wrapper script in-line in the spec file shows that the spec file itself is scriptable. This wrapper script executes the Python byte-compiled code by using the here document.

An example spec file for the pello program

Name:           pello
Version:        0.1.1
Release:        1%{?dist}
Summary:        Hello World example implemented in Python

License:        GPLv3+
URL:            https://www.example.com/%{name}
Source0:        https://www.example.com/%{name}/releases/%{name}-%{version}.tar.gz

BuildRequires:  python
Requires:       python
Requires:       bash

BuildArch:      noarch

%description
The long-tail description for our Hello World Example implemented in Python.

%prep
%setup -q

%build

python -m compileall %{name}.py

%install

mkdir -p %{buildroot}/%{_bindir}
mkdir -p %{buildroot}/usr/lib/%{name}

cat > %{buildroot}/%{_bindir}/%{name} <<EOF
#!/bin/bash
/usr/bin/python /usr/lib/%{name}/%{name}.pyc
EOF

chmod 0755 %{buildroot}/%{_bindir}/%{name}

install -m 0644 %{name}.py* %{buildroot}/usr/lib/%{name}/

%files
%license LICENSE
%dir /usr/lib/%{name}/
%{_bindir}/%{name}
/usr/lib/%{name}/%{name}.py*

%changelog
* Tue May 31 2016 Adam Miller <maxamillion@fedoraproject.org> - 0.1.1-1
  - First pello package
  • The Requires directive, which specifies run-time dependencies for the package, includes two packages:
    • The python package required to execute the byte-compiled code at runtime.
    • The bash package required to execute the small entry-point script.
  • The BuildRequires directive, which specifies build-time dependencies for the package, includes only the python package. The pello program requires python to perform the byte-compile build process.
  • The %build section, which specifies how to build the software, creates a byte-compiled version of the script. Note that in real-world packaging, it is usually done automatically, depending on the distribution used.
  • The %install section corresponds to the fact that you must install the byte-compiled file into a library directory on the system so that it can be accessed.

An example spec file for a sample C program

Review the following annotated example spec file for a sample program written in the C programming language (cello).

You can install the cello program by using the %make_install macro. This is possible because the Makefile file for the cello program is available.

An example spec file for the cello program

Name:           cello
Version:        1.0
Release:        1%{?dist}
Summary:        Hello World example implemented in C

License:        GPLv3+
URL:            https://www.example.com/%{name}
Source0:        https://www.example.com/%{name}/releases/%{name}-%{version}.tar.gz

Patch0:         cello-output-first-patch.patch

BuildRequires:  gcc
BuildRequires:  make

%description
The long-tail description for our Hello World Example implemented in
C.

%prep
%setup -q

%patch0

%build
make %{?_smp_mflags}

%install
%make_install

%files
%license LICENSE
%{_bindir}/%{name}

%changelog
* Tue May 31 2016 Adam Miller <maxamillion@fedoraproject.org> - 1.0-1
- First cello package
  • The BuildRequires directive, which specifies build-time dependencies for the package, includes the following packages required to perform the compilation build process:
    • gcc
    • make
  • The Requires directive, which specifies run-time dependencies for the package, is omitted in this example. All runtime requirements are handled by rpmbuild, and the cello program does not require anything outside of the core C standard libraries.
  • The %build section reflects the fact that in this example the Makefile file for the cello program was written. Therefore, you can use the GNU make command. However, you must remove the call to %configure because you did not provide a configure script.