Skip to content

Commit 0005e2a

Browse files
committed
win: use windows-autoconf to setup, even for VS2017
1 parent ec5fc36 commit 0005e2a

File tree

4 files changed

+47
-64
lines changed

4 files changed

+47
-64
lines changed

.gitignore

+2
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

+14-55
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,61 +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-
log.verbose('could not find "msbuild.exe" in PATH - finding location in registry')
128-
var notfoundErr = 'Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2008+ installed?'
129-
var cmd = 'reg query "HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions" /s'
130-
if (process.arch !== 'ia32')
131-
cmd += ' /reg:32'
132-
exec(cmd, function (err, stdout, stderr) {
133-
if (err) {
134-
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))
135133
}
136-
var reVers = /ToolsVersions\\([^\\]+)$/i
137-
, rePath = /\r\n[ \t]+MSBuildToolsPath[ \t]+REG_SZ[ \t]+([^\r]+)/i
138-
, msbuilds = []
139-
, r
140-
, msbuildPath
141-
stdout.split('\r\n\r\n').forEach(function(l) {
142-
if (!l) return
143-
l = l.trim()
144-
if (r = reVers.exec(l.substring(0, l.indexOf('\r\n')))) {
145-
var ver = parseFloat(r[1], 10)
146-
if (ver >= 3.5) {
147-
if (r = rePath.exec(l)) {
148-
msbuilds.push({
149-
version: ver,
150-
path: r[1]
151-
})
152-
}
153-
}
154-
}
155-
})
156-
msbuilds.sort(function (x, y) {
157-
return (x.version < y.version ? -1 : 1)
158-
})
159-
;(function verifyMsbuild () {
160-
if (!msbuilds.length) return callback(new Error(notfoundErr))
161-
msbuildPath = path.resolve(msbuilds.pop().path, 'msbuild.exe')
162-
fs.stat(msbuildPath, function (err, stat) {
163-
if (err) {
164-
if (err.code == 'ENOENT') {
165-
if (msbuilds.length) {
166-
return verifyMsbuild()
167-
} else {
168-
callback(new Error(notfoundErr))
169-
}
170-
} else {
171-
callback(err)
172-
}
173-
return
174-
}
175-
command = msbuildPath
176-
copyNodeLib()
177-
})
178-
})()
179-
})
134+
} catch (e) {
135+
return callback(new Error(err.message + '\n' + notfoundErr))
136+
}
137+
command = msbuild_path
138+
copyNodeLib()
180139
}
181140

182141
/**

lib/configure.js

+30-9
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ var fs = require('graceful-fs')
1919
, cp = require('child_process')
2020
, extend = require('util')._extend
2121
, processRelease = require('./process-release')
22-
, win = process.platform == 'win32'
22+
, win = process.platform === 'win32'
2323
, findNodeDirectory = require('./find-node-directory')
2424
, msgFormat = require('util').format
25+
if (win)
26+
var findVS = require('windows-autoconf')
2527

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

@@ -39,15 +41,13 @@ function configure (gyp, argv, callback) {
3941
callback(err)
4042
} else {
4143
python = found
42-
getNodeDir()
44+
// 'python' should be set by now
45+
process.env.PYTHON = python
46+
getNodeDir()
4347
}
4448
})
4549

4650
function getNodeDir () {
47-
48-
// 'python' should be set by now
49-
process.env.PYTHON = python
50-
5151
if (gyp.opts.nodedir) {
5252
// --nodedir was specified. use that for the dev files
5353
nodeDir = gyp.opts.nodedir.replace(/^~/, osenv.home())
@@ -90,6 +90,24 @@ function configure (gyp, argv, callback) {
9090
})
9191
}
9292

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+
93111
function createConfigFile (err) {
94112
if (err) return callback(err)
95113

@@ -137,6 +155,9 @@ function configure (gyp, argv, callback) {
137155
// disable -T "thin" static archives by default
138156
variables.standalone_static_library = gyp.opts.thin ? 0 : 1
139157

158+
if (win)
159+
findVisualStudio2017(defaults)
160+
140161
// loop through the rest of the opts and add the unknown ones as variables.
141162
// this allows for module-specific configure flags like:
142163
//
@@ -201,7 +222,7 @@ function configure (gyp, argv, callback) {
201222
}
202223

203224
function hasMsvsVersion () {
204-
return argv.some(function (arg) {
225+
return argv.find(function (arg) {
205226
return arg.indexOf('msvs_version') === 0
206227
})
207228
}
@@ -317,9 +338,9 @@ function configure (gyp, argv, callback) {
317338
}
318339

319340
/**
320-
* Returns the first file or directory from an array of candidates that is
341+
* Returns the first file or directory from an array of candidates that is
321342
* readable by the current user, or undefined if none of the candidates are
322-
* readable.
343+
* readable.
323344
*/
324345
function findAccessibleSync (logprefix, dir, candidates) {
325346
for (var next = 0; next < candidates.length; next++) {

package.json

+1
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.1",
3738
"which": "1"
3839
},
3940
"engines": {

0 commit comments

Comments
 (0)