Skip to content

Commit 42d5dcf

Browse files
jeffhostetlerdscho
authored andcommitted
MSVC Build: Support VS2017 command line compiler tools
Teach the top-level git Makefile to use whatever VS compiler tool chain is installed on the system. When building git from the command line in a git-sdk BASH window with MAKE, the shell environment has environment variables for GCC tools, but not MSVC tools. MSVC bindings are only avaliable from the various "VcVarsAll.bat" scripts run by the "Developer Command Prompt" shortcuts. Add compat/vcbuild/find_vs_env.bat to the Makefile. It uses the various "VcVarsAll.bat" scripts in a background Developer Command Prompt process to compute the proper environment variables and publish them for use by the Makefile. [jes: fixed typos, used %SystemRoot% instead of C:\WINDOWS] Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 82d9b3f commit 42d5dcf

File tree

4 files changed

+179
-18
lines changed

4 files changed

+179
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/GIT-BUILD-OPTIONS
22
/GIT-CFLAGS
33
/GIT-LDFLAGS
4+
/GIT-MSVC-GEN
45
/GIT-PREFIX
56
/GIT-PERL-DEFINES
67
/GIT-PYTHON-VARS

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,6 +2713,7 @@ ifdef MSVC
27132713
$(RM) $(patsubst %.exe,%.pdb,$(OTHER_PROGRAMS))
27142714
$(RM) $(patsubst %.exe,%.pdb,$(PROGRAMS))
27152715
$(RM) $(patsubst %.exe,%.pdb,$(TEST_PROGRAMS))
2716+
$(RM) GIT-MSVC-GEN
27162717
endif
27172718

27182719
.PHONY: all install profile-clean clean strip

compat/vcbuild/find_vs_env.bat

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
@ECHO OFF
2+
REM ================================================================
3+
REM You can use either GCC (the default) or MSVC to build git
4+
REM using the GIT-SDK command line tools.
5+
REM $ make
6+
REM $ make MSVC=1
7+
REM
8+
REM GIT-SDK BASH windows inherit environment variables with all of
9+
REM the bin/lib/include paths for GCC. It DOES NOT inherit values
10+
REM for the corresponding MSVC tools.
11+
REM
12+
REM During normal (non-git) Windows development, you launch one
13+
REM of the provided "developer command prompts" to set environment
14+
REM variables for the MSVC tools.
15+
REM
16+
REM Therefore, to allow MSVC command line builds of git from BASH
17+
REM and MAKE, we must blend these two different worlds. This script
18+
REM attempts to do that.
19+
REM ================================================================
20+
REM This BAT file starts in a plain (non-developer) command prompt,
21+
REM searches for the "best" commmand prompt setup script, installs
22+
REM it into the current CMD process, and exports the various MSVC
23+
REM environment variables for use by MAKE.
24+
REM
25+
REM The output of this script should be written to a make "include
26+
REM file" and referenced by the top-level Makefile.
27+
REM
28+
REM See "config.mak.uname" (look for GIT-MSVC-GEN).
29+
REM ================================================================
30+
REM The provided command prompts are custom to each VS release and
31+
REM filled with lots of internal knowledge (such as Registry settings);
32+
REM even their names vary by release, so it is not appropriate for us
33+
REM to look inside them. Rather, just run them in a subordinate
34+
REM process and extract the settings we need.
35+
REM ================================================================
36+
REM
37+
REM Current (VS2017 and beyond)
38+
REM -------------------
39+
REM Visual Studio 2017 introduced a new installation layout and
40+
REM support for side-by-side installation of multiple versions of
41+
REM VS2017. Furthermore, these can all coexist with installations
42+
REM of previous versions of VS (which have a completely different
43+
REM layout on disk).
44+
REM
45+
REM VS2017 Update 2 introduced a "vswhere.exe" command:
46+
REM https://github.com/Microsoft/vswhere
47+
REM https://blogs.msdn.microsoft.com/heaths/2017/02/25/vswhere-available/
48+
REM https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/
49+
REM
50+
REM VS2015
51+
REM ------
52+
REM Visual Studio 2015 uses the traditional VcVarsAll.
53+
REM
54+
REM Earlier Versions
55+
REM ----------------
56+
REM TODO
57+
REM
58+
REM ================================================================
59+
REM Note: Throughout this script we use "dir <path> && <cmd>" rather
60+
REM than "if exist <path>" because of script problems with pathnames
61+
REM containing spaces.
62+
REM ================================================================
63+
64+
REM Sanitize PATH to prevent git-sdk paths from confusing "wmic.exe"
65+
REM (called internally in some of the system BAT files).
66+
SET PATH=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;
67+
68+
REM ================================================================
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+
111+
REM ================================================================
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+
142+
REM ================================================================
143+
144+
:not_2015
145+
REM TODO....
146+
echo TODO support older versions of VS.
147+
EXIT /B 1
148+
149+
REM ================================================================
150+
151+
:print_vars
152+
REM Dump the essential vars to stdout to allow the main
153+
REM Makefile to include it. See config.mak.uname.
154+
REM Include DOS-style and BASH-style path for bin dir.
155+
156+
echo msvc_bin_dir=%msvc_bin_dir%
157+
echo msvc_bin_dir_msys=%msvc_bin_dir:C:=/C%
158+
159+
echo msvc_includes=%msvc_includes%
160+
echo msvc_libs=%msvc_libs%
161+
162+
echo sdk_includes=%sdk_includes%
163+
echo sdk_libs=%sdk_libs%
164+
165+
echo vs_ver=%vs_ver%
166+
167+
EXIT /B 0

config.mak.uname

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ ifdef MSVC
1515
# avoid the MingW and Cygwin configuration sections
1616
uname_S := Windows
1717
uname_O := Windows
18+
19+
# Generate and include makefile variables that point to the
20+
# currently installed set of MSVC command line tools.
21+
GIT-MSVC-GEN: ./compat/vcbuild/find_vs_env.bat
22+
@./compat/vcbuild/find_vs_env.bat | sed 's|\\|/|g' >GIT-MSVC-GEN
23+
-include GIT-MSVC-GEN
1824
endif
1925

2026
# We choose to avoid "if .. else if .. else .. endif endif"
@@ -341,7 +347,7 @@ ifeq ($(uname_S),Windows)
341347
# link.exe next to, and required by, cl.exe, we have to prepend this
342348
# onto the existing $PATH.
343349
#
344-
SANE_TOOL_PATH ?= /c/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64
350+
SANE_TOOL_PATH ?= $(msvc_bin_dir_msys)
345351
HAVE_ALLOCA_H = YesPlease
346352
NO_PREAD = YesPlease
347353
NEEDS_CRYPTO_WITH_SSL = YesPlease
@@ -399,26 +405,12 @@ ifeq ($(uname_S),Windows)
399405
lib =
400406
# Path to the unpacked third-party libraries
401407
MSVC_DEPS = compat/vcbuild/GEN.DEPS
402-
# Compensate for lack of %VCINSTALLDIR%, %LIB%, %LIBPATH%, and etc.
403-
# since vcvars*.bat did not get a chance to setup the environment of
404-
# the user's shell window.
405-
#
406-
# TODO If we ask the user to launch a "x64 Native" command prompt
407-
# TODO and then have it start a git-bash window, these could be
408-
# TODO inherited. So we wouldn't need to add these lines here.
409-
#
410-
MSVC_SDK81 = "c:/Program Files (x86)/Windows Kits/8.1"
411-
MSVC_SDK10 = "c:/Program Files (x86)/Windows Kits/10"
412-
MSVC_VCDIR = "c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC"
413408
BASIC_CFLAGS += \
414409
-I$(MSVC_DEPS)/include -I$(MSVC_DEPS)/include/expat -I$(MSVC_DEPS)/include/zlib \
415410
-L$(MSVC_DEPS)/lib \
416-
-I$(MSVC_SDK81)/Include/um -I$(MSVC_SDK81)/Include/shared \
417-
-L$(MSVC_SDK81)/lib/winv6.3/um/x64 \
418-
-I$(MSVC_SDK10)/Include/10.0.10240.0/ucrt \
419-
-L$(MSVC_SDK10)/lib/10.0.10240.0/ucrt/x64 \
420-
-I$(MSVC_VCDIR)/INCLUDE \
421-
-L$(MSVC_VCDIR)/lib/amd64
411+
$(sdk_includes) $(sdk_libs) \
412+
$(msvc_includes) $(msvc_libs)
413+
422414
# Optionally enable memory leak reporting.
423415
# BASIC_CLFAGS += -DUSE_MSVC_CRTDBG
424416
BASIC_CFLAGS += -DPROTECT_NTFS_DEFAULT=1

0 commit comments

Comments
 (0)