GNU Makefile 相当于 shell ‘TRAP’用于简洁识别退出时构建失败的命令

发布于 2024-11-16 14:01:56 字数 992 浏览 2 评论 0原文

标准:Makefile 是一个 GNU Make Makefile - 我对 makepp、qmake、cmake 等不感兴趣。它们都很好(尤其是 cmake),但这是为了工作,在工作中我们使用 GNU Make。最佳解决方案是纯 Makefile 解决方案,而不是为您解析 make 的 shell 脚本。

我也不想做一个“失败后继续”的解决方案——如果它坏了,它就坏了,需要修复。

情况是这样的,我有一个 makefile 可以并行构建多个目录 - 如果其中一个目录失败,当然整个构建都会失败,但直到所有正在运行的 make 运行完成(或失败)。这意味着 make 实际上失败的原因被埋藏在距 make 输出末尾任意远的地方。

这是我得到的一个示例:

all: $(SUBDIRS)

SUBDIRS = \
  apple \
  orange \
  banana \
  pineapple \
  lemon \
  watermelon \
  grapefruit

$(SUBDIRS):
  cd $@ && $(MAKE) $(MFLAGS) 2>&1 | sed -e "s/^/$(notdir $(@)): /g"

如果我运行“make -j 5”并且“orange”恰好失败 - 我希望在最后看到这样的表 制作过程的

  apple     - passed
  orange    - FAILED
  banana    - passed
  pineapple - passed
  lemon     - passed

我考虑过 && echo“通过”>.结果|| echo "FAILED" >.result,但是 make 仍然需要某种 TRAP 或 __onexit() 清理命令在退出时打印它们。

任何 Makefile ninja 有一个纯 makefile 解决方案吗?

取消编辑 - 我的解决方案实际上并没有按照我希望的方式工作..陷入困境!

Criteria: Makefile is a GNU Make Makefile - I'm not interested in makepp, qmake, cmake, etc. They're all nice (especially cmake), but this is for work and at work we use GNU Make. The optimal solution is a pure Makefile solution rather than a shell script that parses make for you.

I also don't want to do a 'continue on failure' solution - if it's broken, it's broken and needs to be fixed.

The situation is this, I've got a makefile that builds several directories in parallel - if one of them fails, of course the whole build fails, but not until all the running makes run to completion (or failure). This means that the reason why make actually failed is buried somewhere arbitrarily far from the end of make's output.

Here's an example of what I've got:

all: $(SUBDIRS)

SUBDIRS = \
  apple \
  orange \
  banana \
  pineapple \
  lemon \
  watermelon \
  grapefruit

$(SUBDIRS):
  cd $@ && $(MAKE) $(MFLAGS) 2>&1 | sed -e "s/^/$(notdir $(@)): /g"

If I run 'make -j 5' and 'orange' happens to fail - I'd like to see a table like this at the end
of the make process

  apple     - passed
  orange    - FAILED
  banana    - passed
  pineapple - passed
  lemon     - passed

I've considered having an && echo "passed" >.result || echo "FAILED" >.result, but make still needs some sort of TRAP or __onexit() cleanup command to print at them on exit.

Any Makefile ninjas out there have a pure-makefile solution for this?

un-edit - my solution wasn't actually working the way I had hoped.. STYMIED!

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

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

发布评论

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

评论(2

绾颜 2024-11-23 14:01:56

当您希望 make 在第一次失败时中止,立即结束并杀死所有正在进行的作业而不是等待它们完成时,您需要像这样修补 GNU Make
http://lists.gnu.org/archive/html /bug-make/2009-01/msg00035.html

然后,您需要为 make 调用的每个 shell 设置一个陷阱(以及 set -o pipelinefail(如果您使用管道),如本文所述 http://lists.gnu.org/archive/html/help-make/2009-02/msg00011.html

简而言之:

target1:
    trap 'kill $(jobs -p)'; command && something || something-else
target2:
    trap 'kill $(jobs -p)'; set -o pipefail; command | sed '...'

When you want make to abort at the first failure, end immediately and kill all in-flight jobs instead of waiting for them to finish, you need to patch GNU Make like this
http://lists.gnu.org/archive/html/bug-make/2009-01/msg00035.html

Then you need to set a trap for every shell that make invokes (as well as set -o pipefail if you use a pipe), as described in this post http://lists.gnu.org/archive/html/help-make/2009-02/msg00011.html

In a nutshell:

target1:
    trap 'kill $(jobs -p)'; command && something || something-else
target2:
    trap 'kill $(jobs -p)'; set -o pipefail; command | sed '...'
红玫瑰 2024-11-23 14:01:56

我看到的唯一方法是使用子品牌自动执行:

all : subdirs

subdirs : 
    $(MAKE) -f $(lastword $(MAKEFILE_LIST)) subdirs-recursive || cat log

subdirs-recursive: $(SUBDIRS)

The only way I see is self-execution with a sub-make:

all : subdirs

subdirs : 
    $(MAKE) -f $(lastword $(MAKEFILE_LIST)) subdirs-recursive || cat log

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