]> git.tdb.fi Git - builder.git/blob - source/virtualfilesystem.cpp
Prepare tools before querying their system path in the VFS
[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, 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         list<FS::Path> combined_path(path.begin(), path.end());
48         if(use_syspath)
49         {
50                 const Tool::SearchPath &syspath = tool->get_system_path();
51                 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
52         }
53
54         for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
55         {
56                 FS::Path filename = *i/name;
57                 if(FileTarget *tgt = get_target(filename))
58                 {
59                         builder.get_logger().log("vfs", format("Header %s found in %s as existing %s", name, i->str(), tgt->get_type()));
60                         return tgt;
61                 }
62                 else if(file_exists(filename))
63                 {
64                         builder.get_logger().log("vfs", format("Header %s found in %s", name, i->str()));
65                         return dynamic_cast<FileTarget *>(tool->create_source(filename));
66                 }
67
68                 builder.get_logger().log("vfs", format("Header %s not found in %s", name, i->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         list<FS::Path> combined_path(path.begin(), path.end());
77         if(use_syspath)
78         {
79                 Tool &linker = builder.get_toolchain().get_tool("LINK");
80                 linker.prepare();
81                 const Tool::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         list<string> shared_names;
88         if(mode!=BuildInfo::FORCE_STATIC)
89                 shared_names = Pattern::apply_list(arch.get_shared_library_patterns(), lib);
90
91         list<string> static_names;
92         if(mode!=BuildInfo::FORCE_DYNAMIC)
93                 static_names = Pattern::apply_list(arch.get_static_library_patterns(), lib);
94
95         for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
96         {
97                 const list<string> *cur_names = (mode>=BuildInfo::DYNAMIC ? &shared_names : &static_names);
98                 for(list<string>::const_iterator j=cur_names->begin(); j!=cur_names->end(); )
99                 {
100                         FS::Path filename = *i / *j;
101                         if(FileTarget *tgt = get_target(filename))
102                         {
103                                 builder.get_logger().log("vfs", format("Library %s (%s) found in %s as existing %s", lib, *j, i->str(), tgt->get_type()));
104                                 return tgt;
105                         }
106                         else if(file_exists(filename))
107                         {
108                                 builder.get_logger().log("vfs", format("Library %s (%s) found in %s", lib, *j, i->str()));
109                                 if(cur_names==&shared_names)
110                                 {
111                                         /* XXX Hack: create ImportLibraries here; they should be handled
112                                         separately, but I need a more generic way of handling all these
113                                         filename patterns */
114                                         if(FS::extpart(*j)==".a")
115                                                 return new ImportLibrary(builder, filename);
116                                         return new SharedLibrary(builder, filename);
117                                 }
118                                 else
119                                         return new StaticLibrary(builder, filename);
120                         }
121
122                         if(++j==cur_names->end())
123                         {
124                                 if(mode==BuildInfo::DYNAMIC && cur_names==&shared_names)
125                                         cur_names = &static_names;
126                                 else if(mode==BuildInfo::STATIC && cur_names==&static_names)
127                                         cur_names = &shared_names;
128                                 else
129                                         break;
130                                 j = cur_names->begin();
131                         }
132                 }
133
134                 builder.get_logger().log("vfs", format("Library %s not found in %s", lib, i->str()));
135         }
136
137         return 0;
138 }
139
140 FileTarget *VirtualFileSystem::find_binary(const string &name)
141 {
142         SearchPath path;
143         if(FS::Path(name).is_absolute())
144                 path.push_back("/");
145         else if(const char *env_path = getenv("PATH"))
146         {
147                 vector<string> parts = split(env_path, ':');
148                 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
149                         path.push_back(*i);
150         }
151         else
152         {
153                 path.push_back("/bin");
154                 path.push_back("/usr/bin");
155         }
156
157         for(SearchPath::const_iterator i=path.begin(); i!=path.end(); ++i)
158         {
159                 FS::Path filename = *i/name;
160                 if(FileTarget *tgt = get_target(filename))
161                 {
162                         builder.get_logger().log("vfs", format("Binary %s found in %s as existing %s", name, *i, tgt->get_type()));
163                         return tgt;
164                 }
165                 else if(file_exists(filename))
166                 {
167                         builder.get_logger().log("vfs", format("Binary %s found in %s", name, *i));
168                         return new Executable(builder, filename);
169                 }
170
171                 builder.get_logger().log("vfs", format("Binary %s not found in %s", name, *i));
172         }
173
174         return 0;
175 }
176
177 bool VirtualFileSystem::file_exists(const FS::Path &filename)
178 {
179         if(nonexistent.count(filename))
180                 return false;
181         if(FS::is_reg(filename))
182                 return true;
183         nonexistent.insert(filename);
184         return false;
185 }