]> git.tdb.fi Git - builder.git/blob - source/target.cpp
Replace per-file copyright notices with a single file
[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
24 Target *Target::get_buildable_target()
25 {
26         if(!rebuild)
27                 return 0;
28
29         bool self_ok = !building;
30         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
31         {
32                 Target *tgt = (*i)->get_buildable_target();
33                 if(tgt)
34                         return tgt;
35                 else if((*i)->get_rebuild())
36                         self_ok = false;
37         }
38
39         if(self_ok)
40                 return this;
41
42         return 0;
43 }
44
45 void Target::add_depend(Target *dep)
46 {
47         if(dep==this)
48                 throw invalid_argument("Target::add_depend");
49         depends.push_back(dep);
50 }
51
52 void Target::prepare()
53 {
54         if(prepared)
55                 return;
56         if(preparing)
57         {
58                 builder.problem((package ? package->get_name() : string()), "Dependency cycle detected at "+name);
59                 return;
60         }
61
62         preparing = true;
63         for(TargetList::iterator i=depends.begin(); i!=depends.end(); ++i)
64                 (*i)->prepare();
65
66         check_rebuild();
67         preparing = false;
68         prepared = true;
69 }
70
71 Action *Target::build()
72 {
73         if(!buildable)
74         {
75                 rebuild = false;
76                 return 0;
77         }
78
79         // XXX Minor breach of OO here
80         if(FileTarget *ft = dynamic_cast<FileTarget *>(this))
81                 if(!builder.get_dry_run() && FS::exists(ft->get_path()))
82                         FS::unlink(ft->get_path());
83
84         Action *action = create_action();
85         if(action)
86         {
87                 action->signal_done.connect(sigc::mem_fun(this, &Target::build_done));
88
89                 building = true;
90         }
91
92         return action;
93 }
94
95 void Target::mark_rebuild(const std::string &reason)
96 {
97         rebuild = true;
98         rebuild_reason = reason;
99 }
100
101 void Target::build_done()
102 {
103         building = false;
104         rebuild = false;
105 }