+#include <msp/fs/stat.h>
+#include <msp/fs/utils.h>
+#include <msp/strings/format.h>
#include "builder.h"
+#include "filetarget.h"
+#include "sourcepackage.h"
#include "target.h"
+#include "task.h"
+#include "tool.h"
using namespace std;
+using namespace Msp;
-Target *TargetRef::get_target()
+Target::Target(Builder &b, const string &n):
+ builder(b),
+ package(0),
+ component(0),
+ name(n),
+ tool(0),
+ state(INIT)
{
- if(!target)
- target=builder.get_target(name);
- return target;
+ builder.add_target(this);
}
Target *Target::get_buildable_target()
{
- if(rebuild && ready_for_build && !building)
- return this;
+ if(!needs_rebuild())
+ return 0;
- for(list<Target *>::iterator i=depends.begin(); i!=depends.end(); ++i)
+ bool self_ok = state!=BUILDING;
+ for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
{
- Target *tgt=(*i)->get_buildable_target();
+ Target *tgt = (*i)->get_buildable_target();
if(tgt)
return tgt;
+ else if((*i)->needs_rebuild())
+ self_ok = false;
}
+ if(self_ok)
+ return this;
+
return 0;
}
-void Target::add_depend(Target *dep)
+void Target::set_tool(const Tool &t)
{
- depends.push_back(dep);
- dep->rdepends.push_back(this);
+ tool = &t;
}
-Target::Target(Builder &b, const Package *p, const string &n):
- builder(b),
- package(p),
- name(n),
- building(false),
- rebuild(false),
- ready_for_build(false)
-{ }
+void Target::force_rebuild()
+{
+ if(!is_buildable())
+ throw logic_error("Target::force_rebuild");
+ mark_rebuild("Forced rebuild");
+}
+
+void Target::add_depend(Target &dep)
+{
+ if(&dep==this)
+ throw invalid_argument("Target::add_depend");
+ depends.push_back(&dep);
+ if(state>PREPARING)
+ dep.signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
+}
+
+void Target::prepare()
+{
+ if(state>PREPARING)
+ return;
+ if(state==PREPARING)
+ {
+ builder.problem((package ? package->get_name() : string()), "Dependency cycle detected at "+name);
+ return;
+ }
+
+ state = PREPARING;
+ find_depends();
+ if(tool)
+ {
+ if(FileTarget *tool_exe = tool->get_executable())
+ add_depend(*tool_exe);
+ }
+
+ for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
+ (*i)->prepare();
+
+ check_rebuild();
+ if(state==PREPARING)
+ state = UPTODATE;
+
+ for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
+ (*i)->signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
+}
+
+Task *Target::build()
+{
+ if(!tool)
+ {
+ // This special case is needed for VirtualTargets
+ state = UPTODATE;
+ return 0;
+ }
+
+ // XXX Minor breach of OO here
+ if(FileTarget *ft = dynamic_cast<FileTarget *>(this))
+ if(!builder.get_dry_run() && FS::exists(ft->get_path()))
+ FS::unlink(ft->get_path());
+
+ Task *task = tool->run(*this);
+ task->signal_finished.connect(sigc::mem_fun(this, &Target::build_finished));
+ state = BUILDING;
+
+ return task;
+}
+
+void Target::mark_rebuild(const string &reason)
+{
+ if(reason.empty())
+ throw invalid_argument("No reason given for rebuilding "+name);
+
+ state = REBUILD;
+ rebuild_reason = reason;
+
+ builder.get_logger().log("rebuild", format("Rebuilding %s: %s", name, reason));
+
+ signal_bubble_rebuild.emit();
+}
-void Target::mark_rebuild(const std::string &reason)
+void Target::build_finished(bool /*success*/)
{
- rebuild=true;
- rebuild_reason=reason;
+ state = UPTODATE;
}