Reputation: 605
I'm new to nmake and needing to modify a build script. I want to execute a command and capture the result in a variable that will then be used throughout the script. However when attempting to use the shell command I receive the following error message:
makefile(4) : fatal error U1000: syntax error : ')' missing in macro invocation
Stop.
I put together a one line script to test the shell command.
Makefile:
CMDRESULT = $(shell hostname)
all:
echo $(CMDRESULT)
Here's the version of nmake being used:
>nmake /?
Microsoft (R) Program Maintenance Utility Version 11.00.60610.1
Copyright (C) Microsoft Corporation. All rights reserved.
Does nmake support the shell command? Is there another option in nmake to execute a command and capture the result?
Upvotes: 4
Views: 2917
Reputation: 111
It is possible to read another programs output into nmake variable using temporay files if the output contains a single line of text. It is also possible to use external scripts to modify multiple line outputs and make this solution work. An example to read a single line output to nmake variable:
!IF [hostname>hostname.tmp] == 0
H_NAME = \
!INCLUDE hostname.tmp
!IF [del hostname.tmp] == 0
!ENDIF
!ENDIF
This [for execution]
is used to execute some commands and compare the return code
of the program. It is done during pre-processing. After tha a hack described here "Can't figure out how to read file from nmake" is used to read a single line from temporary file.
Upvotes: 5
Reputation: 5893
No. Microsoft NMAKE cannot invoke functions (like shell). The $ operator only expands Nmake variables.
Shell is also not a standard make builtin. It is a gnu make extension.
Nmake only allows the invocation of (shell) commands in two ways, either as a command to be executed as part of a build or during pre-processing. If invoked during pre-processing only the exit code can be returned.
You can do the following:
!IF [..dos command..] != 0
command
!ENDIF
or you can do:
all:
if comparison command
The DOS/Windows command to invoke a command and compare to a string are not trivial and are a separate question.
Update:
I just wanted to show an example of how to actually achieve this as my original answer was a bit terse:
Although this is an old question, there is a method of doing what is asked; its just convoluted, like everything in batch files!
One has to use the combined mechanisms of the fact that make imports environmental variables and that the preprocessor can invoke commands, and then call the Makefile recursively. It assume the Makefile is called Makefile (with no extension, which is the default).
!IFNDEF MAKE
MAKE=NMAKE
!ENDIF
!IFNDEF SHELLVALUE
! IF [echo off && FOR /F "usebackq" %i IN (`hostname`) DO SET SHELLVALUE=%i && $(MAKE) /$(MAKEFLAGS) /nologo /f $(MAKEDIR)\Makefile && exit /b ] == 0
! MESSAGE Make completed
! ELSE
! ERROR Error in nmake
! ENDIF
!ELSE
# $(SHELLVALUE) now contains the string returned from the command USERNAME
!MESSAGE Shellvalue is $(SHELLVALUE)
# Put the parts of the makefile that depend on SHELLVALUE here
!ENDIF
#
# To be a valid makefile it must have some rules to perform
all:
@echo;$(SHELLVALUE)
Yes, I know its horrible, but it does demonstrate how to do the technique, which can be done with any shell command and not just hostname
.
Upvotes: 3