nmake: can a batch file run as part of a make command block, affect the environment of the nmake.exe process? - nmake

I think in nmake if I do this:
example :
set value=77
echo %%value%%
The result will display 77 on the console.
Is there a way for me to invoke a .cmd or .bat file that will affect the environment of the nmake.exe process? Suppose I put the statement set value=77 in a file called "setvalue.cmd". Then change the makefile to this:
example :
setvalue
echo %%value%%
I get:
%value%
Alternatively, if there's a way to set a macro within a command block, that would also work. Or, a way to set the value of a macro from a batch file, even outside a command block.

You can create an nmake snippet during makefile pre-processing, and read that in. Assuming batch.cmd outputs valid nmake syntax, then
!if [batch.cmd >makefile.auto]
!error *** Could not create makefile.auto
!endif
!include makefile.auto
You should ensure batch.cmd sets %errorlevel% appropriately (e.g., exit /b 22).
makefile.auto can contain anything, but you would probably want stuff like value=77. A couple of points:
Dereference value using nmake syntax ($(value))
You can pass parameters to batch.cmd if necessary ([batch.cmd $(OBJECTS) >makefile.auto])

No, I don't think so.

Related

Cmake add command line argument to binary

I create a binary myBinary via cmake/CMakeLists.txt.
I would like to "include" default options on my binary.
In other words, I want my binary to be called with myBinary --option myopt even when I just run ./myBinary
How can I do that?
CMake does not have built-in support for you you want to do.
One solution is to do as #Youka said - change the source code of your program.
Another solution that I have used sometimes is to autogenerate a script that executes an executable:
# Create startup script
MACRO(GEN_START_SCRIPT binName)
# Generate content
SET(fileContent
"#!/bin/bash\n"
"\n"
"# This startup script is auto generated - do not modify!\n"
"\n"
"${binName} -a 23 -b 34 -c 976\n"
"\n"
)
# Write to file
SET(fileName ${CMAKE_CURRENT_BINARY_DIR}/${binName}.sh)
FILE(WRITE ${fileName} ${fileContent})
ENDMACRO()
Then call the macro after defining your executable:
ADD_EXECUTABLE(myBinary file1.c file.2)
GEN_START_SCRIPT(myBinary)
You can of course add other stuff to the script, like environment variables etc.
If you're in control of the sources and you want different default behavior... change the sources!
This is in no way a build system issue (CMake or otherwise).

check for file existence using nmake and delete it

I am using Windows version of NMAKE. I would like to check for file existence in a make file. If it exists I need to delete it. Here is my code:
!IF EXIST ("C:\ABC.XML")
#del ABC.XML
!ELSE
#echo "FILE DOESN'T EXIST
!ENDIF
The above code is not working. I could not figure it out the problem. Please help.
Your code doesn't work because !IF, !ELSE and !ENDIF are a preprocessing directives and the result of preprocessing must produce a valid makefile. Commands are only allowed as part of what Microsoft calls a description block which are required to start with a dependency line with one or more targets and zero or more dependents.
You can get around this by executing your commands during the preprocessing stage by including them in a preprocessing directive surrounded by brackets ([]). Something like this:
!IF EXIST(C:\ABC.XML)
! IF [del C:\ABC.XML]
! ENDIF
!ELSEIF [echo FILE DOESN'T EXIST]
!ENDIF
The second !IF and the !ELSEIF directives are used to provide a context for the commands so they're executed during the preprocessing phase.
However I think you'd probably be better moving the del command to a description block where it's actually needed. For example if file ABC.XML needs to be deleted before it can be rebuilt, use something like this:
ABC.XML: ABC.CSV
-rem The csv2xml translator requires that the XML file not already exist
-#del ABC.XML 2> NUL
csv2xml ABC.CSV ABC.XML

sh: command not found

In my Fortran program I want to call the system to run my code (with alias asv20r3). For it I do:
call system ("asv20r3 " //filename)
But I obtain the following message:
sh: asv20r3: command not found
Is it necessary to define something more in order to make the system understand that I want to execute the code via the alias asv20r3?
Thanks!
The following FORTRAN90 program has all the answer to your question :
program Test
print*, 'Printing environment variables : '
call system("set")
print*, 'Printing environment aliases : '
call system("alias")
end program Test
The output of the program speaks for itself : environment variables are inherited; aliases are not.
You can either choose to rely on the content of an environment variable (using call get_environment_variable(...)) or hardcode the path and/or command like someone else suggested.
You probably need to supply the path to asv20r3 in the system call
your program, asv20r3, needs to be in the $PATH of your system.
if it is in the same directory as where you run your main program, than you must tell your system that by pre-pending a "./" to it, such as "./asv20r3".

Read environment variable in make file

I have a environment variable set with name $MY_ENV_VARIABLE.
How do I use this variable inside my makefile to (for example) include some source files?
LOCAL_SRC_FILES = $(MY_ENV_VARIABLE)/libDEMO.so
Something like above doesn't seem to work.
Note: in my case this is needed for building with the Android NDK but I guess this applies to make in general.
Just to add some information...
The syntax to access the environment variable in make is like other variables in make...
#export the variable. e.g. in the terminal,
export MY_ENV_VARIABLE="hello world"
...
#in the makefile (replace before call)
echo $(MY_ENV_VARIABLE)
This performs the substitution before executing the commmand. If you instead, want the substitution to happen during the command execution, you need to escape the $ (For example, echo $MY_ENV_VARIABLE is incorrect and will attempt to substitute the variable M in make, and append it to Y_ENV_VARIABLE)...
#in the makefile (replace during call)
echo $$MY_ENV_VARIABLE
Make sure you exported the variable from your shell. Running:
echo $MY_ENV_VARIABLE
shows you whether it's set in your shell. But to know whether you've exported it so that subshells and other sub-commands (like make) can see it try running:
env | grep MY_ENV_VARIABLE
If it's not there, be sure to run export MY_ENV_VARIABLE before running make.
That's all you need to do: make automatically imports all environment variables as make variables when it starts up.
I just had a similar issue (under Cygwin):
Running echo $OSTYPE on the shell prints the value, but
running env | grep OSTYPE doesn't give any output.
As I can't guarantee that this variable is exported on all machines I want to run that makefile on, I used the following to get the variable from within the makefile:
OSTYPE = $(shell echo $$OSTYPE)
Which of course can also be used within a condition like the following:
ifeq ($(shell echo $$OSTYPE),cygwin)
# ...do something...
else
# ...do something else...
endif
EDIT:
Some things I found after experimenting with the info from jozxyqk's answer, all from within the makefile:
If I run #echo $$OSTYPE or #echo "$$OSTYPE" in a recipe, the variable is successfully expanded into cygwin.
However, using that in a condition like ifeq ($$OSTYPE,cygwin) or ifeq ("$$OSTYPE","cygwin") doesn't expand it.
Thus it is logical that first setting a variable like TEST = "$$OSTYPE" will lead to echo $(TEST) printing cygwin (the expansion is done by the echo call) but that doesn't work in a condition - ifeq ($(TEST),cygwin) is false.

Accessing command line arguments in NSIS

I am trying to make my setups scripts modular. I am calling setup exe's from within main setup script based to the requirements. I want to pass command line arguments to the exe being called. Can someone please tell me how to access the command line arguments in the script being called.
Thanks in advance.
you can use GetOptions function (FileFunc.nsh must be included above). Following example shows p parameter reading; its value is saved into the variable. $CMDLINE is your command line (absolute or relative, as you called) containing also your parameters.
!include FileFunc.nsh
Var variable
${GetOptions} $CMDLINE "/p" $variable
Try to get options from Command line by their name:
http://nsis.sourceforge.net/Get_command_line_parameter_by_name

Resources