Merge pull request #3600 from henricm/fix-win-network-info-tests
[mono.git] / mcs / class / System / System / MonoExeLocator.cs
1 #if !MOBILE
2
3 using System.Diagnostics;
4 using System.IO;
5 using System.Reflection;
6 using System.Runtime.InteropServices;
7
8 namespace System {
9
10         static class MonoToolsLocator
11         {
12                 public static readonly string Mono;
13                 public static readonly string CSharpCompiler;
14                 public static readonly string VBCompiler;
15                 public static readonly string AssemblyLinker;
16
17                 // TODO: Should be lazy
18                 static MonoToolsLocator ()
19                 {
20                         var gac = typeof (Environment).GetProperty ("GacPath", BindingFlags.Static | BindingFlags.NonPublic);
21                         var getGacMethod = gac.GetGetMethod (true);
22                         var GacPath = Path.GetDirectoryName ((string) getGacMethod.Invoke (null, null));
23
24                         if (Path.DirectorySeparatorChar == '\\') {
25                                 string processExe = Process.GetCurrentProcess ().MainModule.FileName;
26                                 if (processExe != null) {
27                                         string fileName = Path.GetFileName (processExe);
28                                         if (fileName.StartsWith ("mono") && fileName.EndsWith (".exe"))
29                                                 Mono = processExe;
30                                 }
31
32                                 if (!File.Exists (Mono))
33                                         Mono = Path.Combine (
34                                                 Path.GetDirectoryName (
35                                                         Path.GetDirectoryName (GacPath)),
36                                                 "bin\\mono.exe");
37
38                                 if (!File.Exists (Mono))
39                                         Mono = Path.Combine (
40                                                 Path.GetDirectoryName (
41                                                         Path.GetDirectoryName (
42                                                                 Path.GetDirectoryName (GacPath))),
43                                                 "mono\\mini\\mono.exe");
44
45                                 //if (!File.Exists (Mono))
46                                 //      throw new FileNotFoundException ("Windows mono path not found: " + Mono);
47
48                                 CSharpCompiler = Path.Combine (GacPath, "4.5\\mcs.exe");
49                                 if (!File.Exists (CSharpCompiler))
50                                         CSharpCompiler = Path.Combine (Path.GetDirectoryName (GacPath), "lib\\build\\mcs.exe");
51
52                                 //if (!File.Exists (CSharpCompiler))
53                                 //      throw new FileNotFoundException ("C# compiler not found at " + CSharpCompiler);
54
55                                 VBCompiler = Path.Combine (GacPath,  "4.5\\vbnc.exe");
56                                 AssemblyLinker = Path.Combine (GacPath, "4.5\\al.exe");
57
58                                 if (!File.Exists (AssemblyLinker)) {
59                                         AssemblyLinker = Path.Combine (Path.GetDirectoryName (GacPath), "lib\\net_4_x\\al.exe");
60                                 //      if (!File.Exists (AssemblyLinker))
61                                 //              throw new FileNotFoundException ("Windows al path not found: " + AssemblyLinker);
62                                 }
63                         } else {
64                                 Mono = Path.Combine (GacPath, "bin", "mono");
65                                 if (!File.Exists (Mono))
66                                         Mono = "mono";
67
68                                 var mscorlibPath = new Uri (typeof (object).Assembly.CodeBase).LocalPath;
69                                 CSharpCompiler = Path.GetFullPath (Path.Combine (mscorlibPath, "..", "..", "..", "..", "bin", "mcs"));
70                                 if (!File.Exists (CSharpCompiler))
71                                         CSharpCompiler = "mcs";
72
73                                 VBCompiler = Path.GetFullPath (Path.Combine (mscorlibPath, "..", "..", "..", "..", "bin", "vbnc"));
74                                 if (!File.Exists (VBCompiler))
75                                         VBCompiler = "vbnc";
76
77                                 AssemblyLinker = Path.GetFullPath (Path.Combine (mscorlibPath, "..", "..", "..", "..", "bin", "al"));
78                                 if (!File.Exists (AssemblyLinker))
79                                         AssemblyLinker = "al";
80                         }
81                 }
82         }
83 }
84
85 #endif