help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

techniques for overriding/inheriting rules


From: Adam Spiers
Subject: techniques for overriding/inheriting rules
Date: Sat, 6 Sep 2008 12:25:22 +0100
User-agent: Mutt/1.5.18 (2008-05-17)

Hi all,

I saw the technique for overriding rules in the "Overriding Part on
Another Makefile" section of the manual, and had some thoughts which
I'd like to run by the list.

Firstly, the technique relies on recursive invocations of make:

    ############################################################
    # parent makefile:
    foo:
            @echo "foo in parent"

    bar:    @echo "bar in parent"

    ...

    ############################################################
    # child makefile, inherits bar, overrides foo:
    foo:
            @echo "foo in child, overriding foo in parent"

    %: force
            @$(MAKE) -f parent.mk $@
    force: ;

As a result, I was wondering whether, in much more complex examples,
this could potentially cause the kind of problems described in the
(in)famous "Recursive Make Considered Harmful" as a result of
splitting a dependency tree (DAG) into multiple pieces?

Secondly, and with that in mind, I was wondering if a similar effect
could be achieved by placing the parent rules in a separate
"namespace":

    ############################################################
    # parent makefile

    # define a separate namespace
    PKG=parent

    % :: $(PKG)-%
    # We need a command here to activate the match-anything rule
            @echo "Inherited $@ from parent"

    $(PKG)-foo:
            @echo "foo in parent"

    ...

    ############################################################
    # child makefile, inherits bar, overrides foo:

    include parent.mk

    foo:
            @echo "foo in child, overriding foo in parent"

This seems to work great for PHONY rules where the target isn't a real
file, and has a number of other benefits:

   * No danger of harmful effects of recursive make (I hope I'm not
     unintentionally spreading FUD here!)

   * Should work with parallel make

   * Can explicitly determine which rules can be inherited:
      * Inheritable rules start with $(PKG)- prefix, others don't

   * Inheritance can be chained:

       parent-% :: grandparent-%%

     (hmm, would the terminal rules cause problems here?)

   * (Stretching the OO analogy way too far) multiple inheritance can
     also be achieved

       % :: parent2-%

However, there is one small downside - that you have to use namespace
prefix a lot - and one HUGE issue, which is that it doesn't work when
the target *is* a real file.

Can anyone think of a way to solve this?

I thought of two other ways of achieving inheritance/overriding.  One
is simply that it works already as is, so you just need to selectively
hide those annoying warnings ;-)

  make 2>&1 | egrep -v "warning: (overriding commands|ignoring old commands) 
for target $target"

The last one is actually a feature request, but I can't imagine it
would be hard to implement.  You could have a magic variable:

  ALLOW_OVERRIDE = foo bar

which would disable warnings whenever commands for targets `foo' or
`bar' were overridden.  This would seem the cleanest of all four
options to me.

Thoughts very welcome!
Adam




reply via email to

[Prev in Thread] Current Thread [Next in Thread]