2018-07-27 14:38:30 +00:00
include::{partialsdir}/attributes.adoc[]
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
= How to Create a GNU Hello World RPM Package
2017-10-26 21:20:01 +00:00
2020-03-04 13:46:20 +00:00
This is a short hands-on tutorial on writing RPM files, showing how to quickly set up and create simple source and binary software packages.
2018-02-26 16:04:59 +00:00
It assumes some familiarity with using pre-made RPM packages, and with the FOSS building process.
2017-10-26 21:20:01 +00:00
2019-03-22 15:20:33 +00:00
For comprehensive information on how to create RPM files, including more detailed tips, refer to xref:creating-rpm-packages.adoc[How to create an RPM package].
If you plan to create an RPM package for the Fedora repository, follow the process for link:https://fedoraproject.org/wiki/Join_the_package_collection_maintainers[How to join the Fedora Package Collection Maintainers], including following the various Fedora guidance.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
This tutorial demonstrates packaging of the GNU "Hello World" project.
While the C program printing "Hello World" to standard output is trivial, the GNU version contains most of the usual peripheral components associated with a typical FOSS project, including the configuration/build/install environment, documentation, internationalization, etc.
The GNU version, however, traditionally consists of a `tar` file containing the source code and configure/make scripts, but it does not include the packaging information.
Therefore, it's a reasonable vehicle to practice building RPMs on.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
== Development Environment
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
To build RPMs we need a set of development tools.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
IMPORTANT: Make sure you run the following command as the `root` user!
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
This is a one-time-only setup, installed by running these commands from a system administration (`root`) account:
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
# dnf install fedora-packager @development-tools
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
IMPORTANT: Make sure you run the following command as the `root` user!
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
To be able to test the build procedure in a clean chroot you need to configure your non-privileged account to be a member of the 'mock' group:
----
2017-10-26 21:20:01 +00:00
# usermod -a -G mock <your username>
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
Those are the only commands requiring `root` privileges.
All the remaining work should be done from your regular, non-privileged account, or even from a separate account created just for development work.
Modern RPM-based systems, including Fedora, are set up to build and test RPM packages purely from within a non-privileged account.
2020-08-27 23:26:38 +00:00
The following command sets up an RPM build area in your `~/rpmbuild` directory.
2018-02-26 16:04:59 +00:00
This directory will contain several subdirectories, for the project source code, RPM configuration files and for the resulting source and binary packages.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
$ rpmdev-setuptree
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
== Building a "Hello World" RPM
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
We need the source code of the project we are packaging, often referred to as the 'upstream' source.
We will download it from the project's website into the `~/rpmbuild/SOURCE` directory.
We are getting the compressed tarball archive, which happens to be the preferred distribution form for most FOSS projects.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
$ cd ~/rpmbuild/SOURCES
$ wget http://ftp.gnu.org/gnu/hello/hello-2.10.tar.gz
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
The RPM package is configured by `.spec` files.
We will create a template file `hello.spec` in the appropriate directory:
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
$ cd ~/rpmbuild/SPECS
$ rpmdev-newspec hello
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
Recent versions of `Emacs` and `vi` have .spec file editing modes which will also bring up a similar template upon creating a new file.
So you can just use the following command for example to use the template automatically.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
$ emacs hello.spec
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
== Inside a Spec File
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
The fields in our `.spec` file need slight editing.
2019-03-22 15:20:33 +00:00
Please follow the xref:creating-rpm-packages.adoc[Fedora rules] for these fields.
2018-02-26 16:04:59 +00:00
In our case, the file might start as follows:
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
[source, RPMSpec]
----
2017-10-26 21:20:01 +00:00
Name: hello
Version: 2.10
Release: 1
Summary: The "Hello World" program from GNU
License: GPLv3+
2018-02-26 16:04:59 +00:00
URL: https://www.gnu.org/software/hello/
2017-10-26 21:20:01 +00:00
Source0: https://ftp.gnu.org/gnu/hello/hello-%{version}.tar.gz
%description
2018-02-26 16:04:59 +00:00
The "Hello World" program, done with all bells and whistles of a proper FOSS
2017-10-26 21:20:01 +00:00
project, including configuration, build, internationalization, help files, etc.
%changelog
* Thu Jul 07 2011 The Coon of Ty <Ty@coon.org> - 2.10-1
- Initial version of the package
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
2020-03-04 13:46:20 +00:00
The `Version` should mirror the upstream, while `Release` numbers our work within Fedora.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
The first letter of the `Summary` should be uppercase to avoid `rpmlint` complaints.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
It is your responsibility to check the `License` status of the software, by inspecting the source files and/or their `LICENSE` files, and/or by talking to the authors.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
The `Group` tag was historically used to classify the package in accordance with the list in `/usr/share/doc/rpm-``/GROUPS`.
It is being phased out so you will not see it added by default.
However, it doesn't hurt to add it anyway.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
The `%changelog` should document the work on preparing the RPM, especially if there are security and bug patches included on top of the base upstream source.
Changelog data can be displayed by `rpm --changelog -q `, which is very useful for instance to find out if specific bug and security patches were included in the installed software, thanks to the diligent Fedora packagers who include this info with the relevant http://cve.mitre.org/[CVE] numbers.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
The `%changelog` entry should include the version string to avoid `rpmlint` complaints.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
Multi-line sections like `%changelog` or `%description` start on a line under the directive, and end with a blank line.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
Lines which aren't needed (e.g. `BuildRequires` and `Requires`) can be commented out with a hash ('#') for now.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
Many lines in the template don't need to be changed at all in many cases, at least for the initial attempt.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
== Building the Package
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
We are ready for the first run to build source, binary and debugging packages:
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
$ rpmbuild -ba hello.spec
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
It will complain and list the unpackaged files, i.e. the files that would be installed in the system that weren't declared as belonging to the package.
We need to declare them in the `%files` section.
2018-07-29 11:01:46 +00:00
Do not hardcode names like `/usr/bin/`, but use macros, like `%\{_bindir}/hello` instead.
2018-02-26 16:04:59 +00:00
The manual pages should be declared in the `%doc` subsection:
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
....
%doc %{_mandir}/man1/hello.1.*
....
This is an iterative process; after editing the `.spec` file, rerun `rpmbuild`.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
Since our program uses translations and internationalization, we are seeing a lot of undeclared i18 files.
2019-03-25 13:02:19 +00:00
2019-03-28 23:04:54 +00:00
The xref:packaging-guidelines:ROOT:index.adoc#_handling_locale_files[recommended method] to declare them is:
2017-10-26 21:20:01 +00:00
2018-07-29 11:01:46 +00:00
* find the filenames in the `%install` step: `%find_lang %\{name}`
2017-10-26 21:20:01 +00:00
* add the required build dependencies: `BuildRequires: gettext`
2018-07-29 11:01:46 +00:00
* use the found filenames `%files -f %\{name}.lang`
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
If the program uses GNU `info` files, you need to make sure the installation and uninstallation of the package does not interfere with other software on the system, by using this boilerplate:
2017-10-26 21:20:01 +00:00
* delete the `dir` file in `%install`:
2018-02-26 16:04:59 +00:00
----
2020-04-14 17:36:32 +00:00
$ rm -f %{buildroot}%{_infodir}/dir
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
* `Requires(post): info` and `Requires(preun): info`
* add those steps:
2018-02-26 16:04:59 +00:00
[source,RPMSpec]
----
2017-10-26 21:20:01 +00:00
%post
/sbin/install-info %{_infodir}/%{name}.info %{_infodir}/dir || :
%preun
if [ $1 = 0 ] ; then
/sbin/install-info --delete %{_infodir}/%{name}.info %{_infodir}/dir || :
fi
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
This snippet is copied directly from link:https://fedoraproject.org/wiki/Packaging:Scriptlets?rd=Packaging:ScriptletSnippets#Texinfo[here].
That page contains solutions to many common packaging tasks.
If possible, try to copy a solution from there instead of devising your own.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
== A Complete `hello.spec` File
2017-10-26 21:20:01 +00:00
Here's the initial version of `hello.spec`:
2018-02-26 16:04:59 +00:00
[source,RPMSpec]
----
2017-10-26 21:20:01 +00:00
Name: hello
Version: 2.10
Release: 1%{?dist}
Summary: The "Hello World" program from GNU
License: GPLv3+
URL: http://ftp.gnu.org/gnu/%{name}
Source0: http://ftp.gnu.org/gnu/%{name}/%{name}-%{version}.tar.gz
BuildRequires: gettext
2018-02-26 16:04:59 +00:00
2017-10-26 21:20:01 +00:00
Requires(post): info
Requires(preun): info
2018-02-26 16:04:59 +00:00
%description
The "Hello World" program, done with all bells and whistles of a proper FOSS
2017-10-26 21:20:01 +00:00
project, including configuration, build, internationalization, help files, etc.
%prep
%autosetup
%build
%configure
make %{?_smp_mflags}
%install
%make_install
%find_lang %{name}
2020-04-14 17:36:32 +00:00
rm -f %{buildroot}%{_infodir}/dir
2017-10-26 21:20:01 +00:00
%post
/sbin/install-info %{_infodir}/%{name}.info %{_infodir}/dir || :
%preun
if [ $1 = 0 ] ; then
/sbin/install-info --delete %{_infodir}/%{name}.info %{_infodir}/dir || :
fi
%files -f %{name}.lang
%{_mandir}/man1/hello.1.*
%{_infodir}/hello.info.*
%{_bindir}/hello
%doc AUTHORS ChangeLog NEWS README THANKS TODO
%license COPYING
%changelog
* Tue Sep 06 2011 The Coon of Ty <Ty@coon.org> 2.10-1
- Initial version of the package
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
With this `.spec` file, you should be able to successfully complete the build process, and create the source and binary RPM packages.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
Next you should check them for conformance with RPM design rules, by running `rpmlint` on the `.spec` file and all RPMs:
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
$ rpmlint hello.spec ../SRPMS/hello* ../RPMS/*/hello*
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
If there are no warnings or errors, we've succeeded.
Otherwise, use `rpmlint -i` or `rpmlint -I <error_code>` to see a more verbose description of the `rpmlint` diagnostics.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
== The `mock` Builds
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
To check that the package build will succeed in the Fedora restricted build environment, check it with `mock`.
The default `mock` configuration builds the package against Rawhide - the Fedora development branch.
2017-10-26 21:20:01 +00:00
2018-02-26 16:04:59 +00:00
----
2017-10-26 21:20:01 +00:00
$ mock --verbose ../SRPMS/hello-2.10-1.fc25.src.rpm
2018-02-26 16:04:59 +00:00
----
== References
2019-03-22 15:20:33 +00:00
* xref:creating-rpm-packages.adoc[How to create an RPM package]
2018-02-26 16:04:59 +00:00
2019-03-22 15:20:33 +00:00
* link:https://fedoraproject.org/wiki/Building_RPM_packages_(20090405)[Building RPM packages (20090405)]
2018-02-26 16:04:59 +00:00
2019-03-22 15:20:33 +00:00
* link:https://fedoraproject.org/wiki/Using_Mock_to_test_package_builds[Using Mock to test package builds]
2018-02-26 16:04:59 +00:00
2019-03-22 15:20:33 +00:00
* link:https://fedoraproject.org/wiki/Using_the_Koji_build_system[Using the Koji build system]
2018-02-26 16:04:59 +00:00
== History
2020-03-04 13:46:20 +00:00
Przemek Klosowski wrote this tutorial when he worked through link:https://fedoraproject.org/wiki/Building_RPM_packages_(20090405)[Christoph Wickert's IRC session on building RPMs] using Rahul Sundaram's suggestion of GNU "Hello World" as a test case.
2019-03-22 15:20:33 +00:00
After he wrote up his experience, he found out about the excellent and extensive xref:creating-rpm-packages.adoc[How to create an RPM package] page on this wiki, as well as the Christian Lyder Jacobsen's website, which is unfortunately not available anymore.
2019-03-20 22:42:41 +00:00
However, Christian isn't planning to update his site, and it seemed that a 5-minute 'fast food' alternative to the more extensive article might suit some people.
2019-03-22 15:20:33 +00:00
More in-depth information on using and building RPM packages is available from link:dnf.adoc[other sources].