-#include <msp/fs/utils.h>
-#include <msp/io/print.h>
-#include "analyzer.h"
-#include "builder.h"
-#include "objectfile.h"
-#include "package.h"
-#include "sourcefile.h"
-#include "target.h"
-#include "tool.h"
-
-using namespace std;
-using namespace Msp;
-
-Analyzer::Analyzer(Builder &b):
- builder(b),
- mode(DEPS),
- max_depth(0),
- full_paths(false)
-{ }
-
-void Analyzer::analyze()
-{
- if(mode==RDEPS)
- {
- rdepends.clear();
- const Builder::TargetMap &targets = builder.get_targets();
- for(Builder::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
- {
- const Target::Dependencies &depends = i->second->get_depends();
- for(Target::Dependencies::const_iterator j=depends.begin(); j!=depends.end(); ++j)
- rdepends[*j].insert(i->second);
- }
- }
-
- table.clear();
-
- TableRow row;
- row.push_back("Name");
- row.push_back("Package");
- row.push_back("Type");
- row.push_back("Tool");
- row.push_back("Rebuild");
- table.push_back(row);
-
- Target &cmdline = *builder.get_target("cmdline");
- if(mode==RDEPS)
- {
- const Target::Dependencies &deps = cmdline.get_depends();
- for(Target::Dependencies::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)
-{
- Target *real = tgt.get_real_target();
- if(mode==DEPS)
- {
- // Skip trivial targets
- if(real!=&tgt)
- return build_depend_table(*real, depth);
- if(const ObjectFile *obj = dynamic_cast<const ObjectFile *>(&tgt))
- return build_depend_table(obj->get_source(), depth);
- }
- else if(mode==REBUILD && !tgt.needs_rebuild())
- /* All targets that depend on to-be-built targets will be rebuilt
- themselves, so we can stop here. */
- return;
-
- TableRow row;
-
- string name;
- const FileTarget *ft = dynamic_cast<const FileTarget *>(&tgt);
- if(full_paths && ft)
- name = ft->get_path().str();
- else
- name = tgt.get_name();
- row.push_back(string(depth*2, ' ')+name);
-
- const Package *pkg = tgt.get_package();
- if(pkg)
- row.push_back(pkg->get_name());
- else
- row.push_back("");
-
- row.push_back(tgt.get_type());
- const Tool *tool = tgt.get_tool();
- if(tool)
- row.push_back(tool->get_tag());
- else
- row.push_back("");
-
- if(tgt.needs_rebuild())
- {
- if(tgt.get_rebuild_reason().empty())
- row.push_back("Yes (no reason)");
- else
- row.push_back(tgt.get_rebuild_reason());
- }
-
- table.push_back(row);
-
- if(!max_depth || depth<max_depth-1)
- {
- Target::Dependencies 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(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
- build_depend_table(**i, depth+1);
- }
-}
-
-void Analyzer::print_table() const
-{
- vector<string::size_type> col_width;
-
- // Determine column widths
- for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
- {
- if(col_width.size()<i->size())
- col_width.resize(i->size(), 0);
- for(unsigned j=0; j<i->size(); ++j)
- col_width[j] = max(col_width[j], (*i)[j].size());
- }
-
- for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
- {
- string line;
- for(unsigned j=0; j<i->size(); ++j)
- {
- if(j>0)
- line += " ";
- line += lexical_cast((*i)[j], Fmt("%-s").width(col_width[j]));
- }
- IO::print("%s\n", line);
- }
-}
-
-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();
-}