3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/fs/stat.h>
9 #include <msp/fs/utils.h>
10 #include <msp/time/utils.h>
14 #include "sourcepackage.h"
21 Tries to locate a target that will help getting this target built. If all
22 dependencies are up-to-date, returns this target. If there are no targets
23 ready to be built (maybe because they are being built right now), returns 0.
25 Target *Target::get_buildable_target()
30 bool self_ok=!building;
31 for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
33 Target *tgt=(*i)->get_buildable_target();
36 else if((*i)->get_rebuild())
46 void Target::add_depend(Target *dep)
49 throw InvalidParameterValue("A target can't depend on itself");
50 depends.push_back(dep);
51 dep->rdepends.push_back(this);
55 Prepares the target by recursively preparing dependencies, then checking
56 whether rebuilding is needed. A flag is used to prevent unnecessary
59 void Target::prepare()
65 for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
72 Action *Target::build()
80 if(!builder.get_dry_run() && FS::exists(name))
83 Action *action=create_action();
86 action->signal_done.connect(sigc::mem_fun(this, &Target::build_done));
95 Returns the number of targets that need to be rebuilt in order to get this
98 unsigned Target::count_rebuild()
104 unsigned count=rebuild;
105 for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
106 count+=(*i)->count_rebuild();
111 Changes the mtime of the target to the current time.
118 Target::Target(Builder &b, const Package *p, const string &n):
129 builder.add_target(this);
132 if(!FS::stat(name, st))
133 mtime=Time::TimeStamp::from_unixtime(st.st_mtime);
136 void Target::mark_rebuild(const std::string &reason)
139 rebuild_reason=reason;
143 Checks if this target needs to be rebuilt and why.
145 void Target::check_rebuild()
150 if(builder.get_build_all())
151 mark_rebuild("Rebuilding everything");
153 mark_rebuild("Does not exist");
156 for(TargetList::iterator i=depends.begin(); (i!=depends.end() && !rebuild); ++i)
158 if((*i)->get_mtime()>mtime)
159 mark_rebuild(FS::basename((*i)->get_name())+" has changed");
160 else if((*i)->get_rebuild())
161 mark_rebuild(FS::basename((*i)->get_name())+" needs rebuilding");
165 const SourcePackage *spkg=dynamic_cast<const SourcePackage *>(package);
166 if(!rebuild && spkg && spkg->get_config().get_mtime()>mtime)
167 mark_rebuild("Package options changed");
171 Handles for the build_done signal of Action.
173 void Target::build_done()