user3132457
user3132457

Reputation: 1019

Local variable in makefile is not expanded correctly

I have the following function in makefile:

define INSTALL_SCRIPT
SRC_DIR = $(ROOT)\src
cd $(SRC_DIR)
$(SRC_DIR)\stage.bat
endef

I also echo the steps, so here's the output of the above snippet:

$SRC_DIR = C:\project_root\src
'SRC_DIR' is not recognized as an internal or external command,
operable program or batch file.
$cd
C:\project_root
\stage.bat
'\stage.bat' is not recognized as an internal or external command,
operable program or batch file.

It seems that in assignment statement the value is expanded correctly but then $(SRC_DIR) gives an error. Then cd goes to one directory up (and not src), then when I need to execute the batch file, $(SRC_DIR)'s value seems to be empty.

Upvotes: 0

Views: 338

Answers (1)

HardcoreHenry
HardcoreHenry

Reputation: 6377

Assuming you're trying to do this from a recipe context, you would need to do it as follows:

define INSTALL_SCRIPT
   set SRC_DIR=$(ROOT)\\src & \
   cd %SRC_DIR% & \
   %SRC_DIR%\\stage.bat
endef

sometarget:
   @$(INSTALL_SCRIPT)

You need the \ at the end of each line to concatinate them into a single recipe line (other wise the variable you set will fall out of context when the first recipe line's shell terminates). You seem to be using windows so I believe you need to use the %varname% syntax to refer to the variables. Notice that $(ROOT) is a makefile variable in this case, so it still uses the $ syntax. (Note that if you were in bash you would need to use $$ to refer to shell variables). You also need to double the \\ in directory names, as make will interpret the first slash as an escape, and then pass a single slash to cmd.

Note that my windows machine doesn't have make installed on it, so I couldn't test the above, so it's quite possible I missed something.

Upvotes: 1

Related Questions