54433950e7fdc3d58b1ce1ec0aae44a7014ada8a
[mono.git] / mcs / class / System / System / MonoToolsLocator.cs
1 #if !MOBILE
2
3 using System.Diagnostics;
4 using System.IO;
5 using System.Reflection;
6 using System.Runtime.InteropServices;
7 using System.Text;
8
9 namespace System {
10
11         static class MonoToolsLocator
12         {
13                 public static readonly string Mono;
14                 public static readonly string CSharpCompiler;
15                 public static readonly string VBCompiler;
16                 public static readonly string AssemblyLinker;
17
18                 // TODO: Should be lazy
19                 static MonoToolsLocator ()
20                 {
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));
24
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"))
31                                         Mono = processExe;
32
33                                 if (!File.Exists (Mono))
34                                         Mono = Path.Combine (
35                                                 Path.GetDirectoryName (
36                                                         Path.GetDirectoryName (GacPath)),
37                                                 "bin\\mono.exe");
38
39                                 if (!File.Exists (Mono))
40                                         Mono = Path.Combine (
41                                                 Path.GetDirectoryName (
42                                                         Path.GetDirectoryName (
43                                                                 Path.GetDirectoryName (GacPath))),
44                                                 "mono\\mini\\mono.exe");
45
46                                 //if (!File.Exists (Mono))
47                                 //      throw new FileNotFoundException ("Windows mono path not found: " + Mono);
48
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");
52
53                                 //if (!File.Exists (CSharpCompiler))
54                                 //      throw new FileNotFoundException ("C# compiler not found at " + CSharpCompiler);
55
56                                 VBCompiler = Path.Combine (GacPath,  "4.5\\vbnc.exe");
57                                 AssemblyLinker = Path.Combine (GacPath, "4.5\\al.exe");
58
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);
63                                 }
64                         } else {
65                                 Mono = Path.Combine (GacPath, "bin", "mono");
66                                 if (!File.Exists (Mono))
67                                         Mono = "mono";
68
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";
73
74                                 VBCompiler = Path.GetFullPath (Path.Combine (mscorlibPath, "..", "..", "..", "..", "bin", "vbnc"));
75                                 if (!File.Exists (VBCompiler))
76                                         VBCompiler = "vbnc";
77
78                                 AssemblyLinker = Path.GetFullPath (Path.Combine (mscorlibPath, "..", "..", "..", "..", "bin", "al"));
79                                 if (!File.Exists (AssemblyLinker))
80                                         AssemblyLinker = "al";
81                         }
82                 }
83
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).
87                 //
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);
92
93         }
94 }
95
96 #endif