Force Makefile to execute script after building any target (just before exiting)

匿名 (未验证) 提交于 2019-12-03 01:01:02

问题:

I'd like to execute a shell command when make exits, regardless of whatever target it built. Seems like make doesn't have a way to do this directly. However, here's an example of having make execute a shell command upon startup, regardless of the target:

Force Makefile to execute script before building targets

Is there any similar hack have make execute a shell command once just before exiting? I could put the command at the bottom of every target, but a) it would get executed multiple times, and b) that is ugly and difficult to manage.

回答1:

As janos says, there is no facility built into make to do it. You can use a wrapper such as he suggests. You an also, if you're willing to live with some amount of "bleah", push that into your makefile using recursion. It would look something like this, perhaps:

ifdef RECURSING      ... normal makefile goes here ...  else  .DEFAULT all:         @$(MAKE) RECURSING=true $@ ; r=$$? ; \          <extra shell command here>; \          exit $$r  endif 


回答2:

The only solution I see is to create a wrapper script that simply passes all arguments to make and then runs the custom action you want:

#!/bin/sh make $* && ./custom_script.sh 


回答3:

You could use two makefiles, one of which calls the other. For example

wrapper.mk:

foo bar:         make -f other.mk $@         @echo finished .PHONY: foo bar 

other.mk:

foo bar:         @echo Building $@ 

Then running "make -f wrapper.mk foo", wrapper.mk runs other.mk to build foo. The .PHONY target means that the targets will be passed to other.mk for consideration everytime regardless of whether the files exist or not.

This requires listing all your targets in wrapper.mk - there may be ways round this using default rules or match-anything rules.



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