]> git.tdb.fi Git - builder.git/blob - source/installedfile.cpp
More generic system for installing targets under different names
[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         /* XXX Ideally, the tool should handle this (symlinks are not necessary for
17         windows dlls).  However, the tool that created the target being installed
18         knows nothing about the InstalledFile. */
19         string base_fn = FS::basename(source.get_path());
20         const string &inst_fn = source.get_install_filename();
21         if(!inst_fn.empty() && inst_fn!=base_fn)
22         {
23                 link = FS::dirname(path)/base_fn;
24                 builder.get_vfs().register_path(link, this);
25         }
26 }
27
28 FS::Path InstalledFile::generate_target_path(const FS::Path &prefix, const FileTarget &tgt, const string &loc)
29 {
30         if(!tgt.is_installable() && loc.empty())
31                 throw invalid_argument(tgt.get_name()+" is not installable");
32
33         FS::Path mid;
34         if(!loc.empty())
35                 mid = loc;
36         else if(const Component *comp = tgt.get_component())
37                 mid = comp->get_install_map().get_install_location(tgt);
38         else
39                 mid = tgt.get_install_location();
40
41         string fn = tgt.get_install_filename();
42         if(fn.empty())
43                 fn = FS::basename(tgt.get_path());
44
45         return prefix/mid/fn;
46 }
47
48 Target *InstalledFile::get_real_target()
49 {
50         return source.get_real_target();
51 }
52
53 void InstalledFile::check_rebuild()
54 {
55         if(!mtime)
56                 mark_rebuild("Does not exist");
57         else if(source.get_mtime()>mtime || source.get_size()!=size)
58                 mark_rebuild(source.get_name()+" has changed");
59         else if(source.needs_rebuild())
60                 mark_rebuild(source.get_name()+" needs rebuilding");
61         if(!needs_rebuild() && !link.empty())
62         {
63                 if(!FS::exists(link))
64                         mark_rebuild("Symlink does not exist");
65                 else
66                 {
67                         FS::Path rel_path = FS::relative(path, FS::dirname(link));
68                         if(FS::readlink(link)!=rel_path)
69                                 mark_rebuild("Symlink needs updating");
70                 }
71         }
72 }
73
74 void InstalledFile::clean()
75 {
76         if(!link.empty() && mtime)
77                 FS::unlink(link);
78         FileTarget::clean();
79 }