3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
11 #include <msp/path/path.h>
12 #include <msp/path/utils.h>
16 #include "objectfile.h"
23 Analyzer::Analyzer(Builder &b):
31 Performs the analysis and prints out the resulting dependency tree.
33 void Analyzer::analyze()
36 row.push_back("Name");
37 row.push_back("Package");
38 row.push_back("Type");
39 row.push_back("Rebuild");
42 build_depend_table(*builder.get_target("cmdline"), 0);
48 Adds rows to the table for the given target and it' dependencies.
50 @param tgt Target to be processed
51 @param depth Recursion level of the target (top level is 0)
53 void Analyzer::build_depend_table(Target &tgt, unsigned depth)
55 if(mode!=REBUILD && mode!=ALLDEPS)
57 // Skip trivial targets
58 if(dynamic_cast<ObjectFile *>(&tgt))
59 return build_depend_table(*tgt.get_depends().front(), depth);
60 else if(dynamic_cast<Install *>(&tgt))
61 return build_depend_table(*tgt.get_depends().front(), depth);
63 else if(mode==REBUILD && !tgt.get_rebuild())
64 /* All targets that depend on to-be-built targets will be rebuilt
65 themselves, so we cn stop here. */
74 fn=basename(tgt.get_name());
75 row.push_back(string(depth*2, ' ')+fn);
77 const Package *pkg=tgt.get_package();
79 row.push_back(pkg->get_name());
83 row.push_back(tgt.get_type());
87 if(tgt.get_rebuild_reason().empty())
88 row.push_back("Yes (no reason)");
90 row.push_back(tgt.get_rebuild_reason());
95 if(!max_depth || depth<max_depth-1)
97 const TargetList &depends=tgt.get_depends();
98 //XXX If we want to sort the targets, we need to take the value of full_paths into account
99 //depends.sort(target_order);
100 for(TargetList::const_iterator i=depends.begin(); i!=depends.end(); ++i)
101 build_depend_table(**i, depth+1);
106 Prints out the table that resulted from the analysis.
108 void Analyzer::print_table() const
110 vector<unsigned> col_width;
112 // Determine column widths
113 for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
115 if(col_width.size()<i->size())
116 col_width.resize(i->size(), 0);
117 for(unsigned j=0; j<i->size(); ++j)
118 col_width[j]=max(col_width[j], (*i)[j].size());
121 for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
125 for(unsigned j=0; j<i->size(); ++j)
129 ss<<setw(col_width[j])<<(*i)[j];
131 cout<<ss.str()<<'\n';
135 bool Analyzer::target_order(Target *t1, Target *t2)
136 { return t1->get_name()<t2->get_name(); }