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