]> git.tdb.fi Git - builder.git/blob - source/virtualfilesystem.cpp
Make VirtualFileSystem able to find binaries
[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 "misc.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 SearchPath &path)
39 {
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);
42         if(!tool)
43                 return 0;
44         const Tool::SearchPath &syspath = tool->get_system_path();
45
46         list<FS::Path> combined_path(path.begin(), path.end());
47         combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
48
49         for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
50         {
51                 FS::Path filename = *i/name;
52                 if(FileTarget *tgt = get_target(filename))
53                 {
54                         builder.get_logger().log("vfs", format("Header %s found in %s as existing %s", name, i->str(), tgt->get_type()));
55                         return tgt;
56                 }
57                 else if(file_exists(filename))
58                 {
59                         builder.get_logger().log("vfs", format("Header %s found in %s", name, i->str()));
60                         return dynamic_cast<FileTarget *>(tool->create_source(filename));
61                 }
62
63                 builder.get_logger().log("vfs", format("Header %s not found in %s", name, i->str()));
64         }
65
66         return 0;
67 }
68
69 FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath &path, LibMode mode)
70 {
71         const Tool &linker = builder.get_toolchain().get_tool("LINK");
72         const Tool::SearchPath &syspath = linker.get_system_path();
73
74         list<FS::Path> combined_path(path.begin(), path.end());
75         combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
76
77         const Architecture &arch = builder.get_current_arch();
78
79         /* Try dynamic libraries only if library mode permits it */
80         list<string> shared_names;
81         if(mode!=ALL_STATIC)
82         {
83                 const list<Pattern> &shared_patterns = arch.get_shared_library_patterns();
84                 for(list<Pattern>::const_iterator i=shared_patterns.begin(); i!=shared_patterns.end(); ++i)
85                         shared_names.push_back(i->apply(lib));
86         }
87
88         /* Static libraries are always considered, since sometimes shared versions
89         may not be available */
90         list<string> static_names;
91         const list<Pattern> &static_patterns = arch.get_static_library_patterns();
92         for(list<Pattern>::const_iterator i=static_patterns.begin(); i!=static_patterns.end(); ++i)
93                 static_names.push_back(i->apply(lib));
94
95         for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
96         {
97                 bool shared = true;
98                 for(list<string>::const_iterator j=shared_names.begin(); j!=static_names.end(); )
99                 {
100                         if(j==shared_names.end())
101                         {
102                                 j = static_names.begin();
103                                 shared = false;
104                                 continue;
105                         }
106
107                         FS::Path filename = *i / *j;
108                         if(FileTarget *tgt = get_target(filename))
109                         {
110                                 if(!shared || mode==DYNAMIC || !tgt->get_package())
111                                 {
112                                         builder.get_logger().log("vfs", format("Library %s (%s) found in %s as existing %s", lib, *j, i->str(), tgt->get_type()));
113                                         return tgt;
114                                 }
115                         }
116                         else if(file_exists(filename))
117                         {
118                                 builder.get_logger().log("vfs", format("Library %s (%s) found in %s", lib, *j, i->str()));
119                                 if(shared)
120                                         return new SharedLibrary(builder, filename);
121                                 else
122                                         return new StaticLibrary(builder, filename);
123                         }
124
125                         ++j;
126                 }
127
128                 builder.get_logger().log("vfs", format("Library %s not found in %s", lib, i->str()));
129         }
130
131         return 0;
132 }
133
134 FileTarget *VirtualFileSystem::find_binary(const string &name)
135 {
136         SearchPath path;
137         if(const char *env_path = getenv("PATH"))
138         {
139                 vector<string> parts = split(env_path, ':');
140                 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
141                         path.push_back(*i);
142         }
143         else
144         {
145                 path.push_back("/bin");
146                 path.push_back("/usr/bin");
147         }
148
149         for(SearchPath::const_iterator i=path.begin(); i!=path.end(); ++i)
150         {
151                 FS::Path filename = *i/name;
152                 if(FileTarget *tgt = get_target(filename))
153                 {
154                         builder.get_logger().log("vfs", format("Binary %s found in %s as existing %s", name, *i, tgt->get_type()));
155                         return tgt;
156                 }
157                 else if(file_exists(filename))
158                 {
159                         builder.get_logger().log("vfs", format("Binary %s found in %s", name, *i));
160                         return new Executable(builder, filename);
161                 }
162
163                 builder.get_logger().log("vfs", format("Binary %s not found in %s", name, *i));
164         }
165
166         return 0;
167 }
168
169 bool VirtualFileSystem::file_exists(const FS::Path &filename)
170 {
171         if(nonexistent.count(filename))
172                 return false;
173         if(FS::is_reg(filename))
174                 return true;
175         nonexistent.insert(filename);
176         return false;
177 }