Makefile:在包含另一个 makefile 之前需要执行一个目标

发布于 2024-10-14 22:27:36 字数 801 浏览 2 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(2

这样的小城市 2024-10-21 22:27:36

问题 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, since CPUDEPS is a makefile, it will be updated automatically before any other rule is run, so dependencies will always be updated if necessary and make deps is not needed. You can probably notice this by yourself by observing the [DEPS] line being echoed if any of the CCFILES 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.

空袭的梦i 2024-10-21 22:27:36

您试图实现的目标是无用的:您可以使用在先前构建期间创建的依赖项文件。这就够了。

该规则背后的主要原因是:

  • 如果您没有更改任何文件,则依赖项文件是最新的,并且无需构建任何内容。
  • 如果您对先前构建使用的现有文件进行了任何更改,甚至是深入到 #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:

  • if you haven't changed any of your files, then the dependencies file is up-to-date, and there's nothing to build.
  • if you have changed anything, even very deep into your #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.
  • if you change something in a new file (you add that file!) then it was not used by previous build, and not listed in dependencies. But if you really want to use it, then you have to modify at least one of your other files that was used before, and you're back on the previous case.

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.

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