Chapter 4. 简单例子

Table of Contents

4.1. Packaging tarball
4.2. 大致流程
4.3. 什么是 debmake?
4.4. 什么是 debuild?
4.5. 第一步:获取上游源代码
4.6. 第二步:使用 debmake 产生模板文件
4.7. 第三步:编辑模板文件
4.8. 第四步:使用 debuild 构建软件包
4.9. 第三步(备选):修改上游源代码
4.9.1. 使用 diff -u 处理补丁
4.9.2. 使用 dquilt 处理补丁
4.9.3. 使用 dpkg-source --commit 处理补丁

有一句古罗马谚语说得好:“Longum iter est per praecepta, breve et efficax per exempla”(“一例胜千言!”)。

这里给出了从简单的 C 语言源代码创建简单的 Debian 软件包的例子,并假设上游使用了 Makefile 作为构建系统。

我们假设上游源码压缩包(tarball)名称为 debhello-0.0.tar.gz

这一类源代码设计可以用这样的方式安装成为非系统文件:

 $ tar -xzmf debhello-0.0.tar.gz
 $ cd debhello-0.0
 $ make
 $ make install

Debian 的打包需要对“make install”流程进行改变,从而将文件安装至目标系统镜像所在位置,而非通常使用的 /usr/local 下的位置。

[Note] Note

在其它更加复杂的构建系统下构建 Debian 软件包的例子可以在 Chapter 8, 更多示例 找到。

从上游源码压缩包 debhello-0.0.tar.gz 构建单个非原生 Debian 软件包的大致流程可以总结如下:

  • 维护者获取上游源码压缩包 debhello-0.0.tar.gz 并将其内容解压缩至 debhello-0.0 目录中。
  • debmake 命令对上游源码树进行 debian 化(debianize),具体来说,是创建一个 debian 目录并仅向该目录中添加各类模板文件。

    • 名为 debhello_0.0.orig.tar.gz 的符号链接被创建并指向 debhello-0.0.tar.gz 文件。
    • 维护者须自行编辑修改模板文件。
  • debuild 命令基于已 debian 化的源码树构建二进制软件包。

    • debhello-0.0-1.debian.tar.xz 将被创建,它包含了 debian 目录。

软件包构建的大致流程. 

 $ tar -xzmf debhello-0.0.tar.gz
 $ cd debhello-0.0
 $ debmake
   ... manual customization
 $ debuild
   ...

[Tip] Tip

The debuild command in this and following examples may be substituted by equivalent commands such as the sbuild command.

[Tip] Tip

如果上游源码压缩包提供了 .tar.xz 格式文件,请使用这样的压缩包来替代 .tar.gz.tar.bz2 格式。xz 压缩与 gzipbzip2 压缩相比提供了更好的压缩比。

文中的 debmake 命令是用于 Debian 打包的一个帮助脚本。

  • 它总是将大多数选项的状态与参数设置为合理的默认值。
  • 它能产生上游源码包,并按需创建所需的符号链接。
  • 它不会覆写 debian/ 目录下已存在的配置文件。
  • 它支持多架构(multiarch)软件包。
  • 它能创建良好的模板文件,例如符合 DEP-5debian/copyright 文件。

这些特性使得使用 debmake 进行 Debian 打包工作变得简单而现代化。

In retrospective, I created debmake to simplify this documentation. I consider debmake to be more-or-less a demonstration session generator for tutorial purpose.

[Note] Note

Many packages are packaged using only a text editor while imitating how other similar packages are packaged and consulting how the Debian policy requires us to do. This seems to me the most popular method for the real-life packaging activity.

The debmake command isn’t the only helper script to make a Debian package. If you are interested alternative packaging helper tools, please see:

这里给出与 debuild 命令类似的一系列命令的一个汇总。

[Note] Note

如需了解详细内容,请见 dpkg-buildpackage(1)。

我们先要获取上游源代码。

下载 debhello-0.0.tar.gz

 $ wget http://www.example.org/download/debhello-0.0.tar.gz
 ...
 $ tar -xzf debhello-0.0.tar.gz
 $ tree
.
├── debhello-0.0
│   ├── LICENSE
│   ├── Makefile
│   └── src
│       └── hello.c
└── debhello-0.0.tar.gz

2 directories, 4 files

这里的 C 源代码 hello.c 非常的简单。

hello.c

 $ cat debhello-0.0/src/hello.c
#include <stdio.h>
int
main()
{
        printf("Hello, world!\n");
        return 0;
}

这里,源码中的 Makefile 支持 GNU 编码标准FHS(文件系统层级规范)。特别地:

  • 构建二进制程序时会考虑 $(CPPFLAGS)$(CFLAGS)$(LDFLAGS),等等。
  • 安装文件时采纳 $(DESTDIR) 作为目标系统镜像的路径前缀
  • 安装文件时使用 $(prefix) 的值,以便我们将其设置覆盖为 /usr

Makefile

 $ cat debhello-0.0/Makefile
prefix = /usr/local

all: src/hello

src/hello: src/hello.c
        @echo "CFLAGS=$(CFLAGS)" | \
                fold -s -w 70 | \
                sed -e 's/^/# /'
        $(CC) $(CPPFLAGS) $(CFLAGS) $(LDCFLAGS) -o $@ $^

install: src/hello
        install -D src/hello \
                $(DESTDIR)$(prefix)/bin/hello

clean:
        -rm -f src/hello

distclean: clean

uninstall:
        -rm -f $(DESTDIR)$(prefix)/bin/hello

.PHONY: all install clean distclean uninstall

[Note] Note

$(CFLAGS)echo 命令用于在接下来的例子中验证所设置的构建参数。

[Tip] Tip

如果 debmake 命令调用时使用了 -T 选项,程序将为模板文件产生更加详细的注释内容。

debmake 命令的输出十分详细,如下所示,它可以展示程序的具体操作内容。

 $ cd debhello-0.0
 $ debmake
I: set parameters
I: =================================================================
I: package_dir     = /usr/lib/python3/dist-packages
I: base_path       = /usr
I: base_lib_path   = /usr/lib/debmake
I: base_share_path = /usr/share/debmake
I: =================================================================
I: sanity check of parameters
I: pkg="debhello", ver="0.0", rev="1"
I: *** start packaging in "debhello-0.0". ***
I: provide debhello_0.0.orig.tar.gz for non-native Debian package
I: pwd = "/path/to"
I: $ ln -sf debhello-0.0.tar.gz debhello_0.0.orig.tar.gz
I: pwd = "/path/to/debhello-0.0"
I: parse binary package settings:
I: binary package=debhello Type=bin / Arch=any M-A=foreign
I: analyze the source tree
I: build_type = make
I: scan source for copyright+license text and file extensions
I: 100 %, ext = c
I: check_all_licenses
I: ..
I: check_all_licenses completed for 2 files.
I: bunch_all_licenses
I: format_all_licenses
I: make debian/* template files
I: single binary package
I: debmake -x "1" ...
I: creating => debian/control
I: creating => debian/copyright
I: substituting => /usr/share/debmake/extra0/changelog
I: creating => debian/changelog
I: substituting => /usr/share/debmake/extra0/rules
I: creating => debian/rules
I: substituting => /usr/share/debmake/extra1/README.Debian
I: creating => debian/README.Debian
I: substituting => /usr/share/debmake/extra1/watch
I: creating => debian/watch
I: substituting => /usr/share/debmake/extra1source/format
I: creating => debian/source/format
I: substituting => /usr/share/debmake/extra1tests/control
I: creating => debian/source/control
I: substituting => /usr/share/debmake/extra1upstream/metadata
I: creating => debian/upstream/metadata
I: substituting => /usr/share/debmake/extra1tests/control
I: creating => debian/tests/control
I: substituting => /usr/share/debmake/extra1patches/series
I: creating => debian/patches/series
I: substituting => /usr/share/debmake/extra1sourcex/local-options
I: creating => debian/source/local-options
I: substituting => /usr/share/debmake/extra1sourcex/options
I: creating => debian/source/options
I: substituting => /usr/share/debmake/extra1sourcex/patch-header
I: creating => debian/source/patch-header
I: run "debmake -x2" to get more template files
I: $ wrap-and-sort

debmake 命令基于命令行选项产生所有这些模板文件。如果没有指定具体选项,debmake 命令将为您自动选择合理的默认值:

  • 源码包名称:debhello
  • 上游版本:0.0
  • 二进制软件包名称:debhello
  • Debian 修订版本:1
  • 软件包类型: bin(ELF 二进制可执行程序软件包)
  • -x 选项:-x1(是单个二进制软件包的默认值)

我们来检查一下自动产生的模板文件。

基本 debmake 命令运行后的源码树。. 

 $ cd ..
 $ tree
.
├── debhello-0.0
│   ├── LICENSE
│   ├── Makefile
│   ├── debian
│   │   ├── README.Debian
│   │   ├── changelog
│   │   ├── control
│   │   ├── copyright
│   │   ├── patches
│   │   │   └── series
│   │   ├── rules
│   │   ├── source
│   │   │   ├── control
│   │   │   ├── format
│   │   │   ├── local-options
│   │   │   ├── options
│   │   │   └── patch-header
│   │   ├── tests
│   │   │   └── control
│   │   ├── upstream
│   │   │   └── metadata
│   │   └── watch
│   └── src
│       └── hello.c
├── debhello-0.0.tar.gz
└── debhello_0.0.orig.tar.gz -> debhello-0.0.tar.gz

7 directories, 19 files

这里的 debian/rules 文件是应当由软件包维护者提供的构建脚本。此时该文件是由 debmake 命令产生的模板文件。

debian/rules(模板文件):. 

 $ cat debhello-0.0/debian/rules
#!/usr/bin/make -f
# You must remove unused comment lines for the released package.
#export DH_VERBOSE = 1
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

%:
        dh $@

#override_dh_auto_install:
#       dh_auto_install -- prefix=/usr

#override_dh_install:
#       dh_install --list-missing -X.pyc -X.pyo

这便是使用 dh 命令时标准的 debian/rules 文件。(某些内容已被注释,可供后续修改使用。)

这里的 debian/control 文件提供了 Debian 软件包的主要元信息。此时该文件是由 debmake 命令产生的模板文件。

debian/control(模板文件):. 

 $ cat debhello-0.0/debian/control
Source: debhello
Section: unknown
Priority: optional
Maintainer: "Firstname Lastname" <email.address@example.org>
Build-Depends: debhelper-compat (= 13)
Standards-Version: 4.5.1
Homepage: <insert the upstream URL, if relevant>
Rules-Requires-Root: no

Package: debhello
Architecture: any
Multi-Arch: foreign
Depends: ${misc:Depends}, ${shlibs:Depends}
Description: auto-generated package by debmake
 This Debian binary package was auto-generated by the
 debmake(1) command provided by the debmake package.

[Warning] Warning

如果您对 debian/control 模板文件中的“Section: unknown”部分不做修改的话,后续的 lintian 错误可能导致构建失败。

因为这是个 ELF 二进制可执行文件软件包,debmake 命令设置了“Architecture: any”和“Multi-Arch: foreign”两项。同时,它将所需的 substvar 参数设置为“Depends: ${shlibs:Depends}, ${misc:Depends}”。这些内容将在 Chapter 5, 基本内容 部分进行解释。

[Note] Note

请注意这个 debian/control 按照“Debian政策手册”中 5.2 Source package control files — debian/control 的内容使用 RFC-822 风格进行编写。文件对空行和行首空格的使用有特别的要求。

这里的 debian/copyright 提供了 Debian 软件包版权数据的总结。此时该文件是由 debmake 命令产生的模板文件。

debian/copyright(模板文件):. 

 $ cat debhello-0.0/debian/copyright
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: debhello
Upstream-Contact: <preferred name and address to reach the upstream project>
Source: <url://example.com>
#
# Please double check copyright with the licensecheck(1) command.

Files:     Makefile
           src/hello.c
Copyright: __NO_COPYRIGHT_NOR_LICENSE__
License:   __NO_COPYRIGHT_NOR_LICENSE__

#----------------------------------------------------------------------------...
# Files marked as NO_LICENSE_TEXT_FOUND may be covered by the following
# license/copyright files.

#----------------------------------------------------------------------------...
# License file: LICENSE
 License:
 .
 All files in this archive are licensed under the MIT License as below.
 .
 Copyright 2015 Osamu Aoki <osamu@debian.org>
 .
 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
 to deal in the Software without restriction, including without limitation
 the rights to use, copy, modify, merge, publish, distribute, sublicense,
 and/or sell copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following conditions:
 .
 The above copyright notice and this permission notice shall be included
 in all copies or substantial portions of the Software.
 .
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

作为维护者,要制作一个合适的 Debian 软件包当然需要对模板内容进行一些手工的调整。

为了将安装文件变成系统文件的一部分,Makefile 文件中 $(prefix) 默认的 /usr/local 的值需要被覆盖为 /usr。要做到这点,可以按照下面给出的方法,在 debian/rules 文件中添加名为 override_dh_auto_install 的目标,在其中设置“prefix=/usr”。

debian/rules(维护者版本):. 

 $ vim debhello-0.0/debian/rules
 ... hack, hack, hack, ...
 $ cat debhello-0.0/debian/rules
#!/usr/bin/make -f
export DH_VERBOSE = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

%:
        dh $@

override_dh_auto_install:
        dh_auto_install -- prefix=/usr

如上在 debian/rules 文件中导出=DH_VERBOSE 环境变量可以强制 debhelper 工具输出细粒度的构建报告。

如上导出 DEB_BUILD_MAINT_OPTION 变量可以如 dpkg-buildflags(1) 手册页中“FEATURE AREAS/ENVIRONMENT”部分所说,对加固选项进行设置。[9]

如上导出 DEB_CFLAGS_MAINT_APPEND 可以强制 C 编译器给出所有类型的警告内容。

如上导出 DEB_LDFLAGS_MAINT_APPEND 可以强制链接器只对真正需要的库进行链接。[10]

对基于 Makefile 的构建系统来说,dh_auto_install 命令所做的基本上就是“$(MAKE) install DESTDIR=debian/debhello”。这里创建的 override_dh_auto_install 目标将其行为修改为“$(MAKE) install DESTDIR=debian/debhello prefix=/usr”。

这里是维护者版本的 debian/controldebian/copyright 文件。

debian/control(维护者版本):. 

 $ vim debhello-0.0/debian/control
 ... hack, hack, hack, ...
 $ cat debhello-0.0/debian/control
Source: debhello
Section: devel
Priority: optional
Maintainer: Osamu Aoki <osamu@debian.org>
Build-Depends: debhelper-compat (= 13)
Standards-Version: 4.5.1
Homepage: https://salsa.debian.org/debian/debmake-doc
Rules-Requires-Root: no

Package: debhello
Architecture: any
Multi-Arch: foreign
Depends: ${misc:Depends}, ${shlibs:Depends}
Description: Simple packaging example for debmake
 This Debian binary package is an example package.
 (This is an example only)

debian/copyright(维护者版本):. 

 $ vim debhello-0.0/debian/copyright
 ... hack, hack, hack, ...
 $ cat debhello-0.0/debian/copyright
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: debhello
Upstream-Contact: Osamu Aoki <osamu@debian.org>
Source: https://salsa.debian.org/debian/debmake-doc

Files:     *
Copyright: 2015-2021 Osamu Aoki <osamu@debian.org>
License:   Expat
 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
 to deal in the Software without restriction, including without limitation
 the rights to use, copy, modify, merge, publish, distribute, sublicense,
 and/or sell copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following conditions:
 .
 The above copyright notice and this permission notice shall be included
 in all copies or substantial portions of the Software.
 .
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

debian/ 目录下还有一些其它的模板文件。它们也需要进行更新。

debian/. 下面的模板文件(0.0 版):. 

 $ tree debhello-0.0/debian
debhello-0.0/debian
├── README.Debian
├── changelog
├── control
├── copyright
├── patches
│   └── series
├── rules
├── source
│   ├── control
│   ├── format
│   ├── local-options
│   ├── options
│   └── patch-header
├── tests
│   └── control
├── upstream
│   └── metadata
└── watch

4 directories, 14 files

[Tip] Tip

对于来自 debhelper 软件包的各个 dh_* 命令来说,它们在读取所使用的配置文件时通常把以 # 开头的行视为注释行。

您可以使用 debuild 或者等效的命令工具(参见 Section 4.4, “什么是 debuild?”)在这个源码树内构建一个非原生 Debian 软件包。命令的输出通常十分详细,如下所示,它会对构建中执行的操作进行解释。

 $ cd debhello-0.0
 $ debuild
 dpkg-buildpackage -us -uc -ui -i -i
 ...
 debian/rules clean
dh clean
 ...
 debian/rules binary
dh binary
   dh_update_autotools_config
   dh_autoreconf
   dh_auto_configure
        install -d /path/to/debhello-0.0/debian/.debhelper/generated/_source/...
   dh_auto_build
        make -j12 "INSTALL=install --strip-program=true"
make[1]: Entering directory '/path/to/debhello-0.0'
# CFLAGS=-g -O2
# -ffile-prefix-map=/home/osamu/src/public/debmake-doc/debmake-doc/examp
 ...
Now running lintian -i -I --show-overrides debhello_0.0-1_amd64.changes ...
 ...
N:   Renamed from: binary-without-manpage
N:
W: debhello: readme-debian-contains-debmake-template
N:
 ...

这里验证了 CFLAGS 已经得到了更新,添加了 -Wall-pendatic 参数;这是我们先前在 DEB_CFLAGS_MAINT_APPEND 变量中所指定的。

根据 lintian 的报告,您应该如同后文中的例子那样(请见 Chapter 8, 更多示例)为软件包添加 man 手册页。我们这里暂且跳过这部分内容。

现在我们来看看成果如何。

debhello 0.0 版使用 debuild 命令产生的文件:. 

 $ cd ..
 $ tree -FL 1
.
├── debhello-0.0/
├── debhello-0.0.tar.gz
├── debhello-dbgsym_0.0-1_amd64.deb
├── debhello_0.0-1.debian.tar.xz
├── debhello_0.0-1.dsc
├── debhello_0.0-1_amd64.build
├── debhello_0.0-1_amd64.buildinfo
├── debhello_0.0-1_amd64.changes
├── debhello_0.0-1_amd64.deb
└── debhello_0.0.orig.tar.gz -> debhello-0.0.tar.gz

1 directory, 9 files

您可以看见生成的全部文件。

  • debhello_0.0.orig.tar.gz 是指向上游源码压缩包的符号链接。
  • debhello_0.0-1.debian.tar.xz 包含了维护者生成的内容。
  • debhello_0.0-1.dsc 是 Debian 源码包的元数据文件。
  • debhello_0.0-1_amd64.deb 是 Debian 二进制软件包。
  • debhello-dbgsym_0.0-1_amd64.deb 是 Debian 的调试符号二进制软件包。另请参见 Section 5.19.1, “新的 -dbgsym 软件包(Stretch 9.0 或更新)”
  • debhello_0.0-1_amd64.build 是构建日志文件。
  • debhello_0.0-1_amd64.buildinfodpkg-genbuildinfo(1) 生成的元数据文件。
  • debhello_0.0-1_amd64.changes 是 Debian 二进制软件包的元数据文件。

debhello_0.0-1.debian.tar.xz 包含了 Debian 对上游源代码的修改,具体如下所示。

压缩文件 debhello_0.0-1.debian.tar.xz 的内容:. 

 $ tar -tzf debhello-0.0.tar.gz
debhello-0.0/
debhello-0.0/src/
debhello-0.0/src/hello.c
debhello-0.0/LICENSE
debhello-0.0/Makefile
 $ tar --xz -tf debhello_0.0-1.debian.tar.xz
debian/
debian/README.Debian
debian/changelog
debian/control
debian/copyright
debian/patches/
debian/patches/series
debian/rules
debian/source/
debian/source/control
debian/source/format
debian/source/options
debian/source/patch-header
debian/tests/
debian/tests/control
debian/upstream/
debian/upstream/metadata
debian/watch

debhello_0.0-1_amd64.deb 包含了将要安装至目标系统中的文件。

debhello-debsym_0.0-1_amd64.deb 包含了将要安装至目标系统中的调试符号文件。

所有二进制包的包内容:. 

 $ dpkg -c debhello-dbgsym_0.0-1_amd64.deb
drwxr-xr-x root/root ...  ./
drwxr-xr-x root/root ...  ./usr/
drwxr-xr-x root/root ...  ./usr/lib/
drwxr-xr-x root/root ...  ./usr/lib/debug/
drwxr-xr-x root/root ...  ./usr/lib/debug/.build-id/
drwxr-xr-x root/root ...  ./usr/lib/debug/.build-id/be/
-rw-r--r-- root/root ...  ./usr/lib/debug/.build-id/be/11292eded3fc22396a0b62...
drwxr-xr-x root/root ...  ./usr/share/
drwxr-xr-x root/root ...  ./usr/share/doc/
lrwxrwxrwx root/root ...  ./usr/share/doc/debhello-dbgsym -> debhello
 $ dpkg -c debhello_0.0-1_amd64.deb
drwxr-xr-x root/root ...  ./
drwxr-xr-x root/root ...  ./usr/
drwxr-xr-x root/root ...  ./usr/bin/
-rwxr-xr-x root/root ...  ./usr/bin/hello
drwxr-xr-x root/root ...  ./usr/share/
drwxr-xr-x root/root ...  ./usr/share/doc/
drwxr-xr-x root/root ...  ./usr/share/doc/debhello/
-rw-r--r-- root/root ...  ./usr/share/doc/debhello/README.Debian
-rw-r--r-- root/root ...  ./usr/share/doc/debhello/changelog.Debian.gz
-rw-r--r-- root/root ...  ./usr/share/doc/debhello/copyright

生成的依赖列表会给出所有二进制软件包的依赖。

生成的所有二进制软件包的依赖列表(v=0.0):. 

 $ dpkg -f debhello-dbgsym_0.0-1_amd64.deb pre-depends \
            depends recommends conflicts breaks
Depends: debhello (= 0.0-1)
 $ dpkg -f debhello_0.0-1_amd64.deb pre-depends \
            depends recommends conflicts breaks
Depends: libc6 (>= 2.2.5)

[Caution] Caution

在将软件包上传至 Debian 仓库之前,仍然有很多细节需要进行处理。

[Note] Note

如果跳过了对 debmake 命令自动生成的配置文件的手工调整步骤,所生成的二进制软件包可能缺少有用的软件包描述信息,某些政策的要求也无法满足。这个不正式的软件包对于 dpkg 命令来说可以正常处理,也许这样对您本地的部署来说已经足够好了。

上面的例子中,在创建合适的 Debian 软件包时没有修改上游的源代码。

作为维护者,另一个备选的方案是对上游源代码做改动,如修改上游的 Makefile 以将 $(prefix) 的值设定为 /usr

打包操作基本上和上面的分步示例相同,除了在 Section 4.7, “第三步:编辑模板文件” 中的两点:

这个使用一系列补丁文件进行 Debian 打包的备选方案对于应对上游未来的改变可能不够健壮,但是在应对更为复杂的上游源代码时可以更灵活。(参见 Section 7.13, “3.0 源代码格式”。)

[Note] Note

对当前这个特定的打包场景,前文的 Section 4.7, “第三步:编辑模板文件” 中使用 debian/rules 文件的方式更好一些。但为了演示起见,此时我们先使用本节的方式继续操作。

[Tip] Tip

对更复杂的打包场景,可能需要同时应用 Section 4.7, “第三步:编辑模板文件”Section 4.9, “第三步(备选):修改上游源代码” 中的方式。

这里的例子使用 dquilt 命令(一个 quilt 程序的简单封装)创建 000-prefix-usr.patchdquilt 命令的语法和功能与 quilt(1) 命令相同,唯一的区别在于补丁存储在 debian/patches/ 目录中。

 $ cd debhello-0.0
 $ dquilt new 000-prefix-usr.patch
Patch debian/patches/000-prefix-usr.patch is now on top
 $ dquilt add Makefile
File Makefile added to patch debian/patches/000-prefix-usr.patch
 ... hack, hack, hack, ...
 $ head -1 Makefile
prefix = /usr
 $ dquilt refresh
Refreshed patch debian/patches/000-prefix-usr.patch
 $ dquilt header -e --dep3
 ... edit the DEP-3 patch header with editor
 $ tree -a
.
├── .pc
│   ├── .quilt_patches
│   ├── .quilt_series
│   ├── .version
│   ├── 000-prefix-usr.patch
│   │   ├── .timestamp
│   │   └── Makefile
│   └── applied-patches
├── LICENSE
├── Makefile
├── debian
│   ├── README.Debian
│   ├── changelog
│   ├── control
│   ├── copyright
│   ├── patches
│   │   ├── 000-prefix-usr.patch
│   │   └── series
│   ├── rules
│   ├── source
│   │   ├── control
│   │   ├── format
│   │   ├── local-options
│   │   ├── options
│   │   └── patch-header
│   ├── tests
│   │   └── control
│   ├── upstream
│   │   └── metadata
│   └── watch
└── src
    └── hello.c

8 directories, 24 files
 $ cat debian/patches/series
000-prefix-usr.patch
 $ cat debian/patches/000-prefix-usr.patch
Description: set prefix=/usr patch
Author: Osamu Aoki <osamu@debian.org>
Index: debhello-0.0/Makefile
===================================================================
--- debhello-0.0.orig/Makefile
+++ debhello-0.0/Makefile
@@ -1,4 +1,4 @@
-prefix = /usr/local
+prefix = /usr

 all: src/hello

这里,上游源码树中的 Makefile 文件没有恢复到原始状态的必要。在 Section 4.8, “第四步:使用 debuild 构建软件包” 描述的 Debian 打包过程中调用的 dpkg-source 命令能够理解由 dquilt 程序在 .pc/ 目录中记录的补丁应用情况。只要所有这些修改都是由 dquilt 命令完成的,那么 Debian 源码包就可以从经过修改的源码树中进行构建。

[Note] Note

如果 .pc/ 目录不存在,dpkg-source 命令就会假定没有应用任何补丁。这就是更为原始的补丁生成方法,例如 Section 4.9.1, “使用 diff -u 处理补丁” 中未生成 .pc/ 目录的情况下要求将上游源码树进行恢复的原因。

这里给出使用“dpkg-source --commit”命令生成 000-prefix-usr.patch 的例子。

我们先来编辑上游源代码。

 $ cd debhello-0.0
 $ vim Makefile
 ... hack, hack, hack, ...
 $ head -n1 Makefile
prefix = /usr

我们来进行提交。

 $ dpkg-source --commit . 000-prefix-usr.patch
... editor to edit the DEP-3 patch header
...

我们来看看效果如何。

 $ cat debian/patches/series
000-prefix-usr.patch
 $ cat debian/patches/000-prefix-usr.patch
Description: set prefix=/usr patch
Author: Osamu Aoki <osamu@debian.org>
Index: debhello-0.0/Makefile

--- debhello-0.0.orig/Makefile
+++ debhello-0.0/Makefile
@@ -1,4 +1,4 @@
-prefix = /usr/local
+prefix = /usr

 all: src/hello

 $ tree -a
.
├── .pc
│   ├── .quilt_patches
│   ├── .quilt_series
│   ├── .version
│   ├── 000-prefix-usr.patch
│   │   ├── .timestamp
│   │   └── Makefile
│   └── applied-patches
├── LICENSE
├── Makefile
├── debian
│   ├── README.Debian
│   ├── changelog
│   ├── control
│   ├── copyright
│   ├── patches
│   │   ├── 000-prefix-usr.patch
│   │   └── series
│   ├── rules
│   ├── source
│   │   ├── control
│   │   ├── format
│   │   ├── local-options
│   │   ├── options
│   │   └── patch-header
│   ├── tests
│   │   └── control
│   ├── upstream
│   │   └── metadata
│   └── watch
└── src
    └── hello.c

8 directories, 24 files

这里,dpkg-source 命令完成了与 Section 4.9.2, “使用 dquilt 处理补丁” 一节中使用 dquilt 命令完全相同的流程。



[9] 这里的做法是为了进行加固而强制启用只读重定位链接,以此避免 lintian 的警告“W: debhello: hardening-no-relro usr/bin/hello”。其实它在本例中并不是必要的,但加上也没有什么坏处。对于没有外部链接库的本例来说,lintian 似乎给出了误报的警告。

[10] 这里的做法是为了避免在依赖库情况复杂的情况下过度链接,例如某些 GNOME 程序。这样做对这里的简单例子来说并不是必要的,但应当是无害的。