steffens21 steffens21 - 2 months ago 7
Bash Question

Recursive make loses status code

I have the following makefile structure

$ cat makefile
foo:
$(MAKE) -C bar barbaz

$ cat bar/makefile
barbaz:
exit 111


When I make the target foo I would expect the status code 111 but instead I get status code 2:

$ make foo
make -C bar barbaz
make[1]: Entering directory `bar'
exit 111
make[1]: *** [barbaz] Error 111
make[1]: Leaving directory `bar'
make: *** [foo] Error 2


How can I have the outer make fail with "Error 111" instead of "Error 2"?

BTW: I'm using GNU Make 3.81

Kaz Kaz
Answer

This is not an issue with recursive make invocation; that is a red herring.

If you simply do this from the shell:

$ make barbaz # in bar directory
exit 111
make[1]: *** [barbaz] Error 111
$ echo $?
2

Make isn't propagating the exact exit status of the failing recipe.

This is documented here:

https://www.gnu.org/software/make/manual/html_node/Running.html

"The exit status of make is always one of three values: [...]'