5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Ankit Jain (jankit@novell.com)
8 // (C) 2006 Marek Sieradzki
9 // Copyright 2009 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections.Generic;
35 using System.Reflection;
36 using System.Security;
37 using Microsoft.Build.Framework;
38 using Microsoft.Build.Utilities;
40 namespace Microsoft.Build.Tasks {
41 internal class AssemblyResolver {
43 // name -> (version -> assemblypath)
44 Dictionary<string, TargetFrameworkAssemblies> target_framework_cache;
45 Dictionary<string, Dictionary<Version, string>> gac;
46 TaskLoggingHelper log;
49 public AssemblyResolver ()
51 gac = new Dictionary<string, Dictionary<Version, string>> ();
52 target_framework_cache = new Dictionary <string, TargetFrameworkAssemblies> ();
54 GatherGacAssemblies ();
57 public StringWriter SearchLogger {
61 public void ResetSearchLogger ()
63 sw = new StringWriter ();
68 // NOTE: code from mcs/tools/gacutil/driver.cs
69 PropertyInfo gac = typeof (System.Environment).GetProperty ("GacPath", BindingFlags.Static | BindingFlags.NonPublic);
74 MethodInfo get_gac = gac.GetGetMethod (true);
75 return (string) get_gac.Invoke (null, null);
78 void GatherGacAssemblies ()
80 string gac_path = GetGacPath ();
82 throw new InvalidOperationException ("XBuild must be run on Mono runtime");
83 if (!Directory.Exists (gac_path))
84 return; // in case mono isn't "installed".
87 DirectoryInfo version_info, assembly_info;
89 foreach (string assembly_name in Directory.GetDirectories (gac_path)) {
90 assembly_info = new DirectoryInfo (assembly_name);
91 foreach (string version_token in Directory.GetDirectories (assembly_name)) {
92 foreach (string file in Directory.GetFiles (version_token, "*.dll")) {
93 version_info = new DirectoryInfo (version_token);
94 version = new Version (version_info.Name.Split (
95 new char [] {'_'}, StringSplitOptions.RemoveEmptyEntries) [0]);
97 Dictionary<Version, string> assembliesByVersion = new Dictionary <Version, string> ();
98 if (!gac.TryGetValue (assembly_info.Name, out assembliesByVersion)) {
99 assembliesByVersion = new Dictionary <Version, string> ();
100 gac.Add (assembly_info.Name, assembliesByVersion);
104 if (assembliesByVersion.TryGetValue (version, out found_file) &&
105 File.GetLastWriteTime (file) <= File.GetLastWriteTime (found_file))
106 // Duplicate found, take the newer file
109 assembliesByVersion [version] = file;
115 public ResolvedReference FindInTargetFramework (ITaskItem reference, string framework_dir, bool specific_version)
117 AssemblyName key_aname = new AssemblyName (reference.ItemSpec);
118 TargetFrameworkAssemblies gac_asm;
119 if (!target_framework_cache.TryGetValue (framework_dir, out gac_asm)) {
121 gac_asm = target_framework_cache [framework_dir] = PopulateTargetFrameworkAssemblies (framework_dir);
124 KeyValuePair<AssemblyName, string> pair;
125 if (gac_asm.NameToAssemblyNameCache.TryGetValue (key_aname.Name, out pair)) {
126 if (AssemblyNamesCompatible (key_aname, pair.Key, specific_version)) {
127 // gac and tgt frmwk refs are not copied private
128 return GetResolvedReference (reference, pair.Value, pair.Key, false,
129 SearchPath.TargetFrameworkDirectory);
132 SearchLogger.WriteLine ("Considered target framework dir {0}, assembly name '{1}' did not " +
133 "match the expected '{2}' (SpecificVersion={3})",
134 framework_dir, pair.Key, key_aname, specific_version);
136 SearchLogger.WriteLine ("Considered target framework dir {0}, assembly named '{1}' not found.",
137 framework_dir, key_aname.Name);
142 public ResolvedReference FindInDirectory (ITaskItem reference, string directory, string [] file_extensions)
144 if (reference.ItemSpec.IndexOf (',') < 0) {
146 string path = Path.Combine (directory, reference.ItemSpec);
147 AssemblyName aname = GetAssemblyNameFromFile (path);
149 return GetResolvedReference (reference, path, aname, true, SearchPath.Directory);
151 foreach (string extn in file_extensions) {
152 string path_with_extn = path + extn;
153 aname = GetAssemblyNameFromFile (path_with_extn);
155 return GetResolvedReference (reference, path_with_extn, aname, true,
156 SearchPath.Directory);
160 // Probably an assembly name
161 AssemblyName key_aname = new AssemblyName (reference.ItemSpec);
162 foreach (string extn in file_extensions) {
163 foreach (string file in Directory.GetFiles (directory, "*" + extn)) {
164 AssemblyName found_aname = GetAssemblyNameFromFile (file);
165 if (found_aname == null)
166 // error already logged
169 //FIXME: Extract 'name' and look only for name.dll name.exe ?
170 if (AssemblyNamesCompatible (key_aname, found_aname, false))
171 return GetResolvedReference (reference, file, found_aname, true,
172 SearchPath.Directory);
174 SearchLogger.WriteLine ("Considered {0}, but assembly name wasn't compatible.", file);
181 TargetFrameworkAssemblies PopulateTargetFrameworkAssemblies (string directory)
183 TargetFrameworkAssemblies gac_asm = new TargetFrameworkAssemblies (directory);
184 foreach (string file in Directory.GetFiles (directory, "*.dll")) {
185 AssemblyName aname = GetAssemblyNameFromFile (file);
186 gac_asm.NameToAssemblyNameCache [aname.Name] =
187 new KeyValuePair<AssemblyName, string> (aname, file);
193 public ResolvedReference ResolveGacReference (ITaskItem reference, bool specific_version)
195 AssemblyName name = new AssemblyName (reference.ItemSpec);
196 if (!gac.ContainsKey (name.Name)) {
197 SearchLogger.WriteLine ("Considered {0}, but could not find in the GAC.",
202 if (name.Version != null) {
204 if (gac [name.Name].TryGetValue (name.Version, out ret))
205 return GetResolvedReference (reference, ret, name, false, SearchPath.Gac);
208 if (specific_version) {
209 SearchLogger.WriteLine ("Considered '{0}', but an assembly with the specific version not found.",
215 Version [] versions = new Version [gac [name.Name].Keys.Count];
216 gac [name.Name].Keys.CopyTo (versions, 0);
217 Array.Sort (versions, (IComparer <Version>) null);
218 Version highest = versions [versions.Length - 1];
219 //FIXME: the aname being used here isn't correct, its version should
220 // actually match "highest"
221 return GetResolvedReference (reference, gac [name.Name] [highest], name, false, SearchPath.Gac);
224 public ResolvedReference ResolveHintPathReference (ITaskItem reference, bool specific_version)
226 AssemblyName name = new AssemblyName (reference.ItemSpec);
227 ResolvedReference resolved = null;
229 string hintpath = reference.GetMetadata ("HintPath");
230 if (String.IsNullOrEmpty (hintpath)) {
231 SearchLogger.WriteLine ("HintPath attribute not found");
235 if (!File.Exists (hintpath)) {
236 log.LogMessage (MessageImportance.Low, "HintPath {0} does not exist.", hintpath);
237 SearchLogger.WriteLine ("Considered {0}, but it does not exist.", hintpath);
241 AssemblyName found = GetAssemblyNameFromFile (hintpath);
243 log.LogMessage (MessageImportance.Low, "File at HintPath {0}, is either an invalid assembly or the file does not exist.", hintpath);
247 if (AssemblyNamesCompatible (name, found, specific_version)) {
248 resolved = GetResolvedReference (reference, hintpath, found, true, SearchPath.HintPath);
250 SearchLogger.WriteLine ("Considered {0}, but assembly name '{1}' did not match the " +
251 "expected '{2}' (SpecificVersion={3})", hintpath, found, name, specific_version);
252 log.LogMessage (MessageImportance.Low, "Assembly names are not compatible.");
258 public AssemblyName GetAssemblyNameFromFile (string filename)
260 AssemblyName aname = null;
261 filename = Path.GetFullPath (filename);
263 aname = AssemblyName.GetAssemblyName (filename);
264 } catch (FileNotFoundException) {
265 SearchLogger.WriteLine ("Considered '{0}' as a file, but the file does not exist",
267 } catch (BadImageFormatException) {
268 SearchLogger.WriteLine ("Considered '{0}' as a file, but it is an invalid assembly",
275 internal static bool AssemblyNamesCompatible (AssemblyName a, AssemblyName b, bool specificVersion)
277 if (a.Name != b.Name)
280 if (a.CultureInfo != null && !a.CultureInfo.Equals (b.CultureInfo))
283 if (specificVersion && a.Version != null && a.Version != b.Version)
286 byte [] a_bytes = a.GetPublicKeyToken ();
287 byte [] b_bytes = b.GetPublicKeyToken ();
289 if (specificVersion) {
290 bool a_is_empty = (a_bytes == null || a_bytes.Length == 0);
291 bool b_is_empty = (b_bytes == null || b_bytes.Length == 0);
293 if (a_is_empty && b_is_empty)
296 if (a_is_empty || b_is_empty)
299 for (int i = 0; i < a_bytes.Length; i++)
300 if (a_bytes [i] != b_bytes [i])
307 public bool IsStrongNamed (AssemblyName name)
309 return (name.Version != null &&
310 name.GetPublicKeyToken () != null &&
311 name.GetPublicKeyToken ().Length != 0);
314 // FIXME: to get default values of CopyLocal, compare with TargetFrameworkDirectories
316 // If metadata 'Private' is present then use that or use @default_value
317 // as the value for CopyLocal
318 internal ResolvedReference GetResolvedReference (ITaskItem reference, string filename,
319 AssemblyName aname, bool default_value, SearchPath search_path)
321 string pvt = reference.GetMetadata ("Private");
323 bool copy_local = default_value;
324 if (!String.IsNullOrEmpty (pvt))
325 //FIXME: log a warning for invalid value
326 Boolean.TryParse (pvt, out copy_local);
328 return new ResolvedReference (filename, aname, copy_local, search_path);
331 public TaskLoggingHelper Log {
336 class TargetFrameworkAssemblies {
339 // assembly (simple) name -> (AssemblyName, file path)
340 public Dictionary <string, KeyValuePair<AssemblyName, string>> NameToAssemblyNameCache;
342 public TargetFrameworkAssemblies (string path)
345 NameToAssemblyNameCache = new Dictionary<string, KeyValuePair<AssemblyName, string>> ();
352 TargetFrameworkDirectory,