Question

Given a master.makefile calling a sub-makefile such :

downloads:
    make -f downloads.makefile

And a sub-makefile downloads.makefile such :

download: clean
    curl -o ./data/<itemname>.png 'http://www.mysite.com/<itemname>.png'
    echo "downloaded <itemname>: Done!"

How to set a global variable itemname in my masterfile, available to my sub-makefile ?

Note: My project is actually more complex, with 12 different sub-makefile, which should reuse the same parameters/variable from the master file. The masterfile should assign the value, while the sub-makefile should retrive the variable's value where I put <itemname>.

Was it helpful?

Solution

There are various ways to do it.

First, always, always use $(MAKE) and never make when running a sub-make. Then...

1. You can pass the value on the recursive command line:

itemname = whatever
downloads:
        $(MAKE) -f downloads.makefile itemname=$(itemname)

2. You can export the variable in the parent makefile:

export itemname = myvalue
downloads:
        $(MAKE) -f downloads.makefile

with sub-makefile such :

download: 
    echo "downloaded $(itemname): Done!"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top