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"
16 VirtualFileSystem::VirtualFileSystem(Builder &b):
21 FileTarget *VirtualFileSystem::get_target(const FS::Path &p) const
23 TargetMap::const_iterator i = targets.find(p.str());
25 return static_cast<FileTarget *>(i->second);
29 void VirtualFileSystem::register_path(const FS::Path &path, FileTarget *t)
31 targets.insert(TargetMap::value_type(path.str(), t));
32 nonexistent.erase(path);
33 builder.get_logger().log("vfs", format("Path %s registered to %s", path, t->get_name()));
36 FileTarget *VirtualFileSystem::find_header(const string &name, const SearchPath &path)
38 // XXX This will cause trouble with multiple architectures in a single build
39 const Tool *tool = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(name)), true);
42 const Tool::SearchPath &syspath = tool->get_system_path();
44 list<FS::Path> combined_path(path.begin(), path.end());
45 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
47 for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
49 FS::Path filename = *i/name;
50 if(FileTarget *tgt = get_target(filename))
52 builder.get_logger().log("vfs", format("Header %s found in %s as existing %s", name, i->str(), tgt->get_type()));
55 else if(file_exists(filename))
57 builder.get_logger().log("vfs", format("Header %s found in %s", name, i->str()));
58 return dynamic_cast<FileTarget *>(tool->create_source(filename));
61 builder.get_logger().log("vfs", format("Header %s not found in %s", name, i->str()));
67 FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath &path, LibMode mode)
69 const Tool &linker = builder.get_toolchain().get_tool("LINK");
70 const Tool::SearchPath &syspath = linker.get_system_path();
72 list<FS::Path> combined_path(path.begin(), path.end());
73 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
75 const Architecture &arch = builder.get_current_arch();
77 /* Try dynamic libraries only if library mode permits it */
78 list<string> shared_names;
81 const list<Pattern> &shared_patterns = arch.get_shared_library_patterns();
82 for(list<Pattern>::const_iterator i=shared_patterns.begin(); i!=shared_patterns.end(); ++i)
83 shared_names.push_back(i->apply(lib));
86 /* Static libraries are always considered, since sometimes shared versions
87 may not be available */
88 list<string> static_names;
89 const list<Pattern> &static_patterns = arch.get_static_library_patterns();
90 for(list<Pattern>::const_iterator i=static_patterns.begin(); i!=static_patterns.end(); ++i)
91 static_names.push_back(i->apply(lib));
93 for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
96 for(list<string>::const_iterator j=shared_names.begin(); j!=static_names.end(); )
98 if(j==shared_names.end())
100 j = static_names.begin();
105 FS::Path filename = *i / *j;
106 if(FileTarget *tgt = get_target(filename))
108 if(!shared || mode==DYNAMIC || !tgt->get_package())
110 builder.get_logger().log("vfs", format("Library %s (%s) found in %s as existing %s", lib, *j, i->str(), tgt->get_type()));
114 else if(file_exists(filename))
116 builder.get_logger().log("vfs", format("Library %s (%s) found in %s", lib, *j, i->str()));
118 return new SharedLibrary(builder, filename);
120 return new StaticLibrary(builder, filename);
126 builder.get_logger().log("vfs", format("Library %s not found in %s", lib, i->str()));
132 bool VirtualFileSystem::file_exists(const FS::Path &filename)
134 if(nonexistent.count(filename))
136 if(FS::is_reg(filename))
138 nonexistent.insert(filename);