I'd like to use a function in a recursive call of make
.
I have the following in the principal Makefile:
define my_func
Hello $(1) from $(2)
endef
export my_func
And further, in another one called later, I have :
$(error my_func is $(call my_func,StackOverFlow,Me))
It gives me this output Makefile_rec.mk:1: *** my_func is Hello from . Stop.
But what I'd like is Makefile_rec.mk:1: *** my_func is Hello StackOverFlow from Me. Stop.
Is there a way using make
to export such a variable/function and get it to work when using call
function ?
Thanks !
As pointed by @Renaud Pacalet here, I would like also to use this kind of macro either in the current Makefile and in sub-makes.
If possible, not by including
a file each time I need the macro
If you want this to work you must double the $
signs in your definition:
define my_func
Hello $$(1) from $$(2)
endef
export my_func
From the manual:
To pass down, or export, a variable, make adds the variable and its value to the environment for running each line of the recipe. The sub-make, in turn, uses the environment to initialize its table of variable values.
You must protect the $
from one extra expansion.
Of course, you cannot use the same macro in the top Makefile. If it is a problem, you must define a macro for the top Makefile and another one for the sub-make Makefile:
host> cat Makefile
define my_func
Hello $(1) from $(2)
endef
my_func_1 := $(call my_func,$$(1),$$(2))
export my_func_1
all:
$(MAKE) -f Makefile_rec.mk
$(info TOP: $(call my_func,StackOverFlow,Me))
host> cat Makefile_rec.mk
all:
$(info BOT: $(call my_func_1,StackOverFlow,Me))
host> make --no-print-directory
TOP: Hello StackOverFlow from Me
make -f Makefile_rec.mk
BOT: Hello StackOverFlow from Me
make[1]: 'all' is up to date.