问题描述
我想创建一个 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
,但似乎没有这样做。
BUILT_SOURCES = version.h
全部提交的文件 BUILT_SOURCES
将在任何正常编译规则运行之前构建。
但是,这将创建一个轻微的问题:由于 version.h
将需要在每个 make
调用中重建,每次 foo.c
文件,其中 #include
s version.h
将被触发再次在每个 make
运行。如果重新编译仅在实际发生变化的情况下才会发生。
要解决此问题,请使用 BUILT_SOURCES
依赖于每次都创建的邮票文件(它从未实际创建,因此构建规则每次运行)。该邮票文件的构建规则创建一个新的 version.h
文件作为 version.h.tmp
,只有副本 version.h.tmp
to version.h
if version.h.tmp
实际上不同于 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
这个解决方案会做你所要求的。 p>
不幸的是,当您从dist tarball构建时,会有一个小问题:然后 hg id
会给你伪造的信息,并且您的压缩包中可能没有 version.h
,因此构建将失败或包含虚假版本信息。
我已经解决了此问题,以获取项目正在使用 git
。此解决方案中生成的 git-version.h
文件包含的版本信息不仅仅是一个版本号。您可以在和 BUILT_SOURCES
连接(包括处理希望所有源代码在如果你有兴趣。
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.
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 #include
s 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.
这篇关于标题依赖在automake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!