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 namespace Mono.PkgConfig
35 internal class LibraryPcFileCache: PcFileCache<LibraryPackageInfo>
37 Dictionary<string, PackageAssemblyInfo> assemblyLocations;
39 public LibraryPcFileCache (IPcFileCacheContext<LibraryPackageInfo> ctx): base (ctx)
43 protected override string CacheDirectory {
45 string path = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
46 path = Path.Combine (path, "xbuild");
51 // Returns the location of an assembly, given the full name
52 public PackageAssemblyInfo GetAssemblyLocation (string fullName)
54 return GetAssemblyLocation (fullName, null);
57 public PackageAssemblyInfo GetAssemblyLocation (string fullName, IEnumerable<string> searchPaths)
60 if (assemblyLocations == null) {
62 assemblyLocations = new Dictionary<string, PackageAssemblyInfo> ();
63 foreach (LibraryPackageInfo info in GetPackages (searchPaths)) {
64 if (info.IsValidPackage) {
65 foreach (PackageAssemblyInfo asm in info.Assemblies)
66 assemblyLocations [NormalizeAsmName (asm.FullName)] = asm;
71 // This collection is read-only once built, so there is no need for a lock
72 PackageAssemblyInfo pasm;
73 assemblyLocations.TryGetValue (NormalizeAsmName (fullName), out pasm);
77 public IEnumerable<PackageAssemblyInfo> ResolveAssemblyName (string name)
79 return ResolveAssemblyName (name, null);
82 public IEnumerable<PackageAssemblyInfo> ResolveAssemblyName (string name, IEnumerable<string> searchPaths)
84 foreach (LibraryPackageInfo pinfo in GetPackages (searchPaths)) {
85 if (pinfo.IsValidPackage) {
86 foreach (PackageAssemblyInfo asm in pinfo.Assemblies) {
94 protected override void WritePackageContent (XmlTextWriter tw, string file, LibraryPackageInfo pinfo)
96 foreach (PackageAssemblyInfo asm in pinfo.Assemblies) {
97 tw.WriteStartElement ("Assembly");
98 tw.WriteAttributeString ("name", asm.Name);
99 tw.WriteAttributeString ("version", asm.Version);
100 tw.WriteAttributeString ("culture", asm.Culture);
101 tw.WriteAttributeString ("publicKeyToken", asm.PublicKeyToken);
102 tw.WriteAttributeString ("file", asm.File);
103 tw.WriteEndElement (); // Assembly
107 protected override void ReadPackageContent (XmlReader tr, LibraryPackageInfo pinfo)
109 while (tr.NodeType == XmlNodeType.Element) {
110 PackageAssemblyInfo asm = new PackageAssemblyInfo ();
111 asm.Name = tr.GetAttribute ("name");
112 asm.Version = tr.GetAttribute ("version");
113 asm.Culture = tr.GetAttribute ("culture");
114 asm.PublicKeyToken = tr.GetAttribute ("publicKeyToken");
115 asm.File = tr.GetAttribute ("file");
116 if (pinfo.Assemblies == null)
117 pinfo.Assemblies = new List<PackageAssemblyInfo> ();
118 asm.ParentPackage = pinfo;
119 pinfo.Assemblies.Add (asm);
125 protected override void ParsePackageInfo (PcFile file, LibraryPackageInfo pinfo)
127 List<string> fullassemblies = null;
128 bool gacPackageSet = false;
130 if (file.Libs != null && file.Libs.IndexOf (".dll") != -1) {
131 if (file.Libs.IndexOf ("-lib:") != -1 || file.Libs.IndexOf ("/lib:") != -1) {
132 fullassemblies = GetAssembliesWithLibInfo (file.Libs);
134 fullassemblies = GetAssembliesWithoutLibInfo (file.Libs);
138 string value = file.GetVariable ("Libraries");
139 if (!string.IsNullOrEmpty (value))
140 fullassemblies = GetAssembliesFromLibrariesVar (value);
142 value = file.GetVariable ("GacPackage");
144 value = value.ToLower ();
145 pinfo.IsGacPackage = value == "yes" || value == "true";
146 gacPackageSet = true;
149 if (fullassemblies == null)
152 string pcDir = Path.GetDirectoryName (file.FilePath);
153 string monoPrefix = Path.GetDirectoryName (Path.GetDirectoryName (pcDir));
154 monoPrefix = Path.GetFullPath (monoPrefix + Path.DirectorySeparatorChar + "lib" + Path.DirectorySeparatorChar + "mono" + Path.DirectorySeparatorChar);
156 List<PackageAssemblyInfo> list = new List<PackageAssemblyInfo> ();
157 foreach (string assembly in fullassemblies) {
159 if (Path.IsPathRooted (assembly))
160 asm = Path.GetFullPath (assembly);
162 if (Path.GetDirectoryName (assembly).Length == 0) {
165 asm = Path.GetFullPath (Path.Combine (pcDir, assembly));
168 if (File.Exists (asm)) {
169 PackageAssemblyInfo pi = new PackageAssemblyInfo ();
171 pi.ParentPackage = pinfo;
172 pi.UpdateFromFile (pi.File);
174 if (!gacPackageSet && !asm.StartsWith (monoPrefix) && Path.IsPathRooted (asm)) {
175 // Assembly installed outside $(prefix)/lib/mono. It is most likely not a gac package.
176 gacPackageSet = true;
177 pinfo.IsGacPackage = false;
181 pinfo.Assemblies = list;
184 private List<string> GetAssembliesWithLibInfo (string line)
186 List<string> references = new List<string> ();
187 List<string> libdirs = new List<string> ();
188 List<string> retval = new List<string> ();
189 foreach (string piece in line.Split (' ')) {
190 if (piece.ToLower ().Trim ().StartsWith ("/r:") || piece.ToLower ().Trim ().StartsWith ("-r:")) {
191 references.Add (piece.Substring (3).Trim ());
192 } else if (piece.ToLower ().Trim ().StartsWith ("/lib:") || piece.ToLower ().Trim ().StartsWith ("-lib:")) {
193 libdirs.Add (piece.Substring (5).Trim ());
197 foreach (string refrnc in references) {
198 foreach (string libdir in libdirs) {
199 if (File.Exists (libdir + Path.DirectorySeparatorChar + refrnc)) {
200 retval.Add (libdir + Path.DirectorySeparatorChar + refrnc);
208 List<string> GetAssembliesFromLibrariesVar (string line)
210 List<string> references = new List<string> ();
211 foreach (string reference in line.Split (' ')) {
212 if (!string.IsNullOrEmpty (reference))
213 references.Add (reference);
218 private List<string> GetAssembliesWithoutLibInfo (string line)
220 List<string> references = new List<string> ();
221 foreach (string reference in line.Split (' ')) {
222 if (reference.ToLower ().Trim ().StartsWith ("/r:") || reference.ToLower ().Trim ().StartsWith ("-r:")) {
223 string final_ref = reference.Substring (3).Trim ();
224 references.Add (final_ref);
230 public static string NormalizeAsmName (string name)
232 int i = name.ToLower ().IndexOf (", publickeytoken=null");
234 name = name.Substring (0, i).Trim ();
235 i = name.ToLower ().IndexOf (", processorarchitecture=");
237 name = name.Substring (0, i).Trim ();
242 internal class LibraryPackageInfo: PackageInfo
244 public bool IsGacPackage {
245 get { return GetData ("gacPackage") != "false"; }
248 RemoveData ("gacPackage");
250 SetData ("gacPackage", "false");
254 internal List<PackageAssemblyInfo> Assemblies { get; set; }
256 internal protected override bool IsValidPackage {
257 get { return Assemblies != null && Assemblies.Count > 0; }
261 internal class PackageAssemblyInfo
263 public string File { get; set; }
267 public string Version;
269 public string Culture;
271 public string PublicKeyToken;
273 public string FullName {
275 string fn = Name + ", Version=" + Version;
276 if (!string.IsNullOrEmpty (Culture))
277 fn += ", Culture=" + Culture;
278 if (!string.IsNullOrEmpty (PublicKeyToken))
279 fn += ", PublicKeyToken=" + PublicKeyToken;
284 public LibraryPackageInfo ParentPackage { get; set; }
286 public void UpdateFromFile (string file)
288 Update (System.Reflection.AssemblyName.GetAssemblyName (file));
291 public void Update (System.Reflection.AssemblyName aname)
294 Version = aname.Version.ToString ();
295 if (aname.CultureInfo != null) {
296 if (aname.CultureInfo.LCID == System.Globalization.CultureInfo.InvariantCulture.LCID)
299 Culture = aname.CultureInfo.Name;
301 string fn = aname.ToString ();
302 string key = "publickeytoken=";
303 int i = fn.ToLower().IndexOf (key) + key.Length;
304 int j = fn.IndexOf (',', i);
305 if (j == -1) j = fn.Length;
306 PublicKeyToken = fn.Substring (i, j - i);