7

dpkg (Debian) 控制文件中的大多数字段都很简单。棘手的一个是确定依赖项列表(Depends:)。我希望 dpkg-gencontrol 可以通过查看包目录中可执行文件的 ldd 输出来为我做到这一点。也许它可以,但我无法让它工作。

如果这dpkg-gencontrol是为了,我得到的错误是:

dpkg-gencontrol: error: syntax error in control_template at line 7: first block lacks a source field.

作为参考,命令是dpkg-gencontrol -v1.1 -ccontrol_template -lchangelog -Pdebian. control_template 文件包含以下内容:

Package: my-package
Maintainer: Joe Coder <joe@coder.com>
Description: The my-package system
 A longer description that runs to the end of one line and then 
 extends to another line.
Priority: optional

如果这不是dpkg-gencontrol目的,是否有人对我能做什么有任何建议,或者关于如何设置依赖项列表的建议,最好是自动设置?

诚然,以一般方式推断依赖关系可能是一个非常困难的问题,尤其是当您将搜索扩展到脚本和其他程序时。我希望存在一些在大多数情况下都可以使用的工具。

请注意,这仅适用于内部分发。我不会构建一个包来进入 Linux 发行版,甚至不会被公众下载,所以我很乐意在需要时弯曲/打破标准规则。

4

2 回答 2

12

在受到 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)

就是这样了。希望这个答案能帮助其他人比我更快地取得进步。

于 2011-11-10T08:59:51.977 回答
1

实际上,查看包含的库列表对于 C 程序来说是很常见的,这只是 shlibs 助手 ( dpkg-shlibdeps) 的工作。查看其联机帮助页以获取帮助,但基本上归结为${shlibs:Depends}在您的依赖行中使用。

于 2011-11-06T16:48:33.200 回答