-
Notifications
You must be signed in to change notification settings - Fork 2
/
vstools.cmd
executable file
·305 lines (241 loc) · 8.05 KB
/
vstools.cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
@if defined TRACEON (@echo on) else (@echo off)
REM If this batch file works, then it was written by Fish.
REM If it doesn't then I don't know who the heck wrote it.
goto :BEGIN
::-----------------------------------------------------------------------------
:: VSTOOLS.CMD
::-----------------------------------------------------------------------------
:HELP
echo.
echo NAME
echo.
echo %~n0 -- Initialize Visual Studio build environment
echo.
echo SYNOPSIS
echo.
echo %~nx0 { 32 ^| 64 ^| detect }
echo.
echo ARGUMENTS
echo.
echo 32 ^| 64 Initializes the the desired Visual Studio
echo 32-bit or 64-bit target build environment.
echo.
echo detect Detects a Visual Studio installation and
echo defines variables identifying which one.
echo Refer to the below NOTES section.
echo.
echo OPTIONS
echo.
echo (NONE)
echo.
echo NOTES
echo.
echo Some flavor of Visual Studio must obviously be installed.
echo Supported versions are Visual Studio 2005 through 2022.
echo.
echo The 'detect' call detects which version of Visual Studio
echo is installed and defines certain variables identifying
echo which version it was that was detected, but otherwise
echo does NOT initialize the build environment. It defines
echo some helpful (informative) variables ONLY.
echo.
echo The '32' and '64' calls are the ones that initialize the
echo requested Visual Studio build environment. Note too that
echo both calls also set the same variables as a detect call
echo (i.e. an internal "detect" call is always performed).
echo.
echo The variables which the 'detect' call returns are:
echo.
echo vsname Visual Studio's "short name"
echo (e.g. "vs2019", "vs2008", etc)
echo.
echo vsver Visual Studio's numeric version
echo (e.g. "150", "90", etc)
echo.
echo vshost The detected host architecture
echo (either "x86" or "amd64")
echo.
echo vstarget The requested target architecture:
echo "x86" if '32', "amd64" if '64'.
echo.
echo vs2022..vs2005 The Visual Studio version numbers
echo for each supported version of it
echo (vs2019=160, vs2017=150, etc)
echo.
echo EXIT STATUS
echo.
echo 0 Success
echo 1 Error
echo.
echo AUTHOR
echo.
echo "Fish" (David B. Trout)
echo.
echo VERSION
echo.
echo 3.3 (January 26, 2022)
endlocal
exit /b 1
::-----------------------------------------------------------------------------
:: BEGIN
::-----------------------------------------------------------------------------
:BEGIN
:: Initialize the variables we'll be passing back to the caller.
:: We do this BEFORE our "setlocal" to update THEIR variable pool.
set "vs2005=80"
set "vs2008=90"
set "vs2010=100"
set "vs2012=110"
set "vs2013=120"
set "vs2015=140"
set "vs2017=150"
set "vs2019=160"
set "vs2022=170"
set "VSNAMES=vs2022 vs2019 vs2017 vs2015 vs2013 vs2012 vs2010 vs2008 vs2005"
set "vsname="
set "vsver="
set "vshost="
set "vstarget="
setlocal enabledelayedexpansion
set "TRACE=if defined DEBUG echo"
set "return=goto :EOF"
:: Validate passed arguments...
if /i "%~1" == "" goto :HELP
if /i "%~1" == "?" goto :HELP
if /i "%~1" == "/?" goto :HELP
if /i "%~1" == "-?" goto :HELP
if /i "%~1" == "--help" goto :HELP
if /i not "%~2" == "" goto :HELP
if /i not "%~1" == "detect" (
if /i not "%~1" == "32" (
if /i not "%~1" == "64" (
goto :HELP
)
)
)
:: Define target architecture...
if "%~1" == "32" set "vstarget=x86"
if "%~1" == "64" set "vstarget=amd64"
call :detect_vstudio
if %rc% NEQ 0 (
endlocal
exit /b 1
)
:: Go set the build environment if that's what they want
if /i not "%~1" == "detect" (
goto :SET_VSTUDIO_ENV
)
:: Otherwise all they're interested in are the variables
endlocal ^
&& set "vsname=%vsname%" ^
&& set "vsver=%vsver%" ^
&& set "vshost=%vshost%" ^
&& set "vstarget=%vstarget%"
exit /b 0
::-----------------------------------------------------------------------------
:: SET_VSTUDIO_ENV
::-----------------------------------------------------------------------------
:SET_VSTUDIO_ENV
::--------------------------------------------------------
:: Set the target Visual Studio build environment
::--------------------------------------------------------
:: Note that we must set the build enviroment OUTSIDE
:: the scope of our setlocal/endlocal to ensure that
:: whatever build environment gets set ends up being
:: passed back to the caller.
::--------------------------------------------------------
endlocal ^
&& set "vsname=%vsname%" ^
&& set "vsver=%vsver%" ^
&& set "vshost=%vshost%" ^
&& set "vstarget=%vstarget%" ^
&& set "VCVARSDIR=%VCVARSDIR%"
if %vsver% LSS %vs2017% (
goto :vs2015_or_earlier
)
::-----------------------------------
:: Visual Studio 2017 or LATER
::-----------------------------------
pushd .
echo.
if /i "%vstarget%" == "x86" call "%VCVARSDIR%\vcvars32.bat"
if /i "%vstarget%" == "amd64" call "%VCVARSDIR%\vcvars64.bat"
@if defined TRACEON (@echo on) else (@echo off)
echo.
popd
exit /b 0
:vs2015_or_earlier
::-----------------------------------
:: Visual Studio 2015 or EARLIER
::-----------------------------------
pushd .
echo.
call "%VCVARSDIR%\vcvarsall.bat" %vstarget%
@if defined TRACEON (@echo on) else (@echo off)
echo.
popd
exit /b 0
::-----------------------------------------------------------------------------
:: detect_vstudio
::-----------------------------------------------------------------------------
:detect_vstudio
set "vsver="
set "vsname="
set "VCVARSDIR="
set "rc=0"
set "@VSNAMES=%VSNAMES%"
:detect_vstudio_loop
for /f "tokens=1*" %%a in ("%@VSNAMES%") do (
call :detect_vstudio_sub "%%a"
if defined VCVARSDIR (
goto :detect_vstudio_host
)
if "%%b" == "" (
goto :detect_vstudio_error
)
set "@VSNAMES=%%b"
goto :detect_vstudio_loop
)
:detect_vstudio_error
echo %~nx0: ERROR: No supported version of Visual Studio is installed
set "vsver="
set "vsname="
set "VCVARSDIR="
set "rc=1"
%return%
:detect_vstudio_sub
set "vsname=%~1"
set "vsver=!%vsname%!"
if "!VS%vsver%COMNTOOLS!" == "" (
%return%
)
:: Remove trailing backslash if present
set "VSCOMNTOOLS=!VS%vsver%COMNTOOLS!"
if "%VSCOMNTOOLS:~-1%" == "\" (
set "VSCOMNTOOLS=%VSCOMNTOOLS:~0,-1%"
)
:: Detect accidental typos in VSCOMNTOOLS path
if not exist "%VSCOMNTOOLS%" (
echo %~nx0: ERROR: Defined VS%vsver%COMNTOOLS directory does not exist!
echo %~nx0: INFO: VS%vsver%COMNTOOLS = "%VSCOMNTOOLS%"??
set "vsver="
set "vsname="
set "VCVARSDIR="
set "rc=1"
%return%
)
:: Define directory where "vcvarsxx.bat" files live
set "VCVARSDIR=%VSCOMNTOOLS%\..\..\VC"
if %vsver% GEQ %vs2017% (
set "VCVARSDIR=%VCVARSDIR%\Auxiliary\Build"
)
%return%
:detect_vstudio_host
if /i "%PROCESSOR_ARCHITEW6432%" == "x86" set "vshost=x86"
if /i "%PROCESSOR_ARCHITEW6432%" == "AMD64" set "vshost=amd64"
if not defined vshost (
if /i "%PROCESSOR_ARCHITECTURE%" == "x86" set "vshost=x86"
if /i "%PROCESSOR_ARCHITECTURE%" == "AMD64" set "vshost=amd64"
)
%return%
::-----------------------------------------------------------------------------