2 // PcFileCacheAssembly.cs
5 // Lluis Sanchez Gual <lluis@novell.com>
7 // Copyright (c) 2009 Novell, Inc (http://www.novell.com)
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
31 using System.Collections.Generic;
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.
37 namespace Mono.PkgConfig
39 internal class LibraryPcFileCache: PcFileCache<LibraryPackageInfo>
41 Dictionary<string, PackageAssemblyInfo> assemblyLocations;
43 public LibraryPcFileCache (IPcFileCacheContext<LibraryPackageInfo> ctx): base (ctx)
47 protected override string CacheDirectory {
49 string path = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
50 path = Path.Combine (path, "xbuild");
55 // Returns the location of an assembly, given the full name
56 public PackageAssemblyInfo GetAssemblyLocation (string fullName)
58 return GetAssemblyLocation (fullName, null);
61 public PackageAssemblyInfo GetAssemblyLocation (string fullName, IEnumerable<string> searchPaths)
64 if (assemblyLocations == null) {
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;
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);
81 public IEnumerable<PackageAssemblyInfo> ResolveAssemblyName (string name)
83 return ResolveAssemblyName (name, null);
86 public IEnumerable<PackageAssemblyInfo> ResolveAssemblyName (string name, IEnumerable<string> searchPaths)
88 foreach (LibraryPackageInfo pinfo in GetPackages (searchPaths)) {
89 if (pinfo.IsValidPackage) {
90 foreach (PackageAssemblyInfo asm in pinfo.Assemblies) {
98 protected override void WritePackageContent (XmlTextWriter tw, string file, LibraryPackageInfo pinfo)
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
111 protected override void ReadPackageContent (XmlReader tr, LibraryPackageInfo pinfo)
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);
129 protected override void ParsePackageInfo (PcFile file, LibraryPackageInfo pinfo)
131 List<string> fullassemblies = null;
132 bool gacPackageSet = false;
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);
138 fullassemblies = GetAssembliesWithoutLibInfo (file.Libs);
142 string value = file.GetVariable ("Libraries");
143 if (!string.IsNullOrEmpty (value))
144 fullassemblies = GetAssembliesFromLibrariesVar (value);
146 value = file.GetVariable ("GacPackage");
148 value = value.ToLower ();
149 pinfo.IsGacPackage = value == "yes" || value == "true";
150 gacPackageSet = true;
153 if (fullassemblies == null)
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);
160 List<PackageAssemblyInfo> list = new List<PackageAssemblyInfo> ();
161 foreach (string assembly in fullassemblies) {
163 if (Path.IsPathRooted (assembly))
164 asm = Path.GetFullPath (assembly);
166 if (Path.GetDirectoryName (assembly).Length == 0) {
169 asm = Path.GetFullPath (Path.Combine (pcDir, assembly));
172 if (File.Exists (asm)) {
173 PackageAssemblyInfo pi = new PackageAssemblyInfo ();
175 pi.ParentPackage = pinfo;
176 pi.UpdateFromFile (pi.File);
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;
185 pinfo.Assemblies = list;
188 private List<string> GetAssembliesWithLibInfo (string line)
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 ());
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);
212 List<string> GetAssembliesFromLibrariesVar (string line)
214 List<string> references = new List<string> ();
215 foreach (string reference in line.Split (' ')) {
216 if (!string.IsNullOrEmpty (reference))
217 references.Add (reference);
222 private List<string> GetAssembliesWithoutLibInfo (string line)
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);
234 public static string NormalizeAsmName (string name)
236 int i = name.ToLower ().IndexOf (", publickeytoken=null");
238 name = name.Substring (0, i).Trim ();
239 i = name.ToLower ().IndexOf (", processorarchitecture=");
241 name = name.Substring (0, i).Trim ();
246 internal class LibraryPackageInfo: PackageInfo
248 public bool IsGacPackage {
249 get { return GetData ("gacPackage") != "false"; }
252 RemoveData ("gacPackage");
254 SetData ("gacPackage", "false");
258 internal List<PackageAssemblyInfo> Assemblies { get; set; }
260 internal protected override bool IsValidPackage {
261 get { return Assemblies != null && Assemblies.Count > 0; }
265 internal class PackageAssemblyInfo
267 public string File { get; set; }
271 public string Version;
273 public string Culture;
275 public string PublicKeyToken;
277 public string FullName {
279 string fn = Name + ", Version=" + Version;
280 if (!string.IsNullOrEmpty (Culture))
281 fn += ", Culture=" + Culture;
282 if (!string.IsNullOrEmpty (PublicKeyToken))
283 fn += ", PublicKeyToken=" + PublicKeyToken;
288 public LibraryPackageInfo ParentPackage { get; set; }
290 public void UpdateFromFile (string file)
292 Update (System.Reflection.AssemblyName.GetAssemblyName (file));
295 public void Update (System.Reflection.AssemblyName aname)
298 Version = aname.Version.ToString ();
299 if (aname.CultureInfo != null) {
300 if (aname.CultureInfo.LCID == System.Globalization.CultureInfo.InvariantCulture.LCID)
303 Culture = aname.CultureInfo.Name;
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);