1 #include <msp/core/environ.h>
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"
9 #include "importlibrary.h"
10 #include "sharedlibrary.h"
11 #include "staticlibrary.h"
13 #include "virtualfilesystem.h"
18 FileTarget *VirtualFileSystem::get_target(const FS::Path &p) const
20 auto i = targets.find(p.str());
22 return static_cast<FileTarget *>(i->second);
26 void VirtualFileSystem::register_path(const FS::Path &path, FileTarget *t)
28 targets.insert({ path, t });
29 nonexistent.erase(path);
30 builder.get_logger().log("vfs", "Path %s registered to %s", path, t->get_name());
33 FileTarget *VirtualFileSystem::find_header(const string &name, Tool *tool, const SearchPath &path, bool use_syspath)
36 tool = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(name)), true);
42 SearchPath combined_path = path;
45 const SearchPath &syspath = tool->get_system_path();
46 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
49 for(const FS::Path &p: combined_path)
51 FS::Path filename = p/name;
52 if(FileTarget *tgt = get_target(filename))
54 builder.get_logger().log("vfs", "Header %s found in %s as existing %s", name, p.str(), tgt->get_type());
57 else if(file_exists(filename))
59 builder.get_logger().log("vfs", "Header %s found in %s", name, p.str());
60 return dynamic_cast<FileTarget *>(tool->create_source(filename));
63 builder.get_logger().log("vfs", "Header %s not found in %s", name, p.str());
69 FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath &path, BuildInfo::LibraryMode mode, bool use_syspath)
71 SearchPath combined_path = path;
74 Tool &linker = builder.get_toolchain().get_tool("LINK");
76 const SearchPath &syspath = linker.get_system_path();
77 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
80 const Architecture &arch = builder.get_current_arch();
82 vector<string> shared_names;
83 bool use_import_lib = false;
84 if(mode!=BuildInfo::FORCE_STATIC)
86 shared_names = Pattern::apply_list(arch.get_patterns<ImportLibrary>(), lib);
87 if(!(use_import_lib = !shared_names.empty()))
88 shared_names = Pattern::apply_list(arch.get_patterns<SharedLibrary>(), lib);
91 vector<string> static_names;
92 if(mode!=BuildInfo::FORCE_DYNAMIC)
93 static_names = Pattern::apply_list(arch.get_patterns<StaticLibrary>(), lib);
95 for(const FS::Path &p: combined_path)
97 const vector<string> *cur_names = (mode>=BuildInfo::DYNAMIC ? &shared_names : &static_names);
98 for(auto j=cur_names->begin(); j!=cur_names->end(); )
100 FS::Path filename = p / *j;
101 if(FileTarget *tgt = get_target(filename))
103 builder.get_logger().log("vfs", "Library %s (%s) found in %s as existing %s", lib, *j, p.str(), tgt->get_type());
106 else if(file_exists(filename))
108 builder.get_logger().log("vfs", "Library %s (%s) found in %s", lib, *j, p.str());
109 if(cur_names==&shared_names)
112 return new ImportLibrary(builder, filename);
113 return new SharedLibrary(builder, filename);
116 return new StaticLibrary(builder, filename);
119 if(++j==cur_names->end())
121 if(mode==BuildInfo::DYNAMIC && cur_names==&shared_names)
122 cur_names = &static_names;
123 else if(mode==BuildInfo::STATIC && cur_names==&static_names)
124 cur_names = &shared_names;
127 j = cur_names->begin();
131 builder.get_logger().log("vfs", "Library %s not found in %s", lib, p.str());
137 FileTarget *VirtualFileSystem::find_binary(const string &name)
140 if(FS::Path(name).is_absolute())
144 if(sys_bin_path.empty())
146 string env_path = Msp::getenv("PATH");
147 if(!env_path.empty())
149 for(const string &p: split(env_path, ':'))
150 sys_bin_path.push_back(p);
154 sys_bin_path.push_back("/bin");
155 sys_bin_path.push_back("/usr/bin");
161 for(const FS::Path &p: path)
163 FS::Path filename = p/name;
164 if(FileTarget *tgt = get_target(filename))
166 builder.get_logger().log("vfs", "Binary %s found in %s as existing %s", name, p, tgt->get_type());
169 else if(file_exists(filename))
171 builder.get_logger().log("vfs", "Binary %s found in %s", name, p);
172 return new Executable(builder, filename);
175 builder.get_logger().log("vfs", "Binary %s not found in %s", name, p);
181 bool VirtualFileSystem::file_exists(const FS::Path &filename)
183 if(nonexistent.count(filename))
185 if(FS::is_reg(filename))
187 nonexistent.insert(filename);