Search code examples
androidmakefileandroid-source

In an AOSP Android.mk file, how do I execute a command and fail the build if the command fails?


In an Android.mk file, I have the following line which executes a bash script:

$(info $(shell ($(LOCAL_PATH)/build.sh)))

However, if the command fails the build continues rather than quitting.

How do I make the whole build fail in this situation?


Solution

  • Dump stdout, test the exit status, and error out on failure:

    ifneq (0,$(shell >/dev/null command doesnotexist ; echo $$?))
      $(error "not good")
    endif
    

    Here's what failure looks like:

    [user@host]$ make 
    /bin/sh: doesnotexist: command not found
    Makefile:6: *** not good.  Stop.
    [user@host]$
    

    If you want to see stdout, then you can save it to a variable and test only the lastword:

    FOOBAR_OUTPUT := $(shell echo "I hope this works" ; command foobar ; echo $$?)
    $(info $$(FOOBAR_OUTPUT) == $(FOOBAR_OUTPUT))
    $(info $$(lastword $$(FOOBAR_OUTPUT)) == $(lastword $(FOOBAR_OUTPUT)))
    ifneq (0,$(lastword $(FOOBAR_OUTPUT)))
      $(error not good)
    endif
    

    which gives

    $ make
    /bin/sh: foobar: command not found
    $(FOOBAR_OUTPUT) == I hope this works 127
    $(lastword $(FOOBAR_OUTPUT)) == 127
    Makefile:12: *** not good.  Stop.