]> git.tdb.fi Git - builder.git/blob - source/installedfile.cpp
Make sure that symlinks point to the right place
[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         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&source))
17                 if(!shlib->get_soname().empty())
18                         link = FS::dirname(path)/FS::basename(shlib->get_path());
19
20         if(!link.empty())
21                 builder.get_vfs().register_path(link, this);
22 }
23
24 FS::Path InstalledFile::generate_target_path(const FS::Path &prefix, const FileTarget &tgt, const string &loc)
25 {
26         if(!tgt.is_installable() && loc.empty())
27                 throw invalid_argument(tgt.get_name()+" is not installable");
28
29         FS::Path mid;
30         if(!loc.empty())
31                 mid = loc;
32         else if(const Component *comp = tgt.get_component())
33                 mid = comp->get_install_map().get_install_location(tgt);
34         else
35                 mid = tgt.get_install_location();
36
37         string fn = FS::basename(tgt.get_path());
38         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&tgt))
39                 if(!shlib->get_soname().empty())
40                         fn = shlib->get_soname();
41
42         return prefix/mid/fn;
43 }
44
45 Target *InstalledFile::get_real_target()
46 {
47         return source.get_real_target();
48 }
49
50 void InstalledFile::check_rebuild()
51 {
52         if(!mtime)
53                 mark_rebuild("Does not exist");
54         else if(source.get_mtime()>mtime || source.get_size()!=size)
55                 mark_rebuild(source.get_name()+" has changed");
56         else if(source.needs_rebuild())
57                 mark_rebuild(source.get_name()+" needs rebuilding");
58         if(!needs_rebuild() && !link.empty())
59         {
60                 if(!FS::exists(link))
61                         mark_rebuild("Symlink does not exist");
62                 else
63                 {
64                         FS::Path rel_path = FS::relative(path, FS::dirname(link));
65                         if(FS::readlink(link)!=rel_path)
66                                 mark_rebuild("Symlink needs updating");
67                 }
68         }
69 }
70
71 void InstalledFile::clean()
72 {
73         if(!link.empty() && mtime)
74                 FS::unlink(link);
75         FileTarget::clean();
76 }