]> git.tdb.fi Git - builder.git/blob - source/virtualfilesystem.cpp
Rewrite the find_* functions
[builder.git] / source / virtualfilesystem.cpp
1 #include <msp/fs/stat.h>
2 #include <msp/fs/utils.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/utils.h>
5 #include "builder.h"
6 #include "csourcefile.h"
7 #include "misc.h"
8 #include "sharedlibrary.h"
9 #include "staticlibrary.h"
10 #include "tool.h"
11 #include "virtualfilesystem.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 VirtualFileSystem::VirtualFileSystem(Builder &b):
17         builder(b)
18 {
19 }
20
21 FileTarget *VirtualFileSystem::get_target(const FS::Path &p) const
22 {
23         TargetMap::const_iterator i = targets.find(p.str());
24         if(i!=targets.end())
25                 return static_cast<FileTarget *>(i->second);
26         return 0;
27 }
28
29 void VirtualFileSystem::register_path(const FS::Path &path, FileTarget *t)
30 {
31         targets.insert(TargetMap::value_type(path.str(), t));
32         nonexistent.erase(path);
33         builder.get_logger().log("vfs", format("Path %s registered to %s", path, t->get_name()));
34 }
35
36 FileTarget *VirtualFileSystem::find_header(const string &name, const SearchPath &path)
37 {
38         // XXX This will cause trouble with multiple architectures in a single build
39         const Tool *tool = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(name)), true);
40         if(!tool)
41                 return 0;
42         const Tool::SearchPath &syspath = tool->get_system_path();
43
44         list<FS::Path> combined_path(path.begin(), path.end());
45         combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
46
47         for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
48         {
49                 FS::Path filename = *i/name;
50                 if(FileTarget *tgt = get_target(filename))
51                 {
52                         builder.get_logger().log("vfs", format("Header %s found in %s as existing %s", name, i->str(), tgt->get_type()));
53                         return tgt;
54                 }
55                 else if(file_exists(filename))
56                 {
57                         builder.get_logger().log("vfs", format("Header %s found in %s", name, i->str()));
58                         return dynamic_cast<FileTarget *>(tool->create_source(filename));
59                 }
60
61                 builder.get_logger().log("vfs", format("Header %s not found in %s", name, i->str()));
62         }
63
64         return 0;
65 }
66
67 FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath &path, LibMode mode)
68 {
69         const Tool &linker = builder.get_toolchain().get_tool("LINK");
70         const Tool::SearchPath &syspath = linker.get_system_path();
71
72         list<FS::Path> combined_path(path.begin(), path.end());
73         combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
74
75         const Architecture &arch = builder.get_current_arch();
76
77         /* Try dynamic libraries only if library mode permits it */
78         list<string> shared_names;
79         if(mode!=ALL_STATIC)
80         {
81                 const list<Pattern> &shared_patterns = arch.get_shared_library_patterns();
82                 for(list<Pattern>::const_iterator i=shared_patterns.begin(); i!=shared_patterns.end(); ++i)
83                         shared_names.push_back(i->apply(lib));
84         }
85
86         /* Static libraries are always considered, since sometimes shared versions
87         may not be available */
88         list<string> static_names;
89         const list<Pattern> &static_patterns = arch.get_static_library_patterns();
90         for(list<Pattern>::const_iterator i=static_patterns.begin(); i!=static_patterns.end(); ++i)
91                 static_names.push_back(i->apply(lib));
92
93         for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
94         {
95                 bool shared = true;
96                 for(list<string>::const_iterator j=shared_names.begin(); j!=static_names.end(); )
97                 {
98                         if(j==shared_names.end())
99                         {
100                                 j = static_names.begin();
101                                 shared = false;
102                                 continue;
103                         }
104
105                         FS::Path filename = *i / *j;
106                         if(FileTarget *tgt = get_target(filename))
107                         {
108                                 if(!shared || mode==DYNAMIC || !tgt->get_package())
109                                 {
110                                         builder.get_logger().log("vfs", format("Library %s (%s) found in %s as existing %s", lib, *j, i->str(), tgt->get_type()));
111                                         return tgt;
112                                 }
113                         }
114                         else if(file_exists(filename))
115                         {
116                                 builder.get_logger().log("vfs", format("Library %s (%s) found in %s", lib, *j, i->str()));
117                                 if(shared)
118                                         return new SharedLibrary(builder, filename);
119                                 else
120                                         return new StaticLibrary(builder, filename);
121                         }
122
123                         ++j;
124                 }
125
126                 builder.get_logger().log("vfs", format("Library %s not found in %s", lib, i->str()));
127         }
128
129         return 0;
130 }
131
132 bool VirtualFileSystem::file_exists(const FS::Path &filename)
133 {
134         if(nonexistent.count(filename))
135                 return false;
136         if(FS::is_reg(filename))
137                 return true;
138         nonexistent.insert(filename);
139         return false;
140 }