Skip to content

Commit

Permalink
win: refactor out vs2017 path discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
refack committed Feb 15, 2017
1 parent e267594 commit e9dee6e
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 274 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ node_modules/
npm-debug.log
out/
coverage/
tools/*.exe
5 changes: 5 additions & 0 deletions lib/gyp/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const fs = require('fs');
const execSync = require('child_process').execSync;
const mkdirpSync = require('mkdirp').sync;
const getVS2017Path = require('get-vs2017-path').getVS2017Path;

exports.path = {
dirname: path.dirname,
Expand Down Expand Up @@ -42,3 +43,7 @@ exports.log = function log(message) {
exports.error = function error(message) {
process.stderr.write(message + '\n');
};

exports.getVS2017Path = function (hostBits, target_arch) {
return getVS2017Path(hostBits, target_arch, exports);
};
4 changes: 2 additions & 2 deletions lib/gyp/generator/ninja/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,15 +693,15 @@ NinjaMain.prototype.rulesAndTargets = function rulesAndTargets() {
const ninjaList = this.targetList.map((target, index) => {
let targetDict = this.targetDicts[target].configurations[this.config];
const ninja = new Ninja({
index,
index: index,
outDir: this.outDir,
configDir: this.configDir,
topDir: this.topDir,
target,
targetDict,
ninjas,
config: this.config,
actionNames,
actionNames: actionNames,
targetArch
});
ninjas[target] = ninja;
Expand Down
83 changes: 1 addition & 82 deletions lib/gyp/platform/win.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const gyp = require('../../gyp');
const fs = gyp.bindings.fs;
const path = gyp.bindings.path;
const process = gyp.bindings.process;
const execSync = gyp.bindings.execSync;

const win = exports;

Expand Down Expand Up @@ -392,86 +391,6 @@ win.getOSBits = function getOSBits() {
return 32;
};

function tryVS7_powershell() {
try {
const cs = path.join(__dirname, '..', '..', '..', 'tools', 'VSConfig.cs');
const cmd = `powershell -Command \
"&{ Add-Type -Path ${cs} ; [VisualStudioSetup]::Main()}"`;
const vsSetupRaw = execSync(cmd).toString();
if (!vsSetupRaw) return;
const vsSetup = vsSetupRaw.split(/[\r|\n]+/g).reduce((s, l) => {
const lParts = l.split(': ');
if (lParts.length > 1) s[lParts[0]] = lParts[1];
return s;
}, {});
return vsSetup.InstallationPath;
} catch (e) {
gyp.bindings.log('Couldn\'t find VS7 with powershell' , e.message);
}
}

function tryVS7_CSC() {
const VREG = /.*v(\d+\.\d+).*/;
const dirCMD = 'dir /b /s %windir%\\Microsoft.NET\\Framework\\csc.exe';
try {
const files = execSync(dirCMD)
.toString()
.trim()
.split(/[\r|\n]+/g)
.map(f => [Number(f.replace(VREG, '$1')), f]);
const maxVer = Math.max.apply(Math, files.map(v => v[0]));
const cscPath = files.find(v => v[0] === maxVer)[1];
const toolsPath = path.join(__dirname, '..', '..', '..', 'tools');
const csPath = path.join(toolsPath, 'VSConfig.cs');
const exePath = path.join(toolsPath, 'VSConfig.exe');
execSync(`${cscPath} /out:${exePath} ${csPath}`);
const vsSetupRaw = execSync(exePath).toString();
const vsSetup = vsSetupRaw.split(/[\r|\n]/g).reduce((s, l) => {
const lParts = l.split(': ');
if (lParts.length > 1) s[lParts[0]] = lParts[1];
return s;
}, {});
return vsSetup.InstallationPath;
} catch (e) {
gyp.bindings.log('Couldn\'t find VS7 with a compiled exe', e.message);
}
}

function tryVS7_registry() {
const magicKey = String.raw`HKLM\Software\Microsoft\VisualStudio\SxS\VS7`;
const magicQuery = `reg query ${magicKey} /reg:32`;
const qRet = execSync(magicQuery).toString().trim();
if (qRet.includes('ERROR')) {
gyp.bindings.log('Couldn\'t find VS7 in registry:(');
return;
}
const values = qRet.split(/[\r|\n]+/g).slice(1);
const ret = values.map(v => {
const parts = v.trim().replace(/\s+/g, ' ').split(' ');
return [Number(parts[0]), parts[2]];
});
if (!ret.length) {
gyp.bindings.log('Couldn\'t find VS7 in registry');
return;
}
const maxVer = Math.max.apply(Math, ret.map(v => v[0]));
return ret.find(v => v[0] === maxVer)[1];
}

win._forTesting = {tryVS7_powershell, tryVS7_CSC, tryVS7_registry};

function tryVS7(hostBits, target_arch) {
const btPath = tryVS7_powershell() || tryVS7_CSC() || tryVS7_registry();
if (!btPath) {
gyp.bindings.log('Couldn\'t find VS7 :(');
return;
}
const VsDevCmd = path.join(btPath, 'Common7', 'Tools', 'VsDevCmd.bat');
const argArch = target_arch === 'x64' ? 'amd64' : 'x86';
const argHost = hostBits === 64 ? 'amd64' : 'x86';
return `${VsDevCmd} -arch=${argArch} -host_arch=${argHost} -no_logo`;
}

function findOldVcVarsFile(hostBits, target_arch) {
// NOTE: Largely inspired by MSVSVersion.py
const env = process.env;
Expand Down Expand Up @@ -524,7 +443,7 @@ function findOldVcVarsFile(hostBits, target_arch) {
win.resolveDevEnvironment = function resolveDevEnvironment(target_arch) {
const hostBits = win.getOSBits();

const vcEnvCmd = tryVS7(hostBits, target_arch) ||
const vcEnvCmd = gyp.bindings.getVS2017Path(hostBits, target_arch) ||
findOldVcVarsFile(hostBits, target_arch);
let lines = [];
try {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"rimraf": "^2.5.2"
},
"optionalDependencies": {
"get-vs2017-path": "^1.1.0",
"ninja.js": "^1.1.0"
},
"dependencies": {
Expand Down
17 changes: 0 additions & 17 deletions test/platform-win-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 +228,6 @@ describe('gyp.platform.win', () => {
if (process.platform === 'win32') {
describe('genEnvironment', function () {
this.timeout(20000);

it('try with powershell', () => {
const path = win._forTesting.tryVS7_powershell();
assert(path.includes('BuildTools'));
});

it('try with csc', () => {
const path = win._forTesting.tryVS7_CSC();
assert(path.includes('BuildTools'));
});

it('try with registry', () => {
const path = win._forTesting.tryVS7_registry();
assert(path.includes('BuildTools'));

});

it('resolve for x64', () => {
const env = win.resolveDevEnvironment('x64');
assert(env, 'didn\'t get ENVIRONMENT :(');
Expand Down
172 changes: 0 additions & 172 deletions tools/VSConfig.cs

This file was deleted.

0 comments on commit e9dee6e

Please sign in to comment.