Check file existence in NMake

In the makefile for GNU make, I use this idiom to check if the file exists:

static: ifeq ($(wildcard $(FileName)),) # do something when the file doesn't exist else # do something different when it does endif 

But this does not work in NMake ( fatal error U1000: syntax error : ')' missing in macro invocation ). How can i replace it? It would be ideal if replacement was done on both build systems.

+4
source share
4 answers

csharptest.net the answer is almost appropriate, but it does not contain the smallest details regarding the differences in indentation rules between commands and directives:

 static: !if exists($(FileName)) @echo $(FileName) does exist! #^^^ DO MIND THE INDENTATION HERE, a command shall fail otherwise! # !else !error $(FileName) does not exist! #^^^^^ !directives don't have to be indented, though. # !endif 

Oddly enough, it can be exist (as indicated in white papers) or exists with the same effect.

+1
source

Just to add to Jack Kellyโ€™s sentence, you can run a test for existence in the CMD.EXE shell. NMake also includes the ability to create a temporary .cmd file and execute it.

Here is an example (n) of the makefile syntax that I use for the purpose of "check-syntax". It checks the syntax in a single C # source file, in a multi-file project, performing compilation.

 check-syntax : <<flymake-build.cmd $(CS_SOURCE) SETLOCAL ENABLEDELAYEDEXPANSION set errorlevel= for %%I in (%*) do if NOT x%%I == x$(FLYMAKE_ORIGINAL) ( set filesToBuild=!filesToBuild! %%I ) $(_CSC) /t:module $(LIBSREF) $(FLYMAKE_CHECK) !filesToBuild! ENDLOCAL && exit /b %errorlevel% << 

"flymake-build.cmd" is the name of the temporary cmd file being created. Double angular brackets ( << ) indicate the beginning and end of the content that is included in the temporary file before it starts.

The things after the file name in the first line are the arguments passed to the .cmd file. In this example, the arguments are the contents of a makefile called CS_SOURCE . In my case, this is just a list of file names in the project. File make variables will also be expanded in the text for the temporary file. You can see that I am referring to the compiler ( _CSC ) and some other characters.

Inside the .cmd code, you can do for loops if statements, whatever you like. This is a regular .cmd file.

If you want nmake to respond to errors (for example, interrupted the build) generated by the commands in the .cmd file, then you need to use the exit /b approach to complete cmd.

I found the documentation for this in a dark corner somewhere, so I decided to share it here for a general explanation. ( EDIT : here is the link .)

+7
source

Nmake can run preprocessor directives:

http://msdn.microsoft.com/en-us/library/7y32zxwh.aspx

The conditions may vary, but Exists () is one of them:

http://msdn.microsoft.com/en-us/library/8t2e8d78.aspx

 !IF EXISTS(foo.txt) 

Finally, you use the command line, http://msdn.microsoft.com/en-us/library/wkxa7sac.aspx

 !IF [cmd /C IF NOT EXIST "foo.txt" exit 1] 

In any case, this should work using any of them! IF the above is a complete example:

 !IF !EXISTS(foo.txt) !ERROR Unable to locate foo. !ELSE !MESSAGE I Found foo! !ENDIF 
+6
source

Do this at the shell level instead of the Makefile level. If you can rely on posix tools:

 static: @if test -f $(FileName); then \ echo something; \ else \ echo some other thing; \ fi 

Pay attention to line continuation markers.

If you need to do this in CMD.EXE , then perhaps you can do this using the IF EXIST $(FileName) COMMAND ELSE COMMAND construct.

+2
source

Source: https://habr.com/ru/post/1332490/


All Articles