Override target in makefile to add more commands?

前端 未结 6 700
小蘑菇
小蘑菇 2020-12-23 17:16

At work we use a common makefile that other makefiles include (via the include statement) and it has a generic \"clean\" target that kills some common files. I want to add

6条回答
  •  情话喂你
    2020-12-23 18:14

    You can write your own clean and make it a preq of the common clean.

    clean: myclean
    
    myclean:
        rm whatever
    

    Yours will run first. If for some reason you want the common clean to run first then the solution will be more complicated.

    EDIT:

    Here is the best solution I can see which runs the common rule before the local one:

    include Makefile.common
    
    clean:
        $(MAKE) -f Makefile.common $@
        rm whatever additional things
    

    The include directive is necessary because the local makefile relies on the common one for things other than clean. The local clean rule overrides the common clean rule, but invokes the common clean rule before doing the additional work. (This overriding will cause some warnings, which is a nuisance; I don't know a good way to silence them.)

提交回复
热议问题