Browse Source

build: Simplify target registration and add product input

Modify the inputs of the target registration to accept
multiple variants and allow for an optional "product"
parameter.

Also some refactoring done to promote ease of use.

Change-Id: Ib18cd36734fc35326b8dc675f548d47d9a95cf1c
Signed-off-by: Eric Rosas <[email protected]>
Eric Rosas 1 year ago
parent
commit
b7677782ea
4 changed files with 89 additions and 58 deletions
  1. 3 2
      build/blair.bzl
  2. 3 2
      build/kalama.bzl
  3. 3 2
      build/pineapple.bzl
  4. 80 52
      module_mgr.bzl

+ 3 - 2
build/blair.bzl

@@ -1,9 +1,10 @@
 load(":audio_modules.bzl", "audio_modules")
-load(":module_mgr.bzl", "define_consolidate_gki_modules")
+load(":module_mgr.bzl", "define_target_modules")
 
 def define_blair():
-    define_consolidate_gki_modules(
+    define_target_modules(
         target = "blair",
+        variants = ["consolidate", "gki"],
         registry = audio_modules,
         modules = [
             "q6_dlkm",

+ 3 - 2
build/kalama.bzl

@@ -1,9 +1,10 @@
 load(":audio_modules.bzl", "audio_modules")
-load(":module_mgr.bzl", "define_consolidate_gki_modules")
+load(":module_mgr.bzl", "define_target_modules")
 
 def define_kalama():
-    define_consolidate_gki_modules(
+    define_target_modules(
         target = "kalama",
+        variants = ["consolidate", "gki"],
         registry = audio_modules,
         modules = [
             "q6_dlkm",

+ 3 - 2
build/pineapple.bzl

@@ -1,9 +1,10 @@
 load(":audio_modules.bzl", "audio_modules")
-load(":module_mgr.bzl", "define_consolidate_gki_modules")
+load(":module_mgr.bzl", "define_target_modules")
 
 def define_pineapple():
-    define_consolidate_gki_modules(
+    define_target_modules(
         target = "pineapple",
+        variants = ["consolidate", "gki"],
         registry = audio_modules,
         modules = [
             "q6_dlkm",

+ 80 - 52
module_mgr.bzl

@@ -1,7 +1,5 @@
 load("//build/bazel_common_rules/dist:dist.bzl", "copy_to_dist_dir")
-load("//build/kernel/kleaf:kernel.bzl", "kernel_module",
-                                        "kernel_modules_install",
-                                        "ddk_module",
+load("//build/kernel/kleaf:kernel.bzl", "ddk_module",
                                         "ddk_submodule")
 
 def _create_module_conditional_src_map(conditional_srcs):
@@ -17,7 +15,24 @@ def _create_module_conditional_src_map(conditional_srcs):
 
     return processed_conditional_srcs
 
-def _get_module_srcs(module, options, formatter):
+def _get_enabled_module_objs(registry, modules):
+    undefined_modules = []
+    enabled_module_objs = []
+
+    for module_name in modules:
+        module_obj = registry.get(module_name)
+
+        if not module_obj:
+            undefined_modules.append(module_name)
+        else:
+            enabled_module_objs.append(module_obj)
+
+    if undefined_modules:
+        fail("FAILED. Tried to enable the following undefined modules: \n{}".format("\n".join(undefined_modules)))
+    else:
+        return enabled_module_objs
+
+def _get_module_srcs(module, options):
     srcs = [] + module.srcs
     module_path = "{}/".format(module.path) if module.path else ""
 
@@ -25,49 +40,29 @@ def _get_module_srcs(module, options, formatter):
         is_option_enabled = option in options
         srcs.extend(module.conditional_srcs[option].get(is_option_enabled, []))
 
-    return ["{}{}".format(module_path, formatter(src)) for src in srcs]
+    return ["{}{}".format(module_path, src) for src in srcs]
 
-def _combine_target_module_options(modules, config_options):
+def _combine_target_module_options(enabled_modules, config_options):
     all_options = {option: True for option in config_options}
-    all_options = all_options | {module.config_option: True for module in modules if module.config_option}
-
-    return all_options
-
-def create_module_registry(hdrs = []):
-    module_map = {}
-
-    def register(name, path = None, config_option = None, srcs = [], conditional_srcs = {}, deps = []):
-        module_map[name] = struct(
-            name = name,
-            path = path,
-            srcs = srcs,
-            conditional_srcs = _create_module_conditional_src_map(conditional_srcs),
-            config_option = config_option,
-            deps = deps,
-        )
-
-    return struct(
-        module_map = module_map,
-        hdrs = hdrs,
-        register = register,
-        get = module_map.get,
-    )
-
-def define_target_modules(target, variant, registry, modules, config_options = []):
-    formatter = lambda s : s.replace("%t", target)\
-                            .replace("%v", variant)\
-                            .replace("%b", "{}_{}".format(target, variant))
-    kernel_build = "{}_{}".format(target, variant)
-    kernel_build_label = "//msm-kernel:{}".format(kernel_build)
-    modules = [registry.get(module_name) for module_name in modules]
-    options = _combine_target_module_options(modules, config_options)
+    modules_options = {module.config_option: True for module in enabled_modules if module.config_option}
+
+    return all_options | modules_options
+
+def _define_target_modules(target, variant, registry, modules, product = None, config_options = []):
+    dep_formatter = lambda s : s.replace("%t", target)\
+                                .replace("%v", variant)\
+                                .replace("%p", product if product else "")\
+                                .replace("%b", "{}_{}".format(target, variant))
+    rule_prefix = "{}_{}_{}".format(target, variant, product) if product else "{}_{}".format(target, variant)
+    enabled_modules = _get_enabled_module_objs(registry, modules)
+    options = _combine_target_module_options(enabled_modules, config_options)
     headers = ["//msm-kernel:all_headers"] + registry.hdrs
-    all_module_rules = []
+    submodule_rules = []
 
-    for module in modules:
-        rule_name = "{}_{}".format(kernel_build, module.name)
-        srcs = _get_module_srcs(module, options, formatter)
-        deps = headers + [formatter(dep) for dep in module.deps]
+    for module in enabled_modules:
+        rule_name = "{}_{}".format(rule_prefix, module.name)
+        srcs = _get_module_srcs(module, options)
+        deps = headers + [dep_formatter(dep) for dep in module.deps]
 
         if not srcs:
             continue
@@ -80,17 +75,17 @@ def define_target_modules(target, variant, registry, modules, config_options = [
             local_defines = options.keys(),
         )
 
-        all_module_rules.append(rule_name)
+        submodule_rules.append(rule_name)
 
     ddk_module(
-        name = "{}_modules".format(kernel_build),
-        kernel_build = kernel_build_label,
-        deps = all_module_rules
+        name = "{}_modules".format(rule_prefix),
+        kernel_build = "//msm-kernel:{}_{}".format(target, variant),
+        deps = submodule_rules
     )
 
     copy_to_dist_dir(
-        name = "{}_modules_dist".format(kernel_build),
-        data = [":{}_modules".format(kernel_build)],
+        name = "{}_modules_dist".format(rule_prefix),
+        data = [":{}_modules".format(rule_prefix)],
         dist_dir = "out/target/product/{}/dlkm/lib/modules/".format(target),
         flat = True,
         wipe_dist_dir = False,
@@ -99,6 +94,39 @@ def define_target_modules(target, variant, registry, modules, config_options = [
         log = "info"
     )
 
-def define_consolidate_gki_modules(target, registry, modules, config_options = []):
-    define_target_modules(target, "consolidate", registry, modules, config_options)
-    define_target_modules(target, "gki", registry, modules, config_options)
+def create_module_registry(hdrs = []):
+    module_map = {}
+
+    def register(name, path = None, config_option = None, srcs = [], conditional_srcs = {}, deps = []):
+        module_map[name] = struct(
+            name = name,
+            path = path,
+            srcs = srcs,
+            conditional_srcs = _create_module_conditional_src_map(conditional_srcs),
+            config_option = config_option,
+            deps = deps,
+        )
+
+    return struct(
+        module_map = module_map,
+        hdrs = hdrs,
+        register = register,
+        get = module_map.get,
+    )
+
+def define_target_modules(target, variants, registry, modules, config_options = [], products = []):
+    for variant in variants:
+        if products:
+            for product in products:
+                _define_target_modules(target = target,
+                                       variant = variant,
+                                       registry = registry,
+                                       modules = modules,
+                                       product = product,
+                                       config_options = config_options)
+        else:
+            _define_target_modules(target = target,
+                                   variant = variant,
+                                   registry = registry,
+                                   modules = modules,
+                                   config_options = config_options)