[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: $(call ...) versus (macro-name ...)
From: |
Ken Smith |
Subject: |
Re: $(call ...) versus (macro-name ...) |
Date: |
Wed, 15 Feb 2006 11:36:08 -0500 |
User-agent: |
mutt-ng/devel-r569 (Linux) |
I have found value in calling functions which take no arguments because,
as Paul quoted from the manual, $(0) is defined. The appended makefile
demonstrates two such uses. One use is that your function can run
differently based on how it is called. The other use I demonstrate here
is that you can "namespace" variables by prepending $(0) to them. The
variables are still global but you can ignore them because it is
improbable that you'll have other variables with similar names.
Ken Smith
# Implemented with GNU Make 3.81beta4
override EMPTY :=
SPACE := $(EMPTY) $(EMPTY)
function-variant1 = $(function-implementation)
function-variant2 = $(function-implementation)
function-implementation = \
$(strip \
$(if $(0),,$(error This function must be called)) \
\
$(info 0="$(0)") \
\
$(eval $(0).variant := $(subst -,$(SPACE),$(0))) \
$(eval $(0).variant := $(word 2,$($(0).variant))) \
\
$(if $(filter variant1,$($(0).variant)), \
$(info Hi!), \
$(if $(filter variant2,$($(0).variant)), \
$(info Hello.), \
$(error Unrecognized variant "$($(0).variant)".) \
) \
) \
)
$(call function-variant1)
$(call function-variant2)
# Generates an error.
# $(call function-implementation)
# Generates an error.
# $(function-implementation)
# Generates an error.
# $(function-variant1)
# Generates an error.
# $(function-variant1)
.PHONY: all
all:
On Tue, Feb 14, 2006 at 07:53:13PM +0100, John Graham-Cumming wrote:
> Paul D. Smith wrote:
> >However, you're correct that in the special case where call is invoked
> >and not passed any arguments, the variable is simply expanded. So,
> >saying "$(call FOO)" is exactly identically equivalent to saying
> >"$(FOO)" (except a tiny bit slower).
>
> Actually there is one small difference. When you do $(call FOO) GNU Make's
> code resets the counter used to detect a variable that refers to itself and
> hence it's possible to use $(call FOO) to avoid a 'variable refers to itself'
> error if you really know what you are doing.
>
> I recently used this quirk to add interactive breakpoints to my GNU Make
> Debugger project.
>
> John.
> --
> John Graham-Cumming
> address@hidden
>
> Home: http://www.jgc.org/
> Blog: http://www.jgc.org/blog/
>
> POPFile: http://getpopfile.org/
> GNU Make Standard Library: http://gmsl.sf.net/
> GNU Make Debugger: http://gmd.sf.net/
> Fast, Parallel Builds: http://www.electric-cloud.com/
>
> Sign up for my Spam and Anti-spam Newsletter
> at http://www.jgc.org/
>
>
> _______________________________________________
> Help-make mailing list
> address@hidden
> http://lists.gnu.org/mailman/listinfo/help-make