From 305c7bf66e7114f4b25336d092bf4d0efed8797e Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Tue, 7 Feb 2017 10:00:40 -0500 Subject: [PATCH] ninja: harden cmd wrap mechanism --- lib/gyp.js | 2 +- lib/gyp/generator/ninja/index.js | 10 +++++----- lib/gyp/platform/win.js | 22 +++++++++++----------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/gyp.js b/lib/gyp.js index 623bd7c..222cb02 100644 --- a/lib/gyp.js +++ b/lib/gyp.js @@ -358,7 +358,7 @@ gyp.main = function main(args, extra) { gyp_binary: args[0], home_dot_gyp: homeDotGyp, root_targets: options.root_targets, - target_arch: cmdlineDefaultVariables['target_arch'] || '' + target_arch: cmdlineDefaultVariables['target_arch'] || 'ia32' }; // Start with the default variables from the command line. diff --git a/lib/gyp/generator/ninja/index.js b/lib/gyp/generator/ninja/index.js index f59dd49..0690936 100644 --- a/lib/gyp/generator/ninja/index.js +++ b/lib/gyp/generator/ninja/index.js @@ -354,7 +354,7 @@ Ninja.prototype.actionCmd = function actionCmd(base, toBase, cmds) { return res; // TODO(indutny): escape quotes in res - return `cmd.exe /s /c "${res}"`; + return `${gyp.platform.win.ninjaWrap} powershell -Command ${res.replace(/ & /g, ' ; ')}`; }; Ninja.prototype.copies = function copies() { @@ -558,6 +558,8 @@ function NinjaMain(targetList, targetDicts, data, params, config) { } NinjaMain.prototype.generate = function generate() { + if (process.platform === 'win32') + gyp.platform.win.setupNinjaWrapper(this.params['target_arch']); this.vars(); this.rulesAndTargets(); this.defaults(); @@ -708,8 +710,7 @@ NinjaMain.prototype.rulesAndTargets = function rulesAndTargets() { }); if (process.platform === 'win32') { - gyp.platform.win.ninjaRules(main, this.configDir, - this.options.generator_flags, this.params); + gyp.platform.win.ninjaRules(main, this.configDir, this.params); } else { gyp.platform.unix.ninjaRules(main, useCxx); } @@ -785,8 +786,7 @@ NinjaMain.prototype.build = function build() { execSync('ninja -C ' + this.configDir, { stdio: 'inherit' }); }; -exports.generateOutput = function generateOutput(targetList, targetDicts, data, - params) { +exports.generateOutput = function generateOutput(targetList, targetDicts, data, params) { if (targetList.length === 0) throw new Error('No targets to build!'); diff --git a/lib/gyp/platform/win.js b/lib/gyp/platform/win.js index c25af75..a832a9b 100644 --- a/lib/gyp/platform/win.js +++ b/lib/gyp/platform/win.js @@ -7,18 +7,18 @@ const process = gyp.bindings.process; const win = exports; -win.ninjaRules = function ninjaRules(n, outDir, generatorFlags, params) { - let envFile = win.genEnvironment( - outDir, - generatorFlags['msvs_version'] || 'auto', - params['target_arch'] || 'ia32'); - if (envFile) - envFile = ` -e ${envFile} `; - else - envFile = ''; +win.getEnvFileName = function (target_arch) { + return `environment.${target_arch}`; +}; - const ninjaWrap = `ninja -t msvc ${envFile}--`; +win.setupNinjaWrapper = function (target_arch) { + const envFile = win.getEnvFileName(target_arch); + this.ninjaWrap = `ninja -t msvc -e ${envFile} --`; +}; +win.ninjaRules = function ninjaRules(n, outDir, params) { + win.genEnvironment(outDir, params['target_arch']); + const ninjaWrap = this.ninjaWrap; n.rule('cc', { deps: 'msvc', // TODO(indutny): is /Fd$pdbname_c needed here? @@ -498,8 +498,8 @@ win.genEnvironment = function genEnvironment(outDir, version, arch) { } const envBlock = formatEnvBlock(lines); - const envFile = 'environment.' + arch; + const envFile = win.getEnvFileName(target_arch); fs.writeFileSync(path.join(outDir, envFile), envBlock); return envFile; };