return True
     return False
 
+
+def check_license_flags(d):
+    """
+    This function checks if a recipe has any LICENSE_FLAGs that
+    aren't whitelisted.
+
+    If it does, it returns the first LICENSE_FLAG missing from the
+    whitelist, or all the LICENSE_FLAGs if there is no whitelist.
+
+    If everything is is properly whitelisted, it returns None.
+    """
+
+    def license_flag_matches(flag, whitelist, pn):
+        """
+        Return True if flag matches something in whitelist, None if not.
+
+        Before we test a flag against the whitelist, we append _${PN}
+        to it.  We then try to match that string against the
+        whitelist.  This covers the normal case, where we expect
+        LICENSE_FLAGS to be a simple string like 'commercial', which
+        the user typically matches exactly in the whitelist by
+        explicitly appending the package name e.g 'commercial_foo'.
+        If we fail the match however, we then split the flag across
+        '_' and append each fragment and test until we either match or
+        run out of fragments.
+        """
+        flag_pn = ("%s_%s" % (flag, pn))
+        for candidate in whitelist:
+            if flag_pn == candidate:
+                    return True
+
+        flag_cur = ""
+        flagments = flag_pn.split("_")
+        flagments.pop() # we've already tested the full string
+        for flagment in flagments:
+            if flag_cur:
+                flag_cur += "_"
+            flag_cur += flagment
+            for candidate in whitelist:
+                if flag_cur == candidate:
+                    return True
+        return False
+
+    def all_license_flags_match(license_flags, whitelist):
+        """ Return first unmatched flag, None if all flags match """
+        pn = d.getVar('PN', True)
+        split_whitelist = whitelist.split()
+        for flag in license_flags.split():
+            if not license_flag_matches(flag, split_whitelist, pn):
+                return flag
+        return None
+
+    license_flags = d.getVar('LICENSE_FLAGS', True)
+    if license_flags:
+        whitelist = d.getVar('LICENSE_FLAGS_WHITELIST', True)
+        if not whitelist:
+            return license_flags
+        unmatched_flag = all_license_flags_match(license_flags, whitelist)
+        if unmatched_flag:
+            return unmatched_flag
+    return None
+
+
 SSTATETASKS += "do_populate_lic"
 do_populate_lic[sstate-name] = "populate-lic"
 do_populate_lic[sstate-inputdirs] = "${LICSSTATEDIR}"