Search code examples
windowscmderror-codenmake

Propagate error code from recursive nmake on Windows


I have the following recursive makefile for nmake on Windows:

MODULES = admin char mailnews merintr intro dm chess

all:
    -@for %i in ($(MODULES)) do @if EXIST %i (cd %i && $(MAKE) /$(MAKEFLAGS) && cd ..)

If one of the makes in the subdirectories fails, I want the outer nmake invocation to return an error code. Currently it always returns 0.

I've tried various permutations of setting an error variable if the inner make fails, but I can't get it propagated to the outer level.


Solution

  • You need to allow nmake to perform the loop itself. The following makefile should work:

    MODULES = admin         \
            char            \
            mailnews        \
            merintr         \
            intro           \
            dm              \
            chess           \
    
    all: $(MODULES)
    
    clean:
        @$(MAKE) -$(MAKEFLAGS) $(MODULES) TARGET=$@
    
    $(MODULES): FORCE
        @if exist $@\ cd $@ && $(MAKE) -$(MAKEFLAGS) $(TARGET)
    
    FORCE: