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

梦想的初衷 提交于 2020-01-15 06:53:28

问题


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?


回答1:


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.


来源:https://stackoverflow.com/questions/38142121/in-an-aosp-android-mk-file-how-do-i-execute-a-command-and-fail-the-build-if-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!