Merge pull request #778 from cmorris98/master
[mono.git] / mcs / class / Microsoft.Build.Utilities / Microsoft.Build.Utilities / ToolLocationHelper.cs
1 // 
2 // ToolLocationHelper.cs:
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 using System;
29 using System.IO;
30
31 namespace Microsoft.Build.Utilities
32 {
33         #if MICROSOFT_BUILD_DLL
34         internal
35         #else
36         public
37         #endif
38         static class ToolLocationHelper
39         {
40                 static string lib_mono_dir;
41                 static string [] mono_dir;
42                 static bool runningOnDotNet;
43
44                 static ToolLocationHelper ()
45                 {
46                         string assemblyLocation;
47                         DirectoryInfo t1, t2;
48
49                         // /usr/local/lib/mono/1.0
50                         assemblyLocation = Path.GetDirectoryName (typeof (object).Assembly.Location);
51                         t1 = new DirectoryInfo (assemblyLocation);
52
53                         // usr/local/lib/mono
54                         t2 = t1.Parent;
55
56                         lib_mono_dir = t2.FullName;
57
58 #if NET_4_0
59                         var windowsPath = Environment.GetFolderPath (Environment.SpecialFolder.Windows);
60                         runningOnDotNet = !string.IsNullOrEmpty (windowsPath) && lib_mono_dir.StartsWith (windowsPath);
61 #endif
62
63                         if (Environment.GetEnvironmentVariable ("TESTING_MONO") != null) {
64                                 mono_dir = new string [] {
65                                         Path.Combine (lib_mono_dir, "net_1_0"),
66                                         Path.Combine (lib_mono_dir, "net_2_0"),
67                                         Path.Combine (lib_mono_dir, "net_2_0"),
68                                         Path.Combine (lib_mono_dir, "net_3_5"),
69                                         // mono's 4.0 is not an actual framework directory with all tools etc
70                                         // it's simply reference assemblies. So like .NET we consider 4.5 to
71                                         // be a complete replacement for 4.0.
72                                         Path.Combine (lib_mono_dir, "net_4_5"),
73                                         Path.Combine (lib_mono_dir, "net_4_5"),
74                                         Path.Combine (lib_mono_dir, "net_4_5")
75                                 };      
76                         } else if (runningOnDotNet) {
77                                 mono_dir = new string [] {
78                                         Path.Combine (lib_mono_dir, "v1.0.3705"),
79                                         Path.Combine (lib_mono_dir, "v2.0.50727"),
80                                         Path.Combine (lib_mono_dir, "v2.0.50727"),
81                                         Path.Combine (lib_mono_dir, "v3.5"),
82                                         Path.Combine (lib_mono_dir, "v4.0.30319"),
83                                         Path.Combine (lib_mono_dir, "v4.0.30319"),
84                                         Path.Combine (lib_mono_dir, "v4.0.30319")
85                                 };
86                         } else {
87                                 mono_dir = new string [] {
88                                         Path.Combine (lib_mono_dir, "1.0"),
89                                         Path.Combine (lib_mono_dir, "2.0"),
90                                         Path.Combine (lib_mono_dir, "2.0"),
91                                         Path.Combine (lib_mono_dir, "3.5"),
92                                         // see comment above regarding 4.0/4.5
93                                         Path.Combine (lib_mono_dir, "4.5"),
94                                         Path.Combine (lib_mono_dir, "4.5"),
95                                         Path.Combine (lib_mono_dir, "4.5"),
96                                 };
97                         }
98
99                 }
100
101                 [MonoTODO]
102                 public static string GetDotNetFrameworkRootRegistryKey (TargetDotNetFrameworkVersion version)
103                 {
104                         throw new NotImplementedException ();
105                 }
106
107                 [MonoTODO]
108                 public static string GetDotNetFrameworkSdkInstallKeyValue (TargetDotNetFrameworkVersion version)
109                 {
110                         throw new NotImplementedException ();
111                 }
112
113                 [MonoTODO]
114                 public static string GetDotNetFrameworkVersionFolderPrefix (TargetDotNetFrameworkVersion version)
115                 {
116                         throw new NotImplementedException ();
117                 }
118
119                 public static string GetPathToDotNetFramework (TargetDotNetFrameworkVersion version)
120                 {
121                         return mono_dir [(int)version];
122                 }
123
124                 public static string GetPathToDotNetFrameworkFile (string fileName,
125                                                                   TargetDotNetFrameworkVersion version)
126                 {
127                         string dir = GetPathToDotNetFramework (version);
128                         string file = Path.Combine (dir, fileName);
129                         if (File.Exists (file))
130                                 return file;
131
132                         //Mono doesn't ship multiple versions of tools that are backwards/forwards compatible
133                         if (!runningOnDotNet) {
134 #if NET_3_5
135                                 //most of the 3.5 tools are in the 2.0 directory
136                                 if (version == TargetDotNetFrameworkVersion.Version35)
137                                         return GetPathToDotNetFrameworkFile (fileName, TargetDotNetFrameworkVersion.Version20);
138 #endif
139                                 //unversioned tools are in the 4.5 directory
140                                 if (version == TargetDotNetFrameworkVersion.Version20)
141                                         return GetPathToDotNetFrameworkFile (fileName, (TargetDotNetFrameworkVersion)5);
142                         }
143
144                         return null;
145                 }
146
147                 public static string GetPathToDotNetFrameworkSdk (TargetDotNetFrameworkVersion version)
148                 {
149                         return GetPathToDotNetFramework (version);
150                 }
151
152                 [MonoTODO]
153                 public static string GetPathToDotNetFrameworkSdkFile (string fileName,
154                                                                       TargetDotNetFrameworkVersion version)
155                 {
156                         throw new NotImplementedException ();
157                 }
158
159                 [MonoTODO]
160                 public static string GetPathToSystemFile (string fileName)
161                 {
162                         throw new NotImplementedException ();
163                 }
164
165                 [MonoTODO]
166                 public static string PathToSystem {
167                         get {
168                                 throw new NotImplementedException ();
169                         }
170                 }
171
172 #if XBUILD_12
173                 public static string CurrentToolsVersion {
174                         get {
175                                 return XBuildConsts.Version;
176                         }
177                 }
178
179                 public static string GetPathToBuildTools (string toolsVersion)
180                 {
181                         if (toolsVersion != "12.0")
182                                 return null;
183
184                         if (Environment.GetEnvironmentVariable ("TESTING_MONO") != null)
185                                 return Path.Combine (lib_mono_dir, "xbuild_12");
186
187                         if (runningOnDotNet) {
188                                 //see http://msdn.microsoft.com/en-us/library/vstudio/bb397428(v=vs.120).aspx
189                                 var programFiles = Environment.GetFolderPath (Environment.SpecialFolder.ProgramFilesX86);
190                                 return Path.Combine (programFiles, "MSBuild", toolsVersion, "bin");
191                         }
192
193                         return Path.Combine (lib_mono_dir, "xbuild", toolsVersion, "bin");
194                 }
195 #endif
196         }
197 }