]> git.tdb.fi Git - builder.git/blob - source/filetarget.cpp
Reload includes from CSourceFiles when they are modified
[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
34         builder.get_vfs().register_path(path, this);
35
36         stat();
37 }
38
39 void FileTarget::stat()
40 {
41         if(FS::Stat st = FS::lstat(path))
42         {
43                 mtime = st.get_modify_time();
44                 size = st.get_size();
45         }
46 }
47
48 string FileTarget::generate_name(Builder &builder, const SourcePackage *pkg, const FS::Path &path)
49 {
50         if(pkg && FS::descendant_depth(path, pkg->get_source_directory())>=0)
51         {
52                 FS::Path relpath = FS::relative(path, pkg->get_source_directory());
53                 return format("<%s>%s", pkg->get_name(), relpath.str().substr(1));
54         }
55         else if(FS::descendant_depth(path, builder.get_prefix())>=0)
56         {
57                 FS::Path relpath = FS::relative(path, builder.get_prefix());
58                 return "<prefix>"+relpath.str().substr(1);
59         }
60
61         return path.str();
62 }
63
64 void FileTarget::touch()
65 {
66         mtime = Time::now();
67         modified();
68         signal_bubble_rebuild.emit();
69 }
70
71 void FileTarget::check_rebuild()
72 {
73         if(!tool)
74                 return;
75
76         if(!mtime)
77                 mark_rebuild("Does not exist");
78         else
79         {
80                 for(Dependencies::iterator i=depends.begin(); (i!=depends.end() && !needs_rebuild()); ++i)
81                 {
82                         FileTarget *ft = dynamic_cast<FileTarget *>(*i);
83                         if(ft && ft->get_mtime()>mtime)
84                                 mark_rebuild((*i)->get_name()+" has changed");
85                         else if((*i)->needs_rebuild())
86                                 mark_rebuild((*i)->get_name()+" needs rebuilding");
87                 }
88         }
89
90         if(!needs_rebuild() && package)
91         {
92                 if(package->get_config().get_mtime()>mtime)
93                         mark_rebuild("Package options changed");
94
95                 string build_sig = create_build_signature();
96                 if(package->get_cache().has_key(this, "build_sig"))
97                 {
98                         if(package->get_cache().get_value(this, "build_sig")!=build_sig)
99                                 mark_rebuild("Build signature changed");
100                 }
101         }
102 }
103
104 string FileTarget::create_build_signature() const
105 {
106         if(!package)
107                 return string();
108
109         const BuildInfo &binfo = (component ? component->get_build_info() : package->get_build_info());
110         return tool->create_build_signature(binfo);
111 }
112
113 Task *FileTarget::build()
114 {
115         Task *task = Target::build();
116         task->set_file(path);
117         task->set_unlink(true);
118         return task;
119 }
120
121 void FileTarget::build_finished(bool success)
122 {
123         if(success)
124         {
125                 stat();
126                 if(package)
127                 {
128                         string build_sig = create_build_signature();
129                         if(!build_sig.empty())
130                                 package->get_cache().set_value(this, "build_sig", build_sig);
131                 }
132         }
133
134         Target::build_finished(success);
135 }
136
137 void FileTarget::clean()
138 {
139         if(mtime)
140         {
141                 FS::unlink(path);
142                 mtime = Time::TimeStamp();
143                 size = 0;
144                 check_rebuild();
145         }
146 }