章 4. 簡單例子

內容目錄

4.1. 大致流程
4.2. 什麼是 debmake?
4.3. 什麼是 debuild?
4.4. 第一步:取得上游原始碼
4.5. 第二步:使用 debmake 產生模板檔案
4.6. 第三步:編輯模板檔案
4.7. 第四步:使用 debuild 構建套件
4.8. 第三步(備選):修改上游原始碼
4.8.1. 使用 diff -u 處理補丁
4.8.2. 使用 dquilt 處理補丁
4.8.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 下的位置。

注意

在其它更加複雜的構建系統下構建 Debian 套件的例子可以在 章 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
   ...

提示

此處和下面例子中的 debuild 命令可使用等價的命令進行替換,例如 pdebuild 命令。

提示

如果上游原始碼壓縮包提供了 .tar.xz 格式文件,請使用這樣的壓縮包來替代 .tar.gz.tar.bz2 格式。xz 壓縮與 gzipbzip2 壓縮相比提供了更好的壓縮比。

文中的 debmake 命令是用於 Debian 打包的一個輔助腳本。

  • 它總是將大多數選項的狀態與引數設定為合理的預設值。
  • 它能產生上游原始碼套件,並按需建立所需的符號連結。
  • 它不會覆寫 debian/ 目錄下已存在的配置文件。
  • 它支援多架構(multiarch)套件。
  • 它能建立良好的模板檔案,例如符合 DEP-5debian/copyright 檔案。

這些特性使得使用 debmake 進行 Debian 打包工作變得簡單而現代化。

注意

debmake 命令並不是製作一個 Debian 套件的唯一途徑。許多套件是打包者模仿其它已有的打包範例,僅僅使用文字編輯器而編寫完成打包指令碼的。

這裡給出與 debuild 命令類似的一系列命令的一個彙總。

注意

如需瞭解詳細內容,請見 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

注意

$(CFLAGS)echo 命令用於在接下來的例子中驗證所設定的構建引數。

提示

如果 debmake 命令呼叫時使用了 -T 選項,程式將為模板檔案產生更加詳細的註釋內容。

debmake 命令的輸出十分詳細,如下所示,它可以展示程式的具體操作內容。

 $ cd debhello-0.0
 $ debmake
I: set parameters
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/watch
I: creating => debian/watch
I: substituting => /usr/share/debmake/extra1/README.Debian
I: creating => debian/README.Debian
I: substituting => /usr/share/debmake/extra1source/format
I: creating => debian/source/format
I: substituting => /usr/share/debmake/extra1source/local-options
I: creating => debian/source/local-options
I: substituting => /usr/share/debmake/extra1patches/series
I: creating => debian/patches/series
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
│   │   │   ├── format
│   │   │   └── local-options
│   │   └── watch
│   └── src
│       └── hello.c
├── debhello-0.0.tar.gz
└── debhello_0.0.orig.tar.gz -> debhello-0.0.tar.gz

5 directories, 14 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.0
Homepage: <insert the upstream URL, if relevant>

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.

警告

如果您對 debian/control 模板檔案中的“Section: unknown”部分不做修改的話,後續的 lintian 錯誤可能導致構建失敗。

因為這是個 ELF 二進位制可執行檔案套件,debmake 命令設定了“Architecture: any”和“Multi-Arch: foreign”兩項。同時,它將所需的 substvar 引數設定為“Depends: ${shlibs:Depends}, ${misc:Depends}”。這些內容將在 章 5, 基本內容 部分進行解釋。

注意

請注意這個 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”部分所說,對強化選項進行設定。[8]

如上匯出 DEB_CFLAGS_MAINT_APPEND 可以強制 C 編譯器給出所有型別的警告內容。

如上匯出 DEB_LDFLAGS_MAINT_APPEND 可以強制連結器只對真正需要的程式庫進行連結。[9]

對基於 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.3.0
Homepage: https://salsa.debian.org/debian/debmake-doc

Package: debhello
Architecture: any
Multi-Arch: foreign
Depends: ${misc:Depends}, ${shlibs:Depends}
Description: example package in the debmake-doc package
 This is an example package to demonstrate Debian packaging using
 the debmake command.
 .
 The generated Debian package uses the dh command offered by the
 debhelper package and the dpkg source format `3.0 (quilt)'.

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 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
│   ├── format
│   └── local-options
└── watch

2 directories, 9 files

提示

對於來自 debhelper 套件的各個 dh_* 命令來說,它們在讀取所使用的配置文件時通常把以 # 開頭的行視為註釋行。

您可以使用 debuild 或者等效的命令工具(參見 節 4.3, “什麼是 debuild?”)在這個原始碼樹內構建一個非原生 Debian 套件。命令的輸出通常十分詳細,如下所示,它會對構建中執行的操作進行解釋。

 $ cd debhello-0.0
 $ debuild
 dpkg-buildpackage -us -uc -ui -i -i
 ...
 fakeroot debian/rules clean
dh clean
 ...
 debian/rules build
dh build
   dh_update_autotools_config
   dh_autoreconf
   dh_auto_configure
        install -d /path/to/debhello-0.0/debian/.debhelper/generated/_source/...
   dh_auto_build
        make -j4 "INSTALL=install --strip-program=true"
make[1]: Entering directory '/path/to/debhello-0.0'
# CFLAGS=-g -O2
# -fdebug-prefix-map=/home/osamu/pub/salsa/debmake/debmake-doc/debhello-
 ...
 fakeroot debian/rules binary
dh binary
 ...
Now running lintian -i -I --show-overrides debhello_0.0-1_amd64.changes ...
 ...
W: debhello: binary-without-manpage usr/bin/hello
N:
N:    Each binary in /usr/bin, /usr/sbin, /bin, /sbin or /usr/games should
N:    have a manual page
 ...

這裡驗證了 CFLAGS 已經得到了更新,新增了 -Wall-pendatic 引數;這是我們先前在 DEB_CFLAGS_MAINT_APPEND 變數中所指定的。

根據 lintian 的報告,您應該如同後文中的例子那樣(請見 章 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 的除錯符號二進位制套件。另請參見 節 5.17.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/LICENSE
debhello-0.0/Makefile
debhello-0.0/src/
debhello-0.0/src/hello.c
 $ 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/format
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/66/
-rw-r--r-- root/root ...  ./usr/lib/debug/.build-id/66/73e0826b1e8bd84f511bac...
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)

注意

在將套件上傳至 Debian 倉庫之前,仍然有很多細節需要進行處理。

注意

如果跳過了對 debmake 命令自動生成的配置檔案的手工調整步驟,所生成的二進位制套件可能缺少有用的套件描述資訊,某些政策的要求也無法滿足。這個不正式的套件對於 dpkg 命令來說可以正常處理,也許這樣對您本地的部署來說已經足夠好了。

上面的例子中,在建立合適的 Debian 套件時沒有修改上游的原始碼。

作為維護者,另一個備選的方案是對上游原始碼做改動,如修改上游的 Makefile 以將 $(prefix) 的值設定為 /usr

打包操作基本上和上面的分步範例相同,除了在 節 4.6, “第三步:編輯模板檔案” 中的兩點:

這個使用一系列補丁檔案進行 Debian 打包的備選方案對於應對上游未來的改變可能不夠健壯,但是在應對更為複雜的上游原始碼時可以更靈活。(參見 節 7.13, “3.0 原始碼格式”。)

注意

對當前這個特定的打包場景,前文的 節 4.6, “第三步:編輯模板檔案” 中使用 debian/rules 檔案的方式更好一些。但為了演示起見,此時我們先使用本節的方式繼續操作。

提示

對更復雜的打包場景,可能需要同時應用 節 4.6, “第三步:編輯模板檔案”節 4.8, “第三步(備選):修改上游原始碼” 中的方式。

這裡的例子使用 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
│   │   ├── format
│   │   └── local-options
│   └── watch
└── src
    └── hello.c

6 directories, 19 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 檔案沒有恢復到原始狀態的必要。在 節 4.7, “第四步:使用 debuild 構建套件” 描述的 Debian 打包過程中呼叫的 dpkg-source 命令能夠理解由 dquilt 程式在 .pc/ 目錄中記錄的補丁應用情況。只要所有這些修改都是由 dquilt 命令完成的,那麼 Debian 原始碼套件就可以從經過修改的原始碼樹中進行構建。

注意

如果 .pc/ 目錄不存在,dpkg-source 命令就會假定沒有應用任何補丁。這就是更為原始的補丁生成方法,例如 節 4.8.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
│   │   ├── format
│   │   └── local-options
│   └── watch
└── src
    └── hello.c

6 directories, 19 files

這裡,dpkg-source 命令完成了與 節 4.8.2, “使用 dquilt 處理補丁” 一節中使用 dquilt 命令完全相同的流程。



[8] 這裡的做法是為了進行強化而強制啟用只讀重定位連結,以此避免 lintian 的警告“W: debhello: hardening-no-relro usr/bin/hello”。其實它在本例中並不是必要的,但加上也沒有什麼壞處。對於沒有外部鏈接程式庫的本例來說,lintian 似乎給出了誤報的警告。

[9] 這裡的做法是為了避免在依賴程式庫情況複雜的情況下過度連結,例如某些 GNOME 程式。這樣做對這裡的簡單例子來說並不是必要的,但應當是無害的。