Passing variables from the include to sub-make directive

Look for some help by passing variables from one level to the next in a makefile. I have a source tree that needs to be built to work on different target architectures. To save a higher-level makefile, I created separate makefiles containing architecture-specific information and included only the one that is required using the include directive.

Later in the makefile, I will go to another directory to create the source files. The assembly fails and I see that the failure is caused by the fact that architecture variables are not passed.

ifeq ($(ARCH), my_arch) | include build/my_archdefs.mk | section 1 endif | <more commands> debug: $(MAKE) -C src debug 

The makefile for creating the code tree is in the src directory. As a measure of stopping mileage, I included section 1, mentioned above in the make file of the lower level file, in which case I noticed that the ARCH variable was not passed.

Here are a few links that I found that were related, but I can’t figure out what I need to do to 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 information that I need is hidden in the links to which I referred above, but I just do not see it. Any pointers would be much appreciated.

Thanks.

+6
source share
2 answers

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

At your top level Makefile, just add the export line and all your variables will be exported to your signatures.

Example:

Makefile :

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

Makefile2 file:

 all: @echo ${ID} 

Conclusion:

 $ make asdf make[1]: Entering directory `/home/user/Desktop/a' asdf make[1]: Leaving directory `/home/user/Desktop/a' 
+7
source

Example for your reference:

Below is the "Makefile", which is at the top level.

Makefile

 export ROOT_DIR=${PWD} all: $(MAKE) -C test 

Then in the "test" folder (relative to the current location) there is another "Makefile", as shown below:

Makefile

 all: echo $(ROOT_DIR) 

When you say "do everything" in the top-level folder, then it will be built in accordance with the current Makefile, and then in accordance with the specified rule in the "test" folder (using Makefile at "test" for the assembly rules). Therefore, when parent-Makefile exports some variable, then for this build environment, the exported variable will be visible to all Makefiles subdirectories.

However, if you try to explicitly enter the "test" folder and try to "do everything", you will have to explicitly set the environment variable before it (which in the previous case is set to uppercase, the Makefile level).

Additional information .

+2
source

Source: https://habr.com/ru/post/926784/


All Articles