diff --git a/meta/lib/oe/recipeutils.py b/meta/lib/oe/recipeutils.py
index a0c6974f04..557e0f9bd5 100644
--- a/meta/lib/oe/recipeutils.py
+++ b/meta/lib/oe/recipeutils.py
@@ -21,7 +21,7 @@ import glob
 import bb.tinfoil
 
 from collections import OrderedDict, defaultdict
-from bb.utils import vercmp_string
+from bb.utils import vercmp_string, get_collection_res
 
 # Help us to find places to insert values
 recipe_progression = ['SUMMARY', 'DESCRIPTION', 'AUTHOR', 'HOMEPAGE', 'BUGTRACKER', 'SECTION', 'LICENSE', 'LICENSE_FLAGS', 'LIC_FILES_CHKSUM', 'PROVIDES', 'DEPENDS', 'PR', 'PV', 'SRCREV', 'SRCPV', 'SRC_URI', 'S', 'do_fetch()', 'do_unpack()', 'do_patch()', 'EXTRA_OECONF', 'EXTRA_OECMAKE', 'EXTRA_OESCONS', 'do_configure()', 'EXTRA_OEMAKE', 'do_compile()', 'do_install()', 'do_populate_sysroot()', 'INITSCRIPT', 'USERADD', 'GROUPADD', 'PACKAGES', 'FILES', 'RDEPENDS', 'RRECOMMENDS', 'RSUGGESTS', 'RPROVIDES', 'RREPLACES', 'RCONFLICTS', 'ALLOW_EMPTY', 'populate_packages()', 'do_package()', 'do_deploy()', 'BBCLASSEXTEND']
@@ -928,6 +928,15 @@ def find_layerdir(fn):
             return None
     return layerdir
 
+def get_layer_name(fn, d):
+    """ Get the layer name from a filename """
+    pth = os.path.abspath(fn)
+    collection = get_collection_res(d)
+    # reverse ordering by length to catch nested layers
+    for k, v in dict(sorted(collection.items(), key=lambda item: len(item[1]), reverse=True)).items():
+        if re.match(v, pth):
+            return k
+    return ""
 
 def replace_dir_vars(path, d):
     """Replace common directory paths with appropriate variable references (e.g. /etc becomes ${sysconfdir})"""
