]> git.tdb.fi Git - builder.git/blob - source/installmap.cpp
Add a feature for overlay source directories
[builder.git] / source / installmap.cpp
1 #include <msp/fs/utils.h>
2 #include "component.h"
3 #include "filetarget.h"
4 #include "installmap.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 void InstallMap::add_mapping(const FS::Path &src, const FS::Path &inst)
10 {
11         Entry e;
12         e.source = src;
13         e.install = inst;
14         entries.push_back(e);
15 }
16
17 FS::Path InstallMap::get_install_location(const FileTarget &target) const
18 {
19         const Component *comp = target.get_component();
20         unsigned overlay_depth = 0;
21         if(comp && !comp->get_overlays().empty())
22         {
23                 const Component::OverlayList &overlays = comp->get_overlays();
24                 string last_dir = FS::basename(FS::dirname(target.get_path()));
25                 for(Component::OverlayList::const_iterator i=overlays.begin(); i!=overlays.end(); ++i)
26                         if(last_dir==*i)
27                                 overlay_depth = 1;
28         }
29
30         const FS::Path &source = target.get_path();
31         FS::Path install = target.get_install_location();
32         for(list<Entry>::const_iterator i=entries.begin(); i!=entries.end(); ++i)
33         {
34                 int source_depth = FS::descendant_depth(source, i->source);
35                 if(source_depth>=0)
36                 {
37                         FS::Path install_base = FS::common_ancestor(install, i->install);
38                         if(install_base.size()>1)
39                         {
40                                 install = i->install/source.subpath(i->source.size(), source_depth-1-overlay_depth);
41                                 break;
42                         }
43                 }
44         }
45
46         return install;
47 }
48
49
50 InstallMap::Loader::Loader(InstallMap &m, const FS::Path &s):
51         DataFile::ObjectLoader<InstallMap>(m),
52         source_base(s)
53 {
54         add("map", &Loader::map);
55 }
56
57 void InstallMap::Loader::map(const string &src, const string &inst)
58 {
59         obj.add_mapping(source_base/src, inst);
60 }