]> git.tdb.fi Git - builder.git/blob - source/lib/installedfile.cpp
6e45ad27efc66ac65d8e9643b1763c75a4811a17
[builder.git] / source / lib / 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 &global_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 prefix;
23         FS::Path mid;
24         if(!loc.compare(0, 2, "//"))
25         {
26                 if(!tgt.get_package())
27                         throw invalid_argument("No private install location for "+tgt.get_name());
28
29                 prefix = tgt.get_package()->get_temp_directory();
30                 mid = loc.substr(2);
31         }
32         else
33         {
34                 prefix = global_prefix;
35
36                 if(!loc.empty())
37                         mid = loc;
38                 else if(const Component *comp = tgt.get_component())
39                         mid = comp->get_install_map().get_install_location(tgt);
40         }
41
42         if(mid.empty())
43                 mid = tgt.get_install_location();
44
45         string fn = tgt.get_install_filename();
46         if(fn.empty())
47                 fn = FS::basename(tgt.get_path());
48
49         return prefix/mid/fn;
50 }
51
52 void InstalledFile::set_symlink(const FS::Path &l)
53 {
54         FS::Path al = FS::dirname(path)/l;
55         if(al==path)
56                 throw invalid_argument("InstalledFile::set_symlink");
57         link = FS::dirname(path)/l;
58         builder.get_vfs().register_path(link, this);
59 }
60
61 Target *InstalledFile::get_real_target()
62 {
63         return source.get_real_target();
64 }
65
66 void InstalledFile::check_rebuild()
67 {
68         if(!mtime)
69                 mark_rebuild("Does not exist");
70         else if(source.get_mtime()>mtime || source.get_size()!=size)
71                 mark_rebuild(source.get_name()+" has changed");
72         else if(source.needs_rebuild())
73                 mark_rebuild(source.get_name()+" needs rebuilding");
74         if(!needs_rebuild() && !link.empty())
75         {
76                 if(!FS::exists(link))
77                         mark_rebuild("Symlink does not exist");
78                 else
79                 {
80                         FS::Path rel_path = FS::relative(path, FS::dirname(link));
81                         if(FS::readlink(link)!=rel_path)
82                                 mark_rebuild("Symlink needs updating");
83                 }
84         }
85 }
86
87 void InstalledFile::clean()
88 {
89         if(!link.empty() && mtime)
90                 FS::unlink(link);
91         FileTarget::clean();
92 }