Override target in makefile to add more commands?

前端 未结 6 709
小蘑菇
小蘑菇 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 17:52

    Adding another possible solution I've seen for posterity... I know the OP was wary about changing the common makefile, but something like this works and involves minimal changes.

    local makefile 1:

    CLEAN=MyExe1 MyExe2
    ....
    include /my/common/makefile
    

    local makefile 2:

    CLEAN=MyExe3 MyExe4
    ....
    include /my/common/makefile
    

    common makefile:

    clean:
         rm -f *.dep *.o *.a $(CLEAN)
    

    Basically the idea is to define some variable (in this case CLEAN) in each local makefile with all the specific items you want to delete. Then the common makefile runs rm -f on all the common file types to delete, plus whatever was specifically flagged for deletion in each local makefile via the CLEAN variable. If there's nothing specific to delete, simply omit the variable declaration or leave it empty (CLEAN=)

    So now if we run make clean for local makefile 1, it executes

    rm -f *.dep *.o *.a MyExe1 MyExe2
    

    And if we run make clean for local makefile 2, it executes

    rm -f *.dep *.o *.a MyExe3 MyExe4
    

提交回复
热议问题