3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
11 #include <msp/fs/utils.h>
15 #include "objectfile.h"
22 Analyzer::Analyzer(Builder &b):
30 Performs the analysis and prints out the resulting dependency tree.
32 void Analyzer::analyze()
35 row.push_back("Name");
36 row.push_back("Package");
37 row.push_back("Type");
38 row.push_back("Rebuild");
41 build_depend_table(*builder.get_target("cmdline"), 0);
47 Adds rows to the table for the given target and it' dependencies.
49 @param tgt Target to be processed
50 @param depth Recursion level of the target (top level is 0)
52 void Analyzer::build_depend_table(Target &tgt, unsigned depth)
54 if(mode!=REBUILD && mode!=ALLDEPS)
56 // Skip trivial targets
57 if(dynamic_cast<ObjectFile *>(&tgt))
58 return build_depend_table(*tgt.get_depends().front(), depth);
59 else if(dynamic_cast<Install *>(&tgt))
60 return build_depend_table(*tgt.get_depends().front(), depth);
62 else if(mode==REBUILD && !tgt.get_rebuild())
63 /* All targets that depend on to-be-built targets will be rebuilt
64 themselves, so we cn stop here. */
73 fn=FS::basename(tgt.get_name());
74 row.push_back(string(depth*2, ' ')+fn);
76 const Package *pkg=tgt.get_package();
78 row.push_back(pkg->get_name());
82 row.push_back(tgt.get_type());
86 if(tgt.get_rebuild_reason().empty())
87 row.push_back("Yes (no reason)");
89 row.push_back(tgt.get_rebuild_reason());
94 if(!max_depth || depth<max_depth-1)
96 const TargetList &depends=tgt.get_depends();
97 //XXX If we want to sort the targets, we need to take the value of full_paths into account
98 //depends.sort(target_order);
99 for(TargetList::const_iterator i=depends.begin(); i!=depends.end(); ++i)
100 build_depend_table(**i, depth+1);
105 Prints out the table that resulted from the analysis.
107 void Analyzer::print_table() const
109 vector<unsigned> col_width;
111 // Determine column widths
112 for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
114 if(col_width.size()<i->size())
115 col_width.resize(i->size(), 0);
116 for(unsigned j=0; j<i->size(); ++j)
117 col_width[j]=max(col_width[j], (*i)[j].size());
120 for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
124 for(unsigned j=0; j<i->size(); ++j)
128 ss<<setw(col_width[j])<<(*i)[j];
130 cout<<ss.str()<<'\n';
134 bool Analyzer::target_order(Target *t1, Target *t2)
135 { return t1->get_name()<t2->get_name(); }