]> git.tdb.fi Git - builder.git/blob - source/target.cpp
c0c3c18051bbfcd4d8c76250b59f1c081c694b85
[builder.git] / source / target.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/fs/stat.h>
9 #include <msp/fs/utils.h>
10 #include <msp/time/utils.h>
11 #include "action.h"
12 #include "builder.h"
13 #include "filetarget.h"
14 #include "package.h"
15 #include "sourcepackage.h"
16 #include "target.h"
17
18 using namespace std;
19 using namespace Msp;
20
21 Target::Target(Builder &b, const Package *p, const string &n):
22         builder(b),
23         package(p),
24         name(n),
25         buildable(false),
26         building(false),
27         rebuild(false),
28         deps_ready(false),
29         preparing(false),
30         prepared(false)
31 { }
32
33 Target *Target::get_buildable_target()
34 {
35         if(!rebuild)
36                 return 0;
37
38         bool self_ok = !building;
39         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
40         {
41                 Target *tgt = (*i)->get_buildable_target();
42                 if(tgt)
43                         return tgt;
44                 else if((*i)->get_rebuild())
45                         self_ok = false;
46         }
47
48         if(self_ok)
49                 return this;
50
51         return 0;
52 }
53
54 void Target::add_depend(Target *dep)
55 {
56         if(dep==this)
57                 throw InvalidParameterValue("A target can't depend on itself");
58         depends.push_back(dep);
59 }
60
61 void Target::prepare()
62 {
63         if(prepared)
64                 return;
65         if(preparing)
66         {
67                 builder.problem((package ? package->get_name() : string()), "Dependency cycle detected at "+name);
68                 return;
69         }
70
71         preparing = true;
72         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
73                 (*i)->prepare();
74
75         check_rebuild();
76         preparing = false;
77         prepared = true;
78 }
79
80 Action *Target::build()
81 {
82         if(!buildable)
83         {
84                 rebuild = false;
85                 return 0;
86         }
87
88         if(FileTarget *ft = dynamic_cast<FileTarget *>(this))
89                 if(!builder.get_dry_run() && FS::exists(ft->get_path()))
90                         FS::unlink(ft->get_path());
91
92         Action *action = create_action();
93         if(action)
94         {
95                 action->signal_done.connect(sigc::mem_fun(this, &Target::build_done));
96
97                 building = true;
98         }
99
100         return action;
101 }
102
103 void Target::touch()
104 {
105         mtime = Time::now();
106 }
107
108 void Target::mark_rebuild(const std::string &reason)
109 {
110         rebuild = true;
111         rebuild_reason = reason;
112 }
113
114 void Target::check_rebuild()
115 {
116         if(!buildable)
117                 return;
118
119         if(builder.get_build_all())
120                 mark_rebuild("Rebuilding everything");
121         else if(!mtime)
122                 mark_rebuild("Does not exist");
123         else
124         {
125                 for(TargetList::iterator i=depends.begin(); (i!=depends.end() && !rebuild); ++i)
126                 {
127                         if((*i)->get_mtime()>mtime)
128                                 mark_rebuild((*i)->get_name()+" has changed");
129                         else if((*i)->get_rebuild())
130                                 mark_rebuild((*i)->get_name()+" needs rebuilding");
131                 }
132         }
133
134         const SourcePackage *spkg = dynamic_cast<const SourcePackage *>(package);
135         if(!rebuild && spkg && spkg->get_config().get_mtime()>mtime)
136                 mark_rebuild("Package options changed");
137 }
138
139 void Target::build_done()
140 {
141         building = false;
142         rebuild = false;
143 }