Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got node to build on VS2017 #33

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
"babel-cli": "^6.10.1",
"babel-preset-env": "^1.2.2",
"babel-register": "^6.9.0",
"debug": "^2.6.1",
"eslint": "^2.12.0",
"mocha": "^2.5.3",
"ninja.js": "^1.1.0",
"rimraf": "^2.5.2"
},
"optionalDependencies": {
"windows-autoconf": "^1.9.0",
"ninja.js": "^1.1.0"
},
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/gyp.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ 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'] ||
gyp.bindings.process.arch
};

// Start with the default variables from the command line.
Expand Down
15 changes: 15 additions & 0 deletions src/gyp/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ exports.process = {
env: process.env,
cwd: () => process.cwd(),
platform: process.platform,
arch: process.arch,
exit: (code) => process.exit(code)
};

Expand All @@ -42,3 +43,17 @@ exports.log = function log(message) {
exports.error = function error(message) {
process.stderr.write(message + '\n');
};

Object.defineProperty(exports, 'win', {
get: function get() {
// ====== a late require ========
const getter = require('windows-autoconf');
// We just need all the binding to be set on `exports`
getter.setBindings(exports);
return {
getMSVSVersion: getter.getMSVSVersion,
getOSBits: getter.getOSBits,
resolveDevEnvironment: getter.resolveDevEnvironment
};
}
});
35 changes: 26 additions & 9 deletions src/gyp/generator/ninja/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ function calculateVariables(defaultVariables) {
defaultVariables['STATIC_LIB_SUFFIX'] = '.lib';
defaultVariables['SHARED_LIB_PREFIX'] = '';
defaultVariables['SHARED_LIB_SUFFIX'] = '.dll';
defaultVariables['MSVS_VERSION'] = gyp.platform.win.getMSVSVersion();
defaultVariables['MSVS_OS_BITS'] = gyp.platform.win.getOSBits();
defaultVariables['MSVS_VERSION'] = gyp.bindings.win.getMSVSVersion();
defaultVariables['MSVS_OS_BITS'] = gyp.bindings.win.getOSBits();
} else {
// On Solaris NODE_PLATFORM is `sunos`, while GYP's `OS` variable should be
// `solaris`
Expand Down Expand Up @@ -126,6 +126,9 @@ function Ninja(options) {
this.objExt = this.flavor === 'win32' ? '.obj' : '.o';

this.bashAnd = this.flavor === 'win32' ? '&' : '&&';

this.actionNames = options.actionNames;
this.targetArch = options.targetArch;
}

Ninja.prototype.expand = function expand(p, productDir) {
Expand Down Expand Up @@ -350,8 +353,8 @@ Ninja.prototype.actionCmd = function actionCmd(base, toBase, cmds) {
if (this.flavor !== 'win32')
return res;

// TODO(indutny): escape quotes in res
return `cmd.exe /s /c "${res}"`;
const nw = gyp.platform.win.getNinjaWrapper(this.targetArch);
return `${nw} cmd.exe /s /c "${res}"`;
};

Ninja.prototype.copies = function copies() {
Expand Down Expand Up @@ -395,7 +398,11 @@ Ninja.prototype.actions = function actions() {

let res = [];
list.forEach((action) => {
const actionRule = action.action_name + '_' + this.index;
const safeName = action.action_name.replace(/\s/g, '_');
const an = this.actionNames;
const counterRef = an[safeName] || (an[safeName] = {cnt: 0});
const actionRule = safeName + (counterRef.cnt || '');
counterRef.cnt++;

const base = gyp.common.cachedRelative(this.configDir, this.srcDir);
const toBase = gyp.common.cachedRelative(this.srcDir, this.configDir);
Expand All @@ -410,6 +417,12 @@ Ninja.prototype.actions = function actions() {

res = res.concat(outputs);

if (action.process_outputs_as_sources === '1') {
let trg = this.targetDict;
trg.sources = trg.sources || [];
trg.sources = trg.sources.concat(action.outputs);
}

this.n.build(actionRule, outputs, inputs, {
orderOnlyDeps: deps
});
Expand Down Expand Up @@ -673,16 +686,21 @@ NinjaMain.prototype.rulesAndTargets = function rulesAndTargets() {

let useCxx = false;
const ninjas = this.ninjas;
const actionNames = {};
const targetArch = this.params['target_arch'];
const ninjaList = this.targetList.map((target, index) => {
const targetDict = this.targetDicts[target].configurations[this.config];
const ninja = new Ninja({
index: index,
outDir: this.outDir,
configDir: this.configDir,
topDir: this.topDir,
target: target,
targetDict: this.targetDicts[target].configurations[this.config],
targetDict: targetDict,
ninjas: ninjas,
config: this.config
config: this.config,
actionNames: actionNames,
targetArch: targetArch
});
ninjas[target] = ninja;
return ninja;
Expand All @@ -695,8 +713,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);
}
Expand Down
151 changes: 24 additions & 127 deletions src/gyp/platform/win.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
const gyp = require('../../gyp');
const fs = gyp.bindings.fs;
const path = gyp.bindings.path;
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 getEnvFileName(target_arch) {
return `environment.${target_arch}`;
};

const ninjaWrap = `ninja -t msvc ${envFile}--`;
win.getNinjaWrapper = function getNinjaWrapper(target_arch) {
const envFile = win.getEnvFileName(target_arch);
return `ninja -t msvc -e ${envFile} --`;
};

win.ninjaRules = function ninjaRules(n, outDir, params) {
win.genEnvironment(outDir, params['target_arch']);
const nw = this.getNinjaWrapper(params['target_arch']);
n.rule('cc', {
deps: 'msvc',
// TODO(indutny): is /Fd$pdbname_c needed here?
command: `${ninjaWrap} $cc /nologo /showIncludes /FC ` +
'@$out.rsp /c $in /Fo$out',
command: `${nw} $cc /nologo /showIncludes /FC @$out.rsp /c $in /Fo$out`,
rspfile: '$out.rsp',
rspfile_content: '$defines $includes $cflags $cflags_c',
description: 'CC $out'
Expand All @@ -32,39 +30,37 @@ win.ninjaRules = function ninjaRules(n, outDir, generatorFlags, params) {
n.rule('cxx', {
deps: 'msvc',
// TODO(indutny): is /Fd$pdbname_c needed here?
command: `${ninjaWrap} $cxx /nologo /showIncludes /FC ` +
'@$out.rsp /c $in /Fo$out',
command: `${nw} $cxx /nologo /showIncludes /FC @$out.rsp /c $in /Fo$out`,
rspfile: '$out.rsp',
rspfile_content: '$defines $includes $cflags $cflags_cc',
description: 'CXX $out'
});

n.rule('asm', {
command: `${ninjaWrap} $asm @$out.rsp /nologo /c /Fo $out $in`,
command: `${nw} $asm @$out.rsp /nologo /c /Fo $out $in`,
rspfile: '$out.rsp',
rspfile_content: '$defines $includes $asmflags',
description: 'ASM $out'
});

n.rule('link', {
command: `${ninjaWrap} $ld /nologo /OUT:$out @$out.rsp`,
command: `${nw} $ld /nologo /OUT:$out @$out.rsp`,
rspfile: '$out.rsp',
rspfile_content: '$in_newline $libs $ldflags',
pool: 'link_pool',
description: 'LINK $out'
});

n.rule('alink', {
command: `${ninjaWrap} $ar /nologo /ignore:4221 /OUT:$out @$out.rsp`,
command: `${nw} $ar /nologo /ignore:4221 /OUT:$out @$out.rsp`,
rspfile: '$out.rsp',
rspfile_content: '$in_newline $libs $arflags',
pool: 'link_pool',
description: 'ALINK $out'
});

n.rule('solink', {
command: `${ninjaWrap} $ld /IMPLIB:$out.lib /nologo /DLL /OUT:$out ` +
'@$out.rsp',
command: `${nw} $ld /IMPLIB:$out.lib /nologo /DLL /OUT:$out @$out.rsp`,
rspfile: '$out.rsp',
rspfile_content: '$in_newline $libs $ldflags',
pool: 'link_pool',
Expand Down Expand Up @@ -239,7 +235,7 @@ function linkerFlags(linker) {
if (stack_reserve_size) {
let stack_commit_size = linker.StackCommitSize || '';
if (stack_commit_size) stack_commit_size = ',' + stack_commit_size;
ldflags.push('/STACK' + stack_reserve_size + stack_commit_size);
ldflags.push('/STACK:' + stack_reserve_size + stack_commit_size);
}

ld('TerminalServerAware', {
Expand Down Expand Up @@ -364,112 +360,13 @@ win.detectVersion = function detectVersion() {
throw new Error('No known Visual Studio version found, sorry!');
};

const IMPORTANT_VARS =
/^(include|lib|libpath|path|pathext|systemroot|temp|tmp)=(.*)$/i;

function formatEnvBlock(lines) {
let res = '';
lines.forEach((line) => {
const match = line.match(IMPORTANT_VARS);
if (match === null)
return;

res += match[1].toUpperCase() + '=' + match[2] + '\0';
});
return res;
}

win.getMSVSVersion = function getMSVSVersion(version) {
const env = process.env;

if (!version)
version = env['GYP_MSVS_VERSION'] || 'auto';

// Try to find a MSVS installation
if (version === 'auto' && env['VS140COMNTOOLS'] || version === '2015')
return '2015';
if (version === 'auto' && env['VS120COMNTOOLS'] || version === '2013')
return '2013';
if (version === 'auto' && env['VS100COMNTOOLS'] || version === '2010')
return '2010';

return 'auto';
};

win.getOSBits = function getOSBits() {
const env = process.env;

// PROCESSOR_ARCHITEW6432 - is a system arch
// PROCESSOR_ARCHITECTURE - is a session arch
const hostArch = env['PROCESSOR_ARCHITEW6432'] ||
env['PROCESSOR_ARCHITECTURE'];
if (hostArch === 'AMD64')
return 64;
else
return 32;
};

win.genEnvironment = function genEnvironment(outDir, version, arch) {
const env = process.env;
let tools;

// Try to find a MSVS installation
if (version === 'auto' && env['VS140COMNTOOLS'] || version === '2015') {
version = '2015';
tools = path.join(env.VS140COMNTOOLS, '..', '..');
}
if (version === 'auto' && env['VS120COMNTOOLS'] || version === '2013') {
version = '2013';
tools = path.join(env.VS120COMNTOOLS, '..', '..');
}
// TODO(indutny): more versions?
if (version === 'auto' && env['VS100COMNTOOLS'] || version === '2010') {
version = '2010';
tools = path.join(env.VS120COMNTOOLS, '..', '..');
}
// TODO(indutny): does it work with MSVS Express?

if (version === 'auto') {
gyp.bindings.error('No Visual Studio found. When building - please ' +
'run `ninja` from the MSVS console');
return;
}

// NOTE: Largely inspired by MSVSVersion.py
const bits = win.getOSBits();

let vcvars;
// TODO(indutny): proper escape for the .bat file
if (arch === 'ia32') {
if (bits === 64)
vcvars = '"' + path.join(tools, 'VC', 'vcvarsall.bat') + '" amd64_x86';
else
vcvars = '"' + path.join(tools, 'Common7', 'Tools', 'vsvars32.bat') + '"';
} else if (arch === 'x64') {
let arg;
if (bits === 64)
arg = 'amd64';
else
arg = 'x86_amd64';
vcvars = '"' + path.join(tools, 'VC', 'vcvarsall.bat') + '" ' + arg;
} else {
throw new Error(`Arch: '${arch}' is not supported on windows`);
}

let lines;
try {
lines = gyp.bindings.execSync(`${vcvars} & set`, { env: {} }).toString()
.split(/\r\n/g);
} catch (e) {
gyp.bindings.error(e.message);
return;
}

const envBlock = formatEnvBlock(lines);
const envFile = 'environment.' + arch;

fs.writeFileSync(path.join(outDir, envFile),
envBlock);
win.genEnvironment = function genEnvironment(outDir, target_arch) {
const env = gyp.bindings.win.resolveDevEnvironment(target_arch);
const envBlock = Object.keys(env)
.map(key => `${key}=${env[key]}`)
.join('\0');

const envFile = win.getEnvFileName(target_arch);
fs.writeFileSync(path.join(outDir, envFile), envBlock);
return envFile;
};
3 changes: 3 additions & 0 deletions test/gyppies-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const path = require('path');
const rimraf = require('rimraf');
const ninja = require('ninja.js');
const spawnSync = require('child_process').spawnSync;
const debug = require('debug')('gyp.js');

const rootDir = path.join(__dirname, '..');
const gyp = path.join(rootDir, 'bin', 'gyp');
Expand Down Expand Up @@ -35,9 +36,11 @@ function build(name) {
if (process.env.running_under_istanbul)
argv = istanbulArgs.concat(argv);

debug({execPath: process.execPath, argv, spawnOpts});
let p = spawnSync(process.execPath, argv, spawnOpts);
if (p.status !== 0 && p.stdout)
console.error(p.stdout.toString());
debug(p.stdout.toString());
if (p.error)
throw p.error;
assert.equal(p.status, 0, `cd ${name} && gyp failed`);
Expand Down
4 changes: 2 additions & 2 deletions test/platform-win-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('gyp.platform.win', () => {

it('should preserve quotes', () => {
assert.deepEqual(
win.adjustLibraries([ '"some path/lib1"', '-l"lib2"',
win.adjustLibraries([ '"some path/lib1"', '-l"lib2"',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My IDE keeps eating this trailing white space...
Let it DIE ;)

'-l"lib3.lib"', '"lib4.lib"' ]),
[ '"some path/lib1.lib"', '"lib2.lib"', '"lib3.lib"', '"lib4.lib"' ]);
});
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('gyp.platform.win', () => {
'/MAPINFO:EXPORTS',
'/XXX', '/YYY',
'/SUBSYSTEM:WINDOWS,7',
'/STACK100,200',
'/STACK:100,200',
'/TSAWARE:NO',
'/INCREMENTAL',
'/BASE:1000',
Expand Down