]> git.tdb.fi Git - builder.git/blob - source/filetarget.cpp
Move file-to-target mapping to a separate class
[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, p, make_name(p, a)),
14         path(a),
15         size(0)
16 {
17         builder.get_vfs().register_path(path, this);
18
19         if(FS::Stat st = FS::lstat(path))
20         {
21                 mtime = st.get_modify_time();
22                 size = st.get_size();
23         }
24 }
25
26 void FileTarget::touch()
27 {
28         mtime = Time::now();
29 }
30
31 void FileTarget::check_rebuild()
32 {
33         if(!buildable)
34                 return;
35
36         if(builder.get_build_all())
37                 mark_rebuild("Rebuilding everything");
38         else if(!mtime)
39                 mark_rebuild("Does not exist");
40         else
41         {
42                 for(TargetList::iterator i=depends.begin(); (i!=depends.end() && !rebuild); ++i)
43                 {
44                         FileTarget *ft = dynamic_cast<FileTarget *>(*i);
45                         if(ft && ft->get_mtime()>mtime)
46                                 mark_rebuild((*i)->get_name()+" has changed");
47                         else if((*i)->get_rebuild())
48                                 mark_rebuild((*i)->get_name()+" needs rebuilding");
49                         else
50                         {
51                                 Target *real = ft->get_real_target();
52                                 if(real->get_rebuild())
53                                         mark_rebuild(real->get_name()+" needs rebuilding");
54                         }
55                 }
56         }
57
58         const SourcePackage *spkg = dynamic_cast<const SourcePackage *>(package);
59         if(!rebuild && spkg && spkg->get_config().get_mtime()>mtime)
60                 mark_rebuild("Package options changed");
61 }
62
63 string FileTarget::make_name(const Package *pkg, const FS::Path &pth)
64 {
65         if(const SourcePackage *spkg = dynamic_cast<const SourcePackage *>(pkg))
66         {
67                 if(FS::descendant_depth(pth, spkg->get_source())>=0)
68                 {
69                         FS::Path relpath = FS::relative(pth, spkg->get_source());
70                         return format("<%s>%s", pkg->get_name(), relpath.str().substr(1));
71                 }
72                 else if(FS::descendant_depth(pth, pkg->get_builder().get_prefix())>=0)
73                 {
74                         FS::Path relpath = FS::relative(pth, pkg->get_builder().get_prefix());
75                         return "<prefix>"+relpath.str().substr(1);
76                 }
77         }
78
79         return pth.str();
80 }