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::collect_build_info(BuildInfo &binfo) const
91 binfo.update_from(tool->get_build_info());
93 binfo.update_from(component->get_build_info());
95 binfo.update_from(package->get_build_info());
98 void Target::force_rebuild()
101 throw logic_error("Target::force_rebuild");
102 mark_rebuild("Forced rebuild");
105 void Target::mark_rebuild(const string &reason)
108 throw invalid_argument("No reason given for rebuilding "+name);
111 rebuild_reason = reason;
113 builder.get_logger().log("rebuild", format("Rebuilding %s: %s", name, reason));
115 signal_bubble_rebuild.emit();
118 void Target::prepare()
124 problems.push_back("Dependency cycle detected");
134 bool broken = !problems.empty();
138 if(FileTarget *tool_exe = tool->get_executable())
139 add_dependency(*tool_exe);
140 broken |= !tool->get_problems().empty();
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());
148 for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
151 broken |= (*i)->is_broken();
153 for(Dependencies::iterator i=trans_depends.begin(); i!=trans_depends.end(); ++i)
159 else if(state==PREPARING)
162 for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
163 (*i)->signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
166 Task *Target::build()
169 return primary_target->build();
171 Task *task = tool->run(*this);
172 task->signal_finished.connect(sigc::mem_fun(this, &Target::build_finished));
176 for(Dependencies::const_iterator i=side_effects.begin(); i!=side_effects.end(); ++i)
182 void Target::build_finished(bool success)
188 for(Dependencies::const_iterator i=side_effects.begin(); i!=side_effects.end(); ++i)
189 (*i)->build_finished(success);
190 signal_modified.emit();