HiveBrain v1.2.0
Get Started
← Back to all entries
debugbashMinor

Exit a Makefile target called by parent without throwing an error

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
withoutmakefiletargetcallederrorparentexitthrowing

Problem

Let's say that I have 2 targets:

test:
    # ...
    @$(MAKE) dosomething
    @echo test
    # ...

dosomething:
    # ...
    @if [ -z "$(SOMETHING)" ]; then exit 0; fi
    @echo dosomething
    # ...


I would like echo test to execute even if $(SOMETHING) is empty and echo dosomething only if $(SOMETHING) is not empty (which doesn't work with the example, it's still executed).

exit 0 of course doesn't work here since make ignores non-negative exit in subshells used to execute instructions.

Is there any other way to skip the rest of the target without breaking further execution of test target? I could make the rest a single long instruction of else but wondering if there's a different command I'm maybe missing.

Solution

Makefile

all: test dosomething

test:
    @echo "hello"

dosomething:
    @if [ "a" = "a" ]; then\
        echo "world";\
        exit 0;\
    fi


Running: make will return:

hello
world

Code Snippets

all: test dosomething

test:
    @echo "hello"

dosomething:
    @if [ "a" = "a" ]; then\
        echo "world";\
        exit 0;\
    fi
hello
world

Context

StackExchange DevOps Q#10367, answer score: 1

Revisions (0)

No revisions yet.