views:

677

answers:

2

Hi

How can I forward macros between nmake invocations?

Let's say we have

--- a.mak ---

some_variable = value

all:
   nmake -f b.mak

--- END ---

--- b.mak ---

all:
   @echo some_variable = WHAT TO PUT HERE TO GET VALUE OF some_variable?

--- END ---

I was trying different things like using set and setx commands but value of variable set in parent makefile is not visible in makefiles being invoked from within it.

+1  A: 

Here's the info from MSDN about calling nmake recursively:

Use recursion macros to call NMAKE recursively. Recursive sessions inherit command-line and environment-variable macros and Tools.ini information. They do not inherit makefile-defined inference rules or .SUFFIXES and .PRECIOUS specifications. To pass macros to a recursive NMAKE session, either set an environment variable with SET before the recursive call, define a macro in the command for the recursive call, or define a macro in Tools.ini.

So, you could make a.mak look like:

# ---- a.mak ----
some_variable = value

all:   
    nmake -f b.mak some_variable=$(some_variable)
#--- END ---

Also, note that using the set sommand to put the variable in the environment will work as well, but nmake automatically capitalizes environment variable names (even for weird ones like "windir" which is lowercase in the system for some reason), and is case-sensitive, so to use the environment variable, you must use the variable in uppercase.

From MSDN:

inherited names are converted to uppercase. Inheritance occurs before preprocessing

So, here's what your b.mak should look like if you're going to pass the variable using the environment instead of explicitly on the command line:

#--- b.mak ---
all:
   @echo some_variable is: $(SOME_VARIABLE)
#--- end ---

Because of this, it's probably not a bad idea to standardize on an all-caps naming convention for nmake macro names.

Michael Burr
Thank you very much for your very detailed answer.
Piotr Dobrogost
A: 

Maybe you should consider not calling make recursively, see the paper Recursive Make Considered Harmful. You can still have modular files similar to the makefiles you already have (included by make), just that the rules are only defined in one top level makefile.

hlovdal