From ab7d94c98db300b0b2010d6eb53b053d082640e0 Mon Sep 17 00:00:00 2001
From: Esben Haabendal <esben.haabendal@huawei.com>
Date: Thu, 7 Apr 2022 11:53:03 +0200
Subject: [PATCH] Split basic functionality gn.bbclass into gn_base.bbclass

This provides a gn_base.bbclass which can be used instead of gn.bbclass in
recipes where a different method for managing toolchain configuration is used.

Note, any recipes calling gn_do_configure or gn_do_compile will need to call the
new gn_base_do_configure and gn_base_do_compile respectively instead.

Signed-off-by: Esben Haabendal <esben.haabendal@huawei.com>
---
 meta-oniro-staging/classes/gn.bbclass      | 83 +-------------------
 meta-oniro-staging/classes/gn_base.bbclass | 88 ++++++++++++++++++++++
 2 files changed, 89 insertions(+), 82 deletions(-)
 create mode 100644 meta-oniro-staging/classes/gn_base.bbclass

diff --git a/meta-oniro-staging/classes/gn.bbclass b/meta-oniro-staging/classes/gn.bbclass
index dedbc017..a09cb089 100644
--- a/meta-oniro-staging/classes/gn.bbclass
+++ b/meta-oniro-staging/classes/gn.bbclass
@@ -14,7 +14,7 @@
 # function. For more information please refer do_check_yocto_toolchain_is_used
 # task below. Lastly, recipe has to provide do_install task.
 
-DEPENDS += "gn-native ninja-native"
+inherit gn_base
 
 # Location of the GN toolchain template file, used in write_toolchain_file(). It
 # can be overridden in the recipe as needed. The name of the GN toolchain
@@ -27,61 +27,6 @@ GN_TOOLCHAIN_TMPL_FILE ??= "//build/toolchain/gcc_toolchain.gni"
 GN_TARGET_TOOLCHAIN_TMPL_LABEL ??= "gcc_toolchain"
 GN_HOST_TOOLCHAIN_TMPL_LABEL ??= "gcc_toolchain"
 
-# General GN options, like --dotfile
-GN_OPTIONS ??= ""
-
-# GN_ARGS can be added in the recipe
-GN_ARGS ?= ' \
-            target_cpu="${@gn_target_arch_name(d)}" \
-'
-
-# NINJA_ARGS can be added in the recipe
-NINJA_ARGS ?= ""
-
-B = "${WORKDIR}/out"
-
-do_configure[cleandirs] = "${B}"
-gn_do_configure() {
-    cd ${S}
-    gn gen ${GN_OPTIONS} --args='${GN_ARGS}' ${B}
-}
-
-gn_do_compile() {
-    ninja ${NINJA_ARGS} -C ${B}
-}
-
-gn_do_install() {
-    bbfatal " \
-        Missing do_install task definition! \
-        GN projects don't usually follow any particular convention with regards \
-        to build artifacts, therefore do_install task has to be defined in the \
-        project's recipe. \
-    "
-}
-
-# GN fails with unclear and confusing error logs when build directory is the
-# same as source directory. To avoid that build directory is set to
-# "S{WORKDIR}/out". Nevertheless let's make sure that B != S as devtool default
-# behaviour is to override B variable to be the same as S.
-#
-# NOTE: devtool adds bbappend file which makes recipe to inherit externalsrc
-# class with EXTERNALSRC set to workspace/sources/<recipe-name> and by default
-# EXTERNALSRC_BUILD set to the same value as EXTERNALSRC. To change this
-# behavior --no-same-dir option has to be passed to devtool add command.
-python do_check_B_is_not_S() {
-    bpath = os.path.abspath(d.expand("${B}"))
-    spath = os.path.abspath(d.expand("${S}"))
-    if os.path.abspath(d.expand("${S}")) == os.path.abspath(d.expand("${B}")):
-        bb.fatal('''
-GN requires build and sources directories to be different. By default build
-directory is set to ${WORKDIR}/out. If you're using devtool remember to use
---no-same-dir option, e.g.:
-devtool add --no-same-dir <your-gn-project-name> <your-gn-project-git-url>
-        ''')
-}
-
-addtask check_B_is_not_S after do_patch before do_configure
-
 python do_write_gn_toolchain_file () {
     root_gn_dir = d.expand("${S}")
     # Do not modify the below part: for simplicity hardcoded GN references:
@@ -133,8 +78,6 @@ set_defaults(\"shared_library\") {\n\
 
 addtask do_check_yocto_toolchain_is_used after do_configure before do_compile
 
-EXPORT_FUNCTIONS do_configure do_compile do_install
-
 def is_clang(cc: str) -> bool:
     """ Returns True when the argument (cc) string contains the word `clang`;
     False otherwise"""
@@ -179,30 +122,6 @@ def gn_host_arch_name(d):
             return gn_arch_name
     bb.fatal('Unsuported BUILD_ARCH value: "%s"' % build_arch)
 
-# GN target architecture helpers.
-#
-# Determining the target architecture is more difficult, as there are many
-# different values we can use on the Yocto side (e.g. TUNE_ARCH, TARGET_ARCH,
-# MACHINEOVERRIDES etc). What we do is define the mapping with regular,
-# non-Python variables with overrides that are generic enough (i.e. "x86"
-# instead of "i586") and then use gn_target_arch_name() to return the right
-# value with some validation.
-GN_TARGET_ARCH_NAME:aarch64 = "arm64"
-GN_TARGET_ARCH_NAME:arm = "arm"
-GN_TARGET_ARCH_NAME:x86 = "x86"
-GN_TARGET_ARCH_NAME:x86-64 = "x64"
-GN_TARGET_ARCH_NAME:riscv32 = "riscv32"
-GN_TARGET_ARCH_NAME:riscv64 = "riscv64"
-
-def gn_target_arch_name(d):
-    """Returns a GN architecture name corresponding to the target machine's
-    architecture."""
-    name = d.getVar("GN_TARGET_ARCH_NAME")
-    if name is None:
-        bb.fatal('Unsupported target architecture. A valid override for the '
-                 'GN_TARGET_ARCH_NAME variable could not be found.')
-    return name
-
 def gn_toolchain_file_header(d):
     """Reurns GN toolchain file header as a multi-line string"""
 
diff --git a/meta-oniro-staging/classes/gn_base.bbclass b/meta-oniro-staging/classes/gn_base.bbclass
new file mode 100644
index 00000000..17166567
--- /dev/null
+++ b/meta-oniro-staging/classes/gn_base.bbclass
@@ -0,0 +1,88 @@
+# SPDX-FileCopyrightText: Huawei Inc.
+#
+# SPDX-License-Identifier: MIT
+
+# This class allows building GN-based projects.
+
+DEPENDS += "gn-native ninja-native"
+
+# General GN options, like --dotfile
+GN_OPTIONS ??= ""
+
+# GN_ARGS can be added in the recipe
+GN_ARGS ?= ' \
+            target_cpu="${@gn_target_arch_name(d)}" \
+'
+
+# NINJA_ARGS can be added in the recipe
+NINJA_ARGS ?= ""
+
+B = "${WORKDIR}/out"
+
+do_configure[cleandirs] = "${B}"
+gn_base_do_configure() {
+    cd ${S}
+    gn gen ${GN_OPTIONS} --args='${GN_ARGS}' ${B}
+}
+
+gn_base_do_compile() {
+    ninja ${NINJA_ARGS} -C ${B}
+}
+
+gn_base_do_install() {
+    bbfatal " \
+        Missing do_install task definition! \
+        GN projects don't usually follow any particular convention with regards \
+        to build artifacts, therefore do_install task has to be defined in the \
+        project's recipe. \
+    "
+}
+
+EXPORT_FUNCTIONS do_configure do_compile do_install
+
+# GN fails with unclear and confusing error logs when build directory is the
+# same as source directory. To avoid that build directory is set to
+# "S{WORKDIR}/out". Nevertheless let's make sure that B != S as devtool default
+# behaviour is to override B variable to be the same as S.
+#
+# NOTE: devtool adds bbappend file which makes recipe to inherit externalsrc
+# class with EXTERNALSRC set to workspace/sources/<recipe-name> and by default
+# EXTERNALSRC_BUILD set to the same value as EXTERNALSRC. To change this
+# behavior --no-same-dir option has to be passed to devtool add command.
+python do_check_B_is_not_S() {
+    bpath = os.path.abspath(d.expand("${B}"))
+    spath = os.path.abspath(d.expand("${S}"))
+    if os.path.abspath(d.expand("${S}")) == os.path.abspath(d.expand("${B}")):
+        bb.fatal('''
+GN requires build and sources directories to be different. By default build
+directory is set to ${WORKDIR}/out. If you're using devtool remember to use
+--no-same-dir option, e.g.:
+devtool add --no-same-dir <your-gn-project-name> <your-gn-project-git-url>
+        ''')
+}
+
+addtask check_B_is_not_S after do_patch before do_configure
+
+# GN target architecture helpers.
+#
+# Determining the target architecture is more difficult, as there are many
+# different values we can use on the Yocto side (e.g. TUNE_ARCH, TARGET_ARCH,
+# MACHINEOVERRIDES etc). What we do is define the mapping with regular,
+# non-Python variables with overrides that are generic enough (i.e. "x86"
+# instead of "i586") and then use gn_target_arch_name() to return the right
+# value with some validation.
+GN_TARGET_ARCH_NAME:aarch64 = "arm64"
+GN_TARGET_ARCH_NAME:arm = "arm"
+GN_TARGET_ARCH_NAME:x86 = "x86"
+GN_TARGET_ARCH_NAME:x86-64 = "x64"
+GN_TARGET_ARCH_NAME:riscv32 = "riscv32"
+GN_TARGET_ARCH_NAME:riscv64 = "riscv64"
+
+def gn_target_arch_name(d):
+    """Returns a GN architecture name corresponding to the target machine's
+    architecture."""
+    name = d.getVar("GN_TARGET_ARCH_NAME")
+    if name is None:
+        bb.fatal('Unsupported target architecture. A valid override for the '
+                 'GN_TARGET_ARCH_NAME variable could not be found.')
+    return name
-- 
GitLab