【问题标题】:In an AOSP Android.mk file, how do I execute a command and fail the build if the command fails?在 AOSP Android.mk 文件中,如果命令失败,如何执行命令并导致构建失败?
【发布时间】:2016-07-01 09:42:32
【问题描述】:

在 Android.mk 文件中,我有以下行执行 bash 脚本:

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

但是,如果命令失败,构建会继续而不是退出。

在这种情况下如何使整个构建失败?

【问题讨论】:

    标签: android makefile android-source


    【解决方案1】:

    转储stdout,测试退出状态,失败报错:

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

    下面是失败的样子:

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

    如果您想查看stdout,则可以将其保存到变量中并仅测试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
    

    给了

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

    【讨论】:

    • 谢谢,这很好用,但我如何让它在控制台中也显示标准输出以及标准错误?
    • @CameronMartin,很好。我用捕捉stdout 并通过lastword 测试的技巧修改了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2018-02-18
    • 2020-02-14
    • 2014-03-03
    • 2011-12-22
    • 2021-12-17
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    相关资源
    最近更新 更多