4

我想做类似的事情

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

这很好用,但我想for在构建损坏的情况下“打破”-loop。

有没有办法做到这一点?也许某种if-statement,可以检查成功make吗?

4

2 回答 2

19

您可以使用 Make 本身来实现您正在寻找的内容:

SUBDIRS := $(wildcard */.)

.PHONY : all $(SUBDIRS)
all : $(SUBDIRS)

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

如果您的任何目标失败,Make 将中断执行。

UPD。

支持任意目标:

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:.%=%)
于 2012-06-26T11:49:46.107 回答
4

您可以make通过$?变量检查其退出代码来检查是否已成功退出,然后有一个break语句:

...
make

if [ $? -ne 0 ]; then
    break
fi
于 2012-06-26T11:42:48.597 回答