Using running process to determine mono exe path on windows
authorHenric Müller <hemuller@microsoft.com>
Wed, 7 Sep 2016 12:49:10 +0000 (14:49 +0200)
committerHenric Müller <hemuller@microsoft.com>
Wed, 7 Sep 2016 12:49:10 +0000 (14:49 +0200)
Extracting mono, mcs, vbnc, al and gac path lookup to a common class.
On windows we now also check the running process to try to determine
the mono.exe.

mcs/class/System.Web/System.Web.Compilation/AppResourcesAssemblyBuilder.cs
mcs/class/System.Web/System.Web.dll.sources
mcs/class/System.Windows.Forms/System.Windows.Forms.dll.sources
mcs/class/System.Windows.Forms/System.Windows.Forms/Application.cs
mcs/class/System/Microsoft.CSharp/CSharpCodeCompiler.cs
mcs/class/System/Microsoft.VisualBasic/VBCodeCompiler.cs
mcs/class/System/System.dll.sources
mcs/class/System/System/MonoExeLocator.cs [new file with mode: 0755]

index 895440d3a6a8ec33f843523b24777261e37e3014..064fbde4322c713601865c6381a2ca9feda872e4 100644 (file)
@@ -47,8 +47,6 @@ namespace System.Web.Compilation
 {
        class AppResourcesAssemblyBuilder
        {
-               static string framework_version = "4.5";
-               static string profile_path = "net_4_x";
                CompilationSection config;
                CompilerInfo ci;
                CodeDomProvider _provider;
@@ -233,36 +231,14 @@ namespace System.Web.Compilation
                string SetAlPath (ProcessStartInfo info)
                {                       
                        if (RuntimeHelpers.RunningOnWindows) {
-                               string alPath;
-                               string monoPath;
-                               PropertyInfo gac = typeof (Environment).GetProperty ("GacPath", BindingFlags.Static|BindingFlags.NonPublic);
-                                MethodInfo get_gac = gac.GetGetMethod (true);
-                                string p = Path.GetDirectoryName ((string) get_gac.Invoke (null, null));
-                               monoPath = Path.Combine (Path.GetDirectoryName (Path.GetDirectoryName (p)), "bin\\mono.bat");
-                                if (!File.Exists (monoPath)) {
-                                        monoPath = Path.Combine (Path.GetDirectoryName (Path.GetDirectoryName (p)), "bin\\mono.exe");
-                                       if (!File.Exists (monoPath)) {
-                                               monoPath = Path.Combine (Path.GetDirectoryName (Path.GetDirectoryName (Path.GetDirectoryName (p))), "mono\\mono\\mini\\mono.exe");
-                                               if (!File.Exists (monoPath))
-                                                       throw new FileNotFoundException ("Windows mono path not found: " + monoPath);
-                                       }
-                               }
-                               alPath = Path.Combine (p, framework_version + "\\al.exe");
-                               
-                                if (!File.Exists (alPath)) {
-                                       alPath = Path.Combine (Path.GetDirectoryName (p), "lib\\" + profile_path + "\\al.exe");
-                                       if (!File.Exists (alPath))
-                                               throw new FileNotFoundException ("Windows al path not found: " + alPath);
-                               }
-
-                               info.FileName = monoPath;
-                               return alPath + " ";
+                               info.FileName = MonoExeLocator.MonoPath;
+                               return MonoExeLocator.AlPath + " ";
                        } else {
-                               info.FileName = "al";
+                               info.FileName = MonoExeLocator.AlPath;
                                return String.Empty;
                        }
                }
-               
+
                string BuildAssemblyPath (string cultureName)
                {
                        string baseDir = Path.Combine (baseAssemblyDirectory, cultureName);
index 248b80f3537526f9501de642c6f69698a20bcf75..09a2639d7355a6ccf51210dff4d46f49aac5124d 100644 (file)
@@ -2,6 +2,7 @@ Assembly/AssemblyInfo.cs
 ../../build/common/Consts.cs
 ../../build/common/Locale.cs
 ../../build/common/MonoTODOAttribute.cs
+../System/System/MonoExeLocator.cs
 ../System.Windows.Forms/System.Resources/AssemblyNamesTypeResolutionService.cs
 ../System.Windows.Forms/System.Resources/ByteArrayFromResXHandler.cs
 ../System.Windows.Forms/System.Resources/ResXNullRef.cs
index 90e91d144e12ba407ecd5e9c6ea1a357532dc3d0..c78f10bd89dde97a7d1d7d0fa0704ab550894125 100644 (file)
@@ -2,6 +2,7 @@
 ../../build/common/MonoTODOAttribute.cs
 Assembly/AssemblyInfo.cs
 Assembly/Locale.cs
+../System/System/MonoExeLocator.cs
 System.Resources/AssemblyNamesTypeResolutionService.cs
 System.Resources/ByteArrayFromResXHandler.cs
 System.Resources/ResXNullRef.cs
index e4f980e60df8a82c6a80b770f933bdea8f0f7be2..c3e384227ba610a5e83111f42cc3c10d58b5ca58 100644 (file)
@@ -560,35 +560,7 @@ namespace System.Windows.Forms
                        if (Assembly.GetEntryAssembly () == null)
                                throw new NotSupportedException ("The method 'Restart' is not supported by this application type.");
 
-                       string mono_path = null;
-
-                       //Get mono path
-                       PropertyInfo gac = typeof (Environment).GetProperty ("GacPath", BindingFlags.Static | BindingFlags.NonPublic);
-                       MethodInfo get_gac = null;
-                       if (gac != null)
-                               get_gac = gac.GetGetMethod (true);
-
-                       if (get_gac != null) {
-                               string gac_path = Path.GetDirectoryName ((string)get_gac.Invoke (null, null));
-                               string mono_prefix = Path.GetDirectoryName (Path.GetDirectoryName (gac_path));
-
-                               if (XplatUI.RunningOnUnix) {
-                                       mono_path = Path.Combine (mono_prefix, "bin/mono");
-                                       if (!File.Exists (mono_path))
-                                               mono_path = "mono";
-                               } else {
-                                       mono_path = Path.Combine (mono_prefix, "bin\\mono.bat");
-
-                                       if (!File.Exists (mono_path))
-                                               mono_path = Path.Combine (mono_prefix, "bin\\mono.exe");
-
-                                       if (!File.Exists (mono_path))
-                                               mono_path = Path.Combine (mono_prefix, "mono\\mono\\mini\\mono.exe");
-
-                                       if (!File.Exists (mono_path))
-                                               throw new FileNotFoundException (string.Format ("Windows mono path not found: '{0}'", mono_path));
-                               }
-                       }
+                       string mono_path = MonoExeLocator.MonoPath;
 
                        //Get command line arguments
                        StringBuilder argsBuilder = new StringBuilder ();
index 00424bd70f370d8cc10be4b2e2ddeec4ead96442..2b987b11482fe13041749a3a448b333a1e57d2bd 100644 (file)
@@ -48,54 +48,9 @@ namespace Mono.CSharp
        
        internal class CSharpCodeCompiler : CSharpCodeGenerator, ICodeCompiler
        {
-               static string windowsMcsPath;
-               static string windowsMonoPath;
-               static string unixMcsCommand;
-
                Mutex mcsOutMutex;
                StringCollection mcsOutput;
                
-               static CSharpCodeCompiler ()
-               {
-                       if (Path.DirectorySeparatorChar == '\\') {
-                               PropertyInfo gac = typeof (Environment).GetProperty ("GacPath", BindingFlags.Static|BindingFlags.NonPublic);
-                               MethodInfo get_gac = gac.GetGetMethod (true);
-                               string p = Path.GetDirectoryName (
-                                       (string) get_gac.Invoke (null, null));
-                               windowsMonoPath = Path.Combine (
-                                       Path.GetDirectoryName (
-                                               Path.GetDirectoryName (p)),
-                                       "bin\\mono.bat");
-                               if (!File.Exists (windowsMonoPath))
-                                       windowsMonoPath = Path.Combine (
-                                               Path.GetDirectoryName (
-                                                       Path.GetDirectoryName (p)),
-                                               "bin\\mono.exe");
-                               if (!File.Exists (windowsMonoPath))
-                                       windowsMonoPath = Path.Combine (
-                                               Path.GetDirectoryName (
-                                                       Path.GetDirectoryName (
-                                                               Path.GetDirectoryName (p))),
-                                               "mono\\mono\\mini\\mono.exe");
-                               if (!File.Exists (windowsMonoPath))
-                                       throw new FileNotFoundException ("Windows mono path not found: " + windowsMonoPath);
-
-                               windowsMcsPath = Path.Combine (p, "4.5\\mcs.exe");
-                               if (!File.Exists (windowsMcsPath))
-                                       windowsMcsPath = Path.Combine(Path.GetDirectoryName (p), "lib\\build\\mcs.exe");
-                               
-                               if (!File.Exists (windowsMcsPath))
-                                       throw new FileNotFoundException ("Windows mcs path not found: " + windowsMcsPath);
-                       } else {
-                               var mscorlibPath = new Uri (typeof (object).Assembly.CodeBase).LocalPath;
-                               var unixMcsPath = Path.GetFullPath (Path.Combine (mscorlibPath, "..", "..", "..", "..", "bin", "mcs"));
-                               if (File.Exists (unixMcsPath))
-                                       unixMcsCommand = unixMcsPath;
-                               else
-                                       unixMcsCommand = "mcs";
-                       }
-               }
-
                //
                // Constructors
                //
@@ -177,14 +132,14 @@ namespace Mono.CSharp
 
                        // FIXME: these lines had better be platform independent.
                        if (Path.DirectorySeparatorChar == '\\') {
-                               mcs.StartInfo.FileName = windowsMonoPath;
-                               mcs.StartInfo.Arguments = "\"" + windowsMcsPath + "\" " +
-                                       BuildArgs (options, fileNames, ProviderOptions);
+                               mcs.StartInfo.FileName = MonoExeLocator.MonoPath;
+                               mcs.StartInfo.Arguments = "\"" + MonoExeLocator.McsPath + "\" ";
                        } else {
-                               mcs.StartInfo.FileName=unixMcsCommand;
-                               mcs.StartInfo.Arguments=BuildArgs(options, fileNames, ProviderOptions);
+                               mcs.StartInfo.FileName = MonoExeLocator.McsPath;
                        }
 
+                       mcs.StartInfo.Arguments += BuildArgs (options, fileNames, ProviderOptions);
+
                        mcsOutput = new StringCollection ();
                        mcsOutMutex = new Mutex ();
 /*                    
index bdb08db595adb849f3cea1b849b7b8a12fb9917a..b7ff19b348a6ecb791eab533f7bf87eabd71998d 100644 (file)
@@ -48,38 +48,6 @@ namespace Microsoft.VisualBasic
 {
        internal class VBCodeCompiler : VBCodeGenerator, ICodeCompiler
        {
-               static string windowsMonoPath;
-               static string windowsvbncPath;
-               static string unixVbncCommand;
-
-               static VBCodeCompiler ()
-               {
-                       if (Path.DirectorySeparatorChar == '\\') {
-                               PropertyInfo gac = typeof (Environment).GetProperty ("GacPath", BindingFlags.Static | BindingFlags.NonPublic);
-                               MethodInfo get_gac = gac.GetGetMethod (true);
-                               string p = Path.GetDirectoryName (
-                                       (string) get_gac.Invoke (null, null));
-                               windowsMonoPath = Path.Combine (
-                                       Path.GetDirectoryName (
-                                               Path.GetDirectoryName (p)),
-                                       "bin\\mono.bat");
-                               if (!File.Exists (windowsMonoPath))
-                                       windowsMonoPath = Path.Combine (
-                                               Path.GetDirectoryName (
-                                                       Path.GetDirectoryName (p)),
-                                               "bin\\mono.exe");
-                               windowsvbncPath =
-                                       Path.Combine (p, "2.0\\vbnc.exe");
-                       } else {
-                               var mscorlibPath = new Uri (typeof (object).Assembly.CodeBase).LocalPath;
-                               var unixMcsPath = Path.GetFullPath (Path.Combine (mscorlibPath, "..", "..", "..", "..", "bin", "vbnc"));
-                               if (File.Exists (unixMcsPath))
-                                       unixVbncCommand = unixMcsPath;
-                               else
-                                       unixVbncCommand = "vbnc";
-                       }
-               }
-
                public CompilerResults CompileAssemblyFromDom (CompilerParameters options, CodeCompileUnit e)
                {
                        return CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { e });
@@ -248,10 +216,10 @@ namespace Microsoft.VisualBasic
                        string[] vbnc_output_lines;
                        // FIXME: these lines had better be platform independent.
                        if (Path.DirectorySeparatorChar == '\\') {
-                               vbnc.StartInfo.FileName = windowsMonoPath;
-                               vbnc.StartInfo.Arguments = windowsvbncPath + ' ' + BuildArgs (options, fileNames);
+                               vbnc.StartInfo.FileName = MonoExeLocator.MonoPath;
+                               vbnc.StartInfo.Arguments = MonoExeLocator.VbncPath + ' ' + BuildArgs (options, fileNames);
                        } else {
-                               vbnc.StartInfo.FileName = "vbnc";
+                               vbnc.StartInfo.FileName = MonoExeLocator.VbncPath;
                                vbnc.StartInfo.Arguments = BuildArgs (options, fileNames);
                        }
                        //Console.WriteLine (vbnc.StartInfo.Arguments);
index abb1faf58183a6b9e62820befc3bc07cf497b3aa..f98ed393bd3ee50eb31f10bb488d3c460e776835 100644 (file)
@@ -164,6 +164,7 @@ System.Diagnostics/TraceSourceInfo.cs
 System.Diagnostics/Win32EventLog.cs
 System.Diagnostics.CodeAnalysis/ExcludeFromCodeCoverageAttribute.cs
 System/Platform.cs
+System/MonoExeLocator.cs
 System.IO.Compression/CompressionLevel.cs
 System.IO.Compression/CompressionMode.cs
 System.IO.Compression/DeflateStream.cs
diff --git a/mcs/class/System/System/MonoExeLocator.cs b/mcs/class/System/System/MonoExeLocator.cs
new file mode 100755 (executable)
index 0000000..7e75ce7
--- /dev/null
@@ -0,0 +1,93 @@
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Reflection;
+using System.Runtime.InteropServices;
+
+namespace System {
+
+       internal static class MonoExeLocator {
+
+               public static string GacPath { get; private set; }
+               public static string MonoPath { get; private set; }
+               public static string McsPath { get; private set; }
+               public static string VbncPath { get; private set; }
+               public static string AlPath { get; private set; }
+
+               static MonoExeLocator () {
+
+                       PropertyInfo gac = typeof (Environment).GetProperty ("GacPath", BindingFlags.Static | BindingFlags.NonPublic);
+                       MethodInfo getGacMethod = gac.GetGetMethod (true);
+                       GacPath = Path.GetDirectoryName ((string) getGacMethod.Invoke (null, null));
+
+                       string monoPath = null;
+                       string mcsPath = null;
+                       string vbncPath = null;
+                       string alPath = null;
+
+                       if (Path.DirectorySeparatorChar == '\\') {
+                               string processExe = Process.GetCurrentProcess ().MainModule.FileName;
+                               if (processExe != null) {
+                                       string fileName = Path.GetFileName (processExe);
+                                       if (fileName.StartsWith ("mono") && fileName.EndsWith (".exe"))
+                                               monoPath = processExe;
+                               }
+
+                               if (!File.Exists (monoPath))
+                                       monoPath = Path.Combine (
+                                               Path.GetDirectoryName (
+                                                       Path.GetDirectoryName (GacPath)),
+                                               "bin\\mono.exe");
+
+                               if (!File.Exists (monoPath))
+                                       monoPath = Path.Combine (
+                                               Path.GetDirectoryName (
+                                                       Path.GetDirectoryName (
+                                                               Path.GetDirectoryName (GacPath))),
+                                               "mono\\mini\\mono.exe");
+
+                               if (!File.Exists (monoPath))
+                                       throw new FileNotFoundException ("Windows mono path not found: " + monoPath);
+
+                               mcsPath = Path.Combine (GacPath, "4.5\\mcs.exe");
+                               if (!File.Exists (mcsPath))
+                                       mcsPath = Path.Combine (Path.GetDirectoryName (GacPath), "lib\\build\\mcs.exe");
+
+                               if (!File.Exists (mcsPath))
+                                       throw new FileNotFoundException ("Windows mcs path not found: " + mcsPath);
+
+                               vbncPath = Path.Combine (GacPath,  "4.5\\vbnc.exe");
+                               vbncPath = Path.Combine (GacPath,  "4.5\\vbnc.exe");
+                               alPath = Path.Combine (GacPath, "4.5\\al.exe");
+
+                               if (!File.Exists (alPath)) {
+                                       alPath = Path.Combine (Path.GetDirectoryName (GacPath), "lib\\net_4_x\\al.exe");
+                                       if (!File.Exists (alPath))
+                                               throw new FileNotFoundException ("Windows al path not found: " + alPath);
+                               }
+                       } else {
+                               monoPath = Path.Combine (GacPath, "bin/mono");
+                               if (!File.Exists (MonoPath))
+                                       monoPath = "mono";
+
+                               var mscorlibPath = new Uri (typeof (object).Assembly.CodeBase).LocalPath;
+                               mcsPath = Path.GetFullPath( Path.Combine (mscorlibPath, "..", "..", "..", "..", "bin", "mcs"));
+                               if (!File.Exists (mcsPath))
+                                       mcsPath = "mcs";
+
+                               vbncPath = Path.Combine (Path.GetDirectoryName (mcsPath), "vbnc");
+                               if (!File.Exists (vbncPath))
+                                       vbncPath = "vbnc";
+
+                               alPath = "al";
+                       }
+
+                       McsPath = mcsPath;
+                       MonoPath = monoPath;
+                       VbncPath = vbncPath;
+                       AlPath = alPath;
+               }
+
+               
+       }
+}