How to handle the sub-make in GNU make errors?

不羁的心 提交于 2019-12-08 03:16:44

问题


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

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


回答1:


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).

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

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)

EDIT

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.

You want something like this:

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.

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



来源:https://stackoverflow.com/questions/16492512/how-to-handle-the-sub-make-in-gnu-make-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!