+/* $Id$
+
+This file is part of builder
+Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <msp/path/utils.h>
+#include <msp/time/utils.h>
+#include "action.h"
#include "builder.h"
+#include "package.h"
#include "target.h"
using namespace std;
+using namespace Msp;
-Target *TargetRef::get_target()
-{
- if(!target)
- target=builder.get_target(name);
- return target;
-}
-
+/**
+Tries to locate a target that will help getting this target built. If all
+dependencies are up-to-date, returns this target. If there are no targets
+ready to be built (maybe because they are being built right now), returns 0.
+*/
Target *Target::get_buildable_target()
{
- if(rebuild && ready_for_build && !building)
- return this;
+ if(!rebuild)
+ return 0;
- for(list<Target *>::iterator i=depends.begin(); i!=depends.end(); ++i)
+ bool self_ok=!building;
+ for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
{
Target *tgt=(*i)->get_buildable_target();
if(tgt)
return tgt;
+ else if((*i)->get_rebuild())
+ self_ok=false;
}
+ if(self_ok)
+ return this;
+
return 0;
}
void Target::add_depend(Target *dep)
{
+ if(dep==this)
+ throw InvalidParameterValue("A target can't depend on itself");
depends.push_back(dep);
dep->rdepends.push_back(this);
}
+/**
+Prepares the target by recursively preparing dependencies, then checking
+whether rebuilding is needed. A flag is used to prevent unnecessary
+executions.
+*/
+void Target::prepare()
+{
+ if(prepared)
+ return;
+
+ prepared=true;
+ for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
+ (*i)->prepare();
+
+ check_rebuild();
+
+}
+
+/**
+Returns the number of targets that need to be rebuilt in order to get this
+target up-to-date.
+*/
+unsigned Target::count_rebuild()
+{
+ if(counted)
+ return 0;
+
+ counted=true;
+ unsigned count=rebuild;
+ for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
+ count+=(*i)->count_rebuild();
+ return count;
+}
+
+/**
+Changes the mtime of the target to the current time.
+*/
+void Target::touch()
+{
+ mtime=Time::now();
+}
+
Target::Target(Builder &b, const Package *p, const string &n):
builder(b),
package(p),
name(n),
+ buildable(false),
building(false),
rebuild(false),
- ready_for_build(false)
-{ }
+ deps_ready(false),
+ prepared(false),
+ counted(false)
+{
+ builder.add_target(this);
+
+ struct stat st;
+ if(!Path::stat(name, st))
+ mtime=Time::TimeStamp::from_unixtime(st.st_mtime);
+}
void Target::mark_rebuild(const std::string &reason)
{
rebuild=true;
rebuild_reason=reason;
}
+
+/**
+Checks if this target needs to be rebuilt and why.
+*/
+void Target::check_rebuild()
+{
+ if(!buildable)
+ return;
+
+ if(builder.get_build_all())
+ mark_rebuild("Rebuilding everything");
+ else if(!mtime)
+ mark_rebuild("Does not exist");
+ else
+ {
+ for(TargetList::iterator i=depends.begin(); (i!=depends.end() && !rebuild); ++i)
+ {
+ if((*i)->get_mtime()>mtime)
+ mark_rebuild(Path::basename((*i)->get_name())+" has changed");
+ else if((*i)->get_rebuild())
+ mark_rebuild(Path::basename((*i)->get_name())+" needs rebuilding");
+ }
+ }
+ if(!rebuild && package && package->get_config().get_mtime()>mtime)
+ mark_rebuild("Package options changed");
+}
+
+/**
+Hooks the target up with the given action, then returns it. This should be
+called from the public build() function of buildable targets.
+*/
+Action *Target::build(Action *action)
+{
+ building=true;
+ action->signal_done.connect(sigc::mem_fun(this, &Target::build_done));
+ return action;
+}
+
+/**
+Handles for the build_done signal of Action.
+*/
+void Target::build_done()
+{
+ building=false;
+ rebuild=false;
+}