第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 リンクのコピーリンクがクリップボードにコピーされました!
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 リンクのコピーリンクがクリップボードにコピーされました!
You can create a patch from the original source code by using the diff utility. For example, to patch a Hello world program written in C (cello.c), complete the following steps.
Prerequisites
You installed the
diffutility on your system:# dnf install diffutils
Procedure
Back up the original source code:
$ cp -p cello.c cello.c.origThe
-poption preserves mode, ownership, and timestamps.Modify
cello.cas needed:#include <stdio.h> int main(void) { printf("Hello World from my very first patch!\n"); return 0; }Generate a patch:
$ diff -Naur cello.c.orig cello.c --- cello.c.orig 2016-05-26 17:21:30.478523360 -0500 + cello.c 2016-05-27 14:53:20.668588245 -0500 @@ -1,6 +1,6 @@ #include<stdio.h> int main(void){ - printf("Hello World!\n"); + printf("Hello World from my very first patch!\n"); return 0; } \ No newline at end of fileLines that start with
+replace the lines that start with-.注記Using 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.patchRestore the original
cello.c:$ mv cello.c.orig cello.c重要You 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 リンクのコピーリンクがクリップボードにコピーされました!
To apply code patches on your software, you can use the patch utility.
Prerequisites
You installed the
patchutility on your system:# dnf install patch- You created a patch from the original source code. For instructions, see Creating a patch file for a sample C program.
Procedure
The following steps apply a previously created cello.patch file on the cello.c file.
Redirect the patch file to the
patchcommand:$ patch < cello.patch patching file cello.cCheck that the contents of
cello.cnow reflect the desired change:$ cat cello.c #include<stdio.h> int main(void){ printf("Hello World from my very first patch!\n"); return 1; }
Verification
Build the patched
cello.cprogram:$ make gcc -g -o cello cello.cRun the built
cello.cprogram:$ ./cello Hello World from my very first patch!