]> git.tdb.fi Git - builder.git/blob - source/target.cpp
Don't print the configuration header in help for empty configuration
[builder.git] / source / target.cpp
1 #include <algorithm>
2 #include <msp/fs/stat.h>
3 #include <msp/fs/utils.h>
4 #include <msp/strings/format.h>
5 #include "builder.h"
6 #include "filetarget.h"
7 #include "sourcepackage.h"
8 #include "target.h"
9 #include "task.h"
10 #include "tool.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 Target::Target(Builder &b, const string &n):
16         builder(b),
17         package(0),
18         component(0),
19         name(n),
20         tool(0),
21         state(INIT),
22         primary_target(0)
23 {
24         builder.get_build_graph().add_target(this);
25 }
26
27 void Target::add_dependency(Target &dep)
28 {
29         if(&dep==this)
30                 throw invalid_argument("Target::add_depend");
31         depends.push_back(&dep);
32         if(state>PREPARING)
33                 dep.signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
34 }
35
36 void Target::add_transitive_dependency(Target &dep)
37 {
38         if(&dep==this)
39                 throw invalid_argument("Target::add_transitive_dependency");
40         trans_depends.push_back(&dep);
41 }
42
43 void Target::add_side_effect(Target &se)
44 {
45         side_effects.push_back(&se);
46         if(tool)
47                 se.set_tool(*tool);
48         se.primary_target = this;
49         /* Side effects are checked for rebuild after the primary target.  Recheck
50         the primary if a side effect is marked for rebuild. */
51         se.signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
52 }
53
54 Target *Target::get_buildable_target()
55 {
56         if(primary_target)
57                 return primary_target->get_buildable_target();
58         if(!needs_rebuild())
59                 return 0;
60
61         bool self_ok = state!=BUILDING;
62         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
63         {
64                 // Avoid infinite recursion if a target repends on its own side effect
65                 if(find(side_effects.begin(), side_effects.end(), *i)!=side_effects.end())
66                         continue;
67
68                 Target *tgt = (*i)->get_buildable_target();
69                 if(tgt)
70                         return tgt;
71                 else if((*i)->needs_rebuild())
72                         self_ok = false;
73         }
74
75         if(self_ok)
76                 return this;
77
78         return 0;
79 }
80
81 void Target::set_tool(Tool &t)
82 {
83         tool = &t;
84         for(Dependencies::const_iterator i=side_effects.begin(); i!=side_effects.end(); ++i)
85                 (*i)->set_tool(t);
86 }
87
88 void Target::force_rebuild()
89 {
90         if(!is_buildable())
91                 throw logic_error("Target::force_rebuild");
92         mark_rebuild("Forced rebuild");
93 }
94
95 void Target::mark_rebuild(const string &reason)
96 {
97         if(reason.empty())
98                 throw invalid_argument("No reason given for rebuilding "+name);
99
100         state = REBUILD;
101         rebuild_reason = reason;
102
103         builder.get_logger().log("rebuild", format("Rebuilding %s: %s", name, reason));
104
105         signal_bubble_rebuild.emit();
106 }
107
108 void Target::prepare()
109 {
110         if(state>PREPARING)
111                 return;
112         if(state==PREPARING)
113         {
114                 problems.push_back("Dependency cycle detected");
115                 state = BROKEN;
116                 return;
117         }
118
119         state = PREPARING;
120         if(tool)
121                 tool->prepare();
122
123         find_dependencies();
124         bool broken = !problems.empty();
125
126         if(tool)
127         {
128                 if(FileTarget *tool_exe = tool->get_executable())
129                         add_dependency(*tool_exe);
130                 broken |= !tool->get_problems().empty();
131
132                 // Only check package and component problems for buildable targets
133                 // XXX How to propagate nested package problems?
134                 broken |= (package && !package->get_problems().empty());
135                 broken |= (component && !component->get_problems().empty());
136         }
137
138         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
139         {
140                 (*i)->prepare();
141                 broken |= (*i)->is_broken();
142         }
143
144         check_rebuild();
145         if(broken)
146                 state = BROKEN;
147         else if(state==PREPARING)
148                 state = UPTODATE;
149
150         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
151                 (*i)->signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
152 }
153
154 Task *Target::build()
155 {
156         if(primary_target)
157                 return primary_target->build();
158
159         Task *task = tool->run(*this);
160         task->signal_finished.connect(sigc::mem_fun(this, &Target::build_finished));
161         state = BUILDING;
162
163         build(*task);
164         for(Dependencies::const_iterator i=side_effects.begin(); i!=side_effects.end(); ++i)
165                 (*i)->build(*task);
166
167         return task;
168 }
169
170 void Target::build_finished(bool success)
171 {
172         state = UPTODATE;
173         if(success)
174         {
175                 modified();
176                 for(Dependencies::const_iterator i=side_effects.begin(); i!=side_effects.end(); ++i)
177                         (*i)->build_finished(success);
178                 signal_modified.emit();
179         }
180 }