Override target in makefile to add more commands?

前端 未结 6 699
小蘑菇
小蘑菇 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:01

    I've seen this done at several shops. The most common approach is to use double-colon rules, assuming you're using something like GNU make. In your common makefile you would have something like this:

    clean::
            # standard cleanup, like remove all .o's:
            rm -f *.o
    

    Note that there are two colons following clean, not just one!

    In your other makefile you just declare clean again, as a double-colon rule:

    clean::
            # custom cleanup, like remove my special generated files:
            rm -f *.h.gen
    

    When you invoke make clean, GNU make will automagically run both of these "branches" of the clean rule:

    % make clean
    rm -f *.o
    rm -f *.h.gen
    

    It's simple to set up and it composes quite neatly I think. Note that specifically because it is a double-colon rule, you don't get the "overriding commands" errors you normally get when you define two rules for the same target. That's sort of the point of double-colon rules.

提交回复
热议问题