compat / vcbuild / find_vs_env.baton commit Merge branch 'tg/range-diff-output-update' (43ba21c)
   1@ECHO OFF
   2REM ================================================================
   3REM You can use either GCC (the default) or MSVC to build git
   4REM using the GIT-SDK command line tools.
   5REM        $ make
   6REM        $ make MSVC=1
   7REM
   8REM GIT-SDK BASH windows inherit environment variables with all of
   9REM the bin/lib/include paths for GCC.  It DOES NOT inherit values
  10REM for the corresponding MSVC tools.
  11REM
  12REM During normal (non-git) Windows development, you launch one
  13REM of the provided "developer command prompts" to set environment
  14REM variables for the MSVC tools.
  15REM
  16REM Therefore, to allow MSVC command line builds of git from BASH
  17REM and MAKE, we must blend these two different worlds.  This script
  18REM attempts to do that.
  19REM ================================================================
  20REM This BAT file starts in a plain (non-developer) command prompt,
  21REM searches for the "best" commmand prompt setup script, installs
  22REM it into the current CMD process, and exports the various MSVC
  23REM environment variables for use by MAKE.
  24REM
  25REM The output of this script should be written to a make "include
  26REM file" and referenced by the top-level Makefile.
  27REM
  28REM See "config.mak.uname" (look for compat/vcbuild/MSVC-DEFS-GEN).
  29REM ================================================================
  30REM The provided command prompts are custom to each VS release and
  31REM filled with lots of internal knowledge (such as Registry settings);
  32REM even their names vary by release, so it is not appropriate for us
  33REM to look inside them.  Rather, just run them in a subordinate
  34REM process and extract the settings we need.
  35REM ================================================================
  36REM
  37REM Current (VS2017 and beyond)
  38REM -------------------
  39REM Visual Studio 2017 introduced a new installation layout and
  40REM support for side-by-side installation of multiple versions of
  41REM VS2017.  Furthermore, these can all coexist with installations
  42REM of previous versions of VS (which have a completely different
  43REM layout on disk).
  44REM
  45REM VS2017 Update 2 introduced a "vswhere.exe" command:
  46REM https://github.com/Microsoft/vswhere
  47REM https://blogs.msdn.microsoft.com/heaths/2017/02/25/vswhere-available/
  48REM https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/
  49REM
  50REM VS2015
  51REM ------
  52REM Visual Studio 2015 uses the traditional VcVarsAll.
  53REM
  54REM Earlier Versions
  55REM ----------------
  56REM Currently unsupported.
  57REM
  58REM ================================================================
  59REM Note: Throughout this script we use "dir <path> && <cmd>" rather
  60REM than "if exist <path>" because of script problems with pathnames
  61REM containing spaces.
  62REM ================================================================
  63
  64REM Sanitize PATH to prevent git-sdk paths from confusing "wmic.exe"
  65REM (called internally in some of the system BAT files).
  66SET PATH=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;
  67
  68REM ================================================================
  69
  70:current
  71   SET vs_where=C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe
  72   dir "%vs_where%" >nul 2>nul && GOTO have_vs_where
  73   GOTO not_2017
  74
  75:have_vs_where
  76   REM Try to use VsWhere to get the location of VsDevCmd.
  77
  78   REM Keep VsDevCmd from cd'ing away.
  79   SET VSCMD_START_DIR=.
  80
  81   REM Get the root of the VS product installation.
  82   FOR /F "usebackq tokens=*" %%i IN (`"%vs_where%" -latest -requires Microsoft.VisualStudio.Workload.NativeDesktop -property installationPath`) DO @SET vs_ip=%%i
  83
  84   SET vs_devcmd=%vs_ip%\Common7\Tools\VsDevCmd.bat
  85   dir "%vs_devcmd%" >nul 2>nul && GOTO have_vs_devcmd
  86   GOTO not_2017
  87
  88:have_vs_devcmd
  89   REM Use VsDevCmd to setup the environment of this process.
  90   REM Setup CL for building 64-bit apps using 64-bit tools.
  91   @call "%vs_devcmd%" -no_logo -arch=x64 -host_arch=x64
  92
  93   SET tgt=%VSCMD_ARG_TGT_ARCH%
  94
  95   SET mn=%VCToolsInstallDir%
  96   SET msvc_includes=-I"%mn%INCLUDE"
  97   SET msvc_libs=-L"%mn%lib\%tgt%"
  98   SET msvc_bin_dir=%mn%bin\Host%VSCMD_ARG_HOST_ARCH%\%tgt%
  99
 100   SET sdk_dir=%WindowsSdkDir%
 101   SET sdk_ver=%WindowsSDKVersion%
 102   SET si=%sdk_dir%Include\%sdk_ver%
 103   SET sdk_includes=-I"%si%ucrt" -I"%si%um" -I"%si%shared"
 104   SET sl=%sdk_dir%lib\%sdk_ver%
 105   SET sdk_libs=-L"%sl%ucrt\%tgt%" -L"%sl%um\%tgt%"
 106
 107   SET vs_ver=%VisualStudioVersion%
 108
 109   GOTO print_vars
 110
 111REM ================================================================
 112
 113:not_2017
 114   REM See if VS2015 is installed.
 115
 116   SET vs_2015_bat=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat
 117   dir "%vs_2015_bat%" >nul 2>nul && GOTO have_vs_2015
 118   GOTO not_2015
 119
 120:have_vs_2015
 121   REM Use VcVarsAll like the "x64 Native" command prompt.
 122   REM Setup CL for building 64-bit apps using 64-bit tools.
 123   @call "%vs_2015_bat%" amd64
 124
 125   REM Note that in VS2015 they use "x64" in some contexts and "amd64" in others.
 126   SET mn=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\
 127   SET msvc_includes=-I"%mn%INCLUDE"
 128   SET msvc_libs=-L"%mn%lib\amd64"
 129   SET msvc_bin_dir=%mn%bin\amd64
 130
 131   SET sdk_dir=%WindowsSdkDir%
 132   SET sdk_ver=%WindowsSDKVersion%
 133   SET si=%sdk_dir%Include\%sdk_ver%
 134   SET sdk_includes=-I"%si%ucrt" -I"%si%um" -I"%si%shared" -I"%si%winrt"
 135   SET sl=%sdk_dir%lib\%sdk_ver%
 136   SET sdk_libs=-L"%sl%ucrt\x64" -L"%sl%um\x64"
 137
 138   SET vs_ver=%VisualStudioVersion%
 139
 140   GOTO print_vars
 141
 142REM ================================================================
 143
 144:not_2015
 145   echo "ERROR: unsupported VS version (older than VS2015)" >&2
 146   EXIT /B 1
 147
 148REM ================================================================
 149
 150:print_vars
 151   REM Dump the essential vars to stdout to allow the main
 152   REM Makefile to include it.  See config.mak.uname.
 153   REM Include DOS-style and BASH-style path for bin dir.
 154
 155   echo msvc_bin_dir=%msvc_bin_dir%
 156   SET X1=%msvc_bin_dir:C:=/C%
 157   SET X2=%X1:\=/%
 158   echo msvc_bin_dir_msys=%X2%
 159
 160   echo msvc_includes=%msvc_includes%
 161   echo msvc_libs=%msvc_libs%
 162
 163   echo sdk_includes=%sdk_includes%
 164   echo sdk_libs=%sdk_libs%
 165
 166   echo vs_ver=%vs_ver%
 167
 168   EXIT /B 0