2 #include <msp/fs/stat.h>
3 #include <msp/fs/utils.h>
4 #include <msp/strings/format.h>
6 #include "filetarget.h"
7 #include "sourcepackage.h"
15 Target::Target(Builder &b, const string &n):
24 builder.get_build_graph().add_target(this);
27 void Target::add_dependency(Target &dep)
30 throw invalid_argument("Target::add_depend");
31 depends.push_back(&dep);
33 dep.signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
36 void Target::add_transitive_dependency(Target &dep)
39 throw invalid_argument("Target::add_transitive_dependency");
40 trans_depends.push_back(&dep);
43 void Target::add_side_effect(Target &se)
45 side_effects.push_back(&se);
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));
54 Target *Target::get_buildable_target()
57 return primary_target->get_buildable_target();
61 bool self_ok = state!=BUILDING;
62 for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
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())
68 Target *tgt = (*i)->get_buildable_target();
71 else if((*i)->needs_rebuild())
81 void Target::set_tool(Tool &t)
84 for(Dependencies::const_iterator i=side_effects.begin(); i!=side_effects.end(); ++i)
88 void Target::force_rebuild()
91 throw logic_error("Target::force_rebuild");
92 mark_rebuild("Forced rebuild");
95 void Target::mark_rebuild(const string &reason)
98 throw invalid_argument("No reason given for rebuilding "+name);
101 rebuild_reason = reason;
103 builder.get_logger().log("rebuild", format("Rebuilding %s: %s", name, reason));
105 signal_bubble_rebuild.emit();
108 void Target::prepare()
114 problems.push_back("Dependency cycle detected");
124 bool broken = !problems.empty();
128 if(FileTarget *tool_exe = tool->get_executable())
129 add_dependency(*tool_exe);
130 broken |= !tool->get_problems().empty();
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());
138 for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
141 broken |= (*i)->is_broken();
147 else if(state==PREPARING)
150 for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
151 (*i)->signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
154 Task *Target::build()
157 return primary_target->build();
159 Task *task = tool->run(*this);
160 task->signal_finished.connect(sigc::mem_fun(this, &Target::build_finished));
164 for(Dependencies::const_iterator i=side_effects.begin(); i!=side_effects.end(); ++i)
170 void Target::build_finished(bool success)
176 for(Dependencies::const_iterator i=side_effects.begin(); i!=side_effects.end(); ++i)
177 (*i)->build_finished(success);
178 signal_modified.emit();