1 #include <msp/fs/stat.h>
2 #include <msp/fs/utils.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/utils.h>
6 #include "csourcefile.h"
8 #include "sharedlibrary.h"
9 #include "staticlibrary.h"
11 #include "virtualfilesystem.h"
18 void update_hash(string &hash, const string &value)
20 for(unsigned i=0; i<value.size(); ++i)
21 hash[i%hash.size()] ^= value[i];
27 VirtualFileSystem::VirtualFileSystem(Builder &b):
32 FileTarget *VirtualFileSystem::get_target(const FS::Path &p) const
34 TargetMap::const_iterator i = targets.find(p.str());
36 return static_cast<FileTarget *>(i->second);
40 void VirtualFileSystem::register_path(const FS::Path &path, FileTarget *t)
42 targets.insert(TargetMap::value_type(path.str(), t));
45 FileTarget *VirtualFileSystem::find_header(const string &name, const SearchPath &path)
48 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
49 update_hash(hash, *i);
51 string id = hash+name;
52 TargetMap::iterator i = include_cache.find(id);
53 if(i!=include_cache.end())
56 if(builder.get_verbose()>=5)
57 IO::print("Looking for header %s with path %s\n", name, join(path.begin(), path.end()));
59 // XXX This will cause trouble with multiple architectures in a single build
60 const Tool *tool = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(name)), true);
63 const Tool::SearchPath &syspath = tool->get_system_path();
66 for(SearchPath::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
67 tgt = get_header(FS::Path(*j)/name);
68 for(Tool::SearchPath::const_iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
69 tgt = get_header(FS::Path(*j)/name);
71 include_cache.insert(TargetMap::value_type(id, tgt));
76 FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath &path, LibMode mode)
79 for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
80 update_hash(hash, *i);
82 string id = hash+string(1, mode)+lib;
83 TargetMap::iterator i = library_cache.find(id);
84 if(i!=library_cache.end())
87 const Tool &linker = builder.get_toolchain().get_tool("LINK");
88 const Tool::SearchPath &syspath = linker.get_system_path();
90 if(builder.get_verbose()>=5)
91 IO::print("Looking for library %s with path %s\n", lib, join(path.begin(), path.end()));
94 for(SearchPath::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
95 tgt = get_library(lib, *j, mode);
96 for(Tool::SearchPath::const_iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
97 tgt = get_library(lib, *j, mode);
99 library_cache.insert(TargetMap::value_type(id, tgt));
104 FileTarget *VirtualFileSystem::get_header(const FS::Path &fn)
106 FileTarget *tgt = get_target(fn);
112 tgt = new CSourceFile(builder, fn);
118 FileTarget *VirtualFileSystem::get_library(const string &lib, const FS::Path &path, LibMode mode)
120 const Architecture &arch = builder.get_current_arch();
122 /* Try dynamic libraries only if library mode permits it */
125 FS::Path fn = try_patterns(path, arch.get_shared_library_patterns(), lib);
128 FileTarget *tgt = get_target(fn);
130 return new SharedLibrary(builder, fn);
131 else if(mode==DYNAMIC || !tgt->get_package())
136 /* Static libraries are always considered, since sometimes shared versions
137 may not be available */
138 FS::Path fn = try_patterns(path, arch.get_static_library_patterns(), lib);
141 if(FileTarget *tgt = get_target(fn))
144 return new StaticLibrary(builder, fn);
150 FS::Path VirtualFileSystem::try_patterns(const FS::Path &dir, const list<Pattern> &patterns, const string &base)
152 for(list<Pattern>::const_iterator i=patterns.begin(); i!=patterns.end(); ++i)
154 FS::Path full = dir/i->apply(base);
155 if(get_target(full) || FS::is_reg(full))