Merge pull request #1079 from esdrubal/webclient_cancel
[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 using System.Xml;
31 using System.Linq;
32
33 namespace Microsoft.Build.Utilities
34 {
35         #if MICROSOFT_BUILD_DLL
36         internal
37         #else
38         public
39         #endif
40         static class ToolLocationHelper
41         {
42                 static string lib_mono_dir;
43                 static string [] mono_dir;
44                 static bool runningOnDotNet;
45
46                 static ToolLocationHelper ()
47                 {
48                         string assemblyLocation;
49                         DirectoryInfo t1, t2;
50
51                         // /usr/local/lib/mono/1.0
52                         assemblyLocation = Path.GetDirectoryName (typeof (object).Assembly.Location);
53                         t1 = new DirectoryInfo (assemblyLocation);
54
55                         // usr/local/lib/mono
56                         t2 = t1.Parent;
57
58                         lib_mono_dir = t2.FullName;
59
60 #if NET_4_0
61                         var windowsPath = Environment.GetFolderPath (Environment.SpecialFolder.Windows);
62                         runningOnDotNet = !string.IsNullOrEmpty (windowsPath) && lib_mono_dir.StartsWith (windowsPath);
63 #endif
64
65                         if (Environment.GetEnvironmentVariable ("TESTING_MONO") != null) {
66                                 mono_dir = new string [] {
67                                         Path.Combine (lib_mono_dir, "net_1_0"),
68                                         Path.Combine (lib_mono_dir, "net_2_0"),
69                                         Path.Combine (lib_mono_dir, "net_2_0"),
70                                         Path.Combine (lib_mono_dir, "net_3_5"),
71                                         // mono's 4.0 is not an actual framework directory with all tools etc
72                                         // it's simply reference assemblies. So like .NET we consider 4.5 to
73                                         // be a complete replacement for 4.0.
74                                         Path.Combine (lib_mono_dir, "net_4_5"),
75                                         Path.Combine (lib_mono_dir, "net_4_5"),
76                                         Path.Combine (lib_mono_dir, "net_4_5")
77                                 };      
78                         } else if (runningOnDotNet) {
79                                 mono_dir = new string [] {
80                                         Path.Combine (lib_mono_dir, "v1.0.3705"),
81                                         Path.Combine (lib_mono_dir, "v2.0.50727"),
82                                         Path.Combine (lib_mono_dir, "v2.0.50727"),
83                                         Path.Combine (lib_mono_dir, "v3.5"),
84                                         Path.Combine (lib_mono_dir, "v4.0.30319"),
85                                         Path.Combine (lib_mono_dir, "v4.0.30319"),
86                                         Path.Combine (lib_mono_dir, "v4.0.30319")
87                                 };
88                         } else {
89                                 mono_dir = new string [] {
90                                         Path.Combine (lib_mono_dir, "1.0"),
91                                         Path.Combine (lib_mono_dir, "2.0"),
92                                         Path.Combine (lib_mono_dir, "2.0"),
93                                         Path.Combine (lib_mono_dir, "3.5"),
94                                         // see comment above regarding 4.0/4.5
95                                         Path.Combine (lib_mono_dir, "4.5"),
96                                         Path.Combine (lib_mono_dir, "4.5"),
97                                         Path.Combine (lib_mono_dir, "4.5"),
98                                 };
99                         }
100
101                 }
102
103                 [MonoTODO]
104                 public static string GetDotNetFrameworkRootRegistryKey (TargetDotNetFrameworkVersion version)
105                 {
106                         throw new NotImplementedException ();
107                 }
108
109                 [MonoTODO]
110                 public static string GetDotNetFrameworkSdkInstallKeyValue (TargetDotNetFrameworkVersion version)
111                 {
112                         throw new NotImplementedException ();
113                 }
114
115                 [MonoTODO]
116                 public static string GetDotNetFrameworkVersionFolderPrefix (TargetDotNetFrameworkVersion version)
117                 {
118                         throw new NotImplementedException ();
119                 }
120
121                 public static string GetPathToDotNetFramework (TargetDotNetFrameworkVersion version)
122                 {
123                         return mono_dir [(int)version];
124                 }
125
126                 public static string GetPathToDotNetFrameworkFile (string fileName,
127                                                                   TargetDotNetFrameworkVersion version)
128                 {
129                         string dir = GetPathToDotNetFramework (version);
130                         string file = Path.Combine (dir, fileName);
131                         if (File.Exists (file))
132                                 return file;
133
134                         //Mono doesn't ship multiple versions of tools that are backwards/forwards compatible
135                         if (!runningOnDotNet) {
136 #if NET_3_5
137                                 //most of the 3.5 tools are in the 2.0 directory
138                                 if (version == TargetDotNetFrameworkVersion.Version35)
139                                         return GetPathToDotNetFrameworkFile (fileName, TargetDotNetFrameworkVersion.Version20);
140 #endif
141                                 //unversioned tools are in the 4.5 directory
142                                 if (version == TargetDotNetFrameworkVersion.Version20)
143                                         return GetPathToDotNetFrameworkFile (fileName, (TargetDotNetFrameworkVersion)5);
144                         }
145
146                         return null;
147                 }
148
149                 public static string GetPathToDotNetFrameworkSdk (TargetDotNetFrameworkVersion version)
150                 {
151                         return GetPathToDotNetFramework (version);
152                 }
153
154                 [MonoTODO]
155                 public static string GetPathToDotNetFrameworkSdkFile (string fileName,
156                                                                       TargetDotNetFrameworkVersion version)
157                 {
158                         throw new NotImplementedException ();
159                 }
160
161                 #if NET_4_0
162                 public static string GetPathToStandardLibraries (string targetFrameworkIdentifier,
163                                                                  string targetFrameworkVersion,
164                                                                  string targetFrameworkProfile)
165                 {
166                         return GetPathToStandardLibraries (targetFrameworkIdentifier, targetFrameworkVersion, targetFrameworkProfile, null);
167                 }
168
169                 [MonoTODO]
170                 #if XBUILD_12
171                 public
172                 #endif
173                 static string GetPathToStandardLibraries (string targetFrameworkIdentifier,
174                                                           string targetFrameworkVersion,
175                                                           string targetFrameworkProfile,
176                                                           string platformTarget)
177                 {
178                         // FIXME: support platformTarget
179                         if (platformTarget != null)
180                                 throw new NotImplementedException ("platformTarget support is not implemented");
181                         
182                         var ext = Environment.GetEnvironmentVariable ("XBUILD_FRAMEWORK_FOLDERS_PATH");
183                         var ret = ext != null ? GetPathToStandardLibrariesWith (ext, targetFrameworkIdentifier, targetFrameworkVersion, targetFrameworkProfile) : null;
184                         return ret ?? GetPathToStandardLibrariesWith (Path.GetFullPath (Path.Combine (lib_mono_dir, "xbuild-frameworks")), targetFrameworkIdentifier, targetFrameworkVersion, targetFrameworkProfile);
185                 }
186                         
187                 static string GetPathToStandardLibrariesWith (string xbuildFxDir,
188                                                               string targetFrameworkIdentifier,
189                                                               string targetFrameworkVersion,
190                                                               string targetFrameworkProfile)
191                 {
192                         var path = Path.Combine (xbuildFxDir, targetFrameworkIdentifier);
193                         if (!string.IsNullOrEmpty (targetFrameworkVersion)) {
194                                 path = Path.Combine (path, targetFrameworkVersion);
195                                 if (!string.IsNullOrEmpty (targetFrameworkProfile))
196                                         path = Path.Combine (path, "Profile", targetFrameworkProfile);
197                         }
198                         if (!Directory.Exists (path))
199                                 return null;
200                         var flist = Path.Combine (path, "RedistList", "FrameworkList.xml");
201                         if (!File.Exists (flist))
202                                 return null;
203                         var xml = XmlReader.Create (flist);
204                         xml.MoveToContent ();
205                         var targetFxDir = xml.GetAttribute ("TargetFrameworkDirectory");
206                         targetFxDir = targetFxDir != null ? Path.GetFullPath (Path.Combine (path, "dummy", targetFxDir.Replace ('\\', Path.DirectorySeparatorChar))) : null;
207                         if (Directory.Exists (targetFxDir))
208                                 return targetFxDir;
209                         // I'm not sure if this is completely valid assumption...
210                         return path;
211                 }
212                 #endif
213
214                 [MonoTODO]
215                 public static string GetPathToSystemFile (string fileName)
216                 {
217                         throw new NotImplementedException ();
218                 }
219
220                 [MonoTODO]
221                 public static string PathToSystem {
222                         get {
223                                 throw new NotImplementedException ();
224                         }
225                 }
226
227 #if XBUILD_12
228                 public static string CurrentToolsVersion {
229                         get {
230                                 return XBuildConsts.Version;
231                         }
232                 }
233
234                 public static string GetPathToBuildTools (string toolsVersion)
235                 {
236                         if (toolsVersion != "12.0")
237                                 return null;
238
239                         if (Environment.GetEnvironmentVariable ("TESTING_MONO") != null)
240                                 return Path.Combine (lib_mono_dir, "xbuild_12");
241
242                         if (runningOnDotNet) {
243                                 //see http://msdn.microsoft.com/en-us/library/vstudio/bb397428(v=vs.120).aspx
244                                 var programFiles = Environment.GetFolderPath (Environment.SpecialFolder.ProgramFilesX86);
245                                 return Path.Combine (programFiles, "MSBuild", toolsVersion, "bin");
246                         }
247
248                         return Path.Combine (lib_mono_dir, "xbuild", toolsVersion, "bin");
249                 }
250 #endif
251         }
252 }