如何使失败的$(shell)命令中断 [英] How to make a failing $(shell) command interrupt Make

查看:362
本文介绍了如何使失败的$(shell)命令中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Makefile,该文件通过在应用构建规则之前运行工具开始(该工具为我编写).如果此工具(是python脚本)以非空状态代码退出,则我希望GNU Make在那里停止,而不继续构建程序.

I have a Makefile that starts by running a tool before applying the build rules (which this tool writes for me). If this tool, which is a python script, exits with a non-null status code, I want GNU Make to stop right there and not go on with building the program.

目前,我正在执行类似的操作(顶层,即第1列):

Currently, I do something like this (top level, i.e. column 1):

$(info Generating build rules...)
$(shell python collect_sources.py)
include BuildRules.mk

但是,如果collect_sources.py以状态代码1退出,这并不会停止make.这也捕获了collect_sources.py的标准输出,但没有将其打印出来,所以我觉得我找错了方向.

But this does not stop make if collect_sources.py exits with a status code of 1. This also captures the standard output of collect_sources.py but does not print it out, so I have the feeling I'm looking in the wrong direction.

在可能的情况下,当简单的MS-DOS shell是标准系统shell时,该解决方案甚至应该可以工作.

If at all possible, the solution should even work when a simple MS-DOS shell is the standard system shell.

有什么建议吗?

推荐答案

好,这是我自己的解决方案,不幸的是,该解决方案不是基于collect_sources.py脚本的状态代码,而是对我有效(TM)并允许我看到了脚本产生的任何输出:

Ok, here's my own solution, which is unfortunately not based on the status code of the collect_sources.py script, but which Works For Me (TM) and lets me see any output that the script produces:

SHELL_OUTPUT := $(shell python collect_sources.py 2>&1)
ifeq ($(filter error: [Errno %],$(SHELL_OUTPUT)),)
  $(info $(SHELL_OUTPUT))
else
  $(error $(SHELL_OUTPUT))
endif

编写脚本是为了使任何错误均产生以"collect_sources: error:"开头的输出.此外,如果python无法找到或执行给定脚本,它将输出一条错误消息,其中包含消息"[Errno 2]"或类似消息.因此,这小段代码仅捕获输出(将stderr重定向到stdout)并搜索错误消息.如果未找到,则仅使用$(info)打印输出,否则使用$(error),有效地使Make停止.

The script is written so that any error produces an output beginning with "collect_sources: error:". Additionally, if python cannot find or execute the given script, it outputs an error message containing the message "[Errno 2]" or similar. So this little piece of code just captures the output (redirecting stderr to stdout) and searches for error messages. If none is found, it simply uses $(info) to print the output, otherwise it uses $(error), which effectively makes Make stop.

请注意,ifeq ... endif中的缩进是使用空格完成的.如果使用了制表符,Make会认为您正在尝试调用命令并抱怨该命令.

Note that the indentation in the ifeq ... endif is done with spaces. If tabs are used, Make thinks you're trying to invoke a command and complains about it.

这篇关于如何使失败的$(shell)命令中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆