]> git.tdb.fi Git - builder.git/blob - source/target.cpp
Add a separate set of functions for registering and looking up targets by path
[builder.git] / source / target.cpp
1 #include <msp/fs/stat.h>
2 #include <msp/fs/utils.h>
3 #include "action.h"
4 #include "builder.h"
5 #include "filetarget.h"
6 #include "package.h"
7 #include "target.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 Target::Target(Builder &b, const Package *p, const string &n):
13         builder(b),
14         package(p),
15         name(n),
16         buildable(false),
17         building(false),
18         rebuild(false),
19         deps_ready(false),
20         preparing(false),
21         prepared(false)
22 {
23         builder.add_target(this);
24 }
25
26 Target *Target::get_buildable_target()
27 {
28         if(!rebuild)
29                 return 0;
30
31         bool self_ok = !building;
32         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
33         {
34                 Target *tgt = (*i)->get_buildable_target();
35                 if(tgt)
36                         return tgt;
37                 else if((*i)->get_rebuild())
38                         self_ok = false;
39         }
40
41         if(self_ok)
42                 return this;
43
44         return 0;
45 }
46
47 void Target::add_depend(Target *dep)
48 {
49         if(dep==this)
50                 throw invalid_argument("Target::add_depend");
51         depends.push_back(dep);
52 }
53
54 void Target::prepare()
55 {
56         if(prepared)
57                 return;
58         if(preparing)
59         {
60                 builder.problem((package ? package->get_name() : string()), "Dependency cycle detected at "+name);
61                 return;
62         }
63
64         preparing = true;
65         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
66                 (*i)->prepare();
67
68         check_rebuild();
69         preparing = false;
70         prepared = true;
71 }
72
73 Action *Target::build()
74 {
75         if(!buildable)
76         {
77                 rebuild = false;
78                 return 0;
79         }
80
81         // XXX Minor breach of OO here
82         if(FileTarget *ft = dynamic_cast<FileTarget *>(this))
83                 if(!builder.get_dry_run() && FS::exists(ft->get_path()))
84                         FS::unlink(ft->get_path());
85
86         Action *action = create_action();
87         if(action)
88         {
89                 action->signal_done.connect(sigc::mem_fun(this, &Target::build_done));
90
91                 building = true;
92         }
93
94         return action;
95 }
96
97 void Target::mark_rebuild(const std::string &reason)
98 {
99         rebuild = true;
100         rebuild_reason = reason;
101 }
102
103 void Target::build_done()
104 {
105         building = false;
106         rebuild = false;
107 }