3 using System.Diagnostics;
5 using System.Reflection;
6 using System.Runtime.InteropServices;
11 static class MonoToolsLocator
13 public static readonly string Mono;
14 public static readonly string CSharpCompiler;
15 public static readonly string VBCompiler;
16 public static readonly string AssemblyLinker;
18 // TODO: Should be lazy
19 static MonoToolsLocator ()
21 var gac = typeof (Environment).GetProperty ("GacPath", BindingFlags.Static | BindingFlags.NonPublic);
22 var getGacMethod = gac.GetGetMethod (true);
23 var GacPath = Path.GetDirectoryName ((string) getGacMethod.Invoke (null, null));
25 if (Path.DirectorySeparatorChar == '\\') {
26 StringBuilder moduleName = new StringBuilder (1024);
27 GetModuleFileName (IntPtr.Zero, moduleName, moduleName.Capacity);
28 string processExe = moduleName.ToString ();
29 string fileName = Path.GetFileName (processExe);
30 if (fileName.StartsWith ("mono") && fileName.EndsWith (".exe"))
33 if (!File.Exists (Mono))
35 Path.GetDirectoryName (
36 Path.GetDirectoryName (GacPath)),
39 if (!File.Exists (Mono))
41 Path.GetDirectoryName (
42 Path.GetDirectoryName (
43 Path.GetDirectoryName (GacPath))),
44 "mono\\mini\\mono.exe");
46 //if (!File.Exists (Mono))
47 // throw new FileNotFoundException ("Windows mono path not found: " + Mono);
49 CSharpCompiler = Path.Combine (GacPath, "4.5\\mcs.exe");
50 if (!File.Exists (CSharpCompiler))
51 CSharpCompiler = Path.Combine (Path.GetDirectoryName (GacPath), "lib\\build\\mcs.exe");
53 //if (!File.Exists (CSharpCompiler))
54 // throw new FileNotFoundException ("C# compiler not found at " + CSharpCompiler);
56 VBCompiler = Path.Combine (GacPath, "4.5\\vbnc.exe");
57 AssemblyLinker = Path.Combine (GacPath, "4.5\\al.exe");
59 if (!File.Exists (AssemblyLinker)) {
60 AssemblyLinker = Path.Combine (Path.GetDirectoryName (GacPath), "lib\\net_4_x\\al.exe");
61 // if (!File.Exists (AssemblyLinker))
62 // throw new FileNotFoundException ("Windows al path not found: " + AssemblyLinker);
65 Mono = Path.Combine (GacPath, "bin", "mono");
66 if (!File.Exists (Mono))
69 var mscorlibPath = new Uri (typeof (object).Assembly.CodeBase).LocalPath;
70 CSharpCompiler = Path.GetFullPath (Path.Combine (mscorlibPath, "..", "..", "..", "..", "bin", "mcs"));
71 if (!File.Exists (CSharpCompiler))
72 CSharpCompiler = "mcs";
74 VBCompiler = Path.GetFullPath (Path.Combine (mscorlibPath, "..", "..", "..", "..", "bin", "vbnc"));
75 if (!File.Exists (VBCompiler))
78 AssemblyLinker = Path.GetFullPath (Path.Combine (mscorlibPath, "..", "..", "..", "..", "bin", "al"));
79 if (!File.Exists (AssemblyLinker))
80 AssemblyLinker = "al";
84 // Due to an issue with shadow copying and app domains in mono, we cannot currently use
85 // Process.GetCurrentProcess ().MainModule.FileName (which would give the same result)
86 // when running in an AppDomain (eg System.Web hosts).
88 // Using native Windows API to get current process filename. This will only
89 // be called when running on Windows.
90 [DllImport ("kernel32.dll")]
91 static extern uint GetModuleFileName ([In] IntPtr hModule, [Out] StringBuilder lpFilename, [In] int nSize);