How to get exit status of a shell command used in GNU Makefile?

后端 未结 3 1097
-上瘾入骨i
-上瘾入骨i 2020-12-14 14:59

I have a makefile rule in while I am executing a linux tool. I need to check the exit status of the tool command, and if that command fails the make has to be aborted.

3条回答
  •  囚心锁ツ
    2020-12-14 15:17

    If all you want is for the make to be aborted iff the tool exits with a nonzero status, make will already do that by default.

    Example Makefile:

    a: b
        @echo making $@
    b:
        @echo making $@
        @false
        @echo already failed
    

    . This is what happens with my make:

    $ make
    making b
    make: *** [Makefile:6: b] Error 1
    

    Make sure partially or wholly created targets are removed in case you fail. For instance, this

    a: b
        @gena $+ > $@
    b:
        @genb > $@
    

    is incorrect: if on the first try, genb fails, it will probably leave an incorrect b, which, on the second try, make will assume is correct. So you need to do something like

    a: b
        @gena $+ > $@ || { rm $@; exit 1; }
    b:
        @genb > $@
    

提交回复
热议问题