2 #include <msp/fs/stat.h>
3 #include <msp/fs/utils.h>
4 #include <msp/io/print.h>
5 #include <msp/strings/utils.h>
7 #include "csourcefile.h"
8 #include "executable.h"
10 #include "sharedlibrary.h"
11 #include "staticlibrary.h"
13 #include "virtualfilesystem.h"
18 VirtualFileSystem::VirtualFileSystem(Builder &b):
23 FileTarget *VirtualFileSystem::get_target(const FS::Path &p) const
25 TargetMap::const_iterator i = targets.find(p.str());
27 return static_cast<FileTarget *>(i->second);
31 void VirtualFileSystem::register_path(const FS::Path &path, FileTarget *t)
33 targets.insert(TargetMap::value_type(path.str(), t));
34 nonexistent.erase(path);
35 builder.get_logger().log("vfs", format("Path %s registered to %s", path, t->get_name()));
38 FileTarget *VirtualFileSystem::find_header(const string &name, const SearchPath &path, bool use_syspath)
40 // XXX This will cause trouble with multiple architectures in a single build
41 const Tool *tool = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(name)), true);
45 list<FS::Path> combined_path(path.begin(), path.end());
48 const Tool::SearchPath &syspath = tool->get_system_path();
49 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
52 for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
54 FS::Path filename = *i/name;
55 if(FileTarget *tgt = get_target(filename))
57 builder.get_logger().log("vfs", format("Header %s found in %s as existing %s", name, i->str(), tgt->get_type()));
60 else if(file_exists(filename))
62 builder.get_logger().log("vfs", format("Header %s found in %s", name, i->str()));
63 return dynamic_cast<FileTarget *>(tool->create_source(filename));
66 builder.get_logger().log("vfs", format("Header %s not found in %s", name, i->str()));
72 FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath &path, BuildInfo::LibraryMode mode, bool use_syspath)
74 list<FS::Path> combined_path(path.begin(), path.end());
77 const Tool &linker = builder.get_toolchain().get_tool("LINK");
78 const Tool::SearchPath &syspath = linker.get_system_path();
79 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
82 const Architecture &arch = builder.get_current_arch();
84 list<string> shared_names;
85 if(mode!=BuildInfo::FORCE_STATIC)
86 shared_names = Pattern::apply_list(arch.get_shared_library_patterns(), lib);
88 list<string> static_names;
89 if(mode!=BuildInfo::FORCE_DYNAMIC)
90 static_names = Pattern::apply_list(arch.get_static_library_patterns(), lib);
92 for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
94 const list<string> *cur_names = (mode>=BuildInfo::DYNAMIC ? &shared_names : &static_names);
95 for(list<string>::const_iterator j=cur_names->begin(); j!=cur_names->end(); )
97 FS::Path filename = *i / *j;
98 if(FileTarget *tgt = get_target(filename))
100 if(cur_names!=&shared_names || mode==BuildInfo::DYNAMIC)
102 builder.get_logger().log("vfs", format("Library %s (%s) found in %s as existing %s", lib, *j, i->str(), tgt->get_type()));
106 else if(file_exists(filename))
108 builder.get_logger().log("vfs", format("Library %s (%s) found in %s", lib, *j, i->str()));
109 if(cur_names==&shared_names)
110 return new SharedLibrary(builder, filename);
112 return new StaticLibrary(builder, filename);
115 if(++j==cur_names->end())
117 if(mode==BuildInfo::DYNAMIC && cur_names==&shared_names)
118 cur_names = &static_names;
119 else if(mode==BuildInfo::STATIC && cur_names==&static_names)
120 cur_names = &shared_names;
123 j = cur_names->begin();
127 builder.get_logger().log("vfs", format("Library %s not found in %s", lib, i->str()));
133 FileTarget *VirtualFileSystem::find_binary(const string &name)
136 if(const char *env_path = getenv("PATH"))
138 vector<string> parts = split(env_path, ':');
139 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
144 path.push_back("/bin");
145 path.push_back("/usr/bin");
148 for(SearchPath::const_iterator i=path.begin(); i!=path.end(); ++i)
150 FS::Path filename = *i/name;
151 if(FileTarget *tgt = get_target(filename))
153 builder.get_logger().log("vfs", format("Binary %s found in %s as existing %s", name, *i, tgt->get_type()));
156 else if(file_exists(filename))
158 builder.get_logger().log("vfs", format("Binary %s found in %s", name, *i));
159 return new Executable(builder, filename);
162 builder.get_logger().log("vfs", format("Binary %s not found in %s", name, *i));
168 bool VirtualFileSystem::file_exists(const FS::Path &filename)
170 if(nonexistent.count(filename))
172 if(FS::is_reg(filename))
174 nonexistent.insert(filename);