]> git.tdb.fi Git - builder.git/commitdiff
Implement rdeps mode and sorting in Analyzer
authorMikko Rasa <tdb@tdb.fi>
Fri, 5 Feb 2010 11:44:30 +0000 (11:44 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 5 Feb 2010 11:44:30 +0000 (11:44 +0000)
source/analyzer.cpp
source/analyzer.h
source/target.cpp
source/target.h

index 563143f11c56d2c0b5a72b7f5e4fc58327d23f72..75c08849b39c61fdb222bbdcae8ab981bedfb770 100644 (file)
@@ -27,6 +27,18 @@ Analyzer::Analyzer(Builder &b):
 
 void Analyzer::analyze()
 {
+       if(mode==RDEPS)
+       {
+               rdepends.clear();
+               const TargetMap &targets = builder.get_targets();
+               for(TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
+               {
+                       const TargetList &depends = i->second->get_depends();
+                       for(TargetList::const_iterator j=depends.begin(); j!=depends.end(); ++j)
+                               rdepends[*j].insert(i->second);
+               }
+       }
+
        table.clear();
 
        TableRow row;
@@ -36,19 +48,27 @@ void Analyzer::analyze()
        row.push_back("Rebuild");
        table.push_back(row);
        
-       build_depend_table(*builder.get_target("cmdline"), 0);
+       const Target &cmdline = *builder.get_target("cmdline");
+       if(mode==RDEPS)
+       {
+               const TargetList &deps = cmdline.get_depends();
+               for(TargetList::const_iterator i=deps.begin(); i!=deps.end(); ++i)
+                       build_depend_table(**i, 0);
+       }
+       else
+               build_depend_table(cmdline, 0);
 
        print_table();
 }
 
-void Analyzer::build_depend_table(Target &tgt, unsigned depth)
+void Analyzer::build_depend_table(const Target &tgt, unsigned depth)
 {
-       if(mode!=REBUILD && mode!=ALLDEPS)
+       if(mode==DEPS)
        {
                // Skip trivial targets
-               if(ObjectFile *obj = dynamic_cast<ObjectFile *>(&tgt))
+               if(const ObjectFile *obj = dynamic_cast<const ObjectFile *>(&tgt))
                        return build_depend_table(obj->get_source(), depth);
-               else if(Install *inst = dynamic_cast<Install *>(&tgt))
+               else if(const Install *inst = dynamic_cast<const Install *>(&tgt))
                        return build_depend_table(inst->get_source(), depth);
        }
        else if(mode==REBUILD && !tgt.get_rebuild())
@@ -59,7 +79,7 @@ void Analyzer::build_depend_table(Target &tgt, unsigned depth)
        TableRow row;
 
        string name;
-       FileTarget *ft = dynamic_cast<FileTarget *>(&tgt);
+       const FileTarget *ft = dynamic_cast<const FileTarget *>(&tgt);
        if(full_paths && ft)
                name = ft->get_path().str();
        else
@@ -86,9 +106,17 @@ void Analyzer::build_depend_table(Target &tgt, unsigned depth)
 
        if(!max_depth || depth<max_depth-1)
        {
-               const TargetList &depends = tgt.get_depends();
-               //XXX If we want to sort the targets, we need to take the value of full_paths into account
-               //depends.sort(target_order);
+               TargetList depends;
+               if(mode==RDEPS)
+               {
+                       const set<Target *> &rdeps = rdepends[&tgt];
+                       depends.assign(rdeps.begin(), rdeps.end());
+               }
+               else
+                       depends = tgt.get_depends();
+
+               depends.sort(full_paths ? target_order_full : target_order);
+
                for(TargetList::const_iterator i=depends.begin(); i!=depends.end(); ++i)
                        build_depend_table(**i, depth+1);
        }
@@ -120,5 +148,22 @@ void Analyzer::print_table() const
        }
 }
 
-bool Analyzer::target_order(Target *t1, Target *t2)
-{ return t1->get_name()<t2->get_name(); }
+bool Analyzer::target_order(const Target *t1, const Target *t2)
+{
+       return t1->get_name()<t2->get_name();
+}
+
+bool Analyzer::target_order_full(const Target *t1, const Target *t2)
+{
+       const FileTarget *ft1 = dynamic_cast<const FileTarget *>(t1);
+       const FileTarget *ft2 = dynamic_cast<const FileTarget *>(t2);
+       if(!ft1)
+       {
+               if(ft2)
+                       return true;
+               return target_order(t1, t2);
+       }
+       else if(!ft2)
+               return false;
+       return ft1->get_path().str()<ft2->get_path().str();
+}
index b9afffa9e7e8896a58a10a53535f43fd4d823f85..6d6af9b5ab5706a076617bd6e9a500cf84052ee3 100644 (file)
@@ -9,6 +9,7 @@ Distributed under the LGPL
 #define ANALYZER_H_
 
 #include <list>
+#include <set>
 #include <string>
 #include <vector>
 
@@ -26,7 +27,7 @@ public:
                DEPS,     //< Skip over "trivial" targets such as Install and Compile
                ALLDEPS,  //< Print out absolutely every target
                REBUILD,  //< Print targets that are going to be rebuilt
-               RDEPS     //< Print targets that depend on the given targets (NYI)
+               RDEPS     //< Print targets that depend on the given targets
        };
 
 private:
@@ -38,6 +39,7 @@ private:
        Table table;
        unsigned max_depth;
        bool full_paths;
+       std::map<const Target *, std::set<Target *> > rdepends;
 
 public:
        Analyzer(Builder &);
@@ -54,14 +56,15 @@ private:
        /**
        Adds rows to the table for the given target and its dependencies.
        */
-       void build_depend_table(Target &, unsigned);
+       void build_depend_table(const Target &, unsigned);
 
        /**
        Prints out the table that resulted from the analysis.
        */
        void print_table() const;
 
-       static bool target_order(Target *, Target *);
+       static bool target_order(const Target *, const Target *);
+       static bool target_order_full(const Target *, const Target *);
 };
 
 #endif
index 532901167b51f813228a972a52e7b4941806b64f..c0c3c18051bbfcd4d8c76250b59f1c081c694b85 100644 (file)
@@ -56,7 +56,6 @@ void Target::add_depend(Target *dep)
        if(dep==this)
                throw InvalidParameterValue("A target can't depend on itself");
        depends.push_back(dep);
-       dep->rdepends.push_back(this);
 }
 
 void Target::prepare()
index 80c6a17832edf804760f7d640c3e7993d125492d..ec23df6dfe01a03d2b0b419870d4ec503bc91ae4 100644 (file)
@@ -39,7 +39,6 @@ protected:
        std::string rebuild_reason;
 
        TargetList depends;
-       TargetList rdepends;
        bool deps_ready;
 
        bool preparing;