]> git.tdb.fi Git - builder.git/blob - source/filetarget.cpp
Use getenv from mspcore
[builder.git] / source / filetarget.cpp
1 #include <msp/fs/stat.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
5 #include <msp/time/utils.h>
6 #include "builder.h"
7 #include "filetarget.h"
8 #include "sourcepackage.h"
9 #include "task.h"
10 #include "tool.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 FileTarget::FileTarget(Builder &b, const FS::Path &a):
16         Target(b, generate_name(b, 0, a)),
17         path(a)
18 {
19         init(0);
20 }
21
22 FileTarget::FileTarget(Builder &b, const SourcePackage &p, const FS::Path &a):
23         Target(b, generate_name(b, &p, a)),
24         path(a)
25 {
26         init(&p);
27 }
28
29 void FileTarget::init(const SourcePackage *p)
30 {
31         size = 0;
32         package = p;
33         nested_build_sig = false;
34         arch_in_build_sig = false;
35
36         builder.get_vfs().register_path(path, this);
37
38         stat();
39 }
40
41 void FileTarget::stat()
42 {
43         if(FS::Stat st = FS::lstat(path))
44         {
45                 mtime = st.get_modify_time();
46                 size = st.get_size();
47         }
48 }
49
50 string FileTarget::generate_name(Builder &builder, const SourcePackage *pkg, const FS::Path &path)
51 {
52         if(pkg && FS::descendant_depth(path, pkg->get_source_directory())>=0)
53         {
54                 FS::Path relpath = FS::relative(path, pkg->get_source_directory());
55                 return format("<%s>%s", pkg->get_name(), relpath.str().substr(1));
56         }
57         else if(FS::descendant_depth(path, builder.get_prefix())>=0)
58         {
59                 FS::Path relpath = FS::relative(path, builder.get_prefix());
60                 return "<prefix>"+relpath.str().substr(1);
61         }
62
63         return path.str();
64 }
65
66 void FileTarget::touch()
67 {
68         mtime = Time::now();
69         modified();
70         signal_bubble_rebuild.emit();
71 }
72
73 void FileTarget::check_rebuild()
74 {
75         if(!tool || needs_rebuild())
76                 return;
77
78         if(!mtime)
79                 mark_rebuild("Does not exist");
80         else
81         {
82                 for(Dependencies::iterator i=depends.begin(); (i!=depends.end() && !needs_rebuild()); ++i)
83                 {
84                         FileTarget *ft = dynamic_cast<FileTarget *>(*i);
85                         if(ft && ft->get_mtime()>mtime)
86                                 mark_rebuild((*i)->get_name()+" has changed");
87                         else if((*i)->needs_rebuild())
88                                 mark_rebuild((*i)->get_name()+" needs rebuilding");
89                 }
90         }
91
92         if(!needs_rebuild())
93         {
94                 // Some side effects might not exist
95                 for(Dependencies::iterator i=side_effects.begin(); (i!=side_effects.end() && !needs_rebuild()); ++i)
96                         if((*i)->needs_rebuild())
97                                 mark_rebuild((*i)->get_name()+" needs rebuilding");
98         }
99
100         if(!needs_rebuild() && package)
101         {
102                 if(package->get_config().get_mtime()>mtime)
103                         mark_rebuild("Package options changed");
104
105                 if(tool->get_executable())
106                 {
107                         string build_sig = create_build_signature();
108                         if(package->get_cache().has_key(this, "build_sig"))
109                         {
110                                 if(package->get_cache().get_value(this, "build_sig")!=build_sig)
111                                         mark_rebuild("Build signature changed");
112                         }
113                 }
114         }
115 }
116
117 string FileTarget::create_build_signature() const
118 {
119         if(!package)
120                 return string();
121
122         const BuildInfo &binfo = (component ? component->get_build_info() : package->get_build_info());
123         list<string> sigs;
124         if(nested_build_sig && component)
125         {
126                 set<const Tool *> depend_tools;
127                 for(Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
128                         if((*i)->get_component()==component && (*i)->get_tool())
129                                 depend_tools.insert((*i)->get_tool());
130
131                 for(set<const Tool *>::const_iterator i=depend_tools.begin(); i!=depend_tools.end(); ++i)
132                         sigs.push_back((*i)->create_build_signature(binfo));
133                 sigs.sort();
134                 sigs.push_front(tool->create_build_signature(binfo));
135         }
136         else
137                 sigs.push_back(tool->create_build_signature(binfo));
138
139         if(arch_in_build_sig)
140                 if(const Architecture *arch = tool->get_architecture())
141                         sigs.push_front(arch->get_name());
142
143         return join(sigs.begin(), sigs.end(), ";");
144 }
145
146 void FileTarget::build(Task &task)
147 {
148         task.add_file(path);
149         task.set_unlink(true);
150 }
151
152 void FileTarget::build_finished(bool success)
153 {
154         if(success)
155         {
156                 stat();
157                 if(package)
158                 {
159                         string build_sig = create_build_signature();
160                         if(!build_sig.empty())
161                                 package->get_cache().set_value(this, "build_sig", build_sig);
162                 }
163         }
164
165         Target::build_finished(success);
166 }
167
168 void FileTarget::clean()
169 {
170         if(mtime)
171         {
172                 FS::unlink(path);
173                 mtime = Time::TimeStamp();
174                 size = 0;
175                 check_rebuild();
176         }
177 }