在受到 thiton 的回答启发后进行了一些挖掘,以及大量的反复试验,我终于找到了解决问题的方法。事实证明,dpkg-gencontrol 不是从可执行文件推断包依赖关系的工具,dpkg-shlibdeps 是。但是,需要仔细设置这两个程序以帮助生成包。继续阅读......
运行dpkg-shlibdeps -O <executable>
会生成需要安装该可执行文件才能运行的软件包和版本的列表。完美的。几乎。理想情况下,dpkg-gencontrol 可以在其处理中使用它,它声称能够通过其变量替换功能来完成。
为了使这一切顺利进行,我必须创建一个符合 Debian 打包工具期望的目录结构。它看起来基本上是这样的:
my_project_directory/
main.c (or other source code, etc.)
debian/
changelog (created by hand; see below)
control (this is basically a template, created by hand; see below)
files (created by dpkg-gencontrol)
substvars (created by dpkg-shlibdeps and used by dpkg-gencontrol)
tmp/ (tmp is the root of the target system's filesystem)
path/
to/
my/
project/
executable_1 (this will be installed at /path/to/my/project)
executable_2 (this, too)
var/
www/
index.php (this will be installed at /var/www on target systems)
DEBIAN/ (create this by hand)
control (created by dpkg-gencontrol and used in the final package)
请注意,Debian 打包工具会保留 debian/tmp/ 下所有文件的所有者和组。因此,如果您想要安装时由 root 或其他用户拥有的文件,事情就会变得棘手。一种选择是以 root 身份准备 debian 目录树,并根据需要设置所有者。如果您不想以 root 身份运行,或者不允许,还有另一种方法。
创建一个调用 chown 等的脚本,以根据需要调整所有权,最后一行是dpkg-deb -b debian/tmp .
(构建 .deb 包,请参见下面的示例)。通过另一个 Debian 工具 fakeroot 运行它,如下所示:fakeroot ./fix_ownerships_and_build.sh
. Fakeroot 让程序的行为就好像它们是 root 一样,而无需像 root 那样实际更改内容。它是为这种情况而创建的。
我研究了为什么 dpkg-gencontrol 会生成“第一个块缺少源字段”错误,甚至阅读了它的 Perl 源。正如经常发生的那样,错误代码是精确的,但没有提供足够的上下文来知道要做什么:控制文件确实需要一个名为“source”的字段,在它的第一个(两个)块中。
有两种 Debian 软件包,源代码和二进制。我以为我需要一个二进制文件,因为我只想将已编译的可执行文件放入其中,但我无法让它工作。我尝试了一个源包,并在我的控制文件中添加了一个源字段。这摆脱了“第一个块缺少源字段”错误,但导致了另一个错误。仔细阅读文档,我意识到源包在其控制文件中需要两个“段落” 。一旦我将控制文件更改为如下所示,它就开始工作(几乎):
Source: my-package
Maintainer: Joe Coder <joe@coder.com>
Package: my-package
Priority: optional
Architecture: amd64
Depends: ${shlibs:Depends}, apache2, php5
Description: The My-Package System
A longer description that runs to the end of one line and then
extends to another line.
仍然缺少的是变更日志文件。这是一个保存软件包发布历史的文件,其中包含重大更改、版本号、日期和负责人。我通常以自己的格式维护这样的东西,我小心地将其转换为严格的 Debian 变更日志格式。出于某种原因,最终包中省略了更改日志,因此我将历史记录文件单独放置并使用了占位符,如下所示:
my-package (1.0) unstable; urgency=low
* placeholder changelog to satisfy dpkg-gencontrol
-- Joe Coder <joe@coder.com> Thu, 3 Nov 2011 16:49:00 -0700
* 行上的两个前导空格是必不可少的,- 行上的单个前导空格也是如此,电子邮件地址和日期之间的两个空格也是如此。是的,日期需要精确,时区等等,即使它不需要精确。
将所有内容放在一起,如上所述设置 debian 目录树,构建包所需的命令序列如下:
dpkg-shlibdeps debian/tmp/path/to/my/project/executable_1 \
debian/tmp/path/to/my/project/executable_2
dpkg-gencontrol -v1.1 (or whatever version you are building)
fakeroot ./fix_ownerships_and_build.sh
其中 fix_ownerships_and_build.sh 看起来像这样:
chown -R root:root debian/tmp/path (or whatever user is appropriate)
chown -R www-data:www-data debian/tmp/var/www/* (same goes here)
dpkg-deb -b debian/tmp . (this leads to a nice my-package_1.1_amd64.deb file)
就是这样了。希望这个答案能帮助其他人比我更快地取得进步。