[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how to conditionally pass a function to a function
From: |
Michael Ploujnikov |
Subject: |
Re: how to conditionally pass a function to a function |
Date: |
Wed, 29 Oct 2008 11:40:00 -0400 (EDT) |
----- "Michael Ploujnikov" <address@hidden> wrote:
> Hi,
> I'm trying to write a function (rule-generator), which would generate
> actual Make rules based on functions/macros passed to it. In case when
> the user of rule-generator does not specify functions, default ones
> should be used. Otherwise, rule-generator should make sure to call the
> user-specified functions instead of the defaults. I've decided that
> rule-generator would be called within an $(eval $(call ...)) construct
> while the rule-generator itself will simply $(call ...) the other
> functions. Without the conditional choice between default and
> user-supplied functions my code looks like:
>
> default-start-fun=echo "starting" && \
> echo "started"
> default-end-fun=echo "ending" && \
> echo "ended"
>
> address@hidden "uh, oh, we are doing something different!" &&\
> echo "starting ... started"
>
> define rule-generator
> $(1):
> @echo "$(1) generating $(2)" && \
> $(call $(3)) && \
> $(call $(4))
> endef
>
> $(eval $(call
> rule-generator,my-rule1,package-name,default-start-fun,default-end-fun))
>
> Running this makefile results in:
>
> $ make -f /tmp/simple.mk
> my-rule1 generating package-name
> starting
> started
> ending
> ended
>
> Now, I would like to move the burden of specifying the default
> functions from the user of rule-generator to the actual rule-generator
> "internals". This way the user can either omit passing arguments to
> rule-generator like:
>
> $(eval $(call rule-generator,my-rule1,package-name,,))
>
> Or the user can specify custom functions like:
>
> $(eval $(call rule-generator,my-rule1,package-name,custom-start-fun,))
>
> I've tried a number of things that failed for various reasons. I've
> tried inserting conditionals into the body of rule-generator, but Make
> choked on the line continuation syntax:
>
> define rule-generator
> $(1):
> @echo "$(1) generating $(2)" && \
> ifdef 3
> $(call $(3)) && \
> else
> $(call default-star-fun) && \
> endif
> $(call $(4))
> endef
>
> This resulted in a confusion between make and shell commands:
>
> $ make -f /tmp/simple.mk
> my-rule1 generating package-name
> /bin/sh: ifdef: command not found
> make: *** [my-rule1] Error 127
>
> If I delete the " && \" after the first echo line (which I need in the
> final solution), make also chokes:
I know that I if I don't use && and line continuation ("\") then I can do
something like this:
default-start-fun=echo "starting" && \
echo "started"
default-end-fun=echo "ending" && \
echo "ended"
address@hidden "uh, oh, we are doing something different!" &&\
echo "starting ... started"
define rule-generator
$(1):
@echo "$(1) generating $(2)"
ifneq ($(3),)
$(call $(3))
else
$(call default-start-fun)
endif
$(call $(4))
endef
$(eval $(call
rule-generator,my-rule1,package-name,custom-start-fun,default-end-fun))
However, that would complicate the "meat" code of my real functions. I'm not
yet willing to take this path.
>
> $ make -f /tmp/simple.mk
> /tmp/simple.mk:21: *** missing `endif'. Stop.
>
> I tried setting variables inside the rule-generator, but outside of
> its target body. However, it seems that variable assignment is
> deferred until I don't need them any more:
>
> define rule-generator
> ifdef 3
> start-fun=$(3)
> else
> start-fun=default-star-fun
> endif
> $(1):
> @echo "$(1) generating $(2)" && \
> $(call $(start-fun)) && \
> $(call $(4))
> endef
>
> $(eval $(call rule-generator,my-rule1,package-name,custom-start-fun))
>
> $ make -f /tmp/simple.mk
> /bin/sh: -c: line 0: syntax error near unexpected token `&&'
> /bin/sh: -c: line 0: `echo "my-rule1 generating package-name" && &&
> echo "ending" && echo "ended"'
> make: *** [my-rule1] Error 2
>
> I've also tried $(eval ...) calls inside the if-statements but those
> seem to be executed regardless of the condition so the last one always
> overrides the previous occurrences.
>
> I would like to know if someone has found a solution in GNU Make for
> this kind of problem.
>
> Thanks.
>
> P.S. Please cc me since I'm not subscribed to the list.