]> git.tdb.fi Git - builder.git/blob - source/analyzer.cpp
0db55c76b637942ba997bff9b2ee90d05237e58b
[builder.git] / source / analyzer.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <iomanip>
9 #include <iostream>
10 #include <sstream>
11 #include <msp/path/path.h>
12 #include "analyzer.h"
13 #include "builder.h"
14 #include "install.h"
15 #include "objectfile.h"
16 #include "package.h"
17 #include "target.h"
18
19 using namespace std;
20 using namespace Msp;
21
22 Analyzer::Analyzer(Builder &b):
23         builder(b),
24         mode(DEPS),
25         max_depth(0),
26         full_paths(false)
27 { }
28
29 /**
30 Performs the analysis and prints out the resulting dependency tree.
31 */
32 void Analyzer::analyze()
33 {
34         TableRow row;
35         row.push_back("Name");
36         row.push_back("Package");
37         row.push_back("Type");
38         row.push_back("Rebuild");
39         table.push_back(row);
40         
41         build_depend_table(*builder.get_target("cmdline"), 0);
42
43         print_table();
44 }
45
46 /**
47 Adds rows to the table for the given target and it' dependencies.
48
49 @param   tgt    Target to be processed
50 @param   depth  Recursion level of the target (top level is 0)
51 */
52 void Analyzer::build_depend_table(Target &tgt, unsigned depth)
53 {
54         if(mode!=REBUILD && mode!=ALLDEPS)
55         {
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);
61         }
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. */
65                 return;
66         
67         TableRow row;
68
69         string fn;
70         if(full_paths)
71                 fn=tgt.get_name();
72         else
73                 fn=Path::Path(tgt.get_name())[-1];
74         row.push_back(string(depth*2, ' ')+fn);
75
76         const Package *pkg=tgt.get_package();
77         if(pkg)
78                 row.push_back(pkg->get_name());
79         else
80                 row.push_back("");
81         
82         row.push_back(tgt.get_type());
83         
84         if(tgt.get_rebuild())
85         {
86                 if(tgt.get_rebuild_reason().empty())
87                         row.push_back("Yes (no reason)");
88                 else
89                         row.push_back(tgt.get_rebuild_reason());
90         }
91
92         table.push_back(row);
93
94         if(!max_depth || depth<max_depth-1)
95         {
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);
101         }
102 }
103
104 /**
105 Prints out the table that resulted from the analysis.
106 */
107 void Analyzer::print_table() const
108 {
109         vector<unsigned> col_width;
110
111         // Determine column widths
112         for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
113         {
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());
118         }
119
120         for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
121         {
122                 ostringstream ss;
123                 ss<<left;
124                 for(unsigned j=0; j<i->size(); ++j)
125                 {
126                         if(j>0)
127                                 ss<<"  ";
128                         ss<<setw(col_width[j])<<(*i)[j];
129                 }
130                 cout<<ss.str()<<'\n';
131         }
132 }
133
134 bool Analyzer::target_order(Target *t1, Target *t2)
135 { return t1->get_name()<t2->get_name(); }