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