在 automake 中为目标添加依赖项

Adding a dependency for a target in automake

我 运行 遇到了 automake 的问题,我似乎找不到一个干净的解决方案,这似乎应该是可能的(甚至简单),但没有简单的工作。

基本上我遇到的问题是源文件包含自动生成的 header 文件。我可以添加依赖项来生成 header 文件,一旦 header 存在,一切正常,因为 automake 的自动依赖项生成会处理所有事情。问题是 第一次 你 运行 在干净的树中制作时,依赖文件不存在,所以 automake 不知道生成 header文件,这使得包含 header 的文件的编译失败而不生成任何依赖项。这是一个 chicken-and-egg 问题——您需要手动告诉 (auto)make 构建 header 文件。

显而易见的解决方案是为 header 添加对 Makefile.am 文件的依赖性,但这不起作用,因为对目标的依赖性会覆盖 automake 的自动规则生成,正如文档所说:

Note that Automake does not make any distinction between rules with commands and rules that only specify dependencies. So it is not possible to append new dependencies to an automake-defined target without redefining the entire rule.

现在我已经通过 'hiding' 来自 automake 的依赖解决了这个问题,但这只适用于 GNU-make:

Makefile.am:

bin_PROGRAMS = foo
foo_SOURCES = main.c foobar.c baz.c

gen.h: system.spec
        ...command to regen gen.h

# foobar.c #includes gen.h, so it needs to exist prior to compiling foobar.c
$(eval foo-foobar.o: gen.h)

这确实有效,但看起来很丑陋。有更好的 automake-safe 方法吗?

Automake 提供 BUILT_SOURCES 来解决这个问题。添加到这里的文件是在普通编译完成之前构建的——它专门用于生成的头文件和源代码。

在你的情况下,这应该足够了:

BUILT_SOURCES = gen.h