From: Mikko Rasa Date: Fri, 30 Mar 2012 19:21:39 +0000 (+0300) Subject: Add a separate set of functions for registering and looking up targets by path X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c52efe77690dcf620d3b19f750a91422f771942d;p=builder.git Add a separate set of functions for registering and looking up targets by path --- diff --git a/source/builder.cpp b/source/builder.cpp index 63ae061..7704a96 100644 --- a/source/builder.cpp +++ b/source/builder.cpp @@ -370,13 +370,20 @@ Package *Builder::get_package(const string &name) Target *Builder::get_target(const string &n) const { - // XXX Used for getting targets by path. get_target(const FS::Path &)? TargetMap::const_iterator i = targets.find(n); if(i!=targets.end()) return i->second; return 0; } +FileTarget *Builder::get_target_by_path(const FS::Path &p) const +{ + TargetMap::const_iterator i = targets_by_path.find(p.str()); + if(i!=targets_by_path.end()) + return static_cast(i->second); + return 0; +} + Target *Builder::get_header(const string &include, const FS::Path &from, const list &path) { string hash(8, 0); @@ -495,16 +502,15 @@ void Builder::problem(const string &p, const string &d) problems.push_back(Problem(p, d)); } -void Builder::add_target(FileTarget *t) +void Builder::add_target(Target *t) { - targets.insert(TargetMap::value_type(t->get_path().str(), t)); + targets.insert(TargetMap::value_type(t->get_name(), t)); new_tgts.push_back(t); } -void Builder::add_target(VirtualTarget *t) +void Builder::register_path(const FS::Path &path, FileTarget *t) { - targets.insert(TargetMap::value_type(t->get_name(), t)); - new_tgts.push_back(t); + targets_by_path.insert(TargetMap::value_type(path.str(), t)); } void Builder::usage(const char *reason, const char *argv0, bool brief) @@ -636,7 +642,7 @@ int Builder::create_targets() // Apply what-ifs for(StringList::iterator i=what_if.begin(); i!=what_if.end(); ++i) { - FileTarget *tgt = dynamic_cast(get_target((cwd/ *i).str())); + FileTarget *tgt = get_target_by_path(cwd/ *i); if(!tgt) { IO::print(IO::cerr, "Unknown what-if target %s\n", *i); @@ -651,7 +657,9 @@ int Builder::create_targets() { Target *tgt = get_target(*i); if(!tgt) - tgt = get_target((cwd/ *i).str()); + tgt = get_target_by_path(*i); + if(!tgt) + tgt = get_target_by_path(cwd/ *i); if(!tgt) { IO::print("I don't know anything about %s\n", *i); @@ -672,7 +680,7 @@ int Builder::create_targets() Target *Builder::get_header(const FS::Path &fn) { - Target *tgt = get_target(fn.str()); + Target *tgt = get_target_by_path(fn); if(tgt) return tgt; if(FS::is_reg(fn)) @@ -708,8 +716,8 @@ Target *Builder::get_library(const string &lib, const FS::Path &path, LibMode mo for(StringList::iterator i=candidates.begin(); i!=candidates.end(); ++i) { - string full = (path/ *i).str(); - Target *tgt = get_target(full); + FS::Path full = path/ *i; + Target *tgt = get_target_by_path(full); if(tgt) { @@ -724,7 +732,7 @@ Target *Builder::get_library(const string &lib, const FS::Path &path, LibMode mo } else if(FS::is_reg(full)) { - tgt = new SystemLibrary(*this, full); + tgt = new SystemLibrary(*this, full.str()); return tgt; } } diff --git a/source/builder.h b/source/builder.h index 4cfe0c1..44bf878 100644 --- a/source/builder.h +++ b/source/builder.h @@ -71,6 +71,7 @@ private: bool no_externals; TargetMap targets; + TargetMap targets_by_path; TargetList new_tgts; TargetMap includes; TargetMap libraries; @@ -121,6 +122,8 @@ public: /** Looks up a target by name. Returns 0 if no such target exists. */ Target *get_target(const std::string &) const; + FileTarget *get_target_by_path(const Msp::FS::Path &) const; + const TargetMap &get_targets() const { return targets; } /** Tries to locate a header based on location of including file and include @@ -145,8 +148,8 @@ public: /** Adds a target to both the target map and the new target queue. Called from Target constructor. */ - void add_target(FileTarget *); - void add_target(VirtualTarget *); + void add_target(Target *); + void register_path(const Msp::FS::Path &, FileTarget *); void problem(const std::string &, const std::string &); diff --git a/source/component.cpp b/source/component.cpp index a5076fb..4451a69 100644 --- a/source/component.cpp +++ b/source/component.cpp @@ -123,7 +123,7 @@ void Component::create_targets() const for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i) { FileTarget *ft; - if(Target *tgt = builder.get_target(i->str())) + if(Target *tgt = builder.get_target_by_path(*i)) ft = dynamic_cast(tgt); else ft = new File(builder, *i); @@ -141,7 +141,7 @@ void Component::create_targets() const for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i) { FileTarget *ft; - if(Target *tgt = builder.get_target(i->str())) + if(Target *tgt = builder.get_target_by_path(*i)) ft = dynamic_cast(tgt); else ft = new File(builder, pkg, *i); @@ -151,7 +151,7 @@ void Component::create_targets() const else if(type==DATAFILE) { File *source; - if(Target *tgt = builder.get_target(files.front().str())) + if(Target *tgt = builder.get_target_by_path(files.front())) source = dynamic_cast(tgt); else source = new File(builder, pkg, files.front()); @@ -171,7 +171,7 @@ void Component::create_targets() const string ext = FS::extpart(FS::basename(*i)); if(ext==".h") { - FileTarget *hdr = dynamic_cast(builder.get_target(i->str())); + FileTarget *hdr = builder.get_target_by_path(*i); if(!hdr) hdr = new Header(builder, *this, i->str()); diff --git a/source/filetarget.cpp b/source/filetarget.cpp index d6dcd16..8867231 100644 --- a/source/filetarget.cpp +++ b/source/filetarget.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include "builder.h" #include "filetarget.h" @@ -9,11 +10,11 @@ using namespace std; using namespace Msp; FileTarget::FileTarget(Builder &b, const Package *p, const FS::Path &a): - Target(b, p, FS::basename(a.str())), + Target(b, p, make_name(p, a)), path(a), size(0) { - builder.add_target(this); + builder.register_path(path, this); if(FS::Stat st = FS::lstat(path)) { @@ -58,3 +59,14 @@ void FileTarget::check_rebuild() if(!rebuild && spkg && spkg->get_config().get_mtime()>mtime) mark_rebuild("Package options changed"); } + +string FileTarget::make_name(const Package *pkg, const FS::Path &pth) +{ + if(const SourcePackage *spkg = dynamic_cast(pkg)) + { + FS::Path relpath = FS::relative(pth, spkg->get_source()); + return format("<%s>%s", pkg->get_name(), relpath.str().substr(1)); + } + else + return pth.str(); +} diff --git a/source/filetarget.h b/source/filetarget.h index 52551e4..4363d5a 100644 --- a/source/filetarget.h +++ b/source/filetarget.h @@ -28,6 +28,8 @@ public: protected: virtual void check_rebuild(); +private: + std::string make_name(const Package *, const Msp::FS::Path &); }; #endif diff --git a/source/target.cpp b/source/target.cpp index 341b249..f2a1a13 100644 --- a/source/target.cpp +++ b/source/target.cpp @@ -19,7 +19,9 @@ Target::Target(Builder &b, const Package *p, const string &n): deps_ready(false), preparing(false), prepared(false) -{ } +{ + builder.add_target(this); +} Target *Target::get_buildable_target() { diff --git a/source/virtualtarget.cpp b/source/virtualtarget.cpp index a8692fb..e22d8fc 100644 --- a/source/virtualtarget.cpp +++ b/source/virtualtarget.cpp @@ -8,9 +8,7 @@ using namespace Msp; VirtualTarget::VirtualTarget(Builder &b, const string &n): Target(b, 0, n) -{ - builder.add_target(this); -} +{ } void VirtualTarget::check_rebuild() {