0

I am using a make.bat file to compile my Fortran code. To compile using the ifort command, I first need to initialize the compiler by executing ifortvarsbat intel64 command where I had to setup the path variable.

This works fine, however, the path keeps increasing in size and says command too long after 3-4 runs. Now, I open a command prompt, run the make.bat file once and then comment the ifort setup section so my successive run of the make.bat do not set the path multiple times.

I was hoping I could write an IF ELSE command to check whether ifort command executes or not and only set the path and call ifortvars if its not already set. This would save me commenting and uncommenting the make.bat file every time I open a new command prompt.

Unfortunately, I don't know much about batch script writing. Can someone help me with this.

@echo on
@echo ==============================================
@echo Setting Intel Fortran Compiler, please wait ...
@echo ==============================================

::set MY_PATH=C:\Program Files (x86)\Intel\Composer XE 2013\bin
::call "%MY_PATH%"\ifortvars.bat intel64
::call "%MY_PATH%"\ifortvars.bat ia32
@echo ==============================================
@echo Compiling program:
@echo ==============================================
:: -O3 option is used to optimize the code

ifort -O3 "Main.F90" 
Amitava
  • 431
  • 2
  • 6
  • 21
  • For an in-depth discussion of checking to see if PATH contains a given path already, please see [this question](http://stackoverflow.com/questions/141344/how-to-check-if-directory-exists-in-path). You may want to consider breaking your script into two scripts though; run ifortvars as a parameter to cmd when you open the window, and just use your script to build. – i_am_jorf Jun 18 '15 at 00:41
  • Yeah, he gives exactly the code you want in the second part of the most up-voted answer ([here](http://stackoverflow.com/a/8046515/74815)). – i_am_jorf Jun 18 '15 at 00:46
  • Another approach you could try is run `setlocal` before you call ifortvars and `endlocal` when your compilation completes. See the [setlocal documentation](http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true). – i_am_jorf Jun 18 '15 at 00:48

2 Answers2

2

Instead of checking the path variable, directly check if the compiler is available

where ifort >nul 2>&1 || call "%MY_PATH%\ifortvars.bat" intel64

where command will search for the indicated program inside the path and if found it echoes to console its path. If it does not find the program it echoes an error and sets errorlevel variable.

In the code all the output from where is hidden redirecting the stdout and stderr streams to nul device (>nul 2>&1) and conditional execution is used. The || operator means "execute the following command if the previous one failed"

So, if ifort can not be found then call the configuration batch.

A faster solution could be to directly check a "flag" variable that you define the first time the environment is configured.

if not defined FCompilerSetup (
    call "%MY_PATH%\ifortvars.bat" intel64
    set "FCompilerSetup=1"
)

note: From my point of view, the recomendation from miltonb and i_am_jorf is the correct way to handle the problem. setlocal / endlocal will remove the problem with the path and is cleaner and easier. But the batch file to set the environment will be called each time you compile your program and the changes removed on end. If any of the changes made to the environment is needed after compilation, it will not be available.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thank you all for a number of solutions. I found the `FCompilerSetup=1` being most easy to understand and used that one. Other solutions were also helpful. – Amitava Jun 18 '15 at 16:40
0

Using setlocal will not change your path beyond the scope of the batch file, so that when it runs again it will modify the path again.

Before running: PATH=C:\Mypath;

setlocal
::set MY_PATH=C:\Program Files (x86)\Intel\Composer XE 2013\bin
....other lines....
endlocal

After running: PATH=C:\Mypath;

miltonb
  • 6,905
  • 8
  • 45
  • 55