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 "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 CSourceFile(builder, fn);
159 FileTarget *VirtualFileSystem::get_library(const string &lib, const FS::Path &path, LibMode mode)
161 const Architecture &arch = builder.get_current_arch();
163 /* Try dynamic libraries only if library mode permits it */
166 FS::Path fn = try_patterns(path, arch.get_shared_library_patterns(), lib);
169 FileTarget *tgt = get_target(fn);
171 return new SystemLibrary(builder, fn.str());
172 else if(mode==DYNAMIC || !tgt->get_package())
177 /* Static libraries are always considered, since sometimes shared versions
178 may not be available */
179 FS::Path fn = try_patterns(path, arch.get_static_library_patterns(), lib);
182 if(FileTarget *tgt = get_target(fn))
185 return new SystemLibrary(builder, fn.str());
191 FS::Path VirtualFileSystem::try_patterns(const FS::Path &dir, const list<Pattern> &patterns, const string &base)
193 for(list<Pattern>::const_iterator i=patterns.begin(); i!=patterns.end(); ++i)
195 FS::Path full = dir/i->apply(base);
196 if(get_target(full) || FS::is_reg(full))