automake 中的头文件依赖
我想创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有以
BUILT_SOURCES
提及的文件都将在任何正常编译规则运行之前构建。然而,这会产生一个小问题:由于
version.h
需要在每次make
调用时重新构建,因此需要重新编译每个foo.c
> 文件,其中#include
的version.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
的对象构建:此解决方案将执行你要什么。
但不幸的是,当您从 dist tarball 构建时,会出现一个小问题:然后
hg id
会给您虚假信息,并且可能没有version.h
您的 tarball,因此构建将失败或包含虚假版本信息。我已经为 xf86-video-radeonhd 项目解决了这个问题它正在使用
git
。此解决方案中生成的 git-version.h 文件包含更多版本信息,而不仅仅是一个版本号。您可以在 git_version.sh 和BUILT_SOURCES
连接(包括处理所有out-of-source-tree和 from-dist-tarball 构建案例)位于 RadeonHD.am。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 everymake
invocation, the recompilation of everyfoo.c
file which#include
sversion.h
will be triggered again on everymake
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 newversion.h
file asversion.h.tmp
, and only copiesversion.h.tmp
toversion.h
ifversion.h.tmp
is actually different fromversion.h
(just like yourversion.h
rule does). So if nothing has changed inversion.h
, its timestamp (mtime) remains the same, and no build of objects depending onversion.h
is triggered: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 noversion.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
. Thegit-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 theBUILT_SOURCES
hookup (including handling of hopefully all out-of-source-tree and from-dist-tarball build cases) in RadeonHD.am if you are interested.