]> git.tdb.fi Git - builder.git/blob - source/virtualfilesystem.cpp
Library search fixes
[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 "sharedlibrary.h"
10 #include "staticlibrary.h"
11 #include "tool.h"
12 #include "virtualfilesystem.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 VirtualFileSystem::VirtualFileSystem(Builder &b):
18         builder(b)
19 {
20 }
21
22 FileTarget *VirtualFileSystem::get_target(const FS::Path &p) const
23 {
24         TargetMap::const_iterator i = targets.find(p.str());
25         if(i!=targets.end())
26                 return static_cast<FileTarget *>(i->second);
27         return 0;
28 }
29
30 void VirtualFileSystem::register_path(const FS::Path &path, FileTarget *t)
31 {
32         targets.insert(TargetMap::value_type(path.str(), t));
33         nonexistent.erase(path);
34         builder.get_logger().log("vfs", format("Path %s registered to %s", path, t->get_name()));
35 }
36
37 FileTarget *VirtualFileSystem::find_header(const string &name, const SearchPath &path, bool use_syspath)
38 {
39         // XXX This will cause trouble with multiple architectures in a single build
40         const Tool *tool = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(name)), true);
41         if(!tool)
42                 return 0;
43
44         list<FS::Path> combined_path(path.begin(), path.end());
45         if(use_syspath)
46         {
47                 const Tool::SearchPath &syspath = tool->get_system_path();
48                 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
49         }
50
51         for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
52         {
53                 FS::Path filename = *i/name;
54                 if(FileTarget *tgt = get_target(filename))
55                 {
56                         builder.get_logger().log("vfs", format("Header %s found in %s as existing %s", name, i->str(), tgt->get_type()));
57                         return tgt;
58                 }
59                 else if(file_exists(filename))
60                 {
61                         builder.get_logger().log("vfs", format("Header %s found in %s", name, i->str()));
62                         return dynamic_cast<FileTarget *>(tool->create_source(filename));
63                 }
64
65                 builder.get_logger().log("vfs", format("Header %s not found in %s", name, i->str()));
66         }
67
68         return 0;
69 }
70
71 FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath &path, BuildInfo::LibraryMode mode, bool use_syspath)
72 {
73         list<FS::Path> combined_path(path.begin(), path.end());
74         if(use_syspath)
75         {
76                 const Tool &linker = builder.get_toolchain().get_tool("LINK");
77                 const Tool::SearchPath &syspath = linker.get_system_path();
78                 combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
79         }
80
81         const Architecture &arch = builder.get_current_arch();
82
83         list<string> shared_names;
84         if(mode!=BuildInfo::FORCE_STATIC)
85                 shared_names = Pattern::apply_list(arch.get_shared_library_patterns(), lib);
86
87         list<string> static_names;
88         if(mode!=BuildInfo::FORCE_DYNAMIC)
89                 static_names = Pattern::apply_list(arch.get_static_library_patterns(), lib);
90
91         for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
92         {
93                 const list<string> *cur_names = (mode>=BuildInfo::DYNAMIC ? &shared_names : &static_names);
94                 for(list<string>::const_iterator j=cur_names->begin(); j!=cur_names->end(); )
95                 {
96                         FS::Path filename = *i / *j;
97                         if(FileTarget *tgt = get_target(filename))
98                         {
99                                 builder.get_logger().log("vfs", format("Library %s (%s) found in %s as existing %s", lib, *j, i->str(), tgt->get_type()));
100                                 return tgt;
101                         }
102                         else if(file_exists(filename))
103                         {
104                                 builder.get_logger().log("vfs", format("Library %s (%s) found in %s", lib, *j, i->str()));
105                                 if(cur_names==&shared_names)
106                                 {
107                                         // XXX Hack: create StaticLibrary targets for import libraries
108                                         if(FS::extpart(*j)==".a")
109                                                 return new StaticLibrary(builder, filename);
110                                         return new SharedLibrary(builder, filename);
111                                 }
112                                 else
113                                         return new StaticLibrary(builder, filename);
114                         }
115
116                         if(++j==cur_names->end())
117                         {
118                                 if(mode==BuildInfo::DYNAMIC && cur_names==&shared_names)        
119                                         cur_names = &static_names;
120                                 else if(mode==BuildInfo::STATIC && cur_names==&static_names)
121                                         cur_names = &shared_names;
122                                 else
123                                         break;
124                                 j = cur_names->begin();
125                         }
126                 }
127
128                 builder.get_logger().log("vfs", format("Library %s not found in %s", lib, i->str()));
129         }
130
131         return 0;
132 }
133
134 FileTarget *VirtualFileSystem::find_binary(const string &name)
135 {
136         SearchPath path;
137         if(const char *env_path = getenv("PATH"))
138         {
139                 vector<string> parts = split(env_path, ':');
140                 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
141                         path.push_back(*i);
142         }
143         else
144         {
145                 path.push_back("/bin");
146                 path.push_back("/usr/bin");
147         }
148
149         for(SearchPath::const_iterator i=path.begin(); i!=path.end(); ++i)
150         {
151                 FS::Path filename = *i/name;
152                 if(FileTarget *tgt = get_target(filename))
153                 {
154                         builder.get_logger().log("vfs", format("Binary %s found in %s as existing %s", name, *i, tgt->get_type()));
155                         return tgt;
156                 }
157                 else if(file_exists(filename))
158                 {
159                         builder.get_logger().log("vfs", format("Binary %s found in %s", name, *i));
160                         return new Executable(builder, filename);
161                 }
162
163                 builder.get_logger().log("vfs", format("Binary %s not found in %s", name, *i));
164         }
165
166         return 0;
167 }
168
169 bool VirtualFileSystem::file_exists(const FS::Path &filename)
170 {
171         if(nonexistent.count(filename))
172                 return false;
173         if(FS::is_reg(filename))
174                 return true;
175         nonexistent.insert(filename);
176         return false;
177 }