bash 编译多个项目(使用 makefile),但停止第一个损坏的构建?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11206500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:36:38  来源:igfitidea点击:

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

bashmakefile

提问by Kiril Kirov

I want to do somethinglike:

我想做类似的事情

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

And this works fine, but I want "break" the for-loop in case of a broken build.

这工作正常,但我想for在构建损坏的情况下“中断” -loop。

Is there a way to do this? Maybe some kind of if-statement, that can check for success of make?

有没有办法做到这一点?也许某种 -if语句,可以检查make?

采纳答案by Blagovest Buyukliev

You can check whether the makehas exited successfully by examining its exit code via the $?variable, and then have a breakstatement:

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

...
make

if [ $? -ne 0 ]; then
    break
fi

回答by Eldar Abusalimov

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

您可以使用 Make 本身来实现您想要的:

SUBDIRS := $(wildcard */.)

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

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

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

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

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:.%=%)