]> git.tdb.fi Git - builder.git/blob - source/virtualfilesystem.cpp
Inline simple constructors
[builder.git] / source / virtualfilesystem.cpp
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>
6 #include "builder.h"
7 #include "csourcefile.h"
8 #include "executable.h"
9 #include "importlibrary.h"
10 #include "sharedlibrary.h"
11 #include "staticlibrary.h"
12 #include "tool.h"
13 #include "virtualfilesystem.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 FileTarget *VirtualFileSystem::get_target(const FS::Path &p) const
19 {
20         auto i = targets.find(p.str());
21         if(i!=targets.end())
22                 return static_cast<FileTarget *>(i->second);
23         return 0;
24 }
25
26 void VirtualFileSystem::register_path(const FS::Path &path, FileTarget *t)
27 {
28         targets.insert({ path, t });
29         nonexistent.erase(path);
30         builder.get_logger().log("vfs", "Path %s registered to %s", path, t->get_name());
31 }
32
33 FileTarget *VirtualFileSystem::find_header(const string &name, Tool *tool, const SearchPath &path, bool use_syspath)
34 {
35         if(!tool)
36                 tool = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(name)), true);
37         if(!tool)
38                 return 0;
39
40         tool->prepare();
41
42         SearchPath combined_path = path;
43         if(use_syspath)
44         {
45                 const SearchPath &syspath = tool->get_system_path();
46                 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
47         }
48
49         for(const FS::Path &p: combined_path)
50         {
51                 FS::Path filename = p/name;
52                 if(FileTarget *tgt = get_target(filename))
53                 {
54                         builder.get_logger().log("vfs", "Header %s found in %s as existing %s", name, p.str(), tgt->get_type());
55                         return tgt;
56                 }
57                 else if(file_exists(filename))
58                 {
59                         builder.get_logger().log("vfs", "Header %s found in %s", name, p.str());
60                         return dynamic_cast<FileTarget *>(tool->create_source(filename));
61                 }
62
63                 builder.get_logger().log("vfs", "Header %s not found in %s", name, p.str());
64         }
65
66         return 0;
67 }
68
69 FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath &path, BuildInfo::LibraryMode mode, bool use_syspath)
70 {
71         SearchPath combined_path = path;
72         if(use_syspath)
73         {
74                 Tool &linker = builder.get_toolchain().get_tool("LINK");
75                 linker.prepare();
76                 const SearchPath &syspath = linker.get_system_path();
77                 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
78         }
79
80         const Architecture &arch = builder.get_current_arch();
81
82         vector<string> shared_names;
83         bool use_import_lib = false;
84         if(mode!=BuildInfo::FORCE_STATIC)
85         {
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);
89         }
90
91         vector<string> static_names;
92         if(mode!=BuildInfo::FORCE_DYNAMIC)
93                 static_names = Pattern::apply_list(arch.get_patterns<StaticLibrary>(), lib);
94
95         for(const FS::Path &p: combined_path)
96         {
97                 const vector<string> *cur_names = (mode>=BuildInfo::DYNAMIC ? &shared_names : &static_names);
98                 for(auto j=cur_names->begin(); j!=cur_names->end(); )
99                 {
100                         FS::Path filename = p / *j;
101                         if(FileTarget *tgt = get_target(filename))
102                         {
103                                 builder.get_logger().log("vfs", "Library %s (%s) found in %s as existing %s", lib, *j, p.str(), tgt->get_type());
104                                 return tgt;
105                         }
106                         else if(file_exists(filename))
107                         {
108                                 builder.get_logger().log("vfs", "Library %s (%s) found in %s", lib, *j, p.str());
109                                 if(cur_names==&shared_names)
110                                 {
111                                         if(use_import_lib)
112                                                 return new ImportLibrary(builder, filename);
113                                         return new SharedLibrary(builder, filename);
114                                 }
115                                 else
116                                         return new StaticLibrary(builder, filename);
117                         }
118
119                         if(++j==cur_names->end())
120                         {
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;
125                                 else
126                                         break;
127                                 j = cur_names->begin();
128                         }
129                 }
130
131                 builder.get_logger().log("vfs", "Library %s not found in %s", lib, p.str());
132         }
133
134         return 0;
135 }
136
137 FileTarget *VirtualFileSystem::find_binary(const string &name)
138 {
139         SearchPath path;
140         if(FS::Path(name).is_absolute())
141                 path.push_back("/");
142         else
143         {
144                 if(sys_bin_path.empty())
145                 {
146                         string env_path = Msp::getenv("PATH");
147                         if(!env_path.empty())
148                         {
149                                 for(const string &p: split(env_path, ':'))
150                                         sys_bin_path.push_back(p);
151                         }
152                         else
153                         {
154                                 sys_bin_path.push_back("/bin");
155                                 sys_bin_path.push_back("/usr/bin");
156                         }
157                 }
158                 path = sys_bin_path;
159         }
160
161         for(const FS::Path &p: path)
162         {
163                 FS::Path filename = p/name;
164                 if(FileTarget *tgt = get_target(filename))
165                 {
166                         builder.get_logger().log("vfs", "Binary %s found in %s as existing %s", name, p, tgt->get_type());
167                         return tgt;
168                 }
169                 else if(file_exists(filename))
170                 {
171                         builder.get_logger().log("vfs", "Binary %s found in %s", name, p);
172                         return new Executable(builder, filename);
173                 }
174
175                 builder.get_logger().log("vfs", "Binary %s not found in %s", name, p);
176         }
177
178         return 0;
179 }
180
181 bool VirtualFileSystem::file_exists(const FS::Path &filename)
182 {
183         if(nonexistent.count(filename))
184                 return false;
185         if(FS::is_reg(filename))
186                 return true;
187         nonexistent.insert(filename);
188         return false;
189 }