Compile several projects (with makefile), but stop on first broken build?

前端 未结 2 1275
一个人的身影
一个人的身影 2020-12-29 17:04

I want to do something like:

for i in *
do
    if test -d $i
    then
        cd $i; make clean; make; cd -;
    fi;
done

And this

相关标签:
2条回答
  • 2020-12-29 17:11

    You can use Make itself to achieve what you're looking for:

    SUBDIRS := $(wildcard */.)
    
    .PHONY : all $(SUBDIRS)
    all : $(SUBDIRS)
    
    $(SUBDIRS) :
        $(MAKE) -C $@ clean all
    

    Make will break execution in case when any of your target fails.

    UPD.

    To support arbitrary targets:

    SUBDIRS := $(wildcard */.)  # e.g. "foo/. bar/."
    TARGETS := all clean  # whatever else, but must not contain '/'
    
    # foo/.all bar/.all foo/.clean bar/.clean
    SUBDIRS_TARGETS := \
        $(foreach t,$(TARGETS),$(addsuffix $t,$(SUBDIRS)))
    
    .PHONY : $(TARGETS) $(SUBDIRS_TARGETS)
    
    # static pattern rule, expands into:
    # all clean : % : foo/.% bar/.%
    $(TARGETS) : % : $(addsuffix %,$(SUBDIRS))
        @echo 'Done "$*" target'
    
    # here, for foo/.all:
    #   $(@D) is foo
    #   $(@F) is .all, with leading period
    #   $(@F:.%=%) is just all
    $(SUBDIRS_TARGETS) :
        $(MAKE) -C $(@D) $(@F:.%=%)
    
    0 讨论(0)
  • 2020-12-29 17:34

    You can check whether the make has exited successfully by examining its exit code via the $? variable, and then have a break statement:

    ...
    make
    
    if [ $? -ne 0 ]; then
        break
    fi
    
    0 讨论(0)
提交回复
热议问题