0

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
user3132457
  • 789
  • 2
  • 11
  • 29
  • 1
    The assignment is neither valid `cmd` nor `sh`; the error message indicates you tried to use it in a recipe which you are not showing. In `cmd`, variable assignments use `set`; in `sh`, you cant't have spaces around the equals sign. – tripleee Jun 24 '19 at 18:03
  • It's actually more deeply flawed than just that -- if it's a macro which is expanded in a recipe, then: `SRC_DIR` will be set on the first recipe line, and won't be visible in the second line. Even if you did concatenate the lines, it will expand `$(SRC_DIR)` when it is trying to expand the macro as a makefile variable, so the resulting bash command will be `cd `... – HardcoreHenry Jun 24 '19 at 18:27
  • Ahh, never mind -- it's a windows script... I believe the recipe line thing holds -- the `$` definately doesn't work though... You might need `%SRC_DIR%`... – HardcoreHenry Jun 24 '19 at 18:44

1 Answers1

1

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.

HardcoreHenry
  • 5,909
  • 2
  • 19
  • 44
  • `cmd` doesn't understand `;` `&` is used instead – Matt Jun 24 '19 at 18:54
  • Thanks. As you can tell I'm more of a bash person... Hopefully that runs. – HardcoreHenry Jun 24 '19 at 18:57
  • Unfortunately, there's no way to guess what is make's SHELL on Windows. If there's any `sh.exe` in PATH then gmake will use it instead of cmd. – Matt Jun 24 '19 at 19:01
  • Note to OP: as per @Matt's comment: If it's using the `sh` then the slashes in the directory name are wrong, it would need to use `;`, and `$(SRC_DIR)` would be treated as a command instead of a variable... It would be a completely different answer... – HardcoreHenry Jun 24 '19 at 19:10