tags:

views:

22

answers:

1

Hi everyone,

I have a makefile, definitions.mk, containing the following:

ifneq ($(MY_VARIABLE),true)
define my-function
  < some code defining the function)
endef
else
define my-function
 < different code defining the function>
endef
endif

Now, if MY_VARIABLE is set to false I want to include another file containing the definition of my-function. Like:

ifneq ($(MY_VARIABLE),true)
define my-function
  < some code defining the function)
endef
else
 <include file containing definition of function>
endif

The problem is that I cannot figure out how to do this include. If I write:

include $(BUILD_SYSTEM)/my_function.mk

I will get an error saying:

/bin/bash/: include: command not found

So, how do I go about this?

Any input is appreciated!

+1  A: 

Actually, your second example should work just fine. Based on the error you're seeing I guess that although you have typed the code correctly here, what you actually have in your makefile is something like this:

ifneq ($(MY_VARIABLE),true)
define my-function
    thing1
    thing2
endef
else
define my-function
    include $(BUILD_SYSTEM)/my_function.mk
endef
endif

That is, you have the include directive inside the define directive. That won't work, because of the way define works. Remember, define is just syntactic sugar to make it easier to create variables with multi-line values. That means that, conceptually anyway, this:

define FOO
    something
endef

is the same as this:

FOO=something

So effectively what I suspect you've done is my-function=include my_function.mk. Then when you try to reference my-function, it expands to include my_function.mk, which is subsequently passed off to the shell, which can't make sense out of it, of course.

To be perfectly clear, here's how you can make this work. First, make sure you have the define/endef in my_function.mk:

define my-function
    red fish
    blue fish
endef

and then in definitions.mk you need to have this:

ifneq ($(MY_VARIABLE),true)
define my-function
    thing1
    thing2
endef
else
    include $(BUILD_SYSTEM)/my_function.mk
endif

Note the absence of the define/endef lines around the include line!

Eric Melski
What a great explanation Eric! Thank you so very much! I did have the define/endef in the wrong place. Thanks again.
Suz