automake 中的头文件依赖

发布于 2024-08-12 02:24:38 字数 508 浏览 3 评论 0原文

我想创建一个 Makefile.am 文件,它生成 xxx.c 文件中提到的一个头文件。

假设 xxx.c 包含:

#include <version.h>
...

并且我有一条规则在 Makefile.am 末尾创建它:

version.h:
       echo '#define VERSION "'`hg id`'"' > version.h.tmp
       cmp version.h.tmp version.h || mv version.h.tmp version.h

我需要更改什么才能使 xxx.c编译依赖于version.h?我尝试了 nodist_progname_SOURCES=version.h,但这似乎不起作用。

I'd like to create a Makefile.am file which generates one header file mentioned in a xxx.c file.

Let's say that xxx.c contains:

#include <version.h>
...

and that I have a rule to create it at the end of Makefile.am:

version.h:
       echo '#define VERSION "'`hg id`'"' > version.h.tmp
       cmp version.h.tmp version.h || mv version.h.tmp version.h

What do I have to change to make the xxx.c compilation depend on version.h? I tried nodist_progname_SOURCES=version.h, but that doesn't seem to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李不 2024-08-19 02:24:38
BUILT_SOURCES = version.h

所有以 BUILT_SOURCES 提及的文件都将在任何正常编译规则运行之前构建。

然而,这会产生一个小问题:由于 version.h 需要在每次 make 调用时重新构建,因此需要重新编译每个 foo.c > 文件,其中 #includeversion.h 将在每次 make 运行时再次触发。我们希望仅在确实发生更改时才进行重新编译。

要解决此问题,请对每次“创建”的标记文件使用 BUILT_SOURCES 依赖项(它从未实际创建,因此每次都会运行构建规则)。该标记文件的构建规则创建一个新的 version.h 文件作为 version.h.tmp,并且仅复制 version.h.tmp如果 version.h.tmp 实际上与 version.h 不同(就像您的 version.h 一样),则为 version.h代码>规则)。因此,如果 version.h 中没有任何更改,则其时间戳 (mtime) 保持不变,并且不会触发依赖于 version.h 的对象构建:

BUILT_SOURCES = version.stamp

version.stamp:
        echo '#define VERSION "'`hg id`'"' > version.h.tmp
        cmp version.h.tmp version.h || mv version.h.tmp version.h

此解决方案将执行你要什么。

但不幸的是,当您从 dist tarball 构建时,会出现一个小问题:然后 hg id 会给您虚假信息,并且可能没有 version.h您的 tarball,因此构建将失败或包含虚假版本信息。

我已经为 xf86-video-radeonhd 项目解决了这个问题它正在使用git。此解决方案中生成的 git-version.h 文件包含更多版本信息,而不仅仅是一个版本号。您可以在 git_version.shBUILT_SOURCES 连接(包括处理所有out-of-source-treefrom-dist-tarball 构建案例)位于 RadeonHD.am

BUILT_SOURCES = version.h

All files mentioned as BUILT_SOURCES will be built before any of the normal compilation rules run.

However, this will create a slight problem: As version.h will need to be rebuilt on every make invocation, the recompilation of every foo.c file which #includes version.h will be triggered again on every make run. We would prefer if the recompilation only happens when there is actually something that has changed.

To get around this problem, use a BUILT_SOURCES dependency on a stamp file which is "created" every time (it never is actually created, so the build rule runs every time). The build rule for that stamp file creates a new version.h file as version.h.tmp, and only copies version.h.tmp to version.h if version.h.tmp is actually different from version.h (just like your version.h rule does). So if nothing has changed in version.h, its timestamp (mtime) remains the same, and no build of objects depending on version.h is triggered:

BUILT_SOURCES = version.stamp

version.stamp:
        echo '#define VERSION "'`hg id`'"' > version.h.tmp
        cmp version.h.tmp version.h || mv version.h.tmp version.h

This solution will do what you are asking for.

Unfortunately though, there will be a slight problem when you are building from a dist tarball: Then hg id will give you bogus information, and there probably is no version.h in your tarball, so the build will fail or contain bogus version information.

I have solved this issue for a the xf86-video-radeonhd project which is using git. The git-version.h file generated in this solution contains some more version information than just a single version number. You can see this update-only-if-different solution of mine at the end of git_version.sh and the BUILT_SOURCES hookup (including handling of hopefully all out-of-source-tree and from-dist-tarball build cases) in RadeonHD.am if you are interested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文