]> git.tdb.fi Git - builder.git/blob - source/virtualfilesystem.cpp
Make tools capable of reporting a system-wide path used to locate input files
[builder.git] / source / virtualfilesystem.cpp
1 #include <msp/fs/stat.h>
2 #include <msp/io/print.h>
3 #include <msp/strings/utils.h>
4 #include "builder.h"
5 #include "csourcefile.h"
6 #include "misc.h"
7 #include "sharedlibrary.h"
8 #include "staticlibrary.h"
9 #include "virtualfilesystem.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 namespace {
15
16 void update_hash(string &hash, const string &value)
17 {
18         for(unsigned i=0; i<value.size(); ++i)
19                 hash[i%hash.size()] ^= value[i];
20 }
21
22 }
23
24
25 VirtualFileSystem::VirtualFileSystem(Builder &b):
26         builder(b)
27 {
28 }
29
30 FileTarget *VirtualFileSystem::get_target(const FS::Path &p) const
31 {
32         TargetMap::const_iterator i = targets.find(p.str());
33         if(i!=targets.end())
34                 return static_cast<FileTarget *>(i->second);
35         return 0;
36 }
37
38 void VirtualFileSystem::register_path(const FS::Path &path, FileTarget *t)
39 {
40         targets.insert(TargetMap::value_type(path.str(), t));
41 }
42
43 FileTarget *VirtualFileSystem::find_header(const string &name, const SearchPath &path)
44 {
45         string hash(8, 0);
46         for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
47                 update_hash(hash, *i);
48
49         string id = hash+name;
50         TargetMap::iterator i = include_cache.find(id);
51         if(i!=include_cache.end())
52                 return i->second;
53
54         if(builder.get_verbose()>=5)
55                 IO::print("Looking for header %s with path %s\n", name, join(path.begin(), path.end()));
56
57         SearchPath syspath;
58         const Architecture &arch = builder.get_current_arch();
59         if(arch.is_native())
60                 syspath.push_back("/usr/include");
61         else
62                 syspath.push_back("/usr/"+arch.get_cross_prefix()+"/include");
63         if(cxx_ver!="-")
64                 syspath.push_back((FS::Path("/usr/include/c++/")/cxx_ver).str());
65
66         FileTarget *tgt = 0;
67         for(SearchPath::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
68                 tgt = get_header(FS::Path(*j)/name);
69         for(SearchPath::const_iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
70                 tgt = get_header(FS::Path(*j)/name);
71
72         include_cache.insert(TargetMap::value_type(id, tgt));
73
74         return tgt;
75 }
76
77 FileTarget *VirtualFileSystem::find_library(const string &lib, const list<string> &path, LibMode mode)
78 {
79         string hash(8, 0);
80         for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
81                 update_hash(hash, *i);
82
83         string id = hash+string(1, mode)+lib;
84         TargetMap::iterator i = library_cache.find(id);
85         if(i!=library_cache.end())
86                 return i->second;
87
88         SearchPath syspath;
89         const Architecture &arch = builder.get_current_arch();
90         if(arch.is_native())
91         {
92                 syspath.push_back("/lib");
93                 syspath.push_back("/usr/lib");
94                 if(arch.match_name("pc-32-linux"))
95                         syspath.push_back("/usr/lib/i386-linux-gnu");
96                 else if(arch.match_name("pc-64-linux"))
97                         syspath.push_back("/usr/lib/x86_64-linux-gnu");
98         }
99         else
100                 syspath.push_back("/usr/"+arch.get_cross_prefix()+"/lib");
101
102         if(builder.get_verbose()>=5)
103                 IO::print("Looking for library %s with path %s\n", lib, join(path.begin(), path.end()));
104
105         FileTarget *tgt = 0;
106         for(StringList::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
107                 tgt = get_library(lib, *j, mode);
108         for(StringList::iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
109                 tgt = get_library(lib, *j, mode);
110
111         library_cache.insert(TargetMap::value_type(id, tgt));
112
113         return tgt;
114 }
115
116 FileTarget *VirtualFileSystem::get_header(const FS::Path &fn)
117 {
118         FileTarget *tgt = get_target(fn);
119         if(tgt)
120                 return tgt;
121
122         if(FS::is_reg(fn))
123         {
124                 tgt = new CSourceFile(builder, fn);
125                 return tgt;
126         }
127         return 0;
128 }
129
130 FileTarget *VirtualFileSystem::get_library(const string &lib, const FS::Path &path, LibMode mode)
131 {
132         const Architecture &arch = builder.get_current_arch();
133
134         /* Try dynamic libraries only if library mode permits it */
135         if(mode!=ALL_STATIC)
136         {
137                 FS::Path fn = try_patterns(path, arch.get_shared_library_patterns(), lib);
138                 if(!fn.empty())
139                 {
140                         FileTarget *tgt = get_target(fn);
141                         if(!tgt)
142                                 return new SharedLibrary(builder, fn);
143                         else if(mode==DYNAMIC || !tgt->get_package())
144                                 return tgt;
145                 }
146         }
147
148         /* Static libraries are always considered, since sometimes shared versions
149         may not be available */
150         FS::Path fn = try_patterns(path, arch.get_static_library_patterns(), lib);
151         if(!fn.empty())
152         {
153                 if(FileTarget *tgt = get_target(fn))
154                         return tgt;
155                 else
156                         return new StaticLibrary(builder, fn);
157         }
158
159         return 0;
160 }
161
162 FS::Path VirtualFileSystem::try_patterns(const FS::Path &dir, const list<Pattern> &patterns, const string &base)
163 {
164         for(list<Pattern>::const_iterator i=patterns.begin(); i!=patterns.end(); ++i)
165         {
166                 FS::Path full = dir/i->apply(base);
167                 if(get_target(full) || FS::is_reg(full))
168                         return full;
169         }
170
171         return FS::Path();
172 }