1 #include <msp/fs/stat.h>
2 #include <msp/io/print.h>
3 #include <msp/strings/utils.h>
7 #include "sharedlibrary.h"
8 #include "systemlibrary.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 &include, const FS::Path &from, const list<string> &path)
47 update_hash(hash, from.str());
48 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
49 update_hash(hash, *i);
51 string id = hash+include;
52 TargetMap::iterator i = include_cache.find(id);
53 if(i!=include_cache.end())
56 static string cxx_ver;
59 // XXX This needs to go elsewhere
61 argv.push_back(current_arch->get_tool("CXX"));
62 argv.push_back("--version");
63 if(RegMatch m = Regex("[0-9]\\.[0-9.]+").match(run_command(argv)))
66 while(!cxx_ver.empty() && !FS::is_dir(FS::Path("/usr/include/c++")/cxx_ver))
68 string::size_type dot = cxx_ver.rfind('.');
74 IO::print("C++ version is %s\n", cxx_ver);
80 string fn = include.substr(1);
81 if(builder.get_verbose()>=5)
82 IO::print("Looking for include %s from %s with path %s\n", fn, from, join(path.begin(), path.end()));
85 const Architecture &arch = builder.get_current_arch();
87 syspath.push_back("/usr/include");
89 syspath.push_back("/usr/"+arch.get_cross_prefix()+"/include");
91 syspath.push_back((FS::Path("/usr/include/c++/")/cxx_ver).str());
95 tgt = get_header(FS::Path(from)/fn);
96 for(list<string>::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
97 tgt = get_header(FS::Path(*j)/fn);
98 for(list<string>::const_iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
99 tgt = get_header(FS::Path(*j)/fn);
101 include_cache.insert(TargetMap::value_type(id, tgt));
106 FileTarget *VirtualFileSystem::find_library(const string &lib, const list<string> &path, LibMode mode)
109 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
110 update_hash(hash, *i);
112 string id = hash+string(1, mode)+lib;
113 TargetMap::iterator i = library_cache.find(id);
114 if(i!=library_cache.end())
118 const Architecture &arch = builder.get_current_arch();
121 syspath.push_back("/lib");
122 syspath.push_back("/usr/lib");
123 if(arch.match_name("pc-32-linux"))
124 syspath.push_back("/usr/lib/i386-linux-gnu");
125 else if(arch.match_name("pc-64-linux"))
126 syspath.push_back("/usr/lib/x86_64-linux-gnu");
129 syspath.push_back("/usr/"+arch.get_cross_prefix()+"/lib");
131 if(builder.get_verbose()>=5)
132 IO::print("Looking for library %s with path %s\n", lib, join(path.begin(), path.end()));
135 for(StringList::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
136 tgt = get_library(lib, *j, mode);
137 for(StringList::iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
138 tgt = get_library(lib, *j, mode);
140 library_cache.insert(TargetMap::value_type(id, tgt));
145 FileTarget *VirtualFileSystem::get_header(const FS::Path &fn)
147 FileTarget *tgt = get_target(fn);
153 tgt = new SystemHeader(builder, fn.str());
159 FileTarget *VirtualFileSystem::get_library(const string &lib, const FS::Path &path, LibMode mode)
161 // Populate a list of candidate filenames
162 StringList candidates;
164 const Architecture &arch = builder.get_current_arch();
167 // XXX Should probably let the Architecture populate the list
168 if(arch.get_system()=="windows")
170 candidates.push_back("lib"+lib+".dll");
171 candidates.push_back(lib+".dll");
174 candidates.push_back("lib"+lib+".so");
177 /* Static libraries are always considered, since sometimes shared versions
178 may not be available */
179 candidates.push_back("lib"+lib+".a");
180 if(arch.get_system()=="windows")
181 candidates.push_back("lib"+lib+".dll.a");
183 for(StringList::iterator i=candidates.begin(); i!=candidates.end(); ++i)
185 FS::Path full = path/ *i;
186 FileTarget *tgt = get_target(full);
190 Target *real_tgt = tgt->get_real_target();
192 /* Ignore dynamic libraries from local packages unless library mode is
194 if(dynamic_cast<SharedLibrary *>(real_tgt) && mode!=DYNAMIC)
199 else if(FS::is_reg(full))
201 tgt = new SystemLibrary(builder, full.str());