Search code examples
fortransublimetext3intel-fortranintel-oneapisublime-build

Sublime Text 3 intel oneAPI Fortran build system


Has anyone figured out how to write the build system for the oneAPI Fortran compiler? Previously, i was using Parallel Studio XE ifort, and i managed to get it working using the solution here:

{
    "cmd": ["cmd", "/e:on", "/v:on", "/k", "ipsxe-comp-vars intel64 vs2013 && ifort ${file}"],  
    "file_regex": "^.*\\\\([0-9A-Za-z_]+\\.[A-Za-z0-9]+)\\(([0-9]+)\\):[ ]+error[ ]+#([0-9]+):[ ]+(.*)$",  
    "working_dir":"${file_path}",   
    "selector":"source.f ,source.for ,source.ftn ,source.f90 ,source.fpp ,source.i ,source.i90",
    "encoding":"cp936",
    "path":"C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.4.210\\windows\\bin;${path}",
    "variants":
      [  
           {  
              "name": "Run", 
              "cmd": ["cmd", "/e:on", "/v:on", "/c", "ipsxe-comp-vars intel64 vs2013 && ifort ${file} && ${file_base_name}"] 
          }  
     ]  

}

I tried changing the paths to the new ones but it doesn't work. I get the following error:

"ipsxe-comp-vars" is not recognized as an internal or external command,
program o executable.

Solution

  • I found the answer. Explanation below. Posting the working build system here for visibility.

    This should be the build system:

    {
        "cmd": ["cmd", "/e:on", "/v:on", "/S", "/k", "C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat intel64 vs2022 && ifort ${file}"],  
        "file_regex": "^.*\\\\([0-9A-Za-z_]+\\.[A-Za-z0-9]+)\\(([0-9]+)\\):[ ]+error[ ]+#([0-9]+):[ ]+(.*)$",  
        "working_dir":"${file_path}",   
        "selector":"source.f ,source.for ,source.ftn ,source.f90 ,source.fpp ,source.i ,source.i90",
        "encoding":"cp936",
        "path":"C:\\Program Files (x86)\\Intel\\oneAPI\\compiler\\latest\\windows\\bin\\intel64;${path}",
        "variants":
          [  
               {  
                  "name": "Run", 
                  "cmd": ["cmd", "/e:on", "/v:on", "/s", "/c", "C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat intel64 vs2022 && ifort ${file} && ${file_base_name}"] 
              }  
         ]  
    }
    

    Why the problem happens

    For starters, ipsxe-comp-vars is a batch file which when run, sets up environment variables required to execute the intel compilers. This file is specific to Intel Parallel Studio XE (IPSXE). Now, when installing IPSXE, it would add this batch file to your PATH, meaning you could simply call ipsxe-comp-vars from any directory to set up the required environment variables.

    Intel oneAPI has a differently named file, that essentially does the same thing, called setvars.bat. This file is stored in:

    C:\Program Files (x86)\Intel\oneAPI\setvars.bat
    

    So, at first it seems that calling ipsxe-comp-vars fails because the file is named differently. However, unlike IPSXE did with ipsxe-comp-vars, oneAPI does not add setvars to PATH, so you cannot simply call setvars, you have to usethe full path.

    How to solve it

    With IPSXE, you could call ipsxe-comp-vars and it would run the batch file that sets up environment variables, but with oneAPI either you add the file to PATH (not reccomended because it has a generic name), or you use the full path when calling it (same as above):

    C:\Program Files (x86)\Intel\oneAPI\setvars.bat
    

    Now, because you have to plug this in into the build system config, you need to format it correctly. ST runs the commands in a cmd.exe, so you have to use the correct options and format the path in a way that cmd can understand it: options (you can get a full list by opening a cmd prompt, typing cmd /? and hitting return):

     - /e:on    Enables command extensions
     - /v:on    Enables extension of environment variables
     - /s       Modifies how the string following a /c or /k is read
     - /k       Executes the string command and continues
    

    The path to the setvars.bat file must be formatted as follows:

    C:\\\"Program Files (x86)\"\\Intel\\oneAPI\\setvars.bat
    
    • Each \ separating dirs needs to be escaped (using \ as well)
    • needs to be enclosed in double quotes, since it contains a whitespace. Each double quote needs to escaped as well (once again with )

    The following options are specific to the setvars.bat file:

    - intel64      specifies 64-bit configuration
    - vs2022       specifies Visual Studio 2022 as the developer cmd or 
                   powershell version to use
    

    Finally, ifort is called on the current file with ifort ${file}

    Additionally, the build system is completed with a variant "Run". This variant runs the output file once it has been compiled(&& ${file_base_name}), and will show the output in the Sublime Text 3 console (does not accept inputs, if anyone knows how to setup up sublimeREPL for Fortran please tell me)