Search code examples
escapingnmake

NMake macro definition on CLI which contains a space


I have a Makefile which requires an output path to be set.

Right now I use

nmake /nologo /f Makefile outdir=%OUTDIR% LANGID=%1 ISO=%2

but this doesn't work if the output path contains spaces (%OUTPUT% does not contain quotes).

Additionally there seems to be a bug in the command line argument parser of nmake, so that

nmake /nologo /f Makefile outdir="%OUTDIR%" LANGID=%1 ISO=%2

and

nmake /nologo /f Makefile "outdir=%OUTDIR%" LANGID=%1 ISO=%2

also don't work. In these cases outdir will be set to [outdir]" LANGID=[VALUE1] ISO=[VALUE2] (values I provided on cli in brackets) and LANGID and ISO are not recognized at all.

Right now I'm using the (ugly) workaround (note that there is only on doublequote)

nmake /nologo /f Makefile LANGID=%1 ISO=%2 outdir="%OUTDIR%

however, this only works if only one command line argument has spaces.

How to pass paths to nmake properly so that those also work in case they have a space inside?


Solution

  • I don't see any particular problem with spaces when calling NMAKE. There is, however, a problem with calling NMAKE with directory paths that end in a backslash, as this acts as an escape character!

    I have tried with this example Makefile:

    !MESSAGE Makeflags=$(MAKEFLAGS)
    !MESSAGE OUTDIR=$(OUTDIR) LANGID=$(LANGID) ISO=$(ISO)
    OD=$(OUTDIR)
    LI=$(LANGID)
    IS=$(ISO)
    
    # To be a valid makefile it must have some rules to perform
    all:
        @echo;OUTDIR=$(OUTDIR) LANGID=$(LANGID) ISO=$(ISO)
        @echo;OUTDIR=$(OD)     LANGID=$(LI)     ISO=$(IS)
    

    When run from the command prompt we get:

    C:\Users\Brian>nmake /NOLOGO  "OUTDIR=C:\Program Files\\" "LANGID=C Sharp" "ISO=ISO 8601"
    Makeflags=L
    OUTDIR=C:\Program Files\ LANGID=C Sharp ISO=ISO 8601
    OUTDIR=C:\Program Files\ LANGID=C Sharp ISO=ISO 8601
    OUTDIR=C:\Program Files\ LANGID=C Sharp ISO=ISO 8601
    

    You can see the spaces have been transferred to the macro values correctly. However, you can also see the problem with the trailing backslash in the path name. If I do not escape this properly there will be an error:

    C:\Users\Brian>nmake /NOLOGO  "OUTDIR=C:\Program Files\" "LANGID=C Sharp" "ISO=I
    SO 8601"
    NMAKE : fatal error U1001: Syntaxfehler: ungueltiges Zeichen ' ' in Makro
    Stop.
    

    So any batch file that calls NMAKE must be carefully written to protect against this specific case.

    For completeness; although they do not address your question, they may be useful references: