help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Missing arguments to macros


From: Paul D. Smith
Subject: Re: Missing arguments to macros
Date: Fri, 15 Nov 2002 12:11:46 -0500

%% Robert Mecklenburg <address@hidden> writes:

  rm> The details of macro argument processing aren't clear to me, so
  rm> I'm asking.  It appears that all macro arguments are optional, for
  rm> instance,

  rm> define g
  rm>   $1 $2
  rm> endef

Well, they're optional in the sense that since the call function doesn't
know how many arguments the variable requires, it doesn't complain if
you don't provide enough arguments.

As with all make macros, if you expand one that doesn't have a value it
expands to the empty string.

  rm> The macro g can be called with any number of arguments, e.g.,
  rm> "$(call g,a)" or "$(call g,a,b,c)".  Extra arguments are never
  rm> expanded/substituted and missing arguments have an empty value.
  rm> Is this correct?

Make has a very simple mechanism for variable expansion.  First is that
any time you expand a variable which has never been defined, it expands
to the empty string.

Second is that some commands create a new "variable scope"; for example,
when make is about to run a command script it creates a new variable
scope and defines the automatic variables like $@, etc. inside that
scope.

Likewise, when call is going to expand a function it creates a new
variable scope and assigns the 1, 2, 3, etc. variables in that new
scope.

When a make wants to find the value of a variable it starts at the
innermost scope and looks for that variable.  If it's found, then the
value is substituted.  If it's not found, make moves to the next outward
scope and looks there.  If make gets out to the global scope and the
variable is not defined yet, then it expands to the empty string.


So you can see that nested call functions also nest scopes, and if you
reference $5 in the innermost scope but it doesn't exist there, it will
keep looking up to previous scopes for a value, just as you see.


So.  The question is, would it be a good idea for an invocation of call
to "blank out" all the variables (by declaring that variable with the
empty value in the innermost scope before expanding the function)?  For
example, make could keep track of the maximum number of variables used
so far by any call within this recursive invocation, and whenever it's
about to expand a function with fewer than that number of arguments, it
could set the remaining ones to "".

The reason I had not replied earlier was because I haven't had time to
really think about this: it might not be so straightforward.  For
example, another problem with call is that since it expands all its
arguments up-front you can't use conditionals, etc. as arguments--I
thought this could be fixed but doing so introduced other problems.  So,
we'll have to try it and see I think.

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <address@hidden>          Find some GNU make tips at:
 http://www.gnu.org                      http://make.paulandlesley.org
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist




reply via email to

[Prev in Thread] Current Thread [Next in Thread]