编译几个项目(使用makefile),但在第一次破坏的构建时停止?

Kir*_*rov 4 bash makefile

我想做的事情如下:

for i in *
do
    if test -d $i
    then
        cd $i; make clean; make; cd -;
    fi;
done
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作,但是for如果构建破坏,我希望"打破" -loop.

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

Eld*_*mov 19

您可以使用Make本身来实现您的目标:

SUBDIRS := $(wildcard */.)

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

$(SUBDIRS) :
    $(MAKE) -C $@ clean all
Run Code Online (Sandbox Code Playgroud)

如果任何目标失败,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:.%=%)
Run Code Online (Sandbox Code Playgroud)

  • 如何将"所有"和"干净"的任务分开,以便在顶层完成'make clean'?我已经尝试添加另一个目标但是,因为它还取决于$(SUBDIRS)它做了一个make并且干净. (2认同)
  • 对于任何可能想知道什么是 `: % :` 的人 https://www.gnu.org/software/make/manual/html_node/Static-Usage.html (2认同)