]> git.tdb.fi Git - builder.git/blob - source/installedfile.cpp
723d776415df33e77c8b1fe53287746bf1dbeeab
[builder.git] / source / installedfile.cpp
1 #include <msp/fs/stat.h>
2 #include <msp/fs/utils.h>
3 #include "builder.h"
4 #include "installedfile.h"
5 #include "sharedlibrary.h"
6
7 using namespace std;
8 using namespace Msp;
9
10 InstalledFile::InstalledFile(Builder &b, const SourcePackage &p, FileTarget &s, const string &loc):
11         FileTarget(b, p, generate_target_path(b.get_prefix(), s, loc)),
12         source(s)
13 {
14         add_dependency(source);
15 }
16
17 FS::Path InstalledFile::generate_target_path(const FS::Path &prefix, const FileTarget &tgt, const string &loc)
18 {
19         if(!tgt.is_installable() && loc.empty())
20                 throw invalid_argument(tgt.get_name()+" is not installable");
21
22         FS::Path mid;
23         if(!loc.empty())
24                 mid = loc;
25         else if(const Component *comp = tgt.get_component())
26                 mid = comp->get_install_map().get_install_location(tgt);
27         else
28                 mid = tgt.get_install_location();
29
30         string fn = tgt.get_install_filename();
31         if(fn.empty())
32                 fn = FS::basename(tgt.get_path());
33
34         return prefix/mid/fn;
35 }
36
37 void InstalledFile::set_symlink(const FS::Path &l)
38 {
39         FS::Path al = FS::dirname(path)/l;
40         if(al==path)
41                 throw invalid_argument("InstalledFile::set_symlink");
42         link = FS::dirname(path)/l;
43         builder.get_vfs().register_path(link, this);
44 }
45
46 Target *InstalledFile::get_real_target()
47 {
48         return source.get_real_target();
49 }
50
51 void InstalledFile::check_rebuild()
52 {
53         if(!mtime)
54                 mark_rebuild("Does not exist");
55         else if(source.get_mtime()>mtime || source.get_size()!=size)
56                 mark_rebuild(source.get_name()+" has changed");
57         else if(source.needs_rebuild())
58                 mark_rebuild(source.get_name()+" needs rebuilding");
59         if(!needs_rebuild() && !link.empty())
60         {
61                 if(!FS::exists(link))
62                         mark_rebuild("Symlink does not exist");
63                 else
64                 {
65                         FS::Path rel_path = FS::relative(path, FS::dirname(link));
66                         if(FS::readlink(link)!=rel_path)
67                                 mark_rebuild("Symlink needs updating");
68                 }
69         }
70 }
71
72 void InstalledFile::clean()
73 {
74         if(!link.empty() && mtime)
75                 FS::unlink(link);
76         FileTarget::clean();
77 }