Makefile MAKEOVERRIDES

TL;DR The variable MAKEOVERRIDES passes the variables defined in the make command line to sub-makes (even those make invoked indirectly). Emptying MAKEOVERRIDES can prevent sub-makes from picking up variables unexpected.

In a recent project, I need to wrap up a few tools developed by others to build a flow that runs A, B, C … several tasks in certain order (due to the dependency). On the other side, every few steps would reach a meaningful milestone, such as an executable is built, an output file is written, or a report is generated by parsing the outputs and so on. So I tried Makefile that can define multple targets each for a milestone and also specify the dependency.

The issue occured when I added a variable, MYPWD, to Makefile. I want to execute the tools in different folders each time I kick off the flow so that there is no overwritten. With the variable MYPWD set to different path in the make command line (i.e. make MYPWD=/path/of/your/choice), I can easily get the varying path to invoke other tools. However, the tools from others are also wrapper scripts that involve make underneath, and MYPWD is such a popular variable name that is also used by the other scripts. Even my Makefile does not call the make in the other scripts directly, the make in the order scripts is still considered as sub-make of my Makefile. The sub-make is suppoed to pick up the correct MYPWD value written in a file generated by the other tools in flight. Nevertheless, the MYPWD variable specified in the command line takes higher priority, and fails the tools.

Apparently, the easiest solution should be I change to use another different variable name. But I wondered there should be other ways to limit the scope of variables. First I tried setting the environment variable when invoking the other wrapper scripts in my Makefile. It did not help. Then I learned the standard way for Makefile to communicate variables to a Sub-make is via export and unexport should do the reverse. But unexport does work in my case where the variable is initially set in the command line. For the variables set in the command line, make puts them into another variable MAKEOVERRIDES as explained in the documentation. By the way, similar to the variables taken from command line, other command line options that make takes get into MAKEFLAGS. Sub-make receives the command line info (variables and options) from those two, and if I set MAKEOVERRIDES to empty, then MYPWD I set in the command line would become invisible to Sub-make.

Problem solved ;)

Credits