3
votes

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.

3
By chance, would you have the command to "run the batch script from cmd shell then launch bash" ? I cannot manage to find the command that start a command inside the developer shell.Gabriel Devillers

3 Answers

5
votes

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)"
1
votes

I found that all you need is the path.

Run VsDevCmd.bat from DOS, then pipe out SET to a file (SET > c:\temp\set.txt)

From that file extract the PATH and convert it to Linux format - C:\Program Files becomes /cygdrive/c/Program Files and the \ all become /. The replace the ; with :.

Then edit your "${HOME}/.bashrc" file - append the above to the PATH - don't forget to add a : to the end of the original path.

My "${HOME}/.bashrc" now says the following - but copy it at your own risk.

export PATH="/c/bin:/usr/local/bin:/usr/bin:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.11.25503/bin/HostX86/x86:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Common7/IDE/VC/VCPackages:/cygdrive/c/Program Files/Microsoft SDKs/TypeScript/2.3:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Common7/IDE/CommonExtensions/Microsoft/TeamFoundation/Team Explorer:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/MSBuild/15.0/bin/Roslyn:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Team Tools/Performance Tools:/cygdrive/c/Program Files/HTML Help Workshop:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional//MSBuild/15.0/bin:C:/Windows/Microsoft.NET/Framework/v4.0.30319:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Common7/IDE/:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Common7/Tools/:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.11.25503/bin/HostX86/x86:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Common7/IDE/VC/VCPackages:/cygdrive/c/Program Files/Microsoft SDKs/TypeScript/2.3:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Common7/IDE/CommonExtensions/Microsoft/TeamFoundation/Team Explorer:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/MSBuild/15.0/bin/Roslyn:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Team Tools/Performance Tools:/cygdrive/c/Program Files/HTML Help Workshop:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional//MSBuild/15.0/bin:C:/Windows/Microsoft.NET/Framework/v4.0.30319:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Common7/IDE/:/cygdrive/c/Program Files/Microsoft Visual Studio/2017/Professional/Common7/Tools/:/cygdrive/c/Program Files/Windows Kits/8.0/bin/x86:/cygdrive/c/Program Files/Microsoft SDKs/F#/3.0/Framework/v4.0/:C:/Windows/Microsoft.NET/Framework/v4.0.30319:/cygdrive/c/Program Files/CMake/bin:$PATH:$HOME/bin"

0
votes

I was trying to set the environment variables locally, so I used export instead of printenv. This makes it easy to source the output, as it's already in a format designed to export environment variables.

cmd /c "VsDevCmd.bat -no_logo && bash -c 'export' > vcvars.tmp"
source vcvars.tmp
rm vcvars.tmp