]> git.tdb.fi Git - builder.git/blob - source/virtualfilesystem.cpp
Redesign the library mode system
[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         list<string> shared_names;
80         if(mode!=FORCE_STATIC)
81         {
82                 const list<Pattern> &shared_patterns = arch.get_shared_library_patterns();
83                 for(list<Pattern>::const_iterator i=shared_patterns.begin(); i!=shared_patterns.end(); ++i)
84                         shared_names.push_back(i->apply(lib));
85         }
86
87         list<string> static_names;
88         if(mode!=FORCE_DYNAMIC)
89         {
90                 const list<Pattern> &static_patterns = arch.get_static_library_patterns();
91                 for(list<Pattern>::const_iterator i=static_patterns.begin(); i!=static_patterns.end(); ++i)
92                         static_names.push_back(i->apply(lib));
93         }
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>=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                                 if(cur_names!=&shared_names || mode==DYNAMIC || !tgt->get_package())
104                                 {
105                                         builder.get_logger().log("vfs", format("Library %s (%s) found in %s as existing %s", lib, *j, i->str(), tgt->get_type()));
106                                         return tgt;
107                                 }
108                         }
109                         else if(file_exists(filename))
110                         {
111                                 builder.get_logger().log("vfs", format("Library %s (%s) found in %s", lib, *j, i->str()));
112                                 if(cur_names==&shared_names)
113                                         return new SharedLibrary(builder, filename);
114                                 else
115                                         return new StaticLibrary(builder, filename);
116                         }
117
118                         if(++j==cur_names->end())
119                         {
120                                 if(mode==DYNAMIC && cur_names==&shared_names)   
121                                         cur_names = &static_names;
122                                 else if(mode==STATIC && cur_names==&static_names)
123                                         cur_names = &shared_names;
124                                 else
125                                         break;
126                                 j = cur_names->begin();
127                         }
128                 }
129
130                 builder.get_logger().log("vfs", format("Library %s not found in %s", lib, i->str()));
131         }
132
133         return 0;
134 }
135
136 FileTarget *VirtualFileSystem::find_binary(const string &name)
137 {
138         SearchPath path;
139         if(const char *env_path = getenv("PATH"))
140         {
141                 vector<string> parts = split(env_path, ':');
142                 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
143                         path.push_back(*i);
144         }
145         else
146         {
147                 path.push_back("/bin");
148                 path.push_back("/usr/bin");
149         }
150
151         for(SearchPath::const_iterator i=path.begin(); i!=path.end(); ++i)
152         {
153                 FS::Path filename = *i/name;
154                 if(FileTarget *tgt = get_target(filename))
155                 {
156                         builder.get_logger().log("vfs", format("Binary %s found in %s as existing %s", name, *i, tgt->get_type()));
157                         return tgt;
158                 }
159                 else if(file_exists(filename))
160                 {
161                         builder.get_logger().log("vfs", format("Binary %s found in %s", name, *i));
162                         return new Executable(builder, filename);
163                 }
164
165                 builder.get_logger().log("vfs", format("Binary %s not found in %s", name, *i));
166         }
167
168         return 0;
169 }
170
171 bool VirtualFileSystem::file_exists(const FS::Path &filename)
172 {
173         if(nonexistent.count(filename))
174                 return false;
175         if(FS::is_reg(filename))
176                 return true;
177         nonexistent.insert(filename);
178         return false;
179 }