Saqib Ali
Saqib Ali

Reputation: 12585

Can I make a makefile abort outside of a rule?

At the top of my Makefile, before any of the rules, I have the following:

ifeq ($(ENVIRONMENT),LOCAL)
    TARGET := local_target
else
    TARGET := hello
endif

If the ENVIRONMENT environment variable is not set, or is set to a value other than LOCAL, instead of setting TARGET to hello, I want the makefile to halt and exit with -1.

Can I do that?? When I change TARGET := hello to exit -1, I get the following error:

Makefile:4: *** missing separator.  Stop.

How can I make this work??

Upvotes: 2

Views: 1169

Answers (1)

jfMR
jfMR

Reputation: 24738

exit is a shell command, so you could use the shell assignment operator (i.e.: !=) inside the Makefile to call exit:

TARGET != exit -1

This would be actually equivalent to:

TARGET := $(shell exit -1)

Note again that this calls a shell that in turns runs the shell's exit built-in, i.e.: it does not exit make. The typical approach for exiting while processing a makefile is to call GNU Make's error built-in function instead:

$(error Error-message-here)

Putting everything together:

ifeq ($(ENVIRONMENT),LOCAL)
 TARGET := local_target
else # indent with spaces, not a tab
 $(error ENVIRONMENT not set to LOCAL)
endif

Upvotes: 4

Related Questions