]> git.tdb.fi Git - builder.git/blob - source/filetarget.cpp
Turn the force rebuild logic around
[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/time/utils.h>
5 #include "builder.h"
6 #include "filetarget.h"
7 #include "sourcepackage.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 FileTarget::FileTarget(Builder &b, const Package *p, const FS::Path &a):
13         Target(b, generate_name(p, a)),
14         path(a),
15         size(0)
16 {
17         package = p;
18
19         builder.get_vfs().register_path(path, this);
20
21         if(FS::Stat st = FS::lstat(path))
22         {
23                 mtime = st.get_modify_time();
24                 size = st.get_size();
25         }
26 }
27
28 void FileTarget::touch()
29 {
30         mtime = Time::now();
31         signal_bubble_rebuild.emit();
32 }
33
34 void FileTarget::check_rebuild()
35 {
36         if(!tool)
37                 return;
38
39         if(!mtime)
40                 mark_rebuild("Does not exist");
41         else
42         {
43                 for(Dependencies::iterator i=depends.begin(); (i!=depends.end() && !needs_rebuild()); ++i)
44                 {
45                         FileTarget *ft = dynamic_cast<FileTarget *>(*i);
46                         if(ft && ft->get_mtime()>mtime)
47                                 mark_rebuild((*i)->get_name()+" has changed");
48                         else if((*i)->needs_rebuild())
49                                 mark_rebuild((*i)->get_name()+" needs rebuilding");
50                 }
51         }
52
53         const SourcePackage *spkg = dynamic_cast<const SourcePackage *>(package);
54         if(!needs_rebuild() && spkg && spkg->get_config().get_mtime()>mtime)
55                 mark_rebuild("Package options changed");
56 }
57
58 string FileTarget::generate_name(const Package *pkg, const FS::Path &pth)
59 {
60         if(const SourcePackage *spkg = dynamic_cast<const SourcePackage *>(pkg))
61         {
62                 if(FS::descendant_depth(pth, spkg->get_source())>=0)
63                 {
64                         FS::Path relpath = FS::relative(pth, spkg->get_source());
65                         return format("<%s>%s", pkg->get_name(), relpath.str().substr(1));
66                 }
67                 else if(FS::descendant_depth(pth, pkg->get_builder().get_prefix())>=0)
68                 {
69                         FS::Path relpath = FS::relative(pth, pkg->get_builder().get_prefix());
70                         return "<prefix>"+relpath.str().substr(1);
71                 }
72         }
73
74         return pth.str();
75 }