]> git.tdb.fi Git - builder.git/blob - source/target.cpp
7c6141df7403b1978ccd32f8fdfa6ce911c10f74
[builder.git] / source / target.cpp
1 #include <msp/fs/stat.h>
2 #include <msp/fs/utils.h>
3 #include "builder.h"
4 #include "filetarget.h"
5 #include "package.h"
6 #include "target.h"
7 #include "task.h"
8 #include "tool.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 Target::Target(Builder &b, const Package *p, const string &n):
14         builder(b),
15         package(p),
16         name(n),
17         tool(0),
18         state(INIT),
19         deps_ready(false)
20 {
21         builder.add_target(this);
22 }
23
24 Target *Target::get_buildable_target()
25 {
26         if(!needs_rebuild())
27                 return 0;
28
29         bool self_ok = state!=BUILDING;
30         for(Dependencies::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)->needs_rebuild())
36                         self_ok = false;
37         }
38
39         if(self_ok)
40                 return this;
41
42         return 0;
43 }
44
45 void Target::set_tool(const Tool &t)
46 {
47         tool = &t;
48 }
49
50 void Target::add_depend(Target *dep)
51 {
52         if(dep==this)
53                 throw invalid_argument("Target::add_depend");
54         depends.push_back(dep);
55 }
56
57 void Target::prepare()
58 {
59         if(state>PREPARING)
60                 return;
61         if(state==PREPARING)
62         {
63                 builder.problem((package ? package->get_name() : string()), "Dependency cycle detected at "+name);
64                 return;
65         }
66
67         state = PREPARING;
68         for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
69                 (*i)->prepare();
70
71         check_rebuild();
72         if(state==PREPARING)
73                 state = UPTODATE;
74 }
75
76 Task *Target::build()
77 {
78         if(!tool)
79         {
80                 state = UPTODATE;
81                 return 0;
82         }
83
84         // XXX Minor breach of OO here
85         if(FileTarget *ft = dynamic_cast<FileTarget *>(this))
86                 if(!builder.get_dry_run() && FS::exists(ft->get_path()))
87                         FS::unlink(ft->get_path());
88
89         Task *task = tool->run(*this);
90         task->signal_finished.connect(sigc::mem_fun(this, &Target::build_finished));
91         state = BUILDING;
92
93         return task;
94 }
95
96 void Target::mark_rebuild(const std::string &reason)
97 {
98         state = REBUILD;
99         rebuild_reason = reason;
100 }
101
102 void Target::build_finished(bool /*success*/)
103 {
104         state = UPTODATE;
105 }