Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile call function. How to get all arguments

When I define a custom function in a Makefile, like this

define add_target
${1}: ${2} ${3}
endef

How can I get a list of all arguments, that have been provided to $(call add_target, ...)?

So this $(call add_target, A, B) will be expanded to A: B and this $(call add_target, A, B, C, D) will be expanded to A: B C D

GNU manual only says that

When make expands this function, it assigns each param to temporary variables $(1), $(2), etc. The variable $(0) will contain variable. There is no maximum number of parameter arguments.

but nothing about how to get all params.

like image 538
Viacheslav Kroilov Avatar asked Aug 12 '16 15:08

Viacheslav Kroilov


People also ask

How do you call a function in makefile?

8.8 The call Function The call function is unique in that it can be used to create new parameterized functions. You can write a complex expression as the value of a variable, then use call to expand it with different values. The syntax of the call function is: $(call variable , param , param ,…)

What is $@ in makefile?

The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.

How do I use IFEQ in makefile?

The ifeq directive begins the conditional, and specifies the condition. It contains two arguments, separated by a comma and surrounded by parentheses. Variable substitution is performed on both arguments and then they are compared.

What is eval in makefile?

The eval function is very special: it allows you to define new makefile constructs that are not constant; which are the result of evaluating other variables and functions. The argument to the eval function is expanded, then the results of that expansion are parsed as makefile syntax.


1 Answers

There is no way, except by writing out $1 $2 $3 $4 $5 $6 ... Of course no matter how many variables you enumerate the caller could have used more.

However, in your example the simplest thing to do is just pass two arguments: the target name and the prerequisite list (as a single argument). So it would be:

define add_target
${1}: ${2}
endef

... $(call add_target, A, B)
... $(call add_target, A, B C D)

(no commas separating the prerequisites in the list)

like image 151
MadScientist Avatar answered Oct 18 '22 04:10

MadScientist