]> git.tdb.fi Git - builder.git/blob - source/virtualfilesystem.cpp
869a1e3899c4c3d4adfd6ff06da83fde142230d9
[builder.git] / source / virtualfilesystem.cpp
1 #include <cstdlib>
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         TargetMap::const_iterator 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(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()));
36 }
37
38 FileTarget *VirtualFileSystem::find_header(const string &name, const 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         list<FS::Path> combined_path(path.begin(), path.end());
46         if(use_syspath)
47         {
48                 const Tool::SearchPath &syspath = tool->get_system_path();
49                 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
50         }
51
52         for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
53         {
54                 FS::Path filename = *i/name;
55                 if(FileTarget *tgt = get_target(filename))
56                 {
57                         builder.get_logger().log("vfs", format("Header %s found in %s as existing %s", name, i->str(), tgt->get_type()));
58                         return tgt;
59                 }
60                 else if(file_exists(filename))
61                 {
62                         builder.get_logger().log("vfs", format("Header %s found in %s", name, i->str()));
63                         return dynamic_cast<FileTarget *>(tool->create_source(filename));
64                 }
65
66                 builder.get_logger().log("vfs", format("Header %s not found in %s", name, i->str()));
67         }
68
69         return 0;
70 }
71
72 FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath &path, BuildInfo::LibraryMode mode, bool use_syspath)
73 {
74         list<FS::Path> combined_path(path.begin(), path.end());
75         if(use_syspath)
76         {
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());
80         }
81
82         const Architecture &arch = builder.get_current_arch();
83
84         list<string> shared_names;
85         if(mode!=BuildInfo::FORCE_STATIC)
86                 shared_names = Pattern::apply_list(arch.get_shared_library_patterns(), lib);
87
88         list<string> static_names;
89         if(mode!=BuildInfo::FORCE_DYNAMIC)
90                 static_names = Pattern::apply_list(arch.get_static_library_patterns(), lib);
91
92         for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
93         {
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(); )
96                 {
97                         FS::Path filename = *i / *j;
98                         if(FileTarget *tgt = get_target(filename))
99                         {
100                                 builder.get_logger().log("vfs", format("Library %s (%s) found in %s as existing %s", lib, *j, i->str(), tgt->get_type()));
101                                 return tgt;
102                         }
103                         else if(file_exists(filename))
104                         {
105                                 builder.get_logger().log("vfs", format("Library %s (%s) found in %s", lib, *j, i->str()));
106                                 if(cur_names==&shared_names)
107                                 {
108                                         /* XXX Hack: create ImportLibraries here; they should be handled
109                                         separately, but I need a more generic way of handling all these
110                                         filename patterns */
111                                         if(FS::extpart(*j)==".a")
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", format("Library %s not found in %s", lib, i->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 if(const char *env_path = getenv("PATH"))
143         {
144                 vector<string> parts = split(env_path, ':');
145                 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
146                         path.push_back(*i);
147         }
148         else
149         {
150                 path.push_back("/bin");
151                 path.push_back("/usr/bin");
152         }
153
154         for(SearchPath::const_iterator i=path.begin(); i!=path.end(); ++i)
155         {
156                 FS::Path filename = *i/name;
157                 if(FileTarget *tgt = get_target(filename))
158                 {
159                         builder.get_logger().log("vfs", format("Binary %s found in %s as existing %s", name, *i, tgt->get_type()));
160                         return tgt;
161                 }
162                 else if(file_exists(filename))
163                 {
164                         builder.get_logger().log("vfs", format("Binary %s found in %s", name, *i));
165                         return new Executable(builder, filename);
166                 }
167
168                 builder.get_logger().log("vfs", format("Binary %s not found in %s", name, *i));
169         }
170
171         return 0;
172 }
173
174 bool VirtualFileSystem::file_exists(const FS::Path &filename)
175 {
176         if(nonexistent.count(filename))
177                 return false;
178         if(FS::is_reg(filename))
179                 return true;
180         nonexistent.insert(filename);
181         return false;
182 }