1 #include <msp/core/algorithm.h>
2 #include <msp/fs/utils.h>
4 #include "filetarget.h"
5 #include "installmap.h"
6 #include "sourcepackage.h"
7 #include "templatefile.h"
12 void InstallMap::add_mapping(const FS::Path &src, const FS::Path &inst)
20 FS::Path InstallMap::get_install_location(const FileTarget &target) const
22 const Component *comp = target.get_component();
23 unsigned overlay_depth = 0;
24 if(comp && !comp->get_overlays().empty())
26 // Check if the target resides in an overlay directory
27 string last_dir = FS::basename(FS::dirname(target.get_path()));
28 if(any_equals(comp->get_overlays(), last_dir))
32 FS::Path source = target.get_path();
35 /* Check if the target is a generated source file, residing in the
36 temporary directory */
37 const SourcePackage &pkg = comp->get_package();
38 int temp_depth = FS::descendant_depth(source, pkg.get_temp_directory());
41 // If it is, use the generating template's directory instead
42 for(Target *d: target.get_dependencies())
43 if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(d))
45 source = FS::dirname(tmpl->get_path())/FS::basename(source);
51 /* Look for a mapping entry matching both the target's original location
52 and default install location */
53 FS::Path install = target.get_install_location();
54 for(const Entry &e: entries)
56 int source_depth = FS::descendant_depth(source, e.source);
59 FS::Path install_base = FS::common_ancestor(install, e.install);
60 if(install_base.size()>1)
62 install = e.install/source.subpath(e.source.size(), source_depth-1-overlay_depth);
72 InstallMap::Loader::Loader(InstallMap &m, const FS::Path &s):
73 DataFile::ObjectLoader<InstallMap>(m),
76 add("map", &Loader::map);
79 void InstallMap::Loader::map(const string &src, const string &inst)
81 obj.add_mapping(source_base/src, inst);