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 static string cxx_ver;
57 // XXX This needs to go elsewhere
59 argv.push_back(current_arch->get_tool("CXX"));
60 argv.push_back("--version");
61 if(RegMatch m = Regex("[0-9]\\.[0-9.]+").match(run_command(argv)))
64 while(!cxx_ver.empty() && !FS::is_dir(FS::Path("/usr/include/c++")/cxx_ver))
66 string::size_type dot = cxx_ver.rfind('.');
72 IO::print("C++ version is %s\n", cxx_ver);
78 if(builder.get_verbose()>=5)
79 IO::print("Looking for header %s with path %s\n", name, join(path.begin(), path.end()));
82 const Architecture &arch = builder.get_current_arch();
84 syspath.push_back("/usr/include");
86 syspath.push_back("/usr/"+arch.get_cross_prefix()+"/include");
88 syspath.push_back((FS::Path("/usr/include/c++/")/cxx_ver).str());
91 for(SearchPath::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
92 tgt = get_header(FS::Path(*j)/name);
93 for(SearchPath::const_iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
94 tgt = get_header(FS::Path(*j)/name);
96 include_cache.insert(TargetMap::value_type(id, tgt));
101 FileTarget *VirtualFileSystem::find_library(const string &lib, const list<string> &path, LibMode mode)
104 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
105 update_hash(hash, *i);
107 string id = hash+string(1, mode)+lib;
108 TargetMap::iterator i = library_cache.find(id);
109 if(i!=library_cache.end())
113 const Architecture &arch = builder.get_current_arch();
116 syspath.push_back("/lib");
117 syspath.push_back("/usr/lib");
118 if(arch.match_name("pc-32-linux"))
119 syspath.push_back("/usr/lib/i386-linux-gnu");
120 else if(arch.match_name("pc-64-linux"))
121 syspath.push_back("/usr/lib/x86_64-linux-gnu");
124 syspath.push_back("/usr/"+arch.get_cross_prefix()+"/lib");
126 if(builder.get_verbose()>=5)
127 IO::print("Looking for library %s with path %s\n", lib, join(path.begin(), path.end()));
130 for(StringList::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
131 tgt = get_library(lib, *j, mode);
132 for(StringList::iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
133 tgt = get_library(lib, *j, mode);
135 library_cache.insert(TargetMap::value_type(id, tgt));
140 FileTarget *VirtualFileSystem::get_header(const FS::Path &fn)
142 FileTarget *tgt = get_target(fn);
148 tgt = new CSourceFile(builder, fn);
154 FileTarget *VirtualFileSystem::get_library(const string &lib, const FS::Path &path, LibMode mode)
156 const Architecture &arch = builder.get_current_arch();
158 /* Try dynamic libraries only if library mode permits it */
161 FS::Path fn = try_patterns(path, arch.get_shared_library_patterns(), lib);
164 FileTarget *tgt = get_target(fn);
166 return new SharedLibrary(builder, fn);
167 else if(mode==DYNAMIC || !tgt->get_package())
172 /* Static libraries are always considered, since sometimes shared versions
173 may not be available */
174 FS::Path fn = try_patterns(path, arch.get_static_library_patterns(), lib);
177 if(FileTarget *tgt = get_target(fn))
180 return new StaticLibrary(builder, fn);
186 FS::Path VirtualFileSystem::try_patterns(const FS::Path &dir, const list<Pattern> &patterns, const string &base)
188 for(list<Pattern>::const_iterator i=patterns.begin(); i!=patterns.end(); ++i)
190 FS::Path full = dir/i->apply(base);
191 if(get_target(full) || FS::is_reg(full))