Can GNU make execute a rule whenever an error occurs?

后端 未结 1 1944
闹比i
闹比i 2021-02-05 09:18

This is slightly different from Can a Makefile execute code ONLY when an error has occurred?.

I\'d like a rule or special target that is made whenever an error occurs (i

1条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 09:59

    Gnu doesn't support it explicitly, but there's ways to hack almost anything. Make returns 1 if any of the makes fail. This means that you could, on the command line, rerun make with your error rule if the first make failed:

    make || make error_targ
    

    Of course, I'll assume you just want to put the added complexity within the makefile itself. If this is the case, you can create a recursive make file:

    all: 
         $(MAKE) normal_targ || $(MAKE) error_targ
    
    normal_targ:
          ... normal make rules ...
    
    error_targ:
          ... other make rules ...
    

    This will cause the makefile to try to build normal_targ, and iff it fails, it will run error_targ. It makes it a bit harder to read the makefile for the inexperienced, but it puts all the logic in one place.

    0 讨论(0)
提交回复
热议问题