Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

makefile

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 works fine, but I want "break" the for-loop in case of a broken build.

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

like image 677
Kiril Kirov Avatar asked Nov 28 '22 16:11

Kiril Kirov


2 Answers

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:.%=%)
like image 169
Eldar Abusalimov Avatar answered Dec 06 '22 09:12

Eldar Abusalimov


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
like image 43
Blagovest Buyukliev Avatar answered Dec 06 '22 09:12

Blagovest Buyukliev