In the last article, we looked at what RPM packages are. They are archive files that contain files and metadata. When an RPM is installed or uninstalled, this metadata tells the RPM where to create or delete files. As you will remember in the last article, metadata also contains information about “dependencies,” which can be “run-time” or “build-time” dependency information.

For example, let’s look at fpaste. You can download the RPM using DNF. This will download the latest version of Fpaste available in the Fedora repository. On Fedora 30, the current version is 0.3.9.2:

$ dnf download fpaste ... Fpaste 0.3.9.2-2. Fc30. Noarch. RPMCopy the code

Since this is a build RPM, it contains only the files needed to use fpaste:

$RPM - QPL. / fpaste - 0.3.9.2-2. Fc30. Noarch. RPM/usr/bin/fpaste/usr/share/doc/fpaste/usr/share/doc/fpaste/README. RST /usr/share/doc/fpaste/TODO /usr/share/licenses/fpaste /usr/share/licenses/fpaste/COPYING /usr/share/man/man1/fpaste.1.gzCopy the code

The source RPM

The next link in the chain is the source RPM. All software in Fedora must be built from its source code. We do not include pre-built binaries. Therefore, to make an RPM file, the RPM (tool) needs to:

  • Give the files that must be installed,
  • For example, if you want to compile these files, tell them how to generate them,
  • Tell them where they must be installed,
  • What other dependencies are required for this particular software to work properly.

The source RPM has all this information. Source RPMS are similar to build RPMS, but as their name implies, they do not contain the built binaries, but rather the source files of a piece of software. Let’s download the source RPM for fpaste:

$ dnf download fpaste --source ... Fpaste 0.3.9.2-2. Fc30. SRC. RPMCopy the code

Note the end of the file is src.rpm. All RPMS are built from the source RPMS. You can also easily check the source RPMS of “binary” RPMS using DNF:

$ dnf repoquery --qf "%{SOURCERPM}"Fpaste fpaste 0.3.9.2-2. Fc30. SRC. RPMCopy the code

Also, since this is the source RPM, it does not contain the built files. Instead, it contains source code and instructions on how to build RPMS from it:

$RPM -qpl./fpaste-0.3.9.2-2.fc30.src. RPM fpaste-0.3.9.2.tar.gz fpasteCopy the code

Here, the first file is just the source code for Fpaste. The second is the spec file. The spec file is a recipe that tells RPM tools how to create RPMS using the source code contained in the source RPMS – it contains all the information RPM tools need to build RPMS. In the spec file. When we package maintainers add software to Fedora, we spend most of our time writing and refining spec files. When the package needs to be updated, we go back and adjust the spec file. You can be in src.fedoraproject.org/browse/proj… View the spec files for all the packages in Fedora in the source code repository.

Note that a single source RPM may contain instructions for building multiple RPMS. Fpaste is a very simple piece of software that produces a “binary” RPM from a source RPM. Python is more complicated. Although there is only one source RPM, it generates multiple binary RPMS:

$ sudo dnf repoquery --qf "%{SOURCERPM}"Python3 python3-3.7.3-1.fc30.src. RPM python3-3.7.4-1.fc30.src. RPM $sudo DNF repoQuery --qf"%{SOURCERPM}"Python3-devel python3-3.7.3-1.fc30.src. RPM python3-3.7.4-1.fc30.src. RPM $sudo DNF repoQuery --qf"%{SOURCERPM}"Python3-libs python3-3.7.3-1.fc30.src. RPM python3-3.7.4-1.fc30.src. RPM $sudo DNF repoQuery --qf"%{SOURCERPM}"Python3-idle python3-3.7.3-1.fc30.src. RPM python3-3.7.4-1.fc30.src. RPM $sudo DNF repoQuery --qf"%{SOURCERPM}"Python3 - tkinter python3 3.7.3-1. Fc30. SRC. RPM python3 3.7.4-1. Fc30. SRC. RPMCopy the code

In RPM lingo, “python3” is the “main package,” so the spec file will be called python3.spec. All other software packages are subpackages. You can download the source RPM of Python3 and view its contents. (Tip: patches are also part of the source code) :

$DNF download --source python3 python3-3.7.4-1.fc30.src. RPM $RPM -qpl./python3-3.7.4-1.fc30.src. RPM 00001-rpath.patch  00102-lib64.patch 00111-no-static-lib.patch 00155-avoid-ctypes-thunks.patch 00170-gc-assertions.patch 00178-dont-duplicate-flags-in-sysconfig.patch 00189-use-rpm-wheels.patch 00205-make-libpl-respect-lib64.patch 00251-change-user-install-location.patch 00274-fix-arch-names.patch 00316-mark-bdist_wininst-unsupported.patch Python-3.7.4.tar.xz check-pyc-timestamps. Py idle3.appdata. XML idle3.desktop python3.specCopy the code

Build RPMS from source RPMS

Now that we have the source RPM and what’s in it, we can rebuild the RPM from it. However, before doing this, we should set up the system to build RPMS. First, we install the necessary tools:

$ sudo dnf install fedora-packager
Copy the code

This will install the RPMBuild tool. Rpmbuild needs a default layout so that it knows the location of each required component in the source RPM. Let’s see what they are:

Where will the # spec file appear?
$ rpm -E %{_specdir}
/home/asinha/rpmbuild/SPECS

Where will the source code appear?
$ rpm -E %{_sourcedir}
/home/asinha/rpmbuild/SOURCES

Where is the temporary build directory?
$ rpm -E %{_builddir}
/home/asinha/rpmbuild/BUILD

Where is the build root?
$ rpm -E %{_buildrootdir}
/home/asinha/rpmbuild/BUILDROOT

Where will the source RPM be placed?
$ rpm -E %{_srcrpmdir}
/home/asinha/rpmbuild/SRPMS

Where will the RPMS built be placed?
$ rpm -E %{_rpmdir}
/home/asinha/rpmbuild/RPMS
Copy the code

I’ve set up all these directories on the system:

$ cdTree - L $1 rpmbuild/rpmbuild / ├ ─ ─ BUILD ├ ─ ─ BUILDROOT ├ ─ ─ RPMS ├ ─ ─ SOURCES ├ ─ ─ SPECS └ ─ ─ SRPMS 6 directories, filesCopy the code

RPM also provides a tool that has everything set up for you:

$ rpmdev-setuptree
Copy the code

Then, ensure that all of fpaste’s build dependencies are installed:

Sudo DNF builddep fpaste 0.3.9.2-3. Fc30. SRC. RPMCopy the code

For fpaste, all you need is Python, and it must already be installed on your system (DNF also uses Python). You can also give the builddep command a spec file instead of the source RPM. Learn more in the manual:

$ man dnf.plugin.builddep
Copy the code

Now that we have everything we need, building an RPM from the source is as simple as this:

$rpmbuild --rebuild fpaste-0.3.9.2-3.fc30.src.rpm.. . Tree ~ $/ rpmbuild/RPMS/noarch / / home/asinha/rpmbuild/RPMS/noarch / └ ─ ─ fpaste 0.3.9.2-3. Fc30. Noarch. RPM 0 directories, 1 fileCopy the code

Rpmbuild will install the source RPM and build your RPM from it. You can now use DNF to install RPM to use it. Of course, as mentioned earlier, if you want to make any changes in RPM, you’ll have to modify the spec file, which we’ll cover in the next article.

conclusion

To sum up this article, there are two points:

  • The RPMS we typically install are “binary” RPMS that contain the build version of the software
  • Build RPMS are derived from source RPMS, which include the source code and specification files needed to generate binary RPMS.

If you want to start building the RPM, and help the Fedora community we provide a lot of software maintenance, then you can start from here: fedoraproject.org/wiki/Join_t…

If you have any questions, please email the Fedora developer mailing list, and we’re always here to help!


Via: fedoramagazine.org/how-rpm-pac…

“FranciscoD” by Ankur Sinha, lujun9972

This article is originally compiled by LCTT and released in Linux China