|
1 | 1 | module.exports = {
|
2 | 2 | locateMsbuild: locateMsbuild,
|
3 |
| - locateVS2017: locateVS2017, |
4 |
| - getVS2017WinSDKVersion: getVS2017WinSDKVersion, |
5 | 3 | setGypVS2017Env: setGypVS2017Env
|
6 | 4 | }
|
7 | 5 |
|
8 | 6 | var log = require('npmlog')
|
9 | 7 | , fs = require('fs')
|
10 | 8 | , path = require('path')
|
11 | 9 | , cp = require('child_process')
|
12 |
| - , win = process.platform == 'win32' |
13 |
| - , msgFormat = require('util').format |
14 |
| - , findPython = require('./find-python') |
15 | 10 |
|
16 |
| -var vs2017_install_path |
17 |
| - , vs2017_win_sdk_ver |
18 |
| - |
19 |
| -function run_locate(gyp, callback) { |
20 |
| - if (!win) { |
21 |
| - return callback(null, '', '') |
| 11 | +var vs2017Setup |
| 12 | + |
| 13 | +function tryVS7 (gyp) { |
| 14 | + if (vs2017Setup) return vs2017Setup; |
| 15 | + try { |
| 16 | + const regQuery = path.join(__dirname, '..', 'tools', 'Setup.Configuration.VC.exe'); |
| 17 | + const vsSetupRaw = cp.execSync(regQuery).toString(); |
| 18 | + if (!vsSetupRaw) return; |
| 19 | + const vsSetup = vsSetupRaw.split(/[\r|\n]/g).reduce((s, l) => { |
| 20 | + const lParts = l.split(': '); |
| 21 | + if (lParts.length > 1) s[lParts[0]] = lParts[1]; |
| 22 | + return s; |
| 23 | + }, {}); |
| 24 | + return vs2017Setup = vsSetup; |
| 25 | + } catch (e) { |
| 26 | + gyp.log.verbose('try VS7', 'Couldn\'t find VS2017 :('); |
22 | 27 | }
|
23 |
| - |
24 |
| - if (vs2017_install_path || vs2017_install_path === '') { |
25 |
| - return callback(null, vs2017_install_path, vs2017_win_sdk_ver) |
26 |
| - } |
27 |
| - |
28 |
| - var python = gyp.opts.python || process.env.PYTHON || 'python2' |
29 |
| - , findvc_path = path.join(__dirname, '..', 'find_vs2017.py') |
30 |
| - |
31 |
| - findPython(python, locate_vc); |
32 |
| - |
33 |
| - function locate_vc(err, python_bin) { |
34 |
| - if (err) { |
35 |
| - return callback(err) |
36 |
| - } |
37 |
| - |
38 |
| - log.verbose('find vs2017', 'obtaining vs2017 install path using script %s', |
39 |
| - findvc_path) |
40 |
| - cp.execFile(python_bin, [findvc_path], function(err, stdout, stderr) { |
41 |
| - if (err) { |
42 |
| - return callback(err) |
43 |
| - } |
44 |
| - if (stdout) { |
45 |
| - vs2017_install_path = stdout.split('\r\n')[0] |
46 |
| - log.verbose('find vs2017', 'found Visual Studio 2017 in %s', vs2017_install_path) |
47 |
| - get_sdk_version(python_bin) |
48 |
| - } else { |
49 |
| - log.verbose('find vs2017', |
50 |
| - 'no valid Visual Studio 2017 installation found') |
51 |
| - vs2017_install_path = '' |
52 |
| - vs2017_win_sdk_ver = '' |
53 |
| - } |
54 |
| - }) |
55 |
| - } |
56 |
| - |
57 |
| - function get_sdk_version(python_bin) { |
58 |
| - log.verbose('find vs2017', 'obtaining installed Windows SDKs') |
59 |
| - cp.execFile(python_bin, [findvc_path, vs2017_install_path], |
60 |
| - function(err, stdout, stderr) { |
61 |
| - if (err) { |
62 |
| - return callback(err) |
63 |
| - } |
64 |
| - if (stdout) { |
65 |
| - vs2017_win_sdk_ver = stdout.split('\r\n')[0] |
66 |
| - log.verbose('find vs2017', 'found VS2017 WinSDK %s', vs2017_win_sdk_ver) |
67 |
| - } else { |
68 |
| - log.verbose('find vs2017', 'no installed sdks found') |
69 |
| - } |
70 |
| - |
71 |
| - callback(null, vs2017_install_path, vs2017_win_sdk_ver) |
72 |
| - }) |
73 |
| - } |
74 |
| - |
75 | 28 | }
|
76 | 29 |
|
| 30 | + |
77 | 31 | function locateMsbuild(gyp, callback) {
|
78 |
| - run_locate(gyp, function(err, vs_path, sdk) { |
79 |
| - if (err) { |
80 |
| - return callback(err) |
81 |
| - } |
82 |
| - if (vs_path === '') { |
83 |
| - return callback() |
84 |
| - } |
85 |
| - var msbuild_location = path.join(vs_path, 'MSBuild', |
86 |
| - '15.0', 'Bin', 'MSBuild.exe') |
87 |
| - log.verbose('find vs2017', 'looking for msbuild in %s', msbuild_location) |
88 |
| - fs.access(msbuild_location, function(err) { |
89 |
| - callback(null, err ? null : msbuild_location) |
90 |
| - }) |
91 |
| - }) |
92 |
| -} |
| 32 | + var vsSetup = tryVS7(gyp) |
| 33 | + if (!vsSetup)return callback() |
93 | 34 |
|
94 |
| -function locateVS2017(gyp, callback) { |
95 |
| - run_locate(gyp, function(err, vs_path, sdk) { |
96 |
| - if (err) { |
97 |
| - callback(err) |
98 |
| - } else { |
99 |
| - callback(null, vs_path === '' ? null : vs_path) |
100 |
| - } |
| 35 | + var msbuild_location = path.join(vsSetup.InstallationPath, 'MSBuild', |
| 36 | + '15.0', 'Bin', 'MSBuild.exe') |
| 37 | + log.verbose('find vs2017', 'looking for msbuild in %s', msbuild_location) |
| 38 | + fs.access(msbuild_location, function(err) { |
| 39 | + callback(null, err ? null : msbuild_location) |
101 | 40 | })
|
102 | 41 | }
|
103 | 42 |
|
104 |
| -function getVS2017WinSDKVersion(gyp, callback) { |
105 |
| - run_locate(gyp, function(err, vs_path, sdk) { |
106 |
| - if (err) { |
107 |
| - callback(err) |
108 |
| - } else { |
109 |
| - callback(null, sdk === '' ? null : sdk) |
110 |
| - } |
111 |
| - }) |
112 |
| -} |
113 | 43 |
|
114 | 44 | function setGypVS2017Env(gyp, callback) {
|
115 |
| - locateVS2017(gyp, setPath) |
116 |
| - |
117 |
| - function setPath(err, vs_path) { |
118 |
| - if (err) { |
119 |
| - return callback(err) |
120 |
| - } |
121 |
| - if (vs_path) { |
122 |
| - process.env['vs2017_install'] = vs_path |
123 |
| - getVS2017WinSDKVersion(gyp, setSDK) |
124 |
| - } else { |
125 |
| - callback() |
126 |
| - } |
127 |
| - } |
128 |
| - |
129 |
| - function setSDK(err, sdk) { |
130 |
| - if (err) { |
131 |
| - return callback(err) |
132 |
| - } |
133 |
| - if (sdk) { |
134 |
| - process.env['vs2017_sdk'] = sdk |
135 |
| - } |
136 |
| - callback() |
137 |
| - } |
| 45 | + var vsSetup = tryVS7(gyp); |
| 46 | + if (!vsSetup)return callback() |
| 47 | + |
| 48 | + gyp.opts.msvs_version = '2017'; |
| 49 | + process.env['vs2017_install'] = vsSetup.InstallationPath; |
| 50 | + var sdkRaw = Object.keys(vsSetup).find(k => ~k.indexOf('SDK') && ~k.indexOf('10.')); |
| 51 | + var sdkVerbose = sdkRaw.split('_').pop(); |
| 52 | + var sdk = sdkVerbose.replace(/\d+$/, '0') |
| 53 | + process.env['vs2017_sdk'] = sdk |
| 54 | + callback(); |
138 | 55 | }
|
0 commit comments