From 0738ecd35663cec928d5c7a7b786b8a6b8c0102a Mon Sep 17 00:00:00 2001
From: Andrei Gherzan <andrei.gherzan@huawei.com>
Date: Thu, 18 Aug 2022 14:20:00 +0200
Subject: [PATCH] oniro-sanity.bbclass: Check for bblayers.conf.samples
 incompatibility

The logic is similar to how oe-core and poky handle their set of sample
configurations. The core difference is the fact that we only flag
currently (no automatic migrations yet) by providing a helpful build
error before parsing. In this way we can instruct an user to update
their bblayers.conf when the Oniro introduced new lateyers integrations.

The variables of interest here are ONIRO_LAYERS_CONF_VERSION and
ONIRO_REQUIRED_LAYERS_CONF_VERSION. Whenever the project's
bblayers.conf.sample changes, we need to bump both of these values.

Signed-off-by: Andrei Gherzan <andrei.gherzan@huawei.com>
---
 meta-oniro-core/classes/oniro-sanity.bbclass | 51 ++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/meta-oniro-core/classes/oniro-sanity.bbclass b/meta-oniro-core/classes/oniro-sanity.bbclass
index 18a25718..3713b7b0 100644
--- a/meta-oniro-core/classes/oniro-sanity.bbclass
+++ b/meta-oniro-core/classes/oniro-sanity.bbclass
@@ -72,3 +72,54 @@ python oniro_sanity_handler() {
 
 addhandler oniro_sanity_handler
 oniro_sanity_handler[eventmask] = "bb.event.BuildStarted"
+
+#
+# Build configuration checks and migrations
+#
+
+# Ensure our function runs after the OE-Core's one.
+BBLAYERS_CONF_UPDATE_FUNCS += "conf/bblayers.conf:ONIRO_LAYERS_CONF_VERSION:ONIRO_REQUIRED_LAYERS_CONF_VERSION:oniro_update_bblayersconf"
+
+# All flavours take advanatge of the same bblayers.conf.sample file. The linux
+# one is the actual file while all the others are symlinks to it.
+ONIRO_LAYERS_CONF_SAMPLE = "${ONIRO_COREBASE}/flavours/${ONIRO_FLAVOUR}/bblayers.conf.sample"
+
+python oniro_update_bblayersconf() {
+    version = int(d.getVar('ONIRO_LAYERS_CONF_VERSION', True) or -1)
+    required_version = int(d.getVar('ONIRO_REQUIRED_LAYERS_CONF_VERSION', True) or -1)
+    upgrade_fail_msg = """You need to update the build's bblayers.conf manually for this version transition.
+
+Compare your build\'s bblayers.conf with the Oniro\'s bblayers.conf.sample and merge
+any changes before continuing. You can take advantage of a tool like "${SANITY_DIFF_TOOL}":
+
+"${SANITY_DIFF_TOOL} conf/bblayers.conf ${ONIRO_LAYERS_CONF_SAMPLE}"
+
+Note:
+ Once ONIRO_LAYERS_CONF_VERSION (currently ${ONIRO_LAYERS_CONF_VERSION}) value in conf/bblayers.conf matches the
+ ONIRO_REQUIRED_LAYERS_CONF_VERSION (currently ${ONIRO_REQUIRED_LAYERS_CONF_VERSION}) value in the Oniro build metadata,
+ this error will go away.
+"""
+    upgrade_fail_msg = d.expand(upgrade_fail_msg)
+    downgrade_fail_msg = """Your build's bblayers.conf was generated from a newer build metadata.
+
+Compare your build\'s bblayers.conf with the Oniro\'s bblayers.conf.sample and merge
+any changes before continuing. You can take advantage of a tool like \"${SANITY_DIFF_TOOL}\":
+
+${SANITY_DIFF_TOOL} conf/bblayers.conf ${ONIRO_LAYERS_CONF_SAMPLE}
+
+Note:
+ Once ONIRO_LAYERS_CONF_VERSION (currently ${ONIRO_LAYERS_CONF_VERSION}) value in conf/bblayers.conf matches the
+ ONIRO_REQUIRED_LAYERS_CONF_VERSION (currently ${ONIRO_REQUIRED_LAYERS_CONF_VERSION}) value in the Oniro build metadata,
+ this error will go away.
+"""
+    downgrade_fail_msg = d.expand(downgrade_fail_msg)
+
+    if version == -1 or required_version == -1:
+        bb.fatal(upgrade_fail_msg)
+    elif version < required_version:
+        bb.fatal(upgrade_fail_msg)
+    elif version > required_version:
+        bb.fatal(downgrade_fail_msg)
+
+    bb.fatal("You need to update bblayers.conf manually for this version transition.")
+}
-- 
GitLab