[OE-core] [PATCH 2/2] utils.py: helper function for optional include files

Patrick Ohly patrick.ohly at intel.com
Wed Jun 7 15:31:33 UTC 2017

By using oe.utils.optional_includes(), developers can simplify the
code which selects which additional include files need to be included
in a .bbappend.

In the simple case (one distro feature and one include file) the code
is not shorter, but the intent is clearer than corresponding code
using bb.utils.contains():

   require ${@ oe.utils.optional_includes(d, 'foo-feature:bar.inc') }

More complex cases are also supported, in particular include files
that are required for one of several distro features or multiple
different include files.

To keep the common use case simple, DISTRO_FEATURES are checked by
default. Checking IMAGE_FEATURES might also be useful.

The DISTRO_FEATURES default and the intended usage make this more
suitable for OE-core than bitbake.

Signed-off-by: Patrick Ohly <patrick.ohly at intel.com>
 meta/lib/oe/utils.py | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/meta/lib/oe/utils.py b/meta/lib/oe/utils.py
index 330a5ff..a6c6199 100644
--- a/meta/lib/oe/utils.py
+++ b/meta/lib/oe/utils.py
@@ -126,6 +126,35 @@ def features_backfill(var,d):
     if addfeatures:
         d.appendVar(var, " " + " ".join(addfeatures))
+def optional_includes(d, mapping, key_var="DISTRO_FEATURES"):
+    """
+    This can be used to generate a list of files to include depending on
+    the distro features that are selected. key_var contains the features
+    that are set, mapping_var a space-separated set of <feature(s)>:<file(s)>
+    entries. Features and files are separated by comma. Each file on the
+    right-hand side is included in the result once if any of the features one
+    the left-hand side is set.
+    Example:
+       require ${@ oe.utils.optional_includes(d, "foo,bar:foo-or-bar.inc xyz:x.inc,y.inc,z.inc")}
+    For DISTRO_FEATURES = "foo xyz" that will include four .inc files in the
+    order in which they are listed.
+    """
+    key = set((d.getVar(key_var) or "").split())
+    mapping = mapping.split()
+    includes = []
+    for entry in mapping:
+        parts = entry.split(":", 1)
+        if len(parts) != 2:
+            bb.fatal("%s must contain entries of the form <feature(s)>:<file(s)>, not %s" % (mapping_var, entry))
+        features, files = parts
+        for feature in features.split(","):
+            if feature in key:
+                for file in files.split(","):
+                    if file not in includes:
+                        includes.append(file)
+    return " ".join(includes)
 def packages_filter_out_system(d):
git-series 0.9.1

More information about the Openembedded-core mailing list