Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f473591

Browse files
committedNov 15, 2021
* feat: support vs2022 * feat: build with config.gypi from node headers
1 parent 201df17 commit f473591

10 files changed

+117
-37
lines changed
 

‎node_modules/node-gyp/lib/configure.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,15 @@ function configure (gyp, argv, callback) {
9797
process.env.GYP_MSVS_VERSION = Math.min(vsInfo.versionYear, 2015)
9898
process.env.GYP_MSVS_OVERRIDE_PATH = vsInfo.path
9999
}
100-
createConfigGypi({ gyp, buildDir, nodeDir, vsInfo }, (err, configPath) => {
100+
createConfigGypi({ gyp, buildDir, nodeDir, vsInfo }).then(configPath => {
101101
configs.push(configPath)
102-
findConfigs(err)
102+
findConfigs()
103+
}).catch(err => {
104+
callback(err)
103105
})
104106
}
105107

106-
function findConfigs (err) {
107-
if (err) {
108-
return callback(err)
109-
}
110-
108+
function findConfigs () {
111109
var name = configNames.shift()
112110
if (!name) {
113111
return runGyp()

‎node_modules/node-gyp/lib/create-config-gypi.js

+38-11
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,45 @@ const fs = require('graceful-fs')
44
const log = require('npmlog')
55
const path = require('path')
66

7-
function getBaseConfigGypi () {
8-
const config = JSON.parse(JSON.stringify(process.config))
7+
function parseConfigGypi (config) {
8+
// translated from tools/js2c.py of Node.js
9+
// 1. string comments
10+
config = config.replace(/#.*/g, '')
11+
// 2. join multiline strings
12+
config = config.replace(/'$\s+'/mg, '')
13+
// 3. normalize string literals from ' into "
14+
config = config.replace(/'/g, '"')
15+
return JSON.parse(config)
16+
}
17+
18+
async function getBaseConfigGypi ({ gyp, nodeDir }) {
19+
// try reading $nodeDir/include/node/config.gypi first when:
20+
// 1. --dist-url or --nodedir is specified
21+
// 2. and --force-process-config is not specified
22+
const shouldReadConfigGypi = (gyp.opts.nodedir || gyp.opts['dist-url']) && !gyp.opts['force-process-config']
23+
if (shouldReadConfigGypi && nodeDir) {
24+
try {
25+
const baseConfigGypiPath = path.resolve(nodeDir, 'include/node/config.gypi')
26+
const baseConfigGypi = await fs.promises.readFile(baseConfigGypiPath)
27+
return parseConfigGypi(baseConfigGypi.toString())
28+
} catch (err) {
29+
log.warn('read config.gypi', err.message)
30+
}
31+
}
32+
33+
// fallback to process.config if it is invalid
34+
return JSON.parse(JSON.stringify(process.config))
35+
}
36+
37+
async function getCurrentConfigGypi ({ gyp, nodeDir, vsInfo }) {
38+
const config = await getBaseConfigGypi({ gyp, nodeDir })
939
if (!config.target_defaults) {
1040
config.target_defaults = {}
1141
}
1242
if (!config.variables) {
1343
config.variables = {}
1444
}
15-
return config
16-
}
1745

18-
function getCurrentConfigGypi ({ gyp, nodeDir, vsInfo }) {
19-
const config = getBaseConfigGypi()
2046
const defaults = config.target_defaults
2147
const variables = config.variables
2248

@@ -85,13 +111,13 @@ function getCurrentConfigGypi ({ gyp, nodeDir, vsInfo }) {
85111
return config
86112
}
87113

88-
function createConfigGypi ({ gyp, buildDir, nodeDir, vsInfo }, callback) {
114+
async function createConfigGypi ({ gyp, buildDir, nodeDir, vsInfo }) {
89115
const configFilename = 'config.gypi'
90116
const configPath = path.resolve(buildDir, configFilename)
91117

92118
log.verbose('build/' + configFilename, 'creating config file')
93119

94-
const config = getCurrentConfigGypi({ gyp, nodeDir, vsInfo })
120+
const config = await getCurrentConfigGypi({ gyp, nodeDir, vsInfo })
95121

96122
// ensures that any boolean values in config.gypi get stringified
97123
function boolsToString (k, v) {
@@ -108,12 +134,13 @@ function createConfigGypi ({ gyp, buildDir, nodeDir, vsInfo }, callback) {
108134

109135
const json = JSON.stringify(config, boolsToString, 2)
110136
log.verbose('build/' + configFilename, 'writing out config file: %s', configPath)
111-
fs.writeFile(configPath, [prefix, json, ''].join('\n'), (err) => {
112-
callback(err, configPath)
113-
})
137+
await fs.promises.writeFile(configPath, [prefix, json, ''].join('\n'))
138+
139+
return configPath
114140
}
115141

116142
module.exports = createConfigGypi
117143
module.exports.test = {
144+
parseConfigGypi: parseConfigGypi,
118145
getCurrentConfigGypi: getCurrentConfigGypi
119146
}

‎node_modules/node-gyp/lib/find-visualstudio.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const log = require('npmlog')
44
const execFile = require('child_process').execFile
5+
const fs = require('fs')
56
const path = require('path').win32
67
const logWithPrefix = require('./util').logWithPrefix
78
const regSearchKeys = require('./util').regSearchKeys
@@ -257,22 +258,31 @@ VisualStudioFinder.prototype = {
257258
ret.versionYear = 2019
258259
return ret
259260
}
261+
if (ret.versionMajor === 17) {
262+
ret.versionYear = 2022
263+
return ret
264+
}
260265
this.log.silly('- unsupported version:', ret.versionMajor)
261266
return {}
262267
},
263268

264269
// Helper - process MSBuild information
265270
getMSBuild: function getMSBuild (info, versionYear) {
266271
const pkg = 'Microsoft.VisualStudio.VC.MSBuild.Base'
272+
const msbuildPath = path.join(info.path, 'MSBuild', 'Current', 'Bin', 'MSBuild.exe')
267273
if (info.packages.indexOf(pkg) !== -1) {
268274
this.log.silly('- found VC.MSBuild.Base')
269275
if (versionYear === 2017) {
270276
return path.join(info.path, 'MSBuild', '15.0', 'Bin', 'MSBuild.exe')
271277
}
272278
if (versionYear === 2019) {
273-
return path.join(info.path, 'MSBuild', 'Current', 'Bin', 'MSBuild.exe')
279+
return msbuildPath
274280
}
275281
}
282+
// visual studio 2022 don't has msbuild pkg
283+
if (fs.existsSync(msbuildPath)) {
284+
return msbuildPath
285+
}
276286
return null
277287
},
278288

@@ -293,6 +303,8 @@ VisualStudioFinder.prototype = {
293303
return 'v141'
294304
} else if (versionYear === 2019) {
295305
return 'v142'
306+
} else if (versionYear === 2022) {
307+
return 'v143'
296308
}
297309
this.log.silly('- invalid versionYear:', versionYear)
298310
return null

‎node_modules/node-gyp/lib/node-gyp.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ proto.configDefs = {
7575
'dist-url': String, // 'install'
7676
tarball: String, // 'install'
7777
jobs: String, // 'build'
78-
thin: String // 'configure'
78+
thin: String, // 'configure'
79+
'force-process-config': Boolean // 'configure'
7980
}
8081

8182
/**

‎node_modules/node-gyp/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"bindings",
1212
"gyp"
1313
],
14-
"version": "8.3.0",
14+
"version": "8.4.0",
1515
"installVersion": 9,
1616
"author": "Nathan Rajlich <nathan@tootallnate.net> (http://tootallnate.net)",
1717
"repository": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Test configuration
2+
{
3+
'variables': {
4+
'build_with_electron': true
5+
}
6+
}

‎node_modules/node-gyp/test/test-configure-python.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const configure = requireInject('../lib/configure', {
1111
closeSync: function () { },
1212
writeFile: function (file, data, cb) { cb() },
1313
stat: function (file, cb) { cb(null, {}) },
14-
mkdir: function (dir, options, cb) { cb() }
14+
mkdir: function (dir, options, cb) { cb() },
15+
promises: {
16+
writeFile: function (file, data) { return Promise.resolve(null) }
17+
}
1518
}
1619
})
1720

Original file line numberDiff line numberDiff line change
@@ -1,37 +1,70 @@
11
'use strict'
22

3+
const path = require('path')
34
const { test } = require('tap')
45
const gyp = require('../lib/node-gyp')
56
const createConfigGypi = require('../lib/create-config-gypi')
6-
const { getCurrentConfigGypi } = createConfigGypi.test
7+
const { parseConfigGypi, getCurrentConfigGypi } = createConfigGypi.test
78

8-
test('config.gypi with no options', function (t) {
9+
test('config.gypi with no options', async function (t) {
910
t.plan(2)
1011

1112
const prog = gyp()
1213
prog.parseArgv([])
1314

14-
const config = getCurrentConfigGypi({ gyp: prog, vsInfo: {} })
15+
const config = await getCurrentConfigGypi({ gyp: prog, vsInfo: {} })
1516
t.equal(config.target_defaults.default_configuration, 'Release')
1617
t.equal(config.variables.target_arch, process.arch)
1718
})
1819

19-
test('config.gypi with --debug', function (t) {
20+
test('config.gypi with --debug', async function (t) {
2021
t.plan(1)
2122

2223
const prog = gyp()
2324
prog.parseArgv(['_', '_', '--debug'])
2425

25-
const config = getCurrentConfigGypi({ gyp: prog, vsInfo: {} })
26+
const config = await getCurrentConfigGypi({ gyp: prog, vsInfo: {} })
2627
t.equal(config.target_defaults.default_configuration, 'Debug')
2728
})
2829

29-
test('config.gypi with custom options', function (t) {
30+
test('config.gypi with custom options', async function (t) {
3031
t.plan(1)
3132

3233
const prog = gyp()
3334
prog.parseArgv(['_', '_', '--shared-libxml2'])
3435

35-
const config = getCurrentConfigGypi({ gyp: prog, vsInfo: {} })
36+
const config = await getCurrentConfigGypi({ gyp: prog, vsInfo: {} })
3637
t.equal(config.variables.shared_libxml2, true)
3738
})
39+
40+
test('config.gypi with nodedir', async function (t) {
41+
t.plan(1)
42+
43+
const nodeDir = path.join(__dirname, 'fixtures', 'nodedir')
44+
45+
const prog = gyp()
46+
prog.parseArgv(['_', '_', `--nodedir=${nodeDir}`])
47+
48+
const config = await getCurrentConfigGypi({ gyp: prog, nodeDir, vsInfo: {} })
49+
t.equal(config.variables.build_with_electron, true)
50+
})
51+
52+
test('config.gypi with --force-process-config', async function (t) {
53+
t.plan(1)
54+
55+
const nodeDir = path.join(__dirname, 'fixtures', 'nodedir')
56+
57+
const prog = gyp()
58+
prog.parseArgv(['_', '_', '--force-process-config', `--nodedir=${nodeDir}`])
59+
60+
const config = await getCurrentConfigGypi({ gyp: prog, nodeDir, vsInfo: {} })
61+
t.equal(config.variables.build_with_electron, undefined)
62+
})
63+
64+
test('config.gypi parsing', function (t) {
65+
t.plan(1)
66+
67+
const str = "# Some comments\n{'variables': {'multiline': 'A'\n'B'}}"
68+
const config = parseConfigGypi(str)
69+
t.deepEqual(config, { variables: { multiline: 'AB' } })
70+
})

‎package-lock.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
"mkdirp": "^1.0.4",
128128
"mkdirp-infer-owner": "^2.0.0",
129129
"ms": "^2.1.2",
130-
"node-gyp": "^8.3.0",
130+
"node-gyp": "^8.4.0",
131131
"nopt": "^5.0.0",
132132
"npm-audit-report": "^2.1.5",
133133
"npm-install-checks": "^4.0.0",
@@ -5174,9 +5174,9 @@
51745174
"dev": true
51755175
},
51765176
"node_modules/node-gyp": {
5177-
"version": "8.3.0",
5178-
"resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.3.0.tgz",
5179-
"integrity": "sha512-e+vmKyTiybKgrmvs4M2REFKCnOd+NcrAAnn99Yko6NQA+zZdMlRvbIUHojfsHrSQ1CddLgZnHicnEVgDHziJzA==",
5177+
"version": "8.4.0",
5178+
"resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.4.0.tgz",
5179+
"integrity": "sha512-Bi/oCm5bH6F+FmzfUxJpPaxMEyIhszULGR3TprmTeku8/dMFcdTcypk120NeZqEt54r1BrgEKtm2jJiuIKE28Q==",
51805180
"inBundle": true,
51815181
"dependencies": {
51825182
"env-paths": "^2.2.0",
@@ -13814,9 +13814,9 @@
1381413814
"dev": true
1381513815
},
1381613816
"node-gyp": {
13817-
"version": "8.3.0",
13818-
"resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.3.0.tgz",
13819-
"integrity": "sha512-e+vmKyTiybKgrmvs4M2REFKCnOd+NcrAAnn99Yko6NQA+zZdMlRvbIUHojfsHrSQ1CddLgZnHicnEVgDHziJzA==",
13817+
"version": "8.4.0",
13818+
"resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.4.0.tgz",
13819+
"integrity": "sha512-Bi/oCm5bH6F+FmzfUxJpPaxMEyIhszULGR3TprmTeku8/dMFcdTcypk120NeZqEt54r1BrgEKtm2jJiuIKE28Q==",
1382013820
"requires": {
1382113821
"env-paths": "^2.2.0",
1382213822
"glob": "^7.1.4",

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
"mkdirp": "^1.0.4",
9797
"mkdirp-infer-owner": "^2.0.0",
9898
"ms": "^2.1.2",
99-
"node-gyp": "^8.3.0",
99+
"node-gyp": "^8.4.0",
100100
"nopt": "^5.0.0",
101101
"npm-audit-report": "^2.1.5",
102102
"npm-install-checks": "^4.0.0",

0 commit comments

Comments
 (0)
Please sign in to comment.