X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftarget.cpp;h=6ba64717e4272405fb79b8a008df471dd9f57b4c;hb=bd2a50ecb9f582c6e9569ffc9f33d41f10363c5f;hp=8ba3cc38896faefc3493329da6a2570dfd83a268;hpb=59ac0a44d6edf179c01604c6ced744873213f855;p=builder.git diff --git a/source/target.cpp b/source/target.cpp index 8ba3cc3..6ba6471 100644 --- a/source/target.cpp +++ b/source/target.cpp @@ -1,97 +1,112 @@ -#include -#include "action.h" +#include +#include #include "builder.h" +#include "filetarget.h" +#include "package.h" #include "target.h" +#include "task.h" +#include "tool.h" using namespace std; using namespace Msp; +Target::Target(Builder &b, const Package *p, const string &n): + builder(b), + package(p), + name(n), + tool(0), + buildable(false), + building(false), + rebuild(false), + deps_ready(false), + preparing(false), + prepared(false) +{ + builder.add_target(this); +} + Target *Target::get_buildable_target() { - bool self_ok=true; - for(list::iterator i=depends.begin(); i!=depends.end(); ++i) + if(!rebuild) + return 0; + + bool self_ok = !building; + for(TargetList::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)->get_rebuild()) - self_ok=false; + self_ok = false; } - if(self_ok && rebuild && !building) + if(self_ok) return this; return 0; } +void Target::set_tool(const Tool &t) +{ + tool = &t; +} + void Target::add_depend(Target *dep) { + if(dep==this) + throw invalid_argument("Target::add_depend"); depends.push_back(dep); - dep->rdepends.push_back(this); } void Target::prepare() { if(prepared) return; + if(preparing) + { + builder.problem((package ? package->get_name() : string()), "Dependency cycle detected at "+name); + return; + } - for(list::iterator i=depends.begin(); i!=depends.end(); ++i) + preparing = true; + for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i) (*i)->prepare(); check_rebuild(); + preparing = false; + prepared = true; } -Target::Target(Builder &b, const Package *p, const string &n): - builder(b), - package(p), - name(n), - building(false), - rebuild(false), - prepared(false), - buildable(false) +Task *Target::build() { - struct stat st; - if(!Path::stat(name, st)) + if(!buildable) { - mtime=Time::TimeStamp::from_unixtime(st.st_mtime); - vmtime=mtime; + rebuild = false; + return 0; } -} + if(!tool) + throw logic_error("buildable && !tool"); -void Target::mark_rebuild(const std::string &reason) -{ - rebuild=true; - rebuild_reason=reason; -} + // XXX Minor breach of OO here + if(FileTarget *ft = dynamic_cast(this)) + if(!builder.get_dry_run() && FS::exists(ft->get_path())) + FS::unlink(ft->get_path()); -void Target::check_rebuild() -{ - if(!buildable) - return; + Task *task = tool->run(*this); + task->signal_finished.connect(sigc::mem_fun(this, &Target::build_finished)); + building = true; - if(!mtime) - mark_rebuild("Does not exist"); - else - { - for(list::iterator i=depends.begin(); (i!=depends.end() && !rebuild); ++i) - { - if((*i)->get_virtual_mtime()>mtime) - mark_rebuild((*i)->get_name()+" has changed"); - else if((*i)->get_rebuild()) - mark_rebuild((*i)->get_name()+" needs rebuilding"); - } - } + return task; } -Action *Target::build(Action *action) +void Target::mark_rebuild(const std::string &reason) { - building=true; - action->signal_done.connect(sigc::mem_fun(this, &Target::build_done)); - return action; + rebuild = true; + rebuild_reason = reason; } -void Target::build_done() +void Target::build_finished(bool /*success*/) { - building=false; - rebuild=false; + building = false; + rebuild = false; }