-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfindvs.js
345 lines (323 loc) · 9.14 KB
/
findvs.js
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
'use strict';
const { execFileSync } = require('child_process');
const { readFileSync, statSync } = require('fs');
const { win32: path } = require('path');
const VS_VERSIONS_MODERN = new Map([
[15, {
year: 2017,
msbuild: path.join('MSBuild', '15.0', 'Bin', 'MSBuild.exe'),
toolset: 'v141',
}],
[16, {
year: 2019,
msbuild: path.join('MSBuild', 'Current', 'Bin', 'MSBuild.exe'),
toolset: 'v142',
}],
[17, {
year: 2022,
msbuild:
(process.arch === 'x64'
? path.join('MSBuild', 'Current', 'Bin', 'amd64', 'MSBuild.exe')
: path.join('MSBuild', 'Current', 'Bin', 'MSBuild.exe')),
toolset: 'v143',
}],
]);
const PACKAGES = {
msbuild: /^Microsoft[.]VisualStudio[.]VC[.]MSBuild[.](?:v\d+[.])?Base$/i,
vctools: /^Microsoft[.]VisualStudio[.]Component[.]VC[.]Tools[.]x86[.]x64$/i,
express: /^Microsoft[.]VisualStudio[.]WDExpress$/i,
winsdk:
/^Microsoft[.]VisualStudio[.]Component[.]Windows(81|10|11)SDK(?:[.](\d+)(?:[.]Desktop.*)?)?$/,
};
const SDK_REG = 'HKLM\\Software\\Microsoft\\Microsoft SDKs\\Windows';
const SDK32_REG =
'HKLM\\Software\\Wow6432Node\\Microsoft\\Microsoft SDKs\\Windows';
function checkRequiredPackages(packages) {
if (!Array.isArray(packages))
return false;
let foundMSBuild = false;
let foundVCTools = false;
let foundExpress = false;
for (const pkg of packages) {
if (!foundMSBuild && PACKAGES.msbuild.test(pkg))
foundMSBuild = true;
else if (!foundVCTools && PACKAGES.vctools.test(pkg))
foundVCTools = true;
else if (!foundExpress && PACKAGES.express.test(pkg))
foundExpress = true;
if (foundMSBuild && (foundVCTools || foundExpress))
return true;
}
}
// Sorts newest to oldest
function versionStringCompare(a, b) {
const splitA = a.split('.');
const splitB = b.split('.');
const len = Math.min(splitA.length, splitB.length);
for (let i = 0; i < len; ++i) {
const nA = parseInt(splitA[i], 10);
const nB = parseInt(splitB[i], 10);
if (nA > nB)
return -1;
if (nA < nB)
return 1;
}
if (splitA.length > splitB.length)
return -1;
else if (splitA.length < splitB.length)
return 1;
return 0;
}
function sdkVersionCompare(a, b) {
return versionStringCompare(a.fullVersion, b.fullVersion);
}
function getSDKPaths(fullVer) {
if (typeof fullVer !== 'string' || !fullVer)
return;
try {
const arch = (process.arch === 'ia32' ? 'x86' : 'x64');
const shortVer = `v${/^\d+[.]\d+/.exec(fullVer)[0]}`;
let verPath = getRegValue(`${SDK_REG}\\${shortVer}`, 'InstallationFolder');
if (!verPath)
verPath = getRegValue(`${SDK32_REG}\\${shortVer}`, 'InstallationFolder');
if (!verPath)
return;
// Includes
const includePaths = [];
for (const type of ['shared', 'um', 'ucrt']) {
const testPath = path.resolve(verPath, 'Include', fullVer, type);
statSync(testPath);
includePaths.push(testPath);
}
// Libraries
const libPaths = [];
for (const type of ['um', 'ucrt']) {
const testPath = path.resolve(verPath, 'Lib', fullVer, type, arch);
statSync(testPath);
libPaths.push(testPath);
}
return { includePaths, libPaths };
} catch {}
}
const execOpts = {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
windowsHide: true,
};
function findModernVS() {
const versions = [];
const ps = path.join(
process.env.SystemRoot,
'System32',
'WindowsPowerShell',
'v1.0',
'powershell.exe'
);
const cs = path.resolve(__dirname, '..', 'deps', 'Find-VisualStudio.cs');
const args = [
'-ExecutionPolicy',
'Unrestricted',
'-NoProfile',
'-Command',
`&{Add-Type -Path '${cs}';[VisualStudioConfiguration.Main]::PrintJson()}`
];
try {
const out = execFileSync(ps, args, execOpts);
const info = JSON.parse(out);
if (Array.isArray(info)) {
for (const vs of info) {
const vsPath = path.resolve(vs.path);
let vsVer = /^(?<major>\d+)[.](?<minor>\d+)[.]/.exec(vs.version);
if (!vsVer)
continue;
vsVer = {
full: vs.version,
major: +vsVer.groups.major,
minor: +vsVer.groups.minor,
};
const verInfo = VS_VERSIONS_MODERN.get(vsVer.major);
if (verInfo === undefined)
continue;
if (!checkRequiredPackages(vs.packages))
continue;
const vsSDKs = [];
for (const pkg of vs.packages) {
let fullVersion;
let version;
const m = PACKAGES.winsdk.exec(pkg);
if (!m)
continue;
const sdk = m[1];
switch (sdk) {
case '81':
fullVersion = version = '8.1';
break;
case '10':
case '11': {
if (m[2] === undefined)
continue;
const sdkVer = parseInt(m[2], 10);
if (!isFinite(sdkVer) || sdkVer < 0)
continue;
fullVersion = `10.0.${sdkVer}.0`;
version = '10.0';
break;
}
}
const paths = getSDKPaths(fullVersion);
if (!paths)
continue;
vsSDKs.push({
version,
fullVersion,
...paths,
});
}
if (vsSDKs.length === 0)
continue;
let clPath;
const includePaths = [];
const libPaths = [];
try {
let vcVerFile;
let clVer;
try {
vcVerFile = path.join(
vsPath,
'VC',
'Auxiliary',
'Build',
`Microsoft.VCToolsVersion.${verInfo.toolset}.default.txt`
);
clVer = readFileSync(vcVerFile, { encoding: 'utf8' }).trim();
} catch {}
if (!clVer) {
vcVerFile = path.join(
vsPath,
'VC',
'Auxiliary',
'Build',
'Microsoft.VCToolsVersion.default.txt'
);
clVer = readFileSync(vcVerFile, { encoding: 'utf8' }).trim();
}
const arch = (process.arch === 'ia32' ? 'x86' : 'x64');
let testPath = path.join(
vsPath,
'VC',
'Tools',
'MSVC',
clVer,
'bin',
`Host${arch}`,
arch,
'cl.exe'
);
statSync(testPath);
clPath = testPath;
testPath = path.join(
vsPath,
'VC',
'Tools',
'MSVC',
clVer,
'include'
);
statSync(testPath);
includePaths.push(testPath);
testPath = path.join(
vsPath,
'VC',
'Tools',
'MSVC',
clVer,
'lib',
arch
);
statSync(testPath);
libPaths.push(testPath);
} catch {
continue;
}
vsSDKs.sort(sdkVersionCompare);
versions.push({
path: vsPath,
version: vsVer,
sdks: vsSDKs,
...verInfo,
msbuild: path.join(vsPath, verInfo.msbuild),
cl: clPath,
includePaths,
libPaths,
});
}
}
} catch {}
return versions;
}
const VS_VERSIONS_OLDER = [
{
version: { full: '12.0', major: 12, minor: 0 },
year: 2013,
toolset: 'v120',
},
{
version: { full: '14.0', major: 14, minor: 0 },
year: 2015,
toolset: 'v140',
},
];
const VC_REG = 'HKLM\\Software\\Microsoft\\VisualStudio\\SxS\\VC7';
const VC32_REG =
'HKLM\\Software\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VC7';
const MSBUILD_REG = 'HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions';
function getRegValue(key, value, use32) {
const extraArgs = (use32 ? [ '/reg:32' ] : []);
const regexp = new RegExp(`^\\s+${value}\\s+REG_\\w+\\s+(\\S.*)$`, 'im');
const reg = path.join(process.env.SystemRoot, 'System32', 'reg.exe');
const args = [ 'query', key, '/v', value, ...extraArgs ];
try {
const out = execFileSync(reg, args, execOpts);
const m = regexp.exec(out);
if (m)
return m[1];
} catch {}
}
function findOlderVS() {
const versions = [];
try {
for (const vs of VS_VERSIONS_OLDER) {
let vsPath = getRegValue(VC_REG, vs.version.full);
if (!vsPath)
vsPath = getRegValue(VC32_REG, vs.version.full);
if (!vsPath)
continue;
vsPath = path.resolve(vsPath, '..');
const msbuildPath = getRegValue(
`${MSBUILD_REG}\\${vs.version.full}`,
'MSBuildToolsPath',
(process.arch === 'ia32')
);
if (!msbuildPath)
continue;
versions.push({
path: vsPath,
...vs,
msbuild: path.join(msbuildPath, 'MSBuild.exe'),
cl: path.join(vsPath, 'VC', 'bin', 'cl.exe'),
includePaths: [path.join(vsPath, 'VC', 'include')],
libPaths: [path.join(vsPath, 'VC', 'lib')],
sdks: [],
});
}
} catch {}
return versions;
}
module.exports = () => {
const versions = findModernVS().concat(findOlderVS());
// Sorts newest to oldest
versions.sort((a, b) => {
return versionStringCompare(a.version.full, b.version.full);
});
return versions;
};