Search code examples
makefilegnu-make

How do I evaluate a specific recipe and a wildcard recipe for the same file in GNU Make (latest)?


Suppose I need to perform some task before a specific file type is ready to be created, but then I would like to run the generic rule:

foo.bar:
   # do something to prepare to make foo.bar

%.bar:
   # generate the `.bar` filetype

How do I evaluate the wild card recipe to generate foo.bar, having defined foo.bar for some specific prep work?


Solution

  • You can't. A given target in a makefile can have only one rule and make will never try to build the same target more than one time.

    What you can do is put the "shared" recipe into a variable, then use that variable in both places:

    GEN_BAR = # generate the bar file type
    
    foo.bar:
            # do something to prepare to make foo.bar
            $(GEN_BAR)
    
    %.bar:
            $(GEN_BAR)