Search code examples
makefiledependenciesbuild-processgnu-make

Makefiles: can 'canned recipes' have parameters?


My question concerns GNU's make.

If you have a sequence of commands that are useful as a recipe for several targets, a canned recipe comes in handy. I might look like this:

define run-foo
# Here comes a
# sequence of commands that are
# executed line by line
endef

Now you can use the canned recipe like this:

file1: input1:
    $(run-foo)

$(pattern) : sub/% : %.inp
    $(run-foo)

and so on. I wonder if it is possible to define canned recipes (or something similar) that take parameters, so that I could execute them like this:

file2: input2
    $(run-foo) specific-parameter2 "additional"

file3: input3
    $(run-foo) another-parameter3 "text"

Is this possible? Any hints welcome :)


Solution

  • You do this by:

    • Using parameters $1,$2... etc in your define-ed macro
    • Invoking the macro via $(call ...)

    e.g:

    define recipe
    echo $1
    endef
    
    all: t1 t2
    
    t1:
        $(call recipe,"One")
    
    t2:
        $(call recipe,"Two")