]> git.tdb.fi Git - builder.git/blob - source/target.cpp
b3dc50904717450c938f65f47add7b490073a317
[builder.git] / source / target.cpp
1 #include <msp/path/utils.h>
2 #include "action.h"
3 #include "builder.h"
4 #include "package.h"
5 #include "target.h"
6
7 using namespace std;
8 using namespace Msp;
9
10 Target *Target::get_buildable_target()
11 {
12         bool self_ok=true;
13         for(list<Target *>::iterator i=depends.begin(); i!=depends.end(); ++i)
14         {
15                 Target *tgt=(*i)->get_buildable_target();
16                 if(tgt)
17                         return tgt;
18                 else if((*i)->get_rebuild())
19                         self_ok=false;
20         }
21
22         if(self_ok && rebuild && !building)
23                 return this;
24
25         return 0;
26 }
27
28 void Target::add_depend(Target *dep)
29 {
30         depends.push_back(dep);
31         dep->rdepends.push_back(this);
32 }
33
34 void Target::prepare()
35 {
36         if(prepared)
37                 return;
38
39         for(list<Target *>::iterator i=depends.begin(); i!=depends.end(); ++i)
40                 (*i)->prepare();
41
42         check_rebuild();
43 }
44
45 Target::Target(Builder &b, const Package *p, const string &n):
46         builder(b),
47         package(p),
48         name(n),
49         building(false),
50         rebuild(false),
51         prepared(false),
52         buildable(false)
53 {
54         struct stat st;
55         if(!Path::stat(name, st))
56         {
57                 mtime=Time::TimeStamp::from_unixtime(st.st_mtime);
58                 vmtime=mtime;
59         }
60 }
61
62 void Target::mark_rebuild(const std::string &reason)
63 {
64         rebuild=true;
65         rebuild_reason=reason;
66 }
67
68 void Target::check_rebuild()
69 {
70         if(!buildable)
71                 return;
72
73         if(!mtime)
74                 mark_rebuild("Does not exist");
75         else
76         {
77                 for(list<Target *>::iterator i=depends.begin(); (i!=depends.end() && !rebuild); ++i)
78                 {
79                         if((*i)->get_virtual_mtime()>mtime)
80                                 mark_rebuild((*i)->get_name()+" has changed");
81                         else if((*i)->get_rebuild())
82                                 mark_rebuild((*i)->get_name()+" needs rebuilding");
83                 }
84         }
85         if(!rebuild && package && package->get_config().get_mtime()>mtime)
86                 mark_rebuild("Package options changed");
87 }
88
89 Action *Target::build(Action *action)
90 {
91         building=true;
92         action->signal_done.connect(sigc::mem_fun(this, &Target::build_done));
93         return action;
94 }
95
96 void Target::build_done()
97 {
98         building=false;
99         rebuild=false;
100 }