Search code examples
visual-studiobatch-filevisual-studio-2015cygwin

"Source" VsDevCmd.bat from Cygwin


So VsDevCmd.bat is a nifty script that sets environment variables for Visual Studio tools:

'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat'

I am using Visual Studio tools cl, link, etc. from Cygwin. How can I use that script to set all the same environment variable without reinventing the script in bash?

I can run the batch script from cmd shell then launch bash, but this is unsatisfactory—I need to initiate the process of "sourcing" the batch file from within Cygwin.


Solution

  • Here's what I ended up with. This bash function will ran cmd with VcVarsAll.bat file, then will a nested bash in order to print an environment variable, which could be extracted:

    # Given envar names return their newline-separated values setup for VS2015
    #
    # VcVarsAll.bat is a script that sets up environment variables for Visual
    # Studio command-line builds:
    #
    #   https://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx
    #
    query_vcvarsall() {
      local envars=$*
    
      (cd '/cygdrive/c/Program Files (x86)/Microsoft Visual Studio 14.0/VC' &&
       cmd /c "VcVarsAll.bat amd64 && c:/cygwin/bin/bash -c 'printenv $envars'")
    }
    

    Then set, for example, PATH:

    export PATH="$(query_vcvarsall PATH)"