]> git.tdb.fi Git - builder.git/blob - source/filetarget.cpp
Add a signal to propagate rebuild state after preparation
[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(builder.get_build_all())
40                 mark_rebuild("Rebuilding everything");
41         else if(!mtime)
42                 mark_rebuild("Does not exist");
43         else
44         {
45                 for(Dependencies::iterator i=depends.begin(); (i!=depends.end() && !needs_rebuild()); ++i)
46                 {
47                         FileTarget *ft = dynamic_cast<FileTarget *>(*i);
48                         if(ft && ft->get_mtime()>mtime)
49                                 mark_rebuild((*i)->get_name()+" has changed");
50                         else if((*i)->needs_rebuild())
51                                 mark_rebuild((*i)->get_name()+" needs rebuilding");
52                 }
53         }
54
55         const SourcePackage *spkg = dynamic_cast<const SourcePackage *>(package);
56         if(!needs_rebuild() && spkg && spkg->get_config().get_mtime()>mtime)
57                 mark_rebuild("Package options changed");
58 }
59
60 string FileTarget::generate_name(const Package *pkg, const FS::Path &pth)
61 {
62         if(const SourcePackage *spkg = dynamic_cast<const SourcePackage *>(pkg))
63         {
64                 if(FS::descendant_depth(pth, spkg->get_source())>=0)
65                 {
66                         FS::Path relpath = FS::relative(pth, spkg->get_source());
67                         return format("<%s>%s", pkg->get_name(), relpath.str().substr(1));
68                 }
69                 else if(FS::descendant_depth(pth, pkg->get_builder().get_prefix())>=0)
70                 {
71                         FS::Path relpath = FS::relative(pth, pkg->get_builder().get_prefix());
72                         return "<prefix>"+relpath.str().substr(1);
73                 }
74         }
75
76         return pth.str();
77 }