]> git.tdb.fi Git - builder.git/blob - source/filetarget.cpp
Track build options to rebuild primary targets when build type changes
[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 "tool.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 FileTarget::FileTarget(Builder &b, const FS::Path &a):
15         Target(b, generate_name(b, 0, a)),
16         path(a)
17 {
18         init(0);
19 }
20
21 FileTarget::FileTarget(Builder &b, const SourcePackage &p, const FS::Path &a):
22         Target(b, generate_name(b, &p, a)),
23         path(a)
24 {
25         init(&p);
26 }
27
28 void FileTarget::init(const SourcePackage *p)
29 {
30         size = 0;
31         package = p;
32
33         builder.get_vfs().register_path(path, this);
34
35         stat();
36 }
37
38 void FileTarget::stat()
39 {
40         if(FS::Stat st = FS::lstat(path))
41         {
42                 mtime = st.get_modify_time();
43                 size = st.get_size();
44         }
45 }
46
47 string FileTarget::generate_name(Builder &builder, const SourcePackage *pkg, const FS::Path &path)
48 {
49         if(pkg && FS::descendant_depth(path, pkg->get_source_directory())>=0)
50         {
51                 FS::Path relpath = FS::relative(path, pkg->get_source_directory());
52                 return format("<%s>%s", pkg->get_name(), relpath.str().substr(1));
53         }
54         else if(FS::descendant_depth(path, builder.get_prefix())>=0)
55         {
56                 FS::Path relpath = FS::relative(path, builder.get_prefix());
57                 builder.get_logger().log("debug", format("%s %s %s", path, builder.get_prefix(), relpath));
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         signal_bubble_rebuild.emit();
68 }
69
70 void FileTarget::check_rebuild()
71 {
72         if(!tool)
73                 return;
74
75         if(!mtime)
76                 mark_rebuild("Does not exist");
77         else
78         {
79                 for(Dependencies::iterator i=depends.begin(); (i!=depends.end() && !needs_rebuild()); ++i)
80                 {
81                         FileTarget *ft = dynamic_cast<FileTarget *>(*i);
82                         if(ft && ft->get_mtime()>mtime)
83                                 mark_rebuild((*i)->get_name()+" has changed");
84                         else if((*i)->needs_rebuild())
85                                 mark_rebuild((*i)->get_name()+" needs rebuilding");
86                 }
87         }
88
89         if(!needs_rebuild() && package)
90         {
91                 if(package->get_config().get_mtime()>mtime)
92                         mark_rebuild("Package options changed");
93
94                 string build_sig = create_build_signature();
95                 if(package->get_cache().has_key(this, "build_sig"))
96                 {
97                         if(package->get_cache().get_value(this, "build_sig")!=build_sig)
98                                 mark_rebuild("Build signature changed");
99                 }
100         }
101 }
102
103 string FileTarget::create_build_signature() const
104 {
105         if(!package)
106                 return string();
107
108         const BuildInfo &binfo = (component ? component->get_build_info() : package->get_build_info());
109         return tool->create_build_signature(binfo);
110 }
111
112 Task *FileTarget::build()
113 {
114         if(tool && !builder.get_dry_run() && mtime)
115                 FS::unlink(path);
116
117         return Target::build();
118 }
119
120 void FileTarget::build_finished(bool success)
121 {
122         if(success)
123         {
124                 stat();
125                 if(package)
126                 {
127                         string build_sig = create_build_signature();
128                         if(!build_sig.empty())
129                                 package->get_cache().set_value(this, "build_sig", build_sig);
130                 }
131         }
132
133         Target::build_finished(success);
134 }