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