Chapter 5. Preparing software for RPM packaging
To prepare a piece of software for packaging with RPM, you can first patch the software, create a LICENSE file for it, and archive it as a tarball.
5.1. Patching software Copy linkLink copied to clipboard!
When packaging software, you might need to make certain changes to the original source code, such as fixing a bug or changing a configuration file. In RPM packaging, you can instead leave the original source code intact and apply patches on it.
A patch is a piece of text that updates a source code file. The patch has a diff format, because it represents the difference between two versions of the text. You can create a patch by using the diff utility, and then apply the patch to the source code by using the patch utility.
Software developers often use version control systems such as Git to manage their code base. Such tools offer their own methods of creating diffs or patching software.
5.1.1. Creating a patch file for a sample C program Copy linkLink copied to clipboard!
Create a patch from the original source code of a sample Hello world program written in C (cello.c) by using the diff utility. You can then apply this patch to make changes to the packaged software without making changes to the original source code.
For more information, see the diff(1) man page on your system.
Prerequisites
You installed the
diffutility on your system:dnf install diffutils
# dnf install diffutilsCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Procedure
Back up the original source code:
cp -p cello.c cello.c.orig
$ cp -p cello.c cello.c.origCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
-poption preserves mode, ownership, and timestamps.Modify
cello.cas needed:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Generate a patch:
diff -Naur cello.c.orig cello.c
$ diff -Naur cello.c.orig cello.cCopy to Clipboard Copied! Toggle word wrap Toggle overflow Copy to Clipboard Copied! Toggle word wrap Toggle overflow Lines that start with
+replace the lines that start with-.NoteUsing the
Nauroptions with thediffcommand is recommended because it fits the majority of use cases:-N(--new-file)The
-Noption handles absent files as empty files.-a(--text)The
-aoption treats all files as text. As a result, thediffutility does not ignore the files it classified as binaries.-u(-U NUMor--unified[=NUM])The
-uoption returns output in the form of output NUM (default 3) lines of unified context. This is a compact and an easily readable format commonly used in patch files.-r(--recursive)The
-roption recursively compares any subdirectories that thediffutility found.
However, note that in this particular case, only the
-uoption is necessary.Save the patch to a file:
diff -Naur cello.c.orig cello.c > cello.patch
$ diff -Naur cello.c.orig cello.c > cello.patchCopy to Clipboard Copied! Toggle word wrap Toggle overflow Restore the original
cello.c:mv cello.c.orig cello.c
$ mv cello.c.orig cello.cCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantYou must retain the original
cello.cbecause the RPM package manager uses the original file, not the modified one, when building an RPM package. For more information, see Working with spec files.
5.1.2. Patching a sample C program Copy linkLink copied to clipboard!
Apply code patches to a sample Hello world program written in C (cello.c) by using the patch utility.
Prerequisites
You installed the
patchutility on your system:dnf install patch
# dnf install patchCopy to Clipboard Copied! Toggle word wrap Toggle overflow - You created a patch from the original source code. For instructions, see Creating a patch file for a sample C program.
Procedure
Redirect the patch file to the
patchcommand:patch < cello.patch
$ patch < cello.patchCopy to Clipboard Copied! Toggle word wrap Toggle overflow patching file cello.c
patching file cello.cCopy to Clipboard Copied! Toggle word wrap Toggle overflow Check that the contents of
cello.cnow reflect the desired change:cat cello.c
$ cat cello.cCopy to Clipboard Copied! Toggle word wrap Toggle overflow Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Build the patched
cello.cprogram:make
$ makeCopy to Clipboard Copied! Toggle word wrap Toggle overflow gcc -g -o cello cello.c
gcc -g -o cello cello.cCopy to Clipboard Copied! Toggle word wrap Toggle overflow Run the built
cello.cprogram:./cello
$ ./celloCopy to Clipboard Copied! Toggle word wrap Toggle overflow Hello World from my very first patch!
Hello World from my very first patch!Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.2. Creating a LICENSE file Copy linkLink copied to clipboard!
To enable appropriate software usage, create a license file containing the necessary copyright and legal information for your RPM package. For packaging purposes, you must also supply the license file with the software you are packaging.
It is recommended that you distribute your software with a software license. The software license file informs users of what they can and cannot do with a source code. Having no license for your source code means that you retain all rights to this code and no one can reproduce, distribute, or create derivative works from your source code.
Procedure
Create the
LICENSEfile with the required license statement:vim LICENSE
$ vim LICENSECopy to Clipboard Copied! Toggle word wrap Toggle overflow For example, the GPLv3
LICENSEfile has the following text:cat /tmp/LICENSE
$ cat /tmp/LICENSECopy to Clipboard Copied! Toggle word wrap Toggle overflow This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see \http://www.gnu.org/licenses/.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see \http://www.gnu.org/licenses/.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.3. Creating a source code archive for distribution Copy linkLink copied to clipboard!
To prepare your software for distribution, put your source code and necessary components into an archive file.
An archive file is a file with the .tar.gz or .tgz suffix. Putting source code into the archive is a common way to release the software to be later packaged for distribution.
5.3.1. Creating a source code archive for a sample Bash program Copy linkLink copied to clipboard!
To prepare a sample Bash program for distribution, put the source code and necessary components into a archive file.
The bello project is a Hello World file in Bash. The following example contains only the bello shell script. Therefore, the resulting tar.gz archive has only one file in addition to the LICENSE file.
The patch file is not distributed in the archive with the program. The RPM package manager applies the patch when the RPM is built. The patch will be placed into the ~/rpmbuild/SOURCES/ directory together with the tar.gz archive.
Prerequisites
-
Assume that the
0.1version of thebelloprogram is used. -
You created a
LICENSEfile. For instructions, see Creating a LICENSE file.
Procedure
Move all required files into a single directory:
mkdir bello-0.1
$ mkdir bello-0.1Copy to Clipboard Copied! Toggle word wrap Toggle overflow mv ~/bello bello-0.1/
$ mv ~/bello bello-0.1/Copy to Clipboard Copied! Toggle word wrap Toggle overflow mv LICENSE bello-0.1/
$ mv LICENSE bello-0.1/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the archive for distribution:
tar -cvzf bello-0.1.tar.gz bello-0.1
$ tar -cvzf bello-0.1.tar.gz bello-0.1Copy to Clipboard Copied! Toggle word wrap Toggle overflow bello-0.1/ bello-0.1/LICENSE bello-0.1/bello
bello-0.1/ bello-0.1/LICENSE bello-0.1/belloCopy to Clipboard Copied! Toggle word wrap Toggle overflow Move the created archive to the
~/rpmbuild/SOURCES/directory, which is the default directory where therpmbuildcommand stores the files for building packages:mv bello-0.1.tar.gz ~/rpmbuild/SOURCES/
$ mv bello-0.1.tar.gz ~/rpmbuild/SOURCES/Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.3.2. Creating a source code archive for a sample Python program Copy linkLink copied to clipboard!
To prepare a sample Python program for distribution, put the source code and necessary components into a archive file.
The pello project is a Hello World file in Python. The following example contains only the pello.py program. Therefore, the resulting tar.gz archive has only one file in addition to the LICENSE file.
The patch file is not distributed in the archive with the program. The RPM package manager applies the patch when the RPM is built. The patch will be placed into the ~/rpmbuild/SOURCES/ directory together with the tar.gz archive.
Prerequisites
-
Assume that the
0.1.1version of thepelloprogram is used. -
You created a
LICENSEfile. For instructions, see Creating a LICENSE file.
Procedure
Move all required files into a single directory:
mkdir pello-0.1.1
$ mkdir pello-0.1.1Copy to Clipboard Copied! Toggle word wrap Toggle overflow mv pello.py pello-0.1.1/
$ mv pello.py pello-0.1.1/Copy to Clipboard Copied! Toggle word wrap Toggle overflow mv LICENSE pello-0.1.1/
$ mv LICENSE pello-0.1.1/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the archive for distribution:
tar -cvzf pello-0.1.1.tar.gz pello-0.1.1
$ tar -cvzf pello-0.1.1.tar.gz pello-0.1.1Copy to Clipboard Copied! Toggle word wrap Toggle overflow pello-0.1.1/ pello-0.1.1/LICENSE pello-0.1.1/pello.py
pello-0.1.1/ pello-0.1.1/LICENSE pello-0.1.1/pello.pyCopy to Clipboard Copied! Toggle word wrap Toggle overflow Move the created archive to the
~/rpmbuild/SOURCES/directory, which is the default directory where therpmbuildcommand stores the files for building packages:mv pello-0.1.1.tar.gz ~/rpmbuild/SOURCES/
$ mv pello-0.1.1.tar.gz ~/rpmbuild/SOURCES/Copy to Clipboard Copied! Toggle word wrap Toggle overflow
5.3.3. Creating a source code archive for a sample C program Copy linkLink copied to clipboard!
To prepare a sample C program for distribution, put the source code and necessary components into a archive file.
The cello project is a Hello World file in C. The following example contains only the cello.c and the Makefile files. Therefore, the resulting tar.gz archive has two files in addition to the LICENSE file.
The patch file is not distributed in the archive with the program. The RPM package manager applies the patch when the RPM is built. The patch will be placed into the ~/rpmbuild/SOURCES/ directory together with the tar.gz archive.
Prerequisites
-
Assume that the
1.0version of thecelloprogram is used. -
You created a
LICENSEfile. For instructions, see Creating a LICENSE file.
Procedure
Move all required files into a single directory:
mkdir cello-1.0
$ mkdir cello-1.0Copy to Clipboard Copied! Toggle word wrap Toggle overflow mv cello.c cello-1.0/
$ mv cello.c cello-1.0/Copy to Clipboard Copied! Toggle word wrap Toggle overflow mv Makefile cello-1.0/
$ mv Makefile cello-1.0/Copy to Clipboard Copied! Toggle word wrap Toggle overflow mv LICENSE cello-1.0/
$ mv LICENSE cello-1.0/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the archive for distribution:
tar -cvzf cello-1.0.tar.gz cello-1.0
$ tar -cvzf cello-1.0.tar.gz cello-1.0Copy to Clipboard Copied! Toggle word wrap Toggle overflow cello-1.0/ cello-1.0/Makefile cello-1.0/cello.c cello-1.0/LICENSE
cello-1.0/ cello-1.0/Makefile cello-1.0/cello.c cello-1.0/LICENSECopy to Clipboard Copied! Toggle word wrap Toggle overflow Move the created archive to the
~/rpmbuild/SOURCES/directory, which is the default directory where therpmbuildcommand stores the files for building packages:mv cello-1.0.tar.gz ~/rpmbuild/SOURCES/
$ mv cello-1.0.tar.gz ~/rpmbuild/SOURCES/Copy to Clipboard Copied! Toggle word wrap Toggle overflow