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