Viacheslav Kroilov
Viacheslav Kroilov

Reputation: 1671

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:

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.

Upvotes: 21

Views: 25224

Answers (2)

Alex Cohn
Alex Cohn

Reputation: 57183

sometimes, it helps to use a recursive call in make, e.g.

R=$(wildcard $1)$(if $2,$(call $0,$2,$3,$4,$5,$6,$7,$8,$9),)

but usually $(foreach) works better. The caveat is that there is no way to handle infinite number of parameters, and the formula above stops at the first empty parameter, i.e. you cannot skip one in the middle.

Upvotes: 1

MadScientist
MadScientist

Reputation: 100926

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)

Upvotes: 23

Related Questions