如何处理GNU make错误中的sub-make? [英] How to handle the sub-make in GNU make errors?

查看:848
本文介绍了如何处理GNU make错误中的sub-make?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Makefile(GNU)中使用sub-make.但是,每当子制造失败时,主制造就会继续成功运行.我希望我的主Makefile在子Make失败时也会失败.我怎么做?

I'm using sub-make in my Makefile(GNU). But whenever sub-make fails the main make continues to run successfully after that. I want my main Makefile to fail whenever my sub-make fails. How do I do that?

all:   
    pushd ${STA_DIR};make clean;make ARGS=${A1},${A2};popd

只要STA_DIR中的make失败,主make就不会停止.我想停止主要品牌.

Whenever the make in STA_DIR fails the main make doesn't stop. I would like to have the main make stopped.

推荐答案

由于您没有提供有关规则外观的任何详细信息,因此我们无法回答该问题.当make执行程序时,无论它是编译器,链接器,文档格式化程序还是make的另一个实例,其工作方式均相同:如果退出代码为0,则在UNIX中为成功". ,然后继续执行下一条规则.如果退出代码为非0(在UNIX中为失败"),则进行停止(除非您使用了-k选项).

Since you didn't provide any details whatsoever about what your rules look like, we can't answer that question. When make executes a program, regardless of whether it's a compiler, or a linker, or a document formatter, or another instance of make, it works the same way: if the exit code is 0, which is "success" in UNIX, then make continues with the next rule. If the exit code is non-0, which is "failure" in UNIX, then make stops (unless you used the -k option).

仅当它尝试构建的所有目标都成功时,make程序本身才会以0退出.

The make program itself will exit with a 0 only if all the targets it tried to build succeeded.

因此,如果书写正确,则在子制造失败时,制造将停止.如果不是,那么调用子品牌的规则将丢失退出代码,这就是父品牌不会失败的原因.如果您提供了规则,我们可以告诉您确切的解决方法.

So, when written correctly, your make WILL stop when a sub-make fails. If it doesn't then your rule that invokes the sub-make is losing the exit code and that's why the parent make doesn't fail. If you'd provided your rule we could tell you exactly how to fix it.

仅凭我的经验,我建议您可能会有类似的规则来运行子品牌:

Based on nothing more than my experience I'll suggest you may have a rule like this to run sub-makes:

SUBDIRS = foo bar biz

all:
        for d in $(SUBDIRS); do $(MAKE) -C $$d; done

.PHONY: all

在这里,您没有检查每个子品牌的退出代码,因此它不会停止.仅最后一个命令的最终退出代码从shell发送回make.您应该像下面这样重写您的makefile(如果是这样的话):

Here you are not checking the exit code of each sub-make, so it won't stop. Only the final exit code of the last command is sent back from the shell to make. You should rewrite your makefile (if this is what it looks like) like this:

SUBDIRS = foo bar biz

all: $(SUBDIRS)

$(SUBDIRS):
        $(MAKE) -C $@

.PHONY: all $(SUBDIRS)

编辑

根据您的makefile并阅读以上内容,您会发现问题所在:脚本中的最终命令是popd,它不会失败,因此该命令的退出代码为成功.请注意,这里不需要使用pushdpopd(至少在UNIX系统上不需要),因为每个命令都在单独的shell中运行,因此在退出shell时会重置工作目录.

Based on your makefile and reading the above you can see what's wrong: the final command in your script is the popd which does not fail, and so the exit code of the command is success. Note that you don't need to use pushd and popd here (at least not on UNIX systems), because each command is run inside a separate shell and so the working directory is reset when the shell exits.

您想要这样的东西:

all:
        cd "${STA_DIR}" && ${MAKE} clean && ${MAKE} ARGS="${A1},${A2}"

&&运算符是成功的短路测试;如果这些命令中的任何一个失败,那么整个命令将立即失败.

The && operator is a short-circuiting test for success; if any one of those commands fails then the entire command will fail immediately.

另外,请记住在调用递归make时始终使用$(MAKE)${MAKE}(它们是相同的),请不要使用静态的make.

Also remember to always use $(MAKE) or ${MAKE} (they are identical) when invoking a recursive make, don't use the static make.

这篇关于如何处理GNU make错误中的sub-make?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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