3 This file is part of builder
4 Copyright © 2006-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/fs/utils.h>
9 #include <msp/io/print.h>
13 #include "objectfile.h"
15 #include "sourcefile.h"
21 Analyzer::Analyzer(Builder &b):
28 void Analyzer::analyze()
33 const TargetMap &targets = builder.get_targets();
34 for(TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
36 const TargetList &depends = i->second->get_depends();
37 for(TargetList::const_iterator j=depends.begin(); j!=depends.end(); ++j)
38 rdepends[*j].insert(i->second);
45 row.push_back("Name");
46 row.push_back("Package");
47 row.push_back("Type");
48 row.push_back("Rebuild");
51 const Target &cmdline = *builder.get_target("cmdline");
54 const TargetList &deps = cmdline.get_depends();
55 for(TargetList::const_iterator i=deps.begin(); i!=deps.end(); ++i)
56 build_depend_table(**i, 0);
59 build_depend_table(cmdline, 0);
64 void Analyzer::build_depend_table(const Target &tgt, unsigned depth)
68 // Skip trivial targets
69 if(const ObjectFile *obj = dynamic_cast<const ObjectFile *>(&tgt))
70 return build_depend_table(obj->get_source(), depth);
71 else if(const Install *inst = dynamic_cast<const Install *>(&tgt))
72 return build_depend_table(inst->get_source(), depth);
74 else if(mode==REBUILD && !tgt.get_rebuild())
75 /* All targets that depend on to-be-built targets will be rebuilt
76 themselves, so we cn stop here. */
82 const FileTarget *ft = dynamic_cast<const FileTarget *>(&tgt);
84 name = ft->get_path().str();
86 name = tgt.get_name();
87 row.push_back(string(depth*2, ' ')+name);
89 const Package *pkg = tgt.get_package();
91 row.push_back(pkg->get_name());
95 row.push_back(tgt.get_type());
99 if(tgt.get_rebuild_reason().empty())
100 row.push_back("Yes (no reason)");
102 row.push_back(tgt.get_rebuild_reason());
105 table.push_back(row);
107 if(!max_depth || depth<max_depth-1)
112 const set<Target *> &rdeps = rdepends[&tgt];
113 depends.assign(rdeps.begin(), rdeps.end());
116 depends = tgt.get_depends();
118 depends.sort(full_paths ? target_order_full : target_order);
120 for(TargetList::const_iterator i=depends.begin(); i!=depends.end(); ++i)
121 build_depend_table(**i, depth+1);
125 void Analyzer::print_table() const
127 vector<string::size_type> col_width;
129 // Determine column widths
130 for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
132 if(col_width.size()<i->size())
133 col_width.resize(i->size(), 0);
134 for(unsigned j=0; j<i->size(); ++j)
135 col_width[j] = max(col_width[j], (*i)[j].size());
138 for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
141 for(unsigned j=0; j<i->size(); ++j)
145 line += lexical_cast((*i)[j], Fmt("%-s").width(col_width[j]));
147 IO::print("%s\n", line);
151 bool Analyzer::target_order(const Target *t1, const Target *t2)
153 return t1->get_name()<t2->get_name();
156 bool Analyzer::target_order_full(const Target *t1, const Target *t2)
158 const FileTarget *ft1 = dynamic_cast<const FileTarget *>(t1);
159 const FileTarget *ft2 = dynamic_cast<const FileTarget *>(t2);
164 return target_order(t1, t2);
168 return ft1->get_path().str()<ft2->get_path().str();