diff --git a/.gitignore b/.gitignore index 74289d9..a7289e1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,3 @@ node_modules/ npm-debug.log out/ coverage/ -tools/*.exe diff --git a/lib/gyp/bindings.js b/lib/gyp/bindings.js index 822e5f8..e860325 100644 --- a/lib/gyp/bindings.js +++ b/lib/gyp/bindings.js @@ -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, @@ -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); +}; diff --git a/lib/gyp/generator/ninja/index.js b/lib/gyp/generator/ninja/index.js index 35c37b9..52b5488 100644 --- a/lib/gyp/generator/ninja/index.js +++ b/lib/gyp/generator/ninja/index.js @@ -693,7 +693,7 @@ 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, @@ -701,7 +701,7 @@ NinjaMain.prototype.rulesAndTargets = function rulesAndTargets() { targetDict, ninjas, config: this.config, - actionNames, + actionNames: actionNames, targetArch }); ninjas[target] = ninja; diff --git a/lib/gyp/platform/win.js b/lib/gyp/platform/win.js index dab3e4f..912c1aa 100644 --- a/lib/gyp/platform/win.js +++ b/lib/gyp/platform/win.js @@ -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; @@ -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; @@ -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 { diff --git a/package.json b/package.json index 44915fc..15ea91f 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "rimraf": "^2.5.2" }, "optionalDependencies": { + "get-vs2017-path": "^1.1.0", "ninja.js": "^1.1.0" }, "dependencies": { diff --git a/test/platform-win-test.js b/test/platform-win-test.js index 9fe197a..bdbfc88 100644 --- a/test/platform-win-test.js +++ b/test/platform-win-test.js @@ -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 :('); diff --git a/tools/VSConfig.cs b/tools/VSConfig.cs deleted file mode 100644 index 912100b..0000000 --- a/tools/VSConfig.cs +++ /dev/null @@ -1,172 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using Microsoft.VisualStudio.Setup.Configuration; - - -namespace Microsoft.VisualStudio.Setup.Configuration -{ - [CompilerGenerated] - [Flags] - [TypeIdentifier("310100ba-5f84-4103-abe0-e8132ae862d9", "Microsoft.VisualStudio.Setup.Configuration.InstanceState")] - [ComVisible(true)] - public enum InstanceState : uint - { - None = 0, - Local = 1, - Registered = 2, - NoRebootRequired = 4, - NoErrors = 8, - Complete = 4294967295 - } - - [CompilerGenerated] - [Guid("6380BCFF-41D3-4B2E-8B2E-BF8A6810C848")] - [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - [TypeIdentifier] - [ComImport] - public interface IEnumSetupInstances - { - void Next([MarshalAs(UnmanagedType.U4)] [In] int celt, - [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.Interface)] [Out] ISetupInstance[] rgelt, - [MarshalAs(UnmanagedType.U4)] out int pceltFetched); - } - - - [CompilerGenerated] - [Guid("42843719-DB4C-46C2-8E7C-64F1816EFD5B")] - [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - [TypeIdentifier] - [ComImport] - public interface ISetupConfiguration - { - } - - [CompilerGenerated] - [Guid("26AAB78C-4A60-49D6-AF3B-3C35BC93365D")] - [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - [TypeIdentifier] - [ComImport] - public interface ISetupConfiguration2 : ISetupConfiguration - { - [SpecialName] - [MethodImpl(MethodCodeType = MethodCodeType.Runtime)] - void _VtblGap1_3(); - - [return: MarshalAs(UnmanagedType.Interface)] - IEnumSetupInstances EnumAllInstances(); - } - - - [CompilerGenerated] - [Guid("B41463C3-8866-43B5-BC33-2B0676F7F42E")] - [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - [TypeIdentifier] - [ComImport] - public interface ISetupInstance - { - [SpecialName] - [MethodImpl(MethodCodeType = MethodCodeType.Runtime)] - void _VtblGap1_4(); - - [return: MarshalAs(UnmanagedType.BStr)] - string GetInstallationVersion(); - } - - [CompilerGenerated] - [Guid("89143C9A-05AF-49B0-B717-72E218A2185C")] - [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - [TypeIdentifier] - [ComImport] - public interface ISetupInstance2 : ISetupInstance - { - [return: MarshalAs(UnmanagedType.BStr)] - string GetInstanceId(); - - [SpecialName] - [MethodImpl(MethodCodeType = MethodCodeType.Runtime)] - void _VtblGap1_2(); - - [return: MarshalAs(UnmanagedType.BStr)] - string GetInstallationPath(); - - [SpecialName] - [MethodImpl(MethodCodeType = MethodCodeType.Runtime)] - void _VtblGap2_4(); - - [return: MarshalAs(UnmanagedType.U4)] - InstanceState GetState(); - - [return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UNKNOWN)] - ISetupPackageReference[] GetPackages(); - - ISetupPackageReference GetProduct(); - } - - [CompilerGenerated] - [Guid("DA8D8A16-B2B6-4487-A2F1-594CCCCD6BF5")] - [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - [TypeIdentifier] - [ComImport] - public interface ISetupPackageReference - { - [return: MarshalAs(UnmanagedType.BStr)] - string GetId(); - - [SpecialName] - [MethodImpl(MethodCodeType = MethodCodeType.Runtime)] - void _VtblGap1_4(); - - [return: MarshalAs(UnmanagedType.BStr)] - string GetType(); - } -} - -public static class VisualStudioSetup -{ - public static int Main() - { - try - { - var query = - (ISetupConfiguration2) - Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("177F0C4A-1CD3-4DE7-A32C-71DBBB9FA36D"))); - var enumSetupInstances = query.EnumAllInstances(); - var rgelt = new ISetupInstance2[1]; - int pceltFetched; - do - { - // ISSUE: reference to a compiler-generated method - enumSetupInstances.Next(1, rgelt, out pceltFetched); - if (pceltFetched > 0) - PrintInstance(rgelt[0]); - } while (pceltFetched > 0); - return 0; - } - catch (Exception ex) - { - Console.Error.WriteLine("Error 0x{0:x8}: {1}", ex.HResult, ex.Message); - return ex.HResult; - } - } - - - private static void PrintInstance(ISetupInstance2 setupInstance2) - { - Console.WriteLine("InstallationPath: {0}", setupInstance2.GetInstallationPath()); - Console.WriteLine("Product: {0}", setupInstance2.GetProduct().GetId()); - foreach (var package in setupInstance2.GetPackages()) - { - if (package.GetType() != "Exe") continue;; - var id = package.GetId(); - if (id.IndexOf("SDK", StringComparison.Ordinal) == -1) continue; - var parts = id.Split('_'); - if (parts.Length < 2) continue; - var sdkVer = parts[1]; - char[] chars = { '1', '0', '8' }; - if (sdkVer.IndexOfAny(chars) == -1) continue; - Console.WriteLine("SDK: {0}", sdkVer); - } - Console.WriteLine(); - } -}