RPM 包是如何从源 RPM 制作的
RPM
![](/data/attachment/album/201911/02/093550p3t13v3sxif3vx73.jpg)


在[上一篇文章中，我们研究了什么是 RPM 软件包](/article-11452-1.html)。它们是包含文件和元数据的档案文件。当安装或卸载 RPM 时，此元数据告诉 RPM 在哪里创建或删除文件。正如你将在上一篇文章中记住的，元数据还包含有关“依赖项”的信息，它可以是“运行时”或“构建时”的依赖信息。


例如，让我们来看看 `fpaste`。你可以使用 `dnf` 下载该 RPM。这将下载 Fedora 存储库中可用的 `fpaste` 最新版本。在 Fedora 30 上，当前版本为 0.3.9.2：



```
$ dnf download fpaste

...
fpaste-0.3.9.2-2.fc30.noarch.rpm
```

由于这是个构建 RPM，因此它仅包含使用 `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.gz
```

### 源 RPM


在此链条中的下一个环节是源 RPM。Fedora 中的所有软件都必须从其源代码构建。我们不包括预构建的二进制文件。因此，要制作一个 RPM 文件，RPM（工具）需要：


* 给出必须要安装的文件，
* 例如，如果要编译出这些文件，则告诉它们如何生成这些文件，
* 告知必须在何处安装这些文件，
* 该特定软件需要其他哪些依赖才能正常工作。


源 RPM 拥有所有这些信息。源 RPM 与构建 RPM 相似，但顾名思义，它们不包含已构建的二进制文件，而是包含某个软件的源文件。让我们下载 `fpaste` 的源 RPM：



```
$ dnf download fpaste --source

...
fpaste-0.3.9.2-2.fc30.src.rpm
```

注意文件的结尾是 `src.rpm`。所有的 RPM 都是从源 RPM 构建的。你也可以使用 `dnf` 轻松检查“二进制” RPM 的源 RPM：



```
$ dnf repoquery --qf "%{SOURCERPM}" fpaste
fpaste-0.3.9.2-2.fc30.src.rpm
```

另外，由于这是源 RPM，因此它不包含构建的文件。相反，它包含有关如何从中构建 RPM 的源代码和指令：



```
$ rpm -qpl ./fpaste-0.3.9.2-2.fc30.src.rpm
fpaste-0.3.9.2.tar.gz
fpaste.spec
```

这里，第一个文件只是 `fpaste` 的源代码。第二个是 spec 文件。spec 文件是个配方，可告诉 RPM（工具）如何使用源 RPM 中包含的源代码创建 RPM（档案文件）— 它包含 RPM（工具）构建 RPM（档案文件）所需的所有信息。在 spec 文件中。当我们软件包维护人员添加软件到 Fedora 中时，我们大部分时间都花在编写和完善 spec 文件上。当软件包需要更新时，我们会回过头来调整 spec 文件。你可以在 <https://src.fedoraproject.org/browse/projects/> 的源代码存储库中查看 Fedora 中所有软件包的 spec 文件。


请注意，一个源 RPM 可能包含构建多个 RPM 的说明。`fpaste` 是一款非常简单的软件，一个源 RPM 生成一个“二进制” RPM。而 Python 则更复杂。虽然只有一个源 RPM，但它会生成多个二进制 RPM：



```
$ 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.rpm
```

用 RPM 行话来讲，“python3” 是“主包”，因此该 spec 文件将称为 `python3.spec`。所有其他软件包均为“子软件包”。你可以下载 python3 的源 RPM，并查看其中的内容。（提示：补丁也是源代码的一部分）：



```
$ 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.spec
```

### 从源 RPM 构建 RPM


现在我们有了源 RPM，并且其中有什么内容，我们可以从中重建 RPM。但是，在执行此操作之前，我们应该设置系统以构建 RPM。首先，我们安装必需的工具：



```
$ sudo dnf install fedora-packager
```

这将安装 `rpmbuild` 工具。`rpmbuild` 需要一个默认布局，以便它知道源 RPM 中每个必需组件的位置。让我们看看它们是什么：



```
# spec 文件将出现在哪里？
$ rpm -E %{_specdir}
/home/asinha/rpmbuild/SPECS

# 源代码将出现在哪里？
$ rpm -E %{_sourcedir}
/home/asinha/rpmbuild/SOURCES

# 临时构建目录是哪里？
$ rpm -E %{_builddir}
/home/asinha/rpmbuild/BUILD

# 构建根目录是哪里？
$ rpm -E %{_buildrootdir}
/home/asinha/rpmbuild/BUILDROOT

# 源 RPM 将放在哪里？
$ rpm -E %{_srcrpmdir}
/home/asinha/rpmbuild/SRPMS

# 构建的 RPM 将放在哪里？
$ rpm -E %{_rpmdir}
/home/asinha/rpmbuild/RPMS
```

我已经在系统上设置了所有这些目录：



```
$ cd
$ tree -L 1 rpmbuild/
rpmbuild/
├── BUILD
├── BUILDROOT
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS

6 directories, 0 files
```

RPM 还提供了一个为你全部设置好的工具：



```
$ rpmdev-setuptree
```

然后，确保已安装 `fpaste` 的所有构建依赖项：



```
sudo dnf builddep fpaste-0.3.9.2-3.fc30.src.rpm
```

对于 `fpaste`，你只需要 Python，并且它肯定已经安装在你的系统上（`dnf` 也使用 Python）。还可以给 `builddep` 命令一个 spec 文件，而不是源 RPM。在手册页中了解更多信息：



```
$ man dnf.plugin.builddep
```

现在我们有了所需的一切，从源 RPM 构建一个 RPM 就像这样简单：



```
$ 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 file
```

`rpmbuild` 将安装源 RPM 并从中构建你的 RPM。现在，你可以使用 `dnf` 安装 RPM 以使用它。当然，如前所述，如果你想在 RPM 中进行任何更改，则必须修改 spec 文件，我们将在下一篇文章中介绍 spec 文件。


### 总结


总结一下这篇文章有两点：


* 我们通常安装使用的 RPM 是包含软件的构建版本的 “二进制” RPM
* 构建 RPM 来自于源 RPM，源 RPM 包括用于生成二进制 RPM 所需的源代码和规范文件。


如果你想开始构建 RPM，并帮助 Fedora 社区维护我们提供的大量软件，则可以从这里开始： <https://fedoraproject.org/wiki/Join_the_package_collection_maintainers>


如有任何疑问，请发邮件到 [Fedora 开发人员邮件列表](https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/)，我们随时乐意为你提供帮助！




---


via: <https://fedoramagazine.org/how-rpm-packages-are-made-the-source-rpm/>


作者：[Ankur Sinha FranciscoD](https://fedoramagazine.org/author/ankursinha/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In a [previous post, we looked at what RPM packages are](https://fedoramagazine.org/rpm-packages-explained/). They are archives that contain files and metadata. This metadata tells RPM where to create or remove files from when an RPM is installed or uninstalled. The metadata also contains information on “dependencies”, which you will remember from the previous post, can either be “runtime” or “build time”.

As an example, we will look at *fpaste*. You can download the RPM using *dnf*. This will download the latest version of *fpaste* that is available in the Fedora repositories. On Fedora 30, this is currently 0.3.9.2:

$ dnf download fpaste ... fpaste-0.3.9.2-2.fc30.noarch.rpm

Since this is the built RPM, it contains only 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.gz

## Source RPMs

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. So, for an RPM file to be made, RPM (the tool) needs to be:

- given the files that have to be installed,
- told how to generate these files, if they are to be compiled, for example,
- told where these files must be installed,
- what other dependencies this particular software needs to work properly.

The source RPM holds all of this information. Source RPMs are similar archives to RPM, but as the name suggests, instead of holding the built binary files, they contain the source files for a piece of software. Let’s download the source RPM for *fpaste*:

$ dnf download fpaste --source ... fpaste-0.3.9.2-2.fc30.src.rpm

Notice how the file ends with “src.rpm”. All RPMs are built from source RPMs. You can easily check what source RPM a “binary” RPM comes from using dnf too:

$ dnf repoquery --qf "%{SOURCERPM}" fpaste fpaste-0.3.9.2-2.fc30.src.rpm

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

$ rpm -qpl ./fpaste-0.3.9.2-2.fc30.src.rpm fpaste-0.3.9.2.tar.gz fpaste.spec

Here, the first file is simply the source code for *fpaste*. The second is the “spec” file. The spec file is the recipe that tells RPM (the tool) how to create the RPM (the archive) using the sources contained in the source RPM—all the information that RPM (the tool) needs to build RPMs (the archives) are contained in spec files. When we package maintainers add software to Fedora, most of our time is spent writing and perfecting the individual spec files. When a software package needs an update, we go back and tweak the spec file. You can see the spec files for ALL packages in Fedora at our source repository at [https://src.fedoraproject.org/browse/projects/](https://src.fedoraproject.org/browse/projects/)

Note that one source RPM may contain the instructions to build multiple RPMs. *fpaste* is a very simple piece of software, where one source RPM generates one “binary” RPM. Python, on the other hand is more complex. While 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.rpm

In RPM jargon, “python3” is the “main package”, and so the spec file will be called “python3.spec”. All the other packages are “sub-packages”. You can download the source RPM for python3 and see what’s in it too. (Hint: 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.spec

## Building an RPM from a source RPM

Now that we have the source RPM, and know what’s in it, we can rebuild our RPM from it. Before we do so, though, we should set our system up to build RPMs. First, we install the required tools:

$ sudo dnf install fedora-packager

This will install the rpmbuild tool. rpmbuild requires a default layout so that it knows where each required component of the source rpm is. Let’s see what they are:

# Where should the spec file go? $ rpm -E %{_specdir} /home/asinha/rpmbuild/SPECS # Where should the sources go? $ rpm -E %{_sourcedir} /home/asinha/rpmbuild/SOURCES # Where is temporary build directory? $ rpm -E %{_builddir} /home/asinha/rpmbuild/BUILD # Where is the buildroot? $ rpm -E %{_buildrootdir} /home/asinha/rpmbuild/BUILDROOT # Where will the source rpms be? $ rpm -E %{_srcrpmdir} /home/asinha/rpmbuild/SRPMS # Where will the built rpms be? $ rpm -E %{_rpmdir} /home/asinha/rpmbuild/RPMS

I have all of this set up on my system already:

$ cd $ tree -L 1 rpmbuild/ rpmbuild/ ├── BUILD ├── BUILDROOT ├── RPMS ├── SOURCES ├── SPECS └── SRPMS 6 directories, 0 files

RPM provides a tool that sets it all up for you too:

$ rpmdev-setuptree

Then we ensure that we have all the build dependencies for *fpaste* installed:

sudo dnf builddep fpaste-0.3.9.2-3.fc30.src.rpm

For *fpaste* you only need Python and that must already be installed on your system (dnf uses Python too). The builddep command can also be given a spec file instead of an source RPM. Read more in the man page:

$ man dnf.plugin.builddep

Now that we have all that we need, building an RPM from a source RPM is as simple as:

$ 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 file

rpmbuild will install the source RPM and build your RPM from it. You can now install the RPM to use it as you do–using dnf. Of course, as said before, if you want to change anything in the RPM, you must modify the spec file—we’ll cover spec files in next post.

## Summary

To summarise this post in two short points:

- the RPMs we generally install to use software are “binary” RPMs that contain built versions of the software
- these are built from source RPMs that include the source code and the spec file that are needed to generate the binary RPMs.

If you’d like to get started with building RPMs, and help the Fedora community maintain the massive amount of software we provide, you can start here: [https://fedoraproject.org/wiki/Join_the_package_collection_maintainers](https://fedoraproject.org/wiki/Join_the_package_collection_maintainers)

For any queries, post to the [Fedora developers mailing list](https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/)—we’re always happy to help!

## Silvia Sanchez

The banner image makes me so happy… that’s the Stadtbibliothek Stuttgart!

Lovely. Thanks.

Great article, by the way.

## Kees de Jong

Excellent!

## tr

brew are easy,

meybe in future somebody wrote converter brew ruby prog to rpm

## jackorp

You can make rubygem RPMs very easily with “gem2rpm” utility which basically converts gemspec to RPM spec.

## Adam Miller

There’s gem2rpm. https://github.com/fedora-ruby/gem2rpm

## jackorp

We have an even better tool 🙂 , gem2rpm, converting source gemspec into RPM spec file

## MX

man mock

## Leandro

Great article! Thanks!!

## Adam Miller

Here’s a comprehensive intro packaging guide if anyone is interested.

https://rpm-packaging-guide.github.io/

## leslie Satenstein

Hi Ankur

Thank you for this and the provious articles. I have a favour to ask.

Is there any chance that you can put the previous articles and this one together as a downloadable PDF file. Your expository is most informative. I do like

“under-the-cover” articles explaining important features of package management.

I tried with cut/paste to Libreoffice, but sections of the pasted output have very unappealing results within LibreOffice’s writer program.

## Paul W. Frields

Just print to PDF from your browser. The results look pretty good from what I can see.