]> git.tdb.fi Git - builder.git/blob - source/virtualfilesystem.cpp
Don't pass including file location to find_header
[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         static string cxx_ver;
55         if(cxx_ver.empty())
56         {
57                 // XXX This needs to go elsewhere
58                 /*StringList argv;
59                 argv.push_back(current_arch->get_tool("CXX"));
60                 argv.push_back("--version");
61                 if(RegMatch m = Regex("[0-9]\\.[0-9.]+").match(run_command(argv)))
62                 {
63                         cxx_ver = m[0].str;
64                         while(!cxx_ver.empty() && !FS::is_dir(FS::Path("/usr/include/c++")/cxx_ver))
65                         {
66                                 string::size_type dot = cxx_ver.rfind('.');
67                                 if(dot==string::npos)
68                                         break;
69                                 cxx_ver.erase(dot);
70                         }
71                         if(verbose>=5)
72                                 IO::print("C++ version is %s\n", cxx_ver);
73                 }
74                 else*/
75                         cxx_ver = "-";
76         }
77
78         if(builder.get_verbose()>=5)
79                 IO::print("Looking for header %s with path %s\n", name, join(path.begin(), path.end()));
80
81         SearchPath syspath;
82         const Architecture &arch = builder.get_current_arch();
83         if(arch.is_native())
84                 syspath.push_back("/usr/include");
85         else
86                 syspath.push_back("/usr/"+arch.get_cross_prefix()+"/include");
87         if(cxx_ver!="-")
88                 syspath.push_back((FS::Path("/usr/include/c++/")/cxx_ver).str());
89
90         FileTarget *tgt = 0;
91         for(SearchPath::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
92                 tgt = get_header(FS::Path(*j)/name);
93         for(SearchPath::const_iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
94                 tgt = get_header(FS::Path(*j)/name);
95
96         include_cache.insert(TargetMap::value_type(id, tgt));
97
98         return tgt;
99 }
100
101 FileTarget *VirtualFileSystem::find_library(const string &lib, const list<string> &path, LibMode mode)
102 {
103         string hash(8, 0);
104         for(list<string>::const_iterator i=path.begin(); i!=path.end(); ++i)
105                 update_hash(hash, *i);
106
107         string id = hash+string(1, mode)+lib;
108         TargetMap::iterator i = library_cache.find(id);
109         if(i!=library_cache.end())
110                 return i->second;
111
112         SearchPath syspath;
113         const Architecture &arch = builder.get_current_arch();
114         if(arch.is_native())
115         {
116                 syspath.push_back("/lib");
117                 syspath.push_back("/usr/lib");
118                 if(arch.match_name("pc-32-linux"))
119                         syspath.push_back("/usr/lib/i386-linux-gnu");
120                 else if(arch.match_name("pc-64-linux"))
121                         syspath.push_back("/usr/lib/x86_64-linux-gnu");
122         }
123         else
124                 syspath.push_back("/usr/"+arch.get_cross_prefix()+"/lib");
125
126         if(builder.get_verbose()>=5)
127                 IO::print("Looking for library %s with path %s\n", lib, join(path.begin(), path.end()));
128
129         FileTarget *tgt = 0;
130         for(StringList::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
131                 tgt = get_library(lib, *j, mode);
132         for(StringList::iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
133                 tgt = get_library(lib, *j, mode);
134
135         library_cache.insert(TargetMap::value_type(id, tgt));
136
137         return tgt;
138 }
139
140 FileTarget *VirtualFileSystem::get_header(const FS::Path &fn)
141 {
142         FileTarget *tgt = get_target(fn);
143         if(tgt)
144                 return tgt;
145
146         if(FS::is_reg(fn))
147         {
148                 tgt = new CSourceFile(builder, fn);
149                 return tgt;
150         }
151         return 0;
152 }
153
154 FileTarget *VirtualFileSystem::get_library(const string &lib, const FS::Path &path, LibMode mode)
155 {
156         const Architecture &arch = builder.get_current_arch();
157
158         /* Try dynamic libraries only if library mode permits it */
159         if(mode!=ALL_STATIC)
160         {
161                 FS::Path fn = try_patterns(path, arch.get_shared_library_patterns(), lib);
162                 if(!fn.empty())
163                 {
164                         FileTarget *tgt = get_target(fn);
165                         if(!tgt)
166                                 return new SharedLibrary(builder, fn);
167                         else if(mode==DYNAMIC || !tgt->get_package())
168                                 return tgt;
169                 }
170         }
171
172         /* Static libraries are always considered, since sometimes shared versions
173         may not be available */
174         FS::Path fn = try_patterns(path, arch.get_static_library_patterns(), lib);
175         if(!fn.empty())
176         {
177                 if(FileTarget *tgt = get_target(fn))
178                         return tgt;
179                 else
180                         return new StaticLibrary(builder, fn);
181         }
182
183         return 0;
184 }
185
186 FS::Path VirtualFileSystem::try_patterns(const FS::Path &dir, const list<Pattern> &patterns, const string &base)
187 {
188         for(list<Pattern>::const_iterator i=patterns.begin(); i!=patterns.end(); ++i)
189         {
190                 FS::Path full = dir/i->apply(base);
191                 if(get_target(full) || FS::is_reg(full))
192                         return full;
193         }
194
195         return FS::Path();
196 }