1 #include <msp/fs/utils.h>
2 #include <msp/io/print.h>
6 #include "objectfile.h"
8 #include "sourcefile.h"
15 Analyzer::Analyzer(Builder &b):
22 void Analyzer::analyze()
27 const Builder::TargetMap &targets = builder.get_targets();
28 for(Builder::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
30 const TargetList &depends = i->second->get_depends();
31 for(TargetList::const_iterator j=depends.begin(); j!=depends.end(); ++j)
32 rdepends[*j].insert(i->second);
39 row.push_back("Name");
40 row.push_back("Package");
41 row.push_back("Type");
42 row.push_back("Tool");
43 row.push_back("Rebuild");
46 Target &cmdline = *builder.get_target("cmdline");
49 const TargetList &deps = cmdline.get_depends();
50 for(TargetList::const_iterator i=deps.begin(); i!=deps.end(); ++i)
51 build_depend_table(**i, 0);
54 build_depend_table(cmdline, 0);
59 void Analyzer::build_depend_table(Target &tgt, unsigned depth)
61 Target *real = tgt.get_real_target();
64 // Skip trivial targets
66 return build_depend_table(*real, depth);
67 if(const ObjectFile *obj = dynamic_cast<const ObjectFile *>(&tgt))
68 return build_depend_table(obj->get_source(), depth);
70 else if(mode==REBUILD && !tgt.get_rebuild() && !real->get_rebuild())
71 /* All targets that depend on to-be-built targets will be rebuilt
72 themselves, so we can stop here. */
78 const FileTarget *ft = dynamic_cast<const FileTarget *>(&tgt);
80 name = ft->get_path().str();
82 name = tgt.get_name();
83 row.push_back(string(depth*2, ' ')+name);
85 const Package *pkg = tgt.get_package();
87 row.push_back(pkg->get_name());
91 row.push_back(tgt.get_type());
92 const Tool *tool = tgt.get_tool();
94 row.push_back(tool->get_tag());
100 if(tgt.get_rebuild_reason().empty())
101 row.push_back("Yes (no reason)");
103 row.push_back(tgt.get_rebuild_reason());
106 table.push_back(row);
108 if(!max_depth || depth<max_depth-1)
113 const set<Target *> &rdeps = rdepends[&tgt];
114 depends.assign(rdeps.begin(), rdeps.end());
117 depends = tgt.get_depends();
119 depends.sort(full_paths ? target_order_full : target_order);
121 for(TargetList::const_iterator i=depends.begin(); i!=depends.end(); ++i)
122 build_depend_table(**i, depth+1);
126 void Analyzer::print_table() const
128 vector<string::size_type> col_width;
130 // Determine column widths
131 for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
133 if(col_width.size()<i->size())
134 col_width.resize(i->size(), 0);
135 for(unsigned j=0; j<i->size(); ++j)
136 col_width[j] = max(col_width[j], (*i)[j].size());
139 for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
142 for(unsigned j=0; j<i->size(); ++j)
146 line += lexical_cast((*i)[j], Fmt("%-s").width(col_width[j]));
148 IO::print("%s\n", line);
152 bool Analyzer::target_order(const Target *t1, const Target *t2)
154 return t1->get_name()<t2->get_name();
157 bool Analyzer::target_order_full(const Target *t1, const Target *t2)
159 const FileTarget *ft1 = dynamic_cast<const FileTarget *>(t1);
160 const FileTarget *ft2 = dynamic_cast<const FileTarget *>(t2);
165 return target_order(t1, t2);
169 return ft1->get_path().str()<ft2->get_path().str();