From 088c11694a9f575e5c0fe10ab9efb55d14019be7 Mon Sep 17 00:00:00 2001 From: Gar Date: Mon, 15 Nov 2021 06:57:11 -0800 Subject: [PATCH] deps: node-gyp@8.4.0 * feat: support vs2022 * feat: build with config.gypi from node headers PR-URL: https://github.com/npm/cli/pull/4045 Credit: @wraithgar Close: #4045 Reviewed-by: @lukekarrys --- node_modules/node-gyp/lib/configure.js | 12 ++--- .../node-gyp/lib/create-config-gypi.js | 49 ++++++++++++++----- .../node-gyp/lib/find-visualstudio.js | 14 +++++- node_modules/node-gyp/lib/node-gyp.js | 3 +- node_modules/node-gyp/package.json | 2 +- .../fixtures/nodedir/include/node/config.gypi | 6 +++ .../node-gyp/test/test-configure-python.js | 5 +- .../node-gyp/test/test-create-config-gypi.js | 47 +++++++++++++++--- package-lock.json | 14 +++--- package.json | 2 +- 10 files changed, 117 insertions(+), 37 deletions(-) create mode 100644 node_modules/node-gyp/test/fixtures/nodedir/include/node/config.gypi diff --git a/node_modules/node-gyp/lib/configure.js b/node_modules/node-gyp/lib/configure.js index 682f1e6f95a71..d9b8fe340ecdd 100644 --- a/node_modules/node-gyp/lib/configure.js +++ b/node_modules/node-gyp/lib/configure.js @@ -97,17 +97,15 @@ function configure (gyp, argv, callback) { process.env.GYP_MSVS_VERSION = Math.min(vsInfo.versionYear, 2015) process.env.GYP_MSVS_OVERRIDE_PATH = vsInfo.path } - createConfigGypi({ gyp, buildDir, nodeDir, vsInfo }, (err, configPath) => { + createConfigGypi({ gyp, buildDir, nodeDir, vsInfo }).then(configPath => { configs.push(configPath) - findConfigs(err) + findConfigs() + }).catch(err => { + callback(err) }) } - function findConfigs (err) { - if (err) { - return callback(err) - } - + function findConfigs () { var name = configNames.shift() if (!name) { return runGyp() diff --git a/node_modules/node-gyp/lib/create-config-gypi.js b/node_modules/node-gyp/lib/create-config-gypi.js index 4d137e6b59d4b..dbcb8b485c5df 100644 --- a/node_modules/node-gyp/lib/create-config-gypi.js +++ b/node_modules/node-gyp/lib/create-config-gypi.js @@ -4,19 +4,45 @@ const fs = require('graceful-fs') const log = require('npmlog') const path = require('path') -function getBaseConfigGypi () { - const config = JSON.parse(JSON.stringify(process.config)) +function parseConfigGypi (config) { + // translated from tools/js2c.py of Node.js + // 1. string comments + config = config.replace(/#.*/g, '') + // 2. join multiline strings + config = config.replace(/'$\s+'/mg, '') + // 3. normalize string literals from ' into " + config = config.replace(/'/g, '"') + return JSON.parse(config) +} + +async function getBaseConfigGypi ({ gyp, nodeDir }) { + // try reading $nodeDir/include/node/config.gypi first when: + // 1. --dist-url or --nodedir is specified + // 2. and --force-process-config is not specified + const shouldReadConfigGypi = (gyp.opts.nodedir || gyp.opts['dist-url']) && !gyp.opts['force-process-config'] + if (shouldReadConfigGypi && nodeDir) { + try { + const baseConfigGypiPath = path.resolve(nodeDir, 'include/node/config.gypi') + const baseConfigGypi = await fs.promises.readFile(baseConfigGypiPath) + return parseConfigGypi(baseConfigGypi.toString()) + } catch (err) { + log.warn('read config.gypi', err.message) + } + } + + // fallback to process.config if it is invalid + return JSON.parse(JSON.stringify(process.config)) +} + +async function getCurrentConfigGypi ({ gyp, nodeDir, vsInfo }) { + const config = await getBaseConfigGypi({ gyp, nodeDir }) if (!config.target_defaults) { config.target_defaults = {} } if (!config.variables) { config.variables = {} } - return config -} -function getCurrentConfigGypi ({ gyp, nodeDir, vsInfo }) { - const config = getBaseConfigGypi() const defaults = config.target_defaults const variables = config.variables @@ -85,13 +111,13 @@ function getCurrentConfigGypi ({ gyp, nodeDir, vsInfo }) { return config } -function createConfigGypi ({ gyp, buildDir, nodeDir, vsInfo }, callback) { +async function createConfigGypi ({ gyp, buildDir, nodeDir, vsInfo }) { const configFilename = 'config.gypi' const configPath = path.resolve(buildDir, configFilename) log.verbose('build/' + configFilename, 'creating config file') - const config = getCurrentConfigGypi({ gyp, nodeDir, vsInfo }) + const config = await getCurrentConfigGypi({ gyp, nodeDir, vsInfo }) // ensures that any boolean values in config.gypi get stringified function boolsToString (k, v) { @@ -108,12 +134,13 @@ function createConfigGypi ({ gyp, buildDir, nodeDir, vsInfo }, callback) { const json = JSON.stringify(config, boolsToString, 2) log.verbose('build/' + configFilename, 'writing out config file: %s', configPath) - fs.writeFile(configPath, [prefix, json, ''].join('\n'), (err) => { - callback(err, configPath) - }) + await fs.promises.writeFile(configPath, [prefix, json, ''].join('\n')) + + return configPath } module.exports = createConfigGypi module.exports.test = { + parseConfigGypi: parseConfigGypi, getCurrentConfigGypi: getCurrentConfigGypi } diff --git a/node_modules/node-gyp/lib/find-visualstudio.js b/node_modules/node-gyp/lib/find-visualstudio.js index f2cce327e7cd3..64af7be3460ef 100644 --- a/node_modules/node-gyp/lib/find-visualstudio.js +++ b/node_modules/node-gyp/lib/find-visualstudio.js @@ -2,6 +2,7 @@ const log = require('npmlog') const execFile = require('child_process').execFile +const fs = require('fs') const path = require('path').win32 const logWithPrefix = require('./util').logWithPrefix const regSearchKeys = require('./util').regSearchKeys @@ -257,6 +258,10 @@ VisualStudioFinder.prototype = { ret.versionYear = 2019 return ret } + if (ret.versionMajor === 17) { + ret.versionYear = 2022 + return ret + } this.log.silly('- unsupported version:', ret.versionMajor) return {} }, @@ -264,15 +269,20 @@ VisualStudioFinder.prototype = { // Helper - process MSBuild information getMSBuild: function getMSBuild (info, versionYear) { const pkg = 'Microsoft.VisualStudio.VC.MSBuild.Base' + const msbuildPath = path.join(info.path, 'MSBuild', 'Current', 'Bin', 'MSBuild.exe') if (info.packages.indexOf(pkg) !== -1) { this.log.silly('- found VC.MSBuild.Base') if (versionYear === 2017) { return path.join(info.path, 'MSBuild', '15.0', 'Bin', 'MSBuild.exe') } if (versionYear === 2019) { - return path.join(info.path, 'MSBuild', 'Current', 'Bin', 'MSBuild.exe') + return msbuildPath } } + // visual studio 2022 don't has msbuild pkg + if (fs.existsSync(msbuildPath)) { + return msbuildPath + } return null }, @@ -293,6 +303,8 @@ VisualStudioFinder.prototype = { return 'v141' } else if (versionYear === 2019) { return 'v142' + } else if (versionYear === 2022) { + return 'v143' } this.log.silly('- invalid versionYear:', versionYear) return null diff --git a/node_modules/node-gyp/lib/node-gyp.js b/node_modules/node-gyp/lib/node-gyp.js index 81fc590919ba8..0f111856412dd 100644 --- a/node_modules/node-gyp/lib/node-gyp.js +++ b/node_modules/node-gyp/lib/node-gyp.js @@ -75,7 +75,8 @@ proto.configDefs = { 'dist-url': String, // 'install' tarball: String, // 'install' jobs: String, // 'build' - thin: String // 'configure' + thin: String, // 'configure' + 'force-process-config': Boolean // 'configure' } /** diff --git a/node_modules/node-gyp/package.json b/node_modules/node-gyp/package.json index cf9af6a0ecd7b..ea06551817143 100644 --- a/node_modules/node-gyp/package.json +++ b/node_modules/node-gyp/package.json @@ -11,7 +11,7 @@ "bindings", "gyp" ], - "version": "8.3.0", + "version": "8.4.0", "installVersion": 9, "author": "Nathan Rajlich (http://tootallnate.net)", "repository": { diff --git a/node_modules/node-gyp/test/fixtures/nodedir/include/node/config.gypi b/node_modules/node-gyp/test/fixtures/nodedir/include/node/config.gypi new file mode 100644 index 0000000000000..e767534082f8c --- /dev/null +++ b/node_modules/node-gyp/test/fixtures/nodedir/include/node/config.gypi @@ -0,0 +1,6 @@ +# Test configuration +{ + 'variables': { + 'build_with_electron': true + } +} diff --git a/node_modules/node-gyp/test/test-configure-python.js b/node_modules/node-gyp/test/test-configure-python.js index ac25f7972ea23..4290e7af1bb1c 100644 --- a/node_modules/node-gyp/test/test-configure-python.js +++ b/node_modules/node-gyp/test/test-configure-python.js @@ -11,7 +11,10 @@ const configure = requireInject('../lib/configure', { closeSync: function () { }, writeFile: function (file, data, cb) { cb() }, stat: function (file, cb) { cb(null, {}) }, - mkdir: function (dir, options, cb) { cb() } + mkdir: function (dir, options, cb) { cb() }, + promises: { + writeFile: function (file, data) { return Promise.resolve(null) } + } } }) diff --git a/node_modules/node-gyp/test/test-create-config-gypi.js b/node_modules/node-gyp/test/test-create-config-gypi.js index 933cae1326bab..eeac73fab1dcc 100644 --- a/node_modules/node-gyp/test/test-create-config-gypi.js +++ b/node_modules/node-gyp/test/test-create-config-gypi.js @@ -1,37 +1,70 @@ 'use strict' +const path = require('path') const { test } = require('tap') const gyp = require('../lib/node-gyp') const createConfigGypi = require('../lib/create-config-gypi') -const { getCurrentConfigGypi } = createConfigGypi.test +const { parseConfigGypi, getCurrentConfigGypi } = createConfigGypi.test -test('config.gypi with no options', function (t) { +test('config.gypi with no options', async function (t) { t.plan(2) const prog = gyp() prog.parseArgv([]) - const config = getCurrentConfigGypi({ gyp: prog, vsInfo: {} }) + const config = await getCurrentConfigGypi({ gyp: prog, vsInfo: {} }) t.equal(config.target_defaults.default_configuration, 'Release') t.equal(config.variables.target_arch, process.arch) }) -test('config.gypi with --debug', function (t) { +test('config.gypi with --debug', async function (t) { t.plan(1) const prog = gyp() prog.parseArgv(['_', '_', '--debug']) - const config = getCurrentConfigGypi({ gyp: prog, vsInfo: {} }) + const config = await getCurrentConfigGypi({ gyp: prog, vsInfo: {} }) t.equal(config.target_defaults.default_configuration, 'Debug') }) -test('config.gypi with custom options', function (t) { +test('config.gypi with custom options', async function (t) { t.plan(1) const prog = gyp() prog.parseArgv(['_', '_', '--shared-libxml2']) - const config = getCurrentConfigGypi({ gyp: prog, vsInfo: {} }) + const config = await getCurrentConfigGypi({ gyp: prog, vsInfo: {} }) t.equal(config.variables.shared_libxml2, true) }) + +test('config.gypi with nodedir', async function (t) { + t.plan(1) + + const nodeDir = path.join(__dirname, 'fixtures', 'nodedir') + + const prog = gyp() + prog.parseArgv(['_', '_', `--nodedir=${nodeDir}`]) + + const config = await getCurrentConfigGypi({ gyp: prog, nodeDir, vsInfo: {} }) + t.equal(config.variables.build_with_electron, true) +}) + +test('config.gypi with --force-process-config', async function (t) { + t.plan(1) + + const nodeDir = path.join(__dirname, 'fixtures', 'nodedir') + + const prog = gyp() + prog.parseArgv(['_', '_', '--force-process-config', `--nodedir=${nodeDir}`]) + + const config = await getCurrentConfigGypi({ gyp: prog, nodeDir, vsInfo: {} }) + t.equal(config.variables.build_with_electron, undefined) +}) + +test('config.gypi parsing', function (t) { + t.plan(1) + + const str = "# Some comments\n{'variables': {'multiline': 'A'\n'B'}}" + const config = parseConfigGypi(str) + t.deepEqual(config, { variables: { multiline: 'AB' } }) +}) diff --git a/package-lock.json b/package-lock.json index 36a8598b8afca..31698c8e74b99 100644 --- a/package-lock.json +++ b/package-lock.json @@ -127,7 +127,7 @@ "mkdirp": "^1.0.4", "mkdirp-infer-owner": "^2.0.0", "ms": "^2.1.2", - "node-gyp": "^8.3.0", + "node-gyp": "^8.4.0", "nopt": "^5.0.0", "npm-audit-report": "^2.1.5", "npm-install-checks": "^4.0.0", @@ -5255,9 +5255,9 @@ "dev": true }, "node_modules/node-gyp": { - "version": "8.3.0", - "resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.3.0.tgz", - "integrity": "sha512-e+vmKyTiybKgrmvs4M2REFKCnOd+NcrAAnn99Yko6NQA+zZdMlRvbIUHojfsHrSQ1CddLgZnHicnEVgDHziJzA==", + "version": "8.4.0", + "resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.4.0.tgz", + "integrity": "sha512-Bi/oCm5bH6F+FmzfUxJpPaxMEyIhszULGR3TprmTeku8/dMFcdTcypk120NeZqEt54r1BrgEKtm2jJiuIKE28Q==", "inBundle": true, "dependencies": { "env-paths": "^2.2.0", @@ -13968,9 +13968,9 @@ "dev": true }, "node-gyp": { - "version": "8.3.0", - "resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.3.0.tgz", - "integrity": "sha512-e+vmKyTiybKgrmvs4M2REFKCnOd+NcrAAnn99Yko6NQA+zZdMlRvbIUHojfsHrSQ1CddLgZnHicnEVgDHziJzA==", + "version": "8.4.0", + "resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.4.0.tgz", + "integrity": "sha512-Bi/oCm5bH6F+FmzfUxJpPaxMEyIhszULGR3TprmTeku8/dMFcdTcypk120NeZqEt54r1BrgEKtm2jJiuIKE28Q==", "requires": { "env-paths": "^2.2.0", "glob": "^7.1.4", diff --git a/package.json b/package.json index 276f8460e5cd2..3c5c3dae73bb7 100644 --- a/package.json +++ b/package.json @@ -96,7 +96,7 @@ "mkdirp": "^1.0.4", "mkdirp-infer-owner": "^2.0.0", "ms": "^2.1.2", - "node-gyp": "^8.3.0", + "node-gyp": "^8.4.0", "nopt": "^5.0.0", "npm-audit-report": "^2.1.5", "npm-install-checks": "^4.0.0",