]> git.tdb.fi Git - builder.git/blob - source/analyzer.cpp
984af61be7167b9ad06e47abdd4838e3e4bbb521
[builder.git] / source / analyzer.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/fs/utils.h>
9 #include <msp/io/print.h>
10 #include "analyzer.h"
11 #include "builder.h"
12 #include "install.h"
13 #include "objectfile.h"
14 #include "package.h"
15 #include "sourcefile.h"
16 #include "target.h"
17
18 using namespace std;
19 using namespace Msp;
20
21 Analyzer::Analyzer(Builder &b):
22         builder(b),
23         mode(DEPS),
24         max_depth(0),
25         full_paths(false)
26 { }
27
28 void Analyzer::analyze()
29 {
30         if(mode==RDEPS)
31         {
32                 rdepends.clear();
33                 const TargetMap &targets = builder.get_targets();
34                 for(TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
35                 {
36                         const TargetList &depends = i->second->get_depends();
37                         for(TargetList::const_iterator j=depends.begin(); j!=depends.end(); ++j)
38                                 rdepends[*j].insert(i->second);
39                 }
40         }
41
42         table.clear();
43
44         TableRow row;
45         row.push_back("Name");
46         row.push_back("Package");
47         row.push_back("Type");
48         row.push_back("Rebuild");
49         table.push_back(row);
50         
51         Target &cmdline = *builder.get_target("cmdline");
52         if(mode==RDEPS)
53         {
54                 const TargetList &deps = cmdline.get_depends();
55                 for(TargetList::const_iterator i=deps.begin(); i!=deps.end(); ++i)
56                         build_depend_table(**i, 0);
57         }
58         else
59                 build_depend_table(cmdline, 0);
60
61         print_table();
62 }
63
64 void Analyzer::build_depend_table(Target &tgt, unsigned depth)
65 {
66         Target *real = tgt.get_real_target();
67         if(mode==DEPS)
68         {
69                 // Skip trivial targets
70                 if(real!=&tgt)
71                         return build_depend_table(*real, depth);
72                 if(const ObjectFile *obj = dynamic_cast<const ObjectFile *>(&tgt))
73                         return build_depend_table(obj->get_source(), depth);
74         }
75         else if(mode==REBUILD && !tgt.get_rebuild() && !real->get_rebuild())
76                 /* All targets that depend on to-be-built targets will be rebuilt
77                 themselves, so we can stop here. */
78                 return;
79         
80         TableRow row;
81
82         string name;
83         const FileTarget *ft = dynamic_cast<const FileTarget *>(&tgt);
84         if(full_paths && ft)
85                 name = ft->get_path().str();
86         else
87                 name = tgt.get_name();
88         row.push_back(string(depth*2, ' ')+name);
89
90         const Package *pkg = tgt.get_package();
91         if(pkg)
92                 row.push_back(pkg->get_name());
93         else
94                 row.push_back("");
95         
96         row.push_back(tgt.get_type());
97         
98         if(tgt.get_rebuild())
99         {
100                 if(tgt.get_rebuild_reason().empty())
101                         row.push_back("Yes (no reason)");
102                 else
103                         row.push_back(tgt.get_rebuild_reason());
104         }
105
106         table.push_back(row);
107
108         if(!max_depth || depth<max_depth-1)
109         {
110                 TargetList depends;
111                 if(mode==RDEPS)
112                 {
113                         const set<Target *> &rdeps = rdepends[&tgt];
114                         depends.assign(rdeps.begin(), rdeps.end());
115                 }
116                 else
117                         depends = tgt.get_depends();
118
119                 depends.sort(full_paths ? target_order_full : target_order);
120
121                 for(TargetList::const_iterator i=depends.begin(); i!=depends.end(); ++i)
122                         build_depend_table(**i, depth+1);
123         }
124 }
125
126 void Analyzer::print_table() const
127 {
128         vector<string::size_type> col_width;
129
130         // Determine column widths
131         for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
132         {
133                 if(col_width.size()<i->size())
134                         col_width.resize(i->size(), 0);
135                 for(unsigned j=0; j<i->size(); ++j)
136                         col_width[j] = max(col_width[j], (*i)[j].size());
137         }
138
139         for(Table::const_iterator i=table.begin(); i!=table.end(); ++i)
140         {
141                 string line;
142                 for(unsigned j=0; j<i->size(); ++j)
143                 {
144                         if(j>0)
145                                 line += "  ";
146                         line += lexical_cast((*i)[j], Fmt("%-s").width(col_width[j]));
147                 }
148                 IO::print("%s\n", line);
149         }
150 }
151
152 bool Analyzer::target_order(const Target *t1, const Target *t2)
153 {
154         return t1->get_name()<t2->get_name();
155 }
156
157 bool Analyzer::target_order_full(const Target *t1, const Target *t2)
158 {
159         const FileTarget *ft1 = dynamic_cast<const FileTarget *>(t1);
160         const FileTarget *ft2 = dynamic_cast<const FileTarget *>(t2);
161         if(!ft1)
162         {
163                 if(ft2)
164                         return true;
165                 return target_order(t1, t2);
166         }
167         else if(!ft2)
168                 return false;
169         return ft1->get_path().str()<ft2->get_path().str();
170 }