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