Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables from include directive to sub-make

Tags:

makefile

Looking for some help passing variables from one level to the next in a makefile. I have a source tree that needs to be built to run on various target architectures. To keep the higher level makefile clean I created separate makefiles that contain architecture specific information and include only the one that is required using the include directive :)

Later in the makefile I transfer to another directory to build the source files. The build fails and I see that the failure is caused by the architecture-specific variables not being passed.

ifeq ($(ARCH), my_arch)        |
include build/my_archdefs.mk   |  section 1 
endif                          |

<more commands>
debug:
      $(MAKE) -C src debug

The makefile to build the code tree is in the src directory. As a stop gap measure I included section 1 referenced above in the lower level makefile and in this case I noticed that the variable ARCH was not being passed down.

Here are a couple of links I found that seemed related but I'm not able to figure out what I need to do make this work. http://www.gnu.org/software/make/manual/html_node/Options_002fRecursion.html#Options_002fRecursion http://www.gnu.org/software/make/manual/html_node/Include.html

It seems to me that the info I need is lurking in the links I referenced above but I'm just not seeing it. Any pointers will be much appreciated.

Thanks.

like image 675
dinesh Avatar asked Oct 02 '12 19:10

dinesh


People also ask

How can we pass variables from top-level makefile to sub makefiles?

Variable values of the top-level make can be passed to the sub- make through the environment by explicit request. These variables are defined in the sub- make as defaults, but they do not override variables defined in the makefile used by the sub- make unless you use the ' -e ' switch (see Summary of Options).

How does include work in makefile?

The include directive tells make to suspend reading the current makefile and read one or more other makefiles before continuing. The directive is a line in the makefile that looks like this: include filenames … filenames can contain shell file name patterns.

What is := in makefile?

= defines a recursively-expanded variable. := defines a simply-expanded variable.


1 Answers

This link should help: http://www.gnu.org/software/make/manual/html_node/Variables_002fRecursion.html#Variables_002fRecursion

In your top level Makefile just add the line export and all of your variables will be exported to your submakes.

Example:

File Makefile:

ID=asdf
export
all:
    @echo ${ID}
    @make -f Makefile2

File Makefile2:

all:
    @echo ${ID}

Output:

$ make
asdf
make[1]: Entering directory `/home/user/Desktop/a'
asdf
make[1]: Leaving directory `/home/user/Desktop/a'
like image 159
Kristina Avatar answered Nov 16 '22 03:11

Kristina