Skip to content

Commit 996263a

Browse files
committed
win: use windows-autoconf to setup, even for VS2017
1 parent ae141e1 commit 996263a

File tree

4 files changed

+44
-94
lines changed

4 files changed

+44
-94
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
gyp/test
22
node_modules
33
test/.node-gyp
4+
!test/node_modules
5+
build/

lib/build.js

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ var fs = require('graceful-fs')
1414
, mkdirp = require('mkdirp')
1515
, exec = require('child_process').exec
1616
, processRelease = require('./process-release')
17-
, win = process.platform === 'win32'
17+
, win = process.platform === 'win32';
18+
if (win)
19+
var findVS = require('windows-autoconf')
1820

1921
exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'
2022

@@ -106,7 +108,7 @@ function build (gyp, argv, callback) {
106108
which(command, function (err, execPath) {
107109
if (err) {
108110
if (win && /not found/.test(err.message)) {
109-
// On windows and no 'msbuild' found. Let's guess where it is
111+
log.verbose('could not find "msbuild.exe" in PATH - finding location in registry')
110112
findMsbuild()
111113
} else {
112114
// Some other error or 'make' not found on Unix, report that to the user
@@ -122,68 +124,18 @@ function build (gyp, argv, callback) {
122124
/**
123125
* Search for the location of "msbuild.exe" file on Windows.
124126
*/
125-
126127
function findMsbuild () {
127-
if (config.variables.msbuild_path) {
128-
command = config.variables.msbuild_path
129-
log.verbose('using MSBuild:', command)
130-
copyNodeLib()
131-
return
132-
}
133-
134-
log.verbose('could not find "msbuild.exe" in PATH - finding location in registry')
135-
var notfoundErr = 'Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2008+ installed?'
136-
var cmd = 'reg query "HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions" /s'
137-
if (process.arch !== 'ia32')
138-
cmd += ' /reg:32'
139-
exec(cmd, function (err, stdout, stderr) {
140-
if (err) {
141-
return callback(new Error(err.message + '\n' + notfoundErr))
128+
var notfoundErr = 'Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio installed?'
129+
try {
130+
var msbuild_path = findVS.locateMsbuild()
131+
if (!msbuild_path) {
132+
return callback(new Error(notfoundErr))
142133
}
143-
var reVers = /ToolsVersions\\([^\\]+)$/i
144-
, rePath = /\r\n[ \t]+MSBuildToolsPath[ \t]+REG_SZ[ \t]+([^\r]+)/i
145-
, msbuilds = []
146-
, r
147-
, msbuildPath
148-
stdout.split('\r\n\r\n').forEach(function(l) {
149-
if (!l) return
150-
l = l.trim()
151-
if (r = reVers.exec(l.substring(0, l.indexOf('\r\n')))) {
152-
var ver = parseFloat(r[1], 10)
153-
if (ver >= 3.5) {
154-
if (r = rePath.exec(l)) {
155-
msbuilds.push({
156-
version: ver,
157-
path: r[1]
158-
})
159-
}
160-
}
161-
}
162-
})
163-
msbuilds.sort(function (x, y) {
164-
return (x.version < y.version ? -1 : 1)
165-
})
166-
;(function verifyMsbuild () {
167-
if (!msbuilds.length) return callback(new Error(notfoundErr))
168-
msbuildPath = path.resolve(msbuilds.pop().path, 'msbuild.exe')
169-
fs.stat(msbuildPath, function (err, stat) {
170-
if (err) {
171-
if (err.code == 'ENOENT') {
172-
if (msbuilds.length) {
173-
return verifyMsbuild()
174-
} else {
175-
callback(new Error(notfoundErr))
176-
}
177-
} else {
178-
callback(err)
179-
}
180-
return
181-
}
182-
command = msbuildPath
183-
copyNodeLib()
184-
})
185-
})()
186-
})
134+
} catch (e) {
135+
return callback(new Error(err.message + '\n' + notfoundErr))
136+
}
137+
command = msbuild_path
138+
copyNodeLib()
187139
}
188140

189141
/**

lib/configure.js

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var fs = require('graceful-fs')
2323
, findNodeDirectory = require('./find-node-directory')
2424
, msgFormat = require('util').format
2525
if (win)
26-
var findVS2017 = require('./find-vs2017')
26+
var findVS = require('windows-autoconf')
2727

2828
exports.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' for the current module'
2929

@@ -41,15 +41,13 @@ function configure (gyp, argv, callback) {
4141
callback(err)
4242
} else {
4343
python = found
44-
getNodeDir()
44+
// 'python' should be set by now
45+
process.env.PYTHON = python
46+
getNodeDir()
4547
}
4648
})
4749

4850
function getNodeDir () {
49-
50-
// 'python' should be set by now
51-
process.env.PYTHON = python
52-
5351
if (gyp.opts.nodedir) {
5452
// --nodedir was specified. use that for the dev files
5553
nodeDir = gyp.opts.nodedir.replace(/^~/, osenv.home())
@@ -88,22 +86,29 @@ function configure (gyp, argv, callback) {
8886
mkdirp(buildDir, function (err, isNew) {
8987
if (err) return callback(err)
9088
log.verbose('build dir', '"build" dir needed to be created?', isNew)
91-
if (win && (!gyp.opts.msvs_version || gyp.opts.msvs_version === '2017')) {
92-
findVS2017(function (err, vsSetup) {
93-
if (err) {
94-
log.verbose('Not using VS2017:', err.message)
95-
createConfigFile()
96-
} else {
97-
createConfigFile(null, vsSetup)
98-
}
99-
})
100-
} else {
101-
createConfigFile()
102-
}
89+
createConfigFile()
10390
})
10491
}
10592

106-
function createConfigFile (err, vsSetup) {
93+
function findVisualStudio2017 (defaults) {
94+
if (gyp.opts.msvs_version && gyp.opts.msvs_version !== '2017')
95+
return
96+
97+
try {
98+
var vsSetup = findVS.getVS2017Setup()
99+
if (!vsSetup || !vsSetup.InstallationPath) return
100+
} catch (_) {
101+
return
102+
}
103+
104+
gyp.opts.msvs_version = '2015'
105+
process.env['GYP_MSVS_VERSION'] = 2015
106+
process.env['GYP_MSVS_OVERRIDE_PATH'] = vsSetup.InstallationPath
107+
defaults['msbuild_toolset'] = 'v141'
108+
defaults['msvs_windows_target_platform_version'] = vsSetup.SDK
109+
}
110+
111+
function createConfigFile (err) {
107112
if (err) return callback(err)
108113

109114
var configFilename = 'config.gypi'
@@ -150,18 +155,8 @@ function configure (gyp, argv, callback) {
150155
// disable -T "thin" static archives by default
151156
variables.standalone_static_library = gyp.opts.thin ? 0 : 1
152157

153-
if (vsSetup) {
154-
// GYP doesn't (yet) have support for VS2017, so we force it to VS2015
155-
// to avoid pulling a floating patch that has not landed upstream.
156-
// Ref: https://chromium-review.googlesource.com/#/c/433540/
157-
gyp.opts.msvs_version = '2015'
158-
process.env['GYP_MSVS_VERSION'] = 2015
159-
process.env['GYP_MSVS_OVERRIDE_PATH'] = vsSetup.path
160-
defaults['msbuild_toolset'] = 'v141'
161-
defaults['msvs_windows_target_platform_version'] = vsSetup.sdk
162-
variables['msbuild_path'] = path.join(vsSetup.path, 'MSBuild', '15.0',
163-
'Bin', 'MSBuild.exe')
164-
}
158+
if (win)
159+
findVisualStudio2017(defaults)
165160

166161
// loop through the rest of the opts and add the unknown ones as variables.
167162
// this allows for module-specific configure flags like:
@@ -227,7 +222,7 @@ function configure (gyp, argv, callback) {
227222
}
228223

229224
function hasMsvsVersion () {
230-
return argv.some(function (arg) {
225+
return argv.find(function (arg) {
231226
return arg.indexOf('msvs_version') === 0
232227
})
233228
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"rimraf": "2",
3535
"semver": "~5.3.0",
3636
"tar": "^2.0.0",
37+
"windows-autoconf": "^1.9.7",
3738
"which": "1"
3839
},
3940
"engines": {

0 commit comments

Comments
 (0)