Search code examples
shellmakefilereturn-valuegnu-make

How to check return value from the shell directive


In my Makefile, I need to test if the current directory is an SVN repo or not and if it is not I want to indicate an error using the $(error) directive in Makefile.

So I plan to use the return value of $(shell svn info .) but I'm not sure how to get this value from within the Makefile.

Note: I'm not trying to get the return value in a recipe, but rather in the middle of the Makefile.

Right now I'm doing something like this, which works just because stdout is blank when it is an error:

SVN_INFO := $(shell svn info . 2> /dev/null)
ifeq ($(SVN_INFO),)
    $(error "Not an SVN repo...")
endif

I'd still like to find out if it is possible to get the return value instead within the Makefile.


Solution

  • This worked fine for me - based on @eriktous' answer with a minor modification of redirecting stdout as well to skip the output from svn info on a valid svn repo.

    SVN_INFO := $(shell svn info . 1>&2 2> /dev/null; echo $$?)
    ifneq ($(SVN_INFO),0)
        $(error "Not an SVN repo...")
    endif