[xbuild] ResolveAssemblyReference - add missing api.
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / LibraryPcFileCache.cs
1 // 
2 // PcFileCacheAssembly.cs
3 //  
4 // Author:
5 //       Lluis Sanchez Gual <lluis@novell.com>
6 // 
7 // Copyright (c) 2009 Novell, Inc (http://www.novell.com)
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 using System;
28 using System.Text;
29 using System.Xml;
30 using System.IO;
31 using System.Collections.Generic;
32
33 // IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT
34 // This code is shared with xbuild, which has to build with .NET 2.0,
35 // so no c# 3.0 syntax is allowed here.
36
37 namespace Mono.PkgConfig
38 {
39         internal class LibraryPcFileCache: PcFileCache<LibraryPackageInfo>
40         {
41                 Dictionary<string, PackageAssemblyInfo> assemblyLocations;
42                 
43                 public LibraryPcFileCache (IPcFileCacheContext<LibraryPackageInfo> ctx): base (ctx)
44                 {
45                 }
46                 
47                 protected override string CacheDirectory {
48                         get {
49                                 string path = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
50                                 path = Path.Combine (path, "xbuild");
51                                 return path;
52                         }
53                 }
54                 
55                 // Returns the location of an assembly, given the full name
56                 public PackageAssemblyInfo GetAssemblyLocation (string fullName)
57                 {
58                         return GetAssemblyLocation (fullName, null);
59                 }
60                 
61                 public PackageAssemblyInfo GetAssemblyLocation (string fullName, IEnumerable<string> searchPaths)
62                 {
63                         lock (SyncRoot) {
64                                 if (assemblyLocations == null) {
65                                         // Populate on demand
66                                         assemblyLocations = new Dictionary<string, PackageAssemblyInfo> ();
67                                         foreach (LibraryPackageInfo info in GetPackages (searchPaths)) {
68                                                 if (info.IsValidPackage) {
69                                                         foreach (PackageAssemblyInfo asm in info.Assemblies)
70                                                                 assemblyLocations [NormalizeAsmName (asm.FullName)] = asm;
71                                                 }
72                                         }
73                                 }
74                         }
75                         // This collection is read-only once built, so there is no need for a lock
76                         PackageAssemblyInfo pasm;
77                         assemblyLocations.TryGetValue (NormalizeAsmName (fullName), out pasm);
78                         return pasm;
79                 }
80                 
81                 public IEnumerable<PackageAssemblyInfo> ResolveAssemblyName (string name)
82                 {
83                         return ResolveAssemblyName (name, null);
84                 }
85                 
86                 public IEnumerable<PackageAssemblyInfo> ResolveAssemblyName (string name, IEnumerable<string> searchPaths)
87                 {
88                         foreach (LibraryPackageInfo pinfo in GetPackages (searchPaths)) {
89                                 if (pinfo.IsValidPackage) {
90                                         foreach (PackageAssemblyInfo asm in pinfo.Assemblies) {
91                                                 if (asm.Name == name)
92                                                         yield return asm;
93                                         }
94                                 }
95                         }
96                 }
97                 
98                 protected override void WritePackageContent (XmlTextWriter tw, string file, LibraryPackageInfo pinfo)
99                 {
100                         foreach (PackageAssemblyInfo asm in pinfo.Assemblies) {
101                                 tw.WriteStartElement ("Assembly");
102                                 tw.WriteAttributeString ("name", asm.Name);
103                                 tw.WriteAttributeString ("version", asm.Version);
104                                 tw.WriteAttributeString ("culture", asm.Culture);
105                                 tw.WriteAttributeString ("publicKeyToken", asm.PublicKeyToken);
106                                 tw.WriteAttributeString ("file", asm.File);
107                                 tw.WriteEndElement (); // Assembly
108                         }
109                 }
110                 
111                 protected override void ReadPackageContent (XmlReader tr, LibraryPackageInfo pinfo)
112                 {
113                         while (tr.NodeType == XmlNodeType.Element) {
114                                 PackageAssemblyInfo asm = new PackageAssemblyInfo ();
115                                 asm.Name = tr.GetAttribute ("name");
116                                 asm.Version = tr.GetAttribute ("version");
117                                 asm.Culture = tr.GetAttribute ("culture");
118                                 asm.PublicKeyToken = tr.GetAttribute ("publicKeyToken");
119                                 asm.File = tr.GetAttribute ("file");
120                                 if (pinfo.Assemblies == null)
121                                         pinfo.Assemblies = new List<PackageAssemblyInfo> ();
122                                 asm.ParentPackage = pinfo;
123                                 pinfo.Assemblies.Add (asm);
124                                 tr.Read ();
125                                 tr.MoveToContent ();
126                         }
127                 }
128                 
129                 protected override void ParsePackageInfo (PcFile file, LibraryPackageInfo pinfo)
130                 {
131                         List<string> fullassemblies = null;
132                         bool gacPackageSet = false;
133                         
134                         if (file.Libs != null && file.Libs.IndexOf (".dll") != -1) {
135                                 if (file.Libs.IndexOf ("-lib:") != -1 || file.Libs.IndexOf ("/lib:") != -1) {
136                                         fullassemblies = GetAssembliesWithLibInfo (file.Libs);
137                                 } else {
138                                         fullassemblies = GetAssembliesWithoutLibInfo (file.Libs);
139                                 }
140                         }
141                         
142                         string value = file.GetVariable ("Libraries");
143                         if (!string.IsNullOrEmpty (value))
144                                 fullassemblies = GetAssembliesFromLibrariesVar (value);
145                         
146                         value = file.GetVariable ("GacPackage");
147                         if (value != null) {
148                                 value = value.ToLower ();
149                                 pinfo.IsGacPackage = value == "yes" || value == "true";
150                                 gacPackageSet = true;
151                         }
152         
153                         if (fullassemblies == null)
154                                 return;
155                         
156                         string pcDir = Path.GetDirectoryName (file.FilePath);
157                         string monoPrefix = Path.GetDirectoryName (Path.GetDirectoryName (pcDir));
158                         monoPrefix = Path.GetFullPath (monoPrefix + Path.DirectorySeparatorChar + "lib" + Path.DirectorySeparatorChar + "mono" + Path.DirectorySeparatorChar);
159
160                         List<PackageAssemblyInfo> list = new List<PackageAssemblyInfo> ();
161                         foreach (string assembly in fullassemblies) {
162                                 string asm;
163                                 if (Path.IsPathRooted (assembly))
164                                         asm = Path.GetFullPath (assembly);
165                                 else {
166                                         if (Path.GetDirectoryName (assembly).Length == 0) {
167                                                 asm = assembly;
168                                         } else {
169                                                 asm = Path.GetFullPath (Path.Combine (pcDir, assembly));
170                                         }
171                                 }
172                                 if (File.Exists (asm)) {
173                                         PackageAssemblyInfo pi = new PackageAssemblyInfo ();
174                                         pi.File = asm;
175                                         pi.ParentPackage = pinfo;
176                                         pi.UpdateFromFile (pi.File);
177                                         list.Add (pi);
178                                         if (!gacPackageSet && !asm.StartsWith (monoPrefix) && Path.IsPathRooted (asm)) {
179                                                 // Assembly installed outside $(prefix)/lib/mono. It is most likely not a gac package.
180                                                 gacPackageSet = true;
181                                                 pinfo.IsGacPackage = false;
182                                         }
183                                 }
184                         }
185                         pinfo.Assemblies = list;
186                 }
187                 
188                 private List<string> GetAssembliesWithLibInfo (string line)
189                 {
190                         List<string> references = new List<string> ();
191                         List<string> libdirs = new List<string> ();
192                         List<string> retval = new List<string> ();
193                         foreach (string piece in line.Split (' ')) {
194                                 if (piece.ToLower ().Trim ().StartsWith ("/r:") || piece.ToLower ().Trim ().StartsWith ("-r:")) {
195                                         references.Add (piece.Substring (3).Trim ());
196                                 } else if (piece.ToLower ().Trim ().StartsWith ("/lib:") || piece.ToLower ().Trim ().StartsWith ("-lib:")) {
197                                         libdirs.Add (piece.Substring (5).Trim ());
198                                 }
199                         }
200         
201                         foreach (string refrnc in references) {
202                                 foreach (string libdir in libdirs) {
203                                         if (File.Exists (libdir + Path.DirectorySeparatorChar + refrnc)) {
204                                                 retval.Add (libdir + Path.DirectorySeparatorChar + refrnc);
205                                         }
206                                 }
207                         }
208         
209                         return retval;
210                 }
211                 
212                 List<string> GetAssembliesFromLibrariesVar (string line)
213                 {
214                         List<string> references = new List<string> ();
215                         foreach (string reference in line.Split (' ')) {
216                                 if (!string.IsNullOrEmpty (reference))
217                                         references.Add (reference);
218                         }
219                         return references;
220                 }
221         
222                 private List<string> GetAssembliesWithoutLibInfo (string line)
223                 {
224                         List<string> references = new List<string> ();
225                         foreach (string reference in line.Split (' ')) {
226                                 if (reference.ToLower ().Trim ().StartsWith ("/r:") || reference.ToLower ().Trim ().StartsWith ("-r:")) {
227                                         string final_ref = reference.Substring (3).Trim ();
228                                         references.Add (final_ref);
229                                 }
230                         }
231                         return references;
232                 }
233                 
234                 public static string NormalizeAsmName (string name)
235                 {
236                         int i = name.ToLower ().IndexOf (", publickeytoken=null");
237                         if (i != -1)
238                                 name = name.Substring (0, i).Trim ();
239                         i = name.ToLower ().IndexOf (", processorarchitecture=");
240                         if (i != -1)
241                                 name = name.Substring (0, i).Trim ();
242                         return name;
243                 }
244         }
245         
246         internal class LibraryPackageInfo: PackageInfo
247         {
248                 public bool IsGacPackage {
249                         get { return GetData ("gacPackage") != "false"; }
250                         set {
251                                 if (value)
252                                         RemoveData ("gacPackage");
253                                 else
254                                         SetData ("gacPackage", "false");
255                         }
256                 }
257                 
258                 internal List<PackageAssemblyInfo> Assemblies { get; set; }
259                 
260                 internal protected override bool IsValidPackage {
261                         get { return Assemblies != null && Assemblies.Count > 0; }
262                 }
263         }
264         
265         internal class PackageAssemblyInfo
266         {
267                 public string File { get; set; }
268                 
269                 public string Name;
270                 
271                 public string Version;
272                 
273                 public string Culture;
274                 
275                 public string PublicKeyToken;
276                 
277                 public string FullName {
278                         get {
279                                 string fn = Name + ", Version=" + Version;
280                                 if (!string.IsNullOrEmpty (Culture))
281                                         fn += ", Culture=" + Culture;
282                                 if (!string.IsNullOrEmpty (PublicKeyToken))
283                                         fn += ", PublicKeyToken=" + PublicKeyToken;
284                                 return fn;
285                         }
286                 }
287                 
288                 public LibraryPackageInfo ParentPackage { get; set; }
289                 
290                 public void UpdateFromFile (string file)
291                 {
292                         Update (System.Reflection.AssemblyName.GetAssemblyName (file));
293                 }
294                 
295                 public void Update (System.Reflection.AssemblyName aname)
296                 {
297                         Name = aname.Name;
298                         Version = aname.Version.ToString ();
299                         if (aname.CultureInfo != null) {
300                                 if (aname.CultureInfo.LCID == System.Globalization.CultureInfo.InvariantCulture.LCID)
301                                         Culture = "neutral";
302                                 else
303                                         Culture = aname.CultureInfo.Name;
304                         }
305                         string fn = aname.ToString ();
306                         string key = "publickeytoken=";
307                         int i = fn.ToLower().IndexOf (key) + key.Length;
308                         int j = fn.IndexOf (',', i);
309                         if (j == -1) j = fn.Length;
310                         PublicKeyToken = fn.Substring (i, j - i);
311                 }
312         }
313 }