1 #include <msp/fs/stat.h>
2 #include <msp/io/print.h>
3 #include <msp/strings/utils.h>
5 #include "csourcefile.h"
7 #include "sharedlibrary.h"
8 #include "staticlibrary.h"
9 #include "virtualfilesystem.h"
16 void update_hash(string &hash, const string &value)
18 for(unsigned i=0; i<value.size(); ++i)
19 hash[i%hash.size()] ^= value[i];
25 VirtualFileSystem::VirtualFileSystem(Builder &b):
30 FileTarget *VirtualFileSystem::get_target(const FS::Path &p) const
32 TargetMap::const_iterator i = targets.find(p.str());
34 return static_cast<FileTarget *>(i->second);
38 void VirtualFileSystem::register_path(const FS::Path &path, FileTarget *t)
40 targets.insert(TargetMap::value_type(path.str(), t));
43 FileTarget *VirtualFileSystem::find_header(const string &name, const SearchPath &path)
46 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
47 update_hash(hash, *i);
49 string id = hash+name;
50 TargetMap::iterator i = include_cache.find(id);
51 if(i!=include_cache.end())
54 if(builder.get_verbose()>=5)
55 IO::print("Looking for header %s with path %s\n", name, join(path.begin(), path.end()));
58 const Architecture &arch = builder.get_current_arch();
60 syspath.push_back("/usr/include");
62 syspath.push_back("/usr/"+arch.get_cross_prefix()+"/include");
64 syspath.push_back((FS::Path("/usr/include/c++/")/cxx_ver).str());
67 for(SearchPath::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
68 tgt = get_header(FS::Path(*j)/name);
69 for(SearchPath::const_iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
70 tgt = get_header(FS::Path(*j)/name);
72 include_cache.insert(TargetMap::value_type(id, tgt));
77 FileTarget *VirtualFileSystem::find_library(const string &lib, const list<string> &path, LibMode mode)
80 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
81 update_hash(hash, *i);
83 string id = hash+string(1, mode)+lib;
84 TargetMap::iterator i = library_cache.find(id);
85 if(i!=library_cache.end())
89 const Architecture &arch = builder.get_current_arch();
92 syspath.push_back("/lib");
93 syspath.push_back("/usr/lib");
94 if(arch.match_name("pc-32-linux"))
95 syspath.push_back("/usr/lib/i386-linux-gnu");
96 else if(arch.match_name("pc-64-linux"))
97 syspath.push_back("/usr/lib/x86_64-linux-gnu");
100 syspath.push_back("/usr/"+arch.get_cross_prefix()+"/lib");
102 if(builder.get_verbose()>=5)
103 IO::print("Looking for library %s with path %s\n", lib, join(path.begin(), path.end()));
106 for(StringList::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
107 tgt = get_library(lib, *j, mode);
108 for(StringList::iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
109 tgt = get_library(lib, *j, mode);
111 library_cache.insert(TargetMap::value_type(id, tgt));
116 FileTarget *VirtualFileSystem::get_header(const FS::Path &fn)
118 FileTarget *tgt = get_target(fn);
124 tgt = new CSourceFile(builder, fn);
130 FileTarget *VirtualFileSystem::get_library(const string &lib, const FS::Path &path, LibMode mode)
132 const Architecture &arch = builder.get_current_arch();
134 /* Try dynamic libraries only if library mode permits it */
137 FS::Path fn = try_patterns(path, arch.get_shared_library_patterns(), lib);
140 FileTarget *tgt = get_target(fn);
142 return new SharedLibrary(builder, fn);
143 else if(mode==DYNAMIC || !tgt->get_package())
148 /* Static libraries are always considered, since sometimes shared versions
149 may not be available */
150 FS::Path fn = try_patterns(path, arch.get_static_library_patterns(), lib);
153 if(FileTarget *tgt = get_target(fn))
156 return new StaticLibrary(builder, fn);
162 FS::Path VirtualFileSystem::try_patterns(const FS::Path &dir, const list<Pattern> &patterns, const string &base)
164 for(list<Pattern>::const_iterator i=patterns.begin(); i!=patterns.end(); ++i)
166 FS::Path full = dir/i->apply(base);
167 if(get_target(full) || FS::is_reg(full))