Search code examples
makefileexit-code

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


Solution

  • 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.