]> git.tdb.fi Git - builder.git/blob - source/target.cpp
Make the name of a FileTarget be its basename instead of full path
[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         dep->rdepends.push_back(this);
60 }
61
62 void Target::prepare()
63 {
64         if(prepared)
65                 return;
66         if(preparing)
67         {
68                 builder.problem((package ? package->get_name() : string()), "Dependency cycle detected at "+name);
69                 return;
70         }
71
72         preparing = true;
73         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
74                 (*i)->prepare();
75
76         check_rebuild();
77         preparing = false;
78         prepared = true;
79 }
80
81 Action *Target::build()
82 {
83         if(!buildable)
84         {
85                 rebuild = false;
86                 return 0;
87         }
88
89         if(FileTarget *ft = dynamic_cast<FileTarget *>(this))
90                 if(!builder.get_dry_run() && FS::exists(ft->get_path()))
91                         FS::unlink(ft->get_path());
92
93         Action *action = create_action();
94         if(action)
95         {
96                 action->signal_done.connect(sigc::mem_fun(this, &Target::build_done));
97
98                 building = true;
99         }
100
101         return action;
102 }
103
104 void Target::touch()
105 {
106         mtime = Time::now();
107 }
108
109 void Target::mark_rebuild(const std::string &reason)
110 {
111         rebuild = true;
112         rebuild_reason = reason;
113 }
114
115 void Target::check_rebuild()
116 {
117         if(!buildable)
118                 return;
119
120         if(builder.get_build_all())
121                 mark_rebuild("Rebuilding everything");
122         else if(!mtime)
123                 mark_rebuild("Does not exist");
124         else
125         {
126                 for(TargetList::iterator i=depends.begin(); (i!=depends.end() && !rebuild); ++i)
127                 {
128                         if((*i)->get_mtime()>mtime)
129                                 mark_rebuild((*i)->get_name()+" has changed");
130                         else if((*i)->get_rebuild())
131                                 mark_rebuild((*i)->get_name()+" needs rebuilding");
132                 }
133         }
134
135         const SourcePackage *spkg = dynamic_cast<const SourcePackage *>(package);
136         if(!rebuild && spkg && spkg->get_config().get_mtime()>mtime)
137                 mark_rebuild("Package options changed");
138 }
139
140 void Target::build_done()
141 {
142         building = false;
143         rebuild = false;
144 }