-#include <cstdlib>
-#include <msp/fs/stat.h>
-#include <msp/fs/utils.h>
-#include <msp/io/print.h>
-#include <msp/strings/utils.h>
-#include "builder.h"
-#include "csourcefile.h"
-#include "executable.h"
-#include "sharedlibrary.h"
-#include "staticlibrary.h"
-#include "tool.h"
-#include "virtualfilesystem.h"
-
-using namespace std;
-using namespace Msp;
-
-VirtualFileSystem::VirtualFileSystem(Builder &b):
- builder(b)
-{
-}
-
-FileTarget *VirtualFileSystem::get_target(const FS::Path &p) const
-{
- TargetMap::const_iterator i = targets.find(p.str());
- if(i!=targets.end())
- return static_cast<FileTarget *>(i->second);
- return 0;
-}
-
-void VirtualFileSystem::register_path(const FS::Path &path, FileTarget *t)
-{
- targets.insert(TargetMap::value_type(path.str(), t));
- nonexistent.erase(path);
- builder.get_logger().log("vfs", format("Path %s registered to %s", path, t->get_name()));
-}
-
-FileTarget *VirtualFileSystem::find_header(const string &name, const SearchPath &path, bool use_syspath)
-{
- // XXX This will cause trouble with multiple architectures in a single build
- const Tool *tool = builder.get_toolchain().get_tool_for_suffix(FS::extpart(FS::basename(name)), true);
- if(!tool)
- return 0;
-
- list<FS::Path> combined_path(path.begin(), path.end());
- if(use_syspath)
- {
- const Tool::SearchPath &syspath = tool->get_system_path();
- combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
- }
-
- for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
- {
- FS::Path filename = *i/name;
- if(FileTarget *tgt = get_target(filename))
- {
- builder.get_logger().log("vfs", format("Header %s found in %s as existing %s", name, i->str(), tgt->get_type()));
- return tgt;
- }
- else if(file_exists(filename))
- {
- builder.get_logger().log("vfs", format("Header %s found in %s", name, i->str()));
- return dynamic_cast<FileTarget *>(tool->create_source(filename));
- }
-
- builder.get_logger().log("vfs", format("Header %s not found in %s", name, i->str()));
- }
-
- return 0;
-}
-
-FileTarget *VirtualFileSystem::find_library(const string &lib, const SearchPath &path, BuildInfo::LibraryMode mode, bool use_syspath)
-{
- list<FS::Path> combined_path(path.begin(), path.end());
- if(use_syspath)
- {
- const Tool &linker = builder.get_toolchain().get_tool("LINK");
- const Tool::SearchPath &syspath = linker.get_system_path();
- combined_path.insert(combined_path.end(), syspath.begin(), syspath.end());
- }
-
- const Architecture &arch = builder.get_current_arch();
-
- list<string> shared_names;
- if(mode!=BuildInfo::FORCE_STATIC)
- shared_names = Pattern::apply_list(arch.get_shared_library_patterns(), lib);
-
- list<string> static_names;
- if(mode!=BuildInfo::FORCE_DYNAMIC)
- static_names = Pattern::apply_list(arch.get_static_library_patterns(), lib);
-
- for(list<FS::Path>::const_iterator i=combined_path.begin(); i!=combined_path.end(); ++i)
- {
- const list<string> *cur_names = (mode>=BuildInfo::DYNAMIC ? &shared_names : &static_names);
- for(list<string>::const_iterator j=cur_names->begin(); j!=cur_names->end(); )
- {
- FS::Path filename = *i / *j;
- if(FileTarget *tgt = get_target(filename))
- {
- builder.get_logger().log("vfs", format("Library %s (%s) found in %s as existing %s", lib, *j, i->str(), tgt->get_type()));
- return tgt;
- }
- else if(file_exists(filename))
- {
- builder.get_logger().log("vfs", format("Library %s (%s) found in %s", lib, *j, i->str()));
- if(cur_names==&shared_names)
- {
- // XXX Hack: create StaticLibrary targets for import libraries
- if(FS::extpart(*j)==".a")
- return new StaticLibrary(builder, filename);
- return new SharedLibrary(builder, filename);
- }
- else
- return new StaticLibrary(builder, filename);
- }
-
- if(++j==cur_names->end())
- {
- if(mode==BuildInfo::DYNAMIC && cur_names==&shared_names)
- cur_names = &static_names;
- else if(mode==BuildInfo::STATIC && cur_names==&static_names)
- cur_names = &shared_names;
- else
- break;
- j = cur_names->begin();
- }
- }
-
- builder.get_logger().log("vfs", format("Library %s not found in %s", lib, i->str()));
- }
-
- return 0;
-}
-
-FileTarget *VirtualFileSystem::find_binary(const string &name)
-{
- SearchPath path;
- if(const char *env_path = getenv("PATH"))
- {
- vector<string> parts = split(env_path, ':');
- for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
- path.push_back(*i);
- }
- else
- {
- path.push_back("/bin");
- path.push_back("/usr/bin");
- }
-
- for(SearchPath::const_iterator i=path.begin(); i!=path.end(); ++i)
- {
- FS::Path filename = *i/name;
- if(FileTarget *tgt = get_target(filename))
- {
- builder.get_logger().log("vfs", format("Binary %s found in %s as existing %s", name, *i, tgt->get_type()));
- return tgt;
- }
- else if(file_exists(filename))
- {
- builder.get_logger().log("vfs", format("Binary %s found in %s", name, *i));
- return new Executable(builder, filename);
- }
-
- builder.get_logger().log("vfs", format("Binary %s not found in %s", name, *i));
- }
-
- return 0;
-}
-
-bool VirtualFileSystem::file_exists(const FS::Path &filename)
-{
- if(nonexistent.count(filename))
- return false;
- if(FS::is_reg(filename))
- return true;
- nonexistent.insert(filename);
- return false;
-}