]> git.tdb.fi Git - builder.git/commitdiff
Add a separate set of functions for registering and looking up targets by path
authorMikko Rasa <tdb@tdb.fi>
Fri, 30 Mar 2012 19:21:39 +0000 (22:21 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 30 Mar 2012 19:22:13 +0000 (22:22 +0300)
source/builder.cpp
source/builder.h
source/component.cpp
source/filetarget.cpp
source/filetarget.h
source/target.cpp
source/virtualtarget.cpp

index 63ae0613a737fc2939dec7e36e86113458777841..7704a9634a0e7b086276086ddac50a236a3fc9d8 100644 (file)
@@ -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<FileTarget *>(i->second);
+       return 0;
+}
+
 Target *Builder::get_header(const string &include, const FS::Path &from, const list<string> &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<FileTarget *>(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;
                }
        }
index 4cfe0c158e3b111546a11d9782af2ae0bbce21e7..44bf878b4efa1e1b24d4793b669a1b08e5b342dc 100644 (file)
@@ -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 &);
 
index a5076fb3a71d75fefdc1f88f6ea88a0e71688b20..4451a69f662f4074260520f4173eb98ec318dac8 100644 (file)
@@ -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<FileTarget *>(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<FileTarget *>(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<File *>(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<FileTarget *>(builder.get_target(i->str()));
+                               FileTarget *hdr = builder.get_target_by_path(*i);
                                if(!hdr)
                                        hdr = new Header(builder, *this, i->str());
 
index d6dcd1681c4d589cdb7976279e7acfdc032dd46c..88672317dc35cab7afa7c5fdef65a8d65edc7e57 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/fs/stat.h>
 #include <msp/fs/utils.h>
+#include <msp/strings/format.h>
 #include <msp/time/utils.h>
 #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<const SourcePackage *>(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();
+}
index 52551e4ed90de64540c3bbc1f5084cf9a40513e5..4363d5a8672f5e8dfbad647570acbe1f22212db5 100644 (file)
@@ -28,6 +28,8 @@ public:
 
 protected:
        virtual void check_rebuild();
+private:
+       std::string make_name(const Package *, const Msp::FS::Path &);
 };
 
 #endif
index 341b249e2b17bb9f84dac682093765d4d7102acd..f2a1a1372e482489a24a1ce5bbf9de38dae011b6 100644 (file)
@@ -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()
 {
index a8692fbbaca2faa634dbc24e62e5d51d1dba5f5d..e22d8fc438e709f6f03d450628861d199ecc732e 100644 (file)
@@ -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()
 {