Makefile:在包含另一个 makefile 之前需要执行一个目标
我的 Makefile 的一部分:
CPUDEPS=./mydeps.cpu
(...)
deps: $(CPUDEPS)
$(CPUDEPS): $(CCFILES)
@echo [DEPS] CPU
$(CMDECHO)makedepend -Y -s'# CPU sources dependencies generated with "make deps"' \
-w4096 -f- -- $(CFLAGS) -- $^ 2> /dev/null > $(CPUDEPS)
(...)
sinclude $(CPUDEPS)
问题 1:包含在处理的第一阶段完成,目标在第二阶段完成;所以,如果 ./mydeps.cpu 不存在并且我“make deps”,我首先得到错误
Makefile:335: ./mydeps.cpu: No such file or directory
我使用 sinclude
而不是 include
隐藏错误,但是问题仍然存在:包含旧文件,而不是刚刚生成的文件。必须运行两次才能包含更新的文件。这是因为 make 进行了两阶段处理;有什么方法可以告诉 make 在解析包含之前完成目标 deps 吗?
问题 2:即使文件 ./mydeps.cpu 不存在并且 make deps
实际上创建了它,我总是得到“make:Nothing to do for deps” 。其他目标不会发生这种情况。我不明白为什么以及如何避免它。
Part of my Makefile:
CPUDEPS=./mydeps.cpu
(...)
deps: $(CPUDEPS)
$(CPUDEPS): $(CCFILES)
@echo [DEPS] CPU
$(CMDECHO)makedepend -Y -s'# CPU sources dependencies generated with "make deps"' \
-w4096 -f- -- $(CFLAGS) -- $^ 2> /dev/null > $(CPUDEPS)
(...)
sinclude $(CPUDEPS)
Problem 1: includes are done during the first phase of processing, targets during the second phase; so, if ./mydeps.cpu doesn't exist and I "make deps", I get first the error
Makefile:335: ./mydeps.cpu: No such file or directory
I hide the error using sinclude
instead of include
, but the problem is still there: the old file is included, not the just-generated-one. Have to run it twice to include the updated file. This is because make does a two-phase processing; is there any way to tell make to complete the target deps before parsing the includes?
Problem 2: even if the file ./mydeps.cpu doesn't exist and make deps
actually creates it, I always get a "make: Nothing to do for deps". This doesn't happen with other targets. I don't understand why and how to avoid it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题 1 不存在:在构建目标之前,
make
自动重建 makefile(如果没有提供显式规则,则使用隐式规则)。因此,制定 makefile 规则可确保始终保持最新状态,无需运行 deps 两次。此外,由于CPUDEPS
是一个makefile,它会在运行任何其他规则之前自动更新,因此如果需要,依赖项将始终更新,并且不需要make deps
。如果任何CCFILES
变得比依赖文件更新,您可能可以通过观察回显的[DEPS]
行来自己注意到这一点。对于问题 2,向配方中添加任何内容可确保 make 不会抱怨无事可做。如果没有其他情况,您可以使用诸如
@echo OK
之类的内容向用户提供反馈,或者如果您更喜欢完全无声的制作,则可以使用简单的@true
。Problem 1 is non-existant: before building a target,
make
automatically rebuilds makefiles (with implicit rules if no explicit rule is provided). So having a rule for the makefile ensures that will always be up to date, there is no need to run deps twice. Additionally, sinceCPUDEPS
is a makefile, it will be updated automatically before any other rule is run, so dependencies will always be updated if necessary andmake deps
is not needed. You can probably notice this by yourself by observing the[DEPS]
line being echoed if any of theCCFILES
becomes more recent that the dependency file.For Problem 2, adding anything to the recipe ensures that make doesn't complain about having nothing to do. If there is nothing else, you can use something like
@echo OK
to give feedback to the user, or a simple@true
if you prefer totally silent makes.您试图实现的目标是无用的:您可以使用在先前构建期间创建的依赖项文件。这就够了。
该规则背后的主要原因是:
#include
链中,那么依赖项文件已经捕获了它。您将重建所需的东西。解决方案是在正常的编译过程中创建依赖项文件,并选择包含它(使用
sinclude
)(如果存在)。What you are trying to achieve is useless: you can use the dependencies file that was created during the previous build. That's enough.
The main reasoning behind that rule is:
#include
chain, on an existing file that were used by previous build, then the dependencies file have already caught it. You'll rebuild what is needed.The solution is to create the dependencies file during the normal process of the compilation, and to optionally include it (with
sinclude
) if it is present.