+ const Component *comp = target.get_component();
+ unsigned overlay_depth = 0;
+ if(comp && !comp->get_overlays().empty())
+ {
+ // Check if the target resides in an overlay directory
+ string last_dir = FS::basename(FS::dirname(target.get_path()));
+ if(any_equals(comp->get_overlays(), last_dir))
+ overlay_depth = 1;
+ }
+
+ FS::Path source = target.get_path();
+ if(comp)
+ {
+ /* Check if the target is a generated source file, residing in the
+ temporary directory */
+ const SourcePackage &pkg = comp->get_package();
+ int temp_depth = FS::descendant_depth(source, pkg.get_temp_directory());
+ if(temp_depth>0)
+ {
+ // If it is, use the generating template's directory instead
+ for(Target *d: target.get_dependencies())
+ if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(d))
+ {
+ source = FS::dirname(tmpl->get_path())/FS::basename(source);
+ break;
+ }
+ }
+ }
+
+ /* Look for a mapping entry matching both the target's original location
+ and default install location */