]> git.tdb.fi Git - builder.git/blob - source/analyzer.cpp
21ed52cec64b2cf463a62a1d9823870ea5917283
[builder.git] / source / analyzer.cpp
1 #include <iomanip>
2 #include <iostream>
3 #include <sstream>
4 #include <msp/path/path.h>
5 #include "analyzer.h"
6 #include "builder.h"
7 #include "install.h"
8 #include "objectfile.h"
9 #include "package.h"
10 #include "target.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 Analyzer::Analyzer(Builder &b):
16         builder(b),
17         max_depth(0),
18         full_paths(false)
19 { }
20
21 void Analyzer::analyze()
22 {
23         TableRow row;
24         row.push_back("Name");
25         row.push_back("Package");
26         row.push_back("Type");
27         row.push_back("Rebuild");
28         table.push_back(row);
29         
30         build_depend_table(*builder.get_target("cmdline"), 0);
31
32         print_table();
33 }
34
35 void Analyzer::build_depend_table(Target &tgt, unsigned depth)
36 {
37         if(mode!=REBUILD && mode!=ALLDEPS)
38         {
39                 if(dynamic_cast<ObjectFile *>(&tgt))
40                         return build_depend_table(*tgt.get_depends().front(), depth);
41                 else if(dynamic_cast<Install *>(&tgt))
42                         return build_depend_table(*tgt.get_depends().front(), depth);
43         }
44         else if(mode==REBUILD && !tgt.get_rebuild())
45                 return;
46         
47         TableRow row;
48
49         string fn;
50         if(full_paths)
51                 fn=tgt.get_name();
52         else
53                 fn=Path::Path(tgt.get_name())[-1];
54         row.push_back(string(depth*2, ' ')+fn);
55
56         const Package *pkg=tgt.get_package();
57         if(pkg)
58                 row.push_back(pkg->get_name());
59         else
60                 row.push_back("");
61         
62         row.push_back(tgt.get_type());
63         
64         if(tgt.get_rebuild())
65         {
66                 if(tgt.get_rebuild_reason().empty())
67                         row.push_back("Yes (no reason)");
68                 else
69                         row.push_back(tgt.get_rebuild_reason());
70         }
71
72         table.push_back(row);
73
74         if(!max_depth || depth<max_depth-1)
75         {
76                 const TargetList &depends=tgt.get_depends();
77                 //depends.sort(target_order);
78                 for(TargetList::const_iterator i=depends.begin(); i!=depends.end(); ++i)
79                         build_depend_table(**i, depth+1);
80         }
81 }
82
83 void Analyzer::print_table() const
84 {
85         vector<unsigned> col_width;
86
87         for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
88         {
89                 if(col_width.size()<i->size())
90                         col_width.resize(i->size(), 0);
91                 for(unsigned j=0; j<i->size(); ++j)
92                         col_width[j]=max(col_width[j], (*i)[j].size());
93         }
94
95         for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
96         {
97                 ostringstream ss;
98                 ss<<left;
99                 for(unsigned j=0; j<i->size(); ++j)
100                 {
101                         if(j>0)
102                                 ss<<"  ";
103                         ss<<setw(col_width[j])<<(*i)[j];
104                 }
105                 cout<<ss.str()<<'\n';
106         }
107 }
108
109 bool Analyzer::target_order(Target *t1, Target *t2)
110 { return t1->get_name()<t2->get_name(); }