Gerardo
Gerardo

Reputation: 7767

Get exit code 1 on Makefile if statement

I'm trying to get the exit code on the ifdef statement if the statement is not true, but I tried by using exit 1 and $(call exit 1)

when using the first on the following code I get "Makefile:11: * missing separator. Stop."

...

ifdef PACKAGE
    PACKAGEDIR = $(HOME)/$(PACKAGE)
else
    exit 1
endif

...

By using $(call exit 1) I get no error but the makefile still keeps executing. What I'm trying to accomplish is to exit the Makefile on the else with the error code 1

Thanks

Upvotes: 3

Views: 10387

Answers (3)

Oli Girling
Oli Girling

Reputation: 763

You can use @exit 1. An example:

ifdef PACKAGE
    PACKAGEDIR = $(HOME)/$(PACKAGE)
else
    @exit 1
endif

Upvotes: 0

MadScientist
MadScientist

Reputation: 100781

As geekosaur says you can't put a shell command like exit 1 as a makefile operation. Makefiles are not shell scripts, although they can contain shell scripts. Shell commands can only appear within a target recipe, and nowhere else.

If you have a sufficiently new version of GNU make you can use the $(error ...) function, like this:

ifdef PACKAGE
    PACKAGEDIR = $(HOME)/$(PACKAGE)
else
    $(error You must define the PACKAGE variable)
endif

Also note that ifdef will be true if the variable is defined, even if it's defined to be the empty string. You may prefer:

ifneq ($(PACKAGE),)
    PACKAGEDIR = $(HOME)/$(PACKAGE)
else
    $(error You must define the PACKAGE variable)
endif

to ensure the variable is set to a non-empty value.

And, it's possible your version of GNU make is too old to support the $(error ...) function, although it's been around for a long time now.

Upvotes: 8

geekosaur
geekosaur

Reputation: 61369

You can't simply have bare code sticking somewhere in a Makefile; code (such as exit 1) is always associated with a build rule of some kind.

In this case you want the $(error) function. It may not be sufficient to drop it in the else, though, for the same reason that exit 1 itself won't work there; you may need to rephrase the whole thing as

PACKAGEDIR := $(if $(flavor PACKAGE),undefined,$(error PACKAGE must be defined!),$(HOME)/$(PACKAGE))

Upvotes: 0

Related Questions