]> git.tdb.fi Git - builder.git/blob - source/target.cpp
Explicitly add -lmingw32 before any other libraries
[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::collect_build_info(BuildInfo &binfo) const
89 {
90         if(tool)
91                 binfo.update_from(tool->get_build_info());
92         if(component)
93                 binfo.update_from(component->get_build_info());
94         else if(package)
95                 binfo.update_from(package->get_build_info());
96 }
97
98 void Target::force_rebuild()
99 {
100         if(!is_buildable())
101                 throw logic_error("Target::force_rebuild");
102         mark_rebuild("Forced rebuild");
103 }
104
105 void Target::mark_rebuild(const string &reason)
106 {
107         if(reason.empty())
108                 throw invalid_argument("No reason given for rebuilding "+name);
109
110         state = REBUILD;
111         rebuild_reason = reason;
112
113         builder.get_logger().log("rebuild", format("Rebuilding %s: %s", name, reason));
114
115         signal_bubble_rebuild.emit();
116 }
117
118 void Target::prepare()
119 {
120         if(state>PREPARING)
121                 return;
122         if(state==PREPARING)
123         {
124                 problems.push_back("Dependency cycle detected");
125                 state = BROKEN;
126                 return;
127         }
128
129         state = PREPARING;
130         if(tool)
131                 tool->prepare();
132
133         find_dependencies();
134         bool broken = !problems.empty();
135
136         if(tool)
137         {
138                 if(FileTarget *tool_exe = tool->get_executable())
139                         add_dependency(*tool_exe);
140                 broken |= !tool->get_problems().empty();
141
142                 // Only check package and component problems for buildable targets
143                 // XXX How to propagate nested package problems?
144                 broken |= (package && !package->get_problems().empty());
145                 broken |= (component && !component->get_problems().empty());
146         }
147
148         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
149         {
150                 (*i)->prepare();
151                 broken |= (*i)->is_broken();
152         }
153         for(Dependencies::iterator i=trans_depends.begin(); i!=trans_depends.end(); ++i)
154                 (*i)->prepare();
155
156         check_rebuild();
157         if(broken)
158                 state = BROKEN;
159         else if(state==PREPARING)
160                 state = UPTODATE;
161
162         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
163                 (*i)->signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
164 }
165
166 Task *Target::build()
167 {
168         if(primary_target)
169                 return primary_target->build();
170
171         Task *task = tool->run(*this);
172         task->signal_finished.connect(sigc::mem_fun(this, &Target::build_finished));
173         state = BUILDING;
174
175         build(*task);
176         for(Dependencies::const_iterator i=side_effects.begin(); i!=side_effects.end(); ++i)
177                 (*i)->build(*task);
178
179         return task;
180 }
181
182 void Target::build_finished(bool success)
183 {
184         state = UPTODATE;
185         if(success)
186         {
187                 modified();
188                 for(Dependencies::const_iterator i=side_effects.begin(); i!=side_effects.end(); ++i)
189                         (*i)->build_finished(success);
190                 signal_modified.emit();
191         }
192 }