章 9. More on packaging

# 章 9. More on packaging

Let’s explore more fundamentals of Debian packaging.

## 9.1. Package customization

All customization data for the Debian source package resides in the debian/ directory as presented in “節 5.7, “第三步：編輯模板檔案””:

- The Debian package build system can be customized through the debian/rules file (see “節 9.2, “Customized debian/rules””).
- 可以使用額外的配置檔案（如 debian/ directory 目錄下的 package.install 和 package.docs 檔案）以配合來自 debhelper 套件的 dh_* 命令設定 Debian 套件檔案的安裝路徑等資訊（請參見 “節 6.14, “Other debian/* files””）。

When these are not sufficient to make a good Debian package, -p1 patches of debian/patches/* files are deployed to modify the upstream source. These are applied in the sequence defined in the debian/patches/series file before building the package as presented in “節 5.9, “Step 3 (alternatives): Modification to the upstream source””.

You should address the root cause of the Debian packaging problem in the least invasive way possible. This approach will make the generated package more robust for future upgrades.

| 注意
If the patch addressing the root cause is useful to the upstream project, send it to the upstream maintainer.

## 9.2. Customized debian/rules

Flexible customization of the 節 6.5, “debian/rules file” is achieved by adding appropriate override_dh_* targets and their rules.

When a special operation is required for a certain dh_foo command invoked by the dh command, its automatic execution can be overridden by adding the makefile target override_dh_foo in the debian/rules file.

構建的過程可以使用某些上游提供的介面進行定製化，如使用傳遞給標準的原始碼構建系統的引數。這些構建系統包括但不限於：

- configure，
- Makefile，
- “python -m build”, or
- Build.PL。

In this case, you should add the override_dh_auto_build target with “dh_auto_build -- arguments”. This ensures that arguments are passed to the build system after the default parameters that dh_auto_build usually passes.

| 提示
Avoid executing bare build system commands directly if they are supported by the dh_auto_build command.

參見：

- “節 5.7, “第三步：編輯模板檔案”” for the basic example.
- “節 10.3, “Note on build system”” to be reminded of the challenge involving the underlying build system.
- “節 10.10, “多體系架構”” for multiarch customization.
- “節 10.6, “編譯強化”” for hardening customization.

## 9.3. Variables for debian/rules

某些對設定 debian/rules 有用的變數定義可以在 /usr/share/dpkg/ 目錄下的檔案中找到。比較重要的包括：

pkg-info.mk
  Set DEB_SOURCE, DEB_VERSION, DEB_VERSION_EPOCH_UPSTREAM, DEB_VERSION_UPSTREAM_REVISION, DEB_VERSION_UPSTREAM, and DEB_DISTRIBUTION variables obtained from dpkg-parsechangelog(1). (useful for backport support etc..)
vendor.mk
  Set DEB_VENDOR and DEB_PARENT_VENDOR variables; and dpkg_vendor_derives_from macro obtained from dpkg-vendor(1). (useful for vendor support (Debian, Ubuntu, …​).)
architecture.mk
  Set DEB_HOST_* and DEB_BUILD_* variables obtained from dpkg-architecture(1).
buildflags.mk
  Set CFLAGS, CPPFLAGS, CXXFLAGS, OBJCFLAGS, OBJCXXFLAGS, GCJFLAGS, FFLAGS, FCFLAGS, and LDFLAGS build flags obtained from dpkg-buildflags(1).

For example, you can add an extra option to CONFIGURE_FLAGS for linux-any target architectures by adding the following to debian/rules:

    DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
     ...
    ifeq ($(DEB_HOST_ARCH_OS),linux)
    CONFIGURE_FLAGS += --enable-wayland
    endif

參見 “節 10.10, “多體系架構””、dpkg-architecture(1) 和 dpkg-buildflags(1)。

## 9.4. 新上游版本

When a new upstream release tarball debhello-newvwesion.tar.xz is released, the Debian source package can be updated by invoking commands in the old source tree as:

    [debhello-0.0] $ uscan
     ... debhello-newversion.tar.xz downloaded
    [debhello-0.0] $ uupdate -v newversion ../debhello-newversion.tar.xz

- The debian/watch file in the old source tree must be a valid one.
- This make symlink ../debhello_newvwesion.orig.tar.xz pointing to ../debhello-newvwesion.tar.xz.
- Files are extracted from ../debhello-newvwesion.tar.xz to ../debhello-newversion/
- Files are copied from ../debhello-oldversion/debian/ to ../debhello-newvesion/debian/ .

After the above, you should refresh debian/patches/* files (see “節 9.5, “Manage patch queue with dquilt””) and update debian/changelog with the dch(1) command.

When “debian uupdate” is specified at the end of line in the debian/watch file, uscan automatically executes uupdate(1) after downloading the tarball.

## 9.5. Manage patch queue with dquilt

You can add, drop, and refresh debian/patches/* files with dquilt to manage patch queue.

- Add a new patch debian/patches/bugname.patch recording the upstream source modification on the file buggy_file as:

      [debhello-0.0] $ dquilt push -a
      [debhello-0.0] $ dquilt new bugname.patch
      [debhello-0.0] $ dquilt add buggy_file
      [debhello-0.0] $ vim buggy_file
        ...
      [debhello-0.0] $ dquilt refresh
      [debhello-0.0] $ dquilt header -e
      [debhello-0.0] $ dquilt pop -a

- Drop (== disable) an existing patch

  - Comment out pertinent line in debian/patches/series
  - Erase the patch itself (optional)

- Refresh debian/patches/* files to make “dpkg-source -b” work as expected after updating a Debian package to the new upstream release.

      [debhello-0.0] $ uscan; uupdate  # updating to the new upstream release
      [debhello-0.0] $ while dquilt push; do dquilt refresh ; done
      [debhello-0.0] $ dquilt pop -a

  - If conflicts are encountered with “dquilt push” in the above, resolve them and run “dquilt refresh” manually for each of them.

## 9.6. Build commands

Here is a recap of popular low level package build commands. There are many ways to do the same thing.

- dpkg-buildpackage = 套件打包工具的核心
- debuild = dpkg-buildpackage + lintian （在清理後的環境變數下構建）
- schroot = core of the Debian chroot environment tool
- sbuild = dpkg-buildpackage on custom schroot (build in the chroot)

## 9.7. Note on sbuild

The sbuild(1) command is a wrapper script of dpkg-buildpackage which builds Debian binary packages in a chroot environment managed by the schroot(1) command. For example, building for Debian unstable suite can be done as:

    [debhello-0.0] $ sudo sbuild -d unstable

In schroot(1) terminology, this builds a Debian package in a clean ephemeral chroot “chroot:unstable-amd64-sbuild” started as a copy of the clean minimal persistent chroot “source:unstable-amd64-sbuild”.

This build environment was set up as described in “節 4.7, “sbuild setup”” with “sbuild-debian-developer-setup -s unstable” which essentially did the following:

    [~] $ sudo mkdir -p /srv/chroot/dist-amd64-sbuild
    [~] $ sudo sbuild-createchroot unstable /srv/chroot/unstable-amd64-sbuild http://deb.debian.org/debian
    [~] $ sudo usermod -a -G sbuild <your_user_name>
    [~] $ sudo shutdown -r now
     ... reboot

The schroot(1) configuration for unstable-amd64-sbuild was generated at /etc/schroot/chroot.d/unstable-amd64-sbuild.$suffix :

    [unstable-amd64-sbuild]
    description=Debian sid/amd64 autobuilder
    groups=root,sbuild
    root-groups=root,sbuild
    profile=sbuild
    type=directory
    directory=/srv/chroot/unstable-amd64-sbuild
    union-type=overlay

其中：

- The profile defined in the /etc/schroot/sbuild/ directory is used to setup the chroot environment.
- /srv/chroot/unstable-amd64-sbuild directory holds the chroot filesystem.
- /etc/sbuild/unstable-amd64-sbuild is symlinked to /srv/chroot/unstable-amd64-sbuild .

You can update this source chroot “source:unstable-amd64-sbuild” by:

    [~] $ sudo sbuild-update -udcar unstable

You can log into this source chroot “source:unstable-amd64-sbuild” by:

    [~] $ sudo sbuild-shell unstable

| 提示
If your source chroot filesystem is missing packages such as libeatmydata1, ccache, and lintian for your needs, you may want to install these by logging into it.

## 9.8. Special build cases

The orig.tar.xz file may need to be uploaded for a Debian revision other than 0 or 1 under some exceptional cases (e.g., for a security upload).

When an essential package becomes a non-essential one (e.g., adduser), you need to remove it manually from the existing chroot environment for its use by piuparts.

## 9.9. Upload orig.tar.xz

When you first upload the package to the archive, you need to include the original orig.tar.xz source, too.

如果 Debian 修訂碼是 1 或者 0，這都是預設的。否則，您必須使用帶有 -sa 選項的 dpkg-buildpackage 命令。

- dpkg-buildpackage -sa
- debuild -sa
- sbuild --debbuildopts=-sa
- gbp buildpackage -sa

| 提示
On the other hand, the -sd option will force the exclusion of the original orig.tar.xz source.

| 提示
Security uploads require including the orig.tar.xz file.

## 9.10. 跳過的上傳

如果當跳過上傳時，你在 debian/changelog 中建立了多個條目，你必須建立一個包含自上次上傳以來所有變更的 debian/changelog 檔案。這可以通過指定 dpkg-buildpackage 選項 -v 以及上次上傳的版本號，比如 1.2 來完成。

- dpkg-buildpackage -v 1.2
- debuild -v 1.2
- sbuild --debbuildopts=-v 1.2
- gbp buildpackage -v 1.2

## 9.11. 錯誤報告

The reportbug(1) command used for the bug report of binarypackage can be customized by the files in usr/share/bug/binarypackage/.

dh_bugfiles 命令將安裝以下位於 debian/ 目錄中的的模板檔案。

- debian/binarypackage.bug-control → usr/share/bug/binarypackage/control

  - 該檔案包含諸如重定向錯誤報告至其它套件的一些指導性內容。

- debian/binarypackage.bug-presubj → usr/share/bug/binarypackage/presubj

  - 該檔案的內容將由 reportbug 命令對使用者展示。

- debian/binarypackage.bug-script → usr/share/bug/binarypackage or usr/share/bug/binarypackage/script

  - reportbug 命令執行此指令碼以生成錯誤報告的模板檔案。

See dh_bugfiles(1) and “reportbug’s Features for Developers (README.developers)”

| 提示
如果您總是需要提醒提交報告的使用者某些注意事項或詢問他們某些問題，使用這些檔案可以將這個過程自動化。

章 8. Sanitization of the source |  | 章 10. 高階打包