Snap for 10302501 from 80393205752fd06790460d06e8704d8577cdd399 to android13-gs-pixel-5.10-release

Change-Id: Ibc0e2e11f281c8036e84138d5ab7fd518dee0170
diff --git a/kleaf/common_kernels.bzl b/kleaf/common_kernels.bzl
index 6c3464d..a040f31 100644
--- a/kleaf/common_kernels.bzl
+++ b/kleaf/common_kernels.bzl
@@ -11,9 +11,9 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+"""Functions that are useful in the common kernel package (usually `//common`)."""
 
 load("@bazel_skylib//lib:dicts.bzl", "dicts")
-load("@bazel_skylib//lib:paths.bzl", "paths")
 load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
 load(
     ":kernel.bzl",
@@ -30,7 +30,6 @@
 load("//build/bazel_common_rules/dist:dist.bzl", "copy_to_dist_dir")
 load("//build/kernel/kleaf/artifact_tests:kernel_test.bzl", "initramfs_modules_options_test")
 load("//build/kernel/kleaf/impl:gki_artifacts.bzl", "gki_artifacts")
-load("//build/kernel/kleaf/impl:utils.bzl", "utils")
 load(
     "//build/kernel/kleaf/impl:constants.bzl",
     "MODULE_OUTS_FILE_OUTPUT_GROUP",
@@ -96,6 +95,9 @@
 # Always collect_unstripped_modules for common kernels.
 _COLLECT_UNSTRIPPED_MODULES = True
 
+# Always keep a copy of Module.symvers for common kernels.
+_KEEP_MODULE_SYMVERS = True
+
 # glob() must be executed in a BUILD thread, so this cannot be a global
 # variable.
 def _default_target_configs():
@@ -174,6 +176,7 @@
         ))
     return ret
 
+# buildifier: disable=unnamed-macro
 def define_common_kernels(
         branch = None,
         target_configs = None,
@@ -509,6 +512,7 @@
             # Sync with KMI_SYMBOL_LIST_MODULE_GROUPING
             module_grouping = None,
             collect_unstripped_modules = _COLLECT_UNSTRIPPED_MODULES,
+            keep_module_symvers = _KEEP_MODULE_SYMVERS,
             toolchain_version = toolchain_version,
             **kernel_build_abi_kwargs
         )
@@ -814,7 +818,7 @@
 
     kernel_images(
         name = test_name + "_fake_images",
-        kernel_modules_install = kernel_build_name + "_modules_install",
+        kernel_modules_install = kernel_modules_install,
         build_initramfs = True,
         modules_options = fake_modules_options,
     )
@@ -833,7 +837,7 @@
 
     kernel_images(
         name = test_name + "_empty_images",
-        kernel_modules_install = kernel_build_name + "_modules_install",
+        kernel_modules_install = kernel_modules_install,
         build_initramfs = True,
         # Not specify module_options
     )
diff --git a/kleaf/constants.bzl b/kleaf/constants.bzl
index 0b57ac0..dbc018f 100644
--- a/kleaf/constants.bzl
+++ b/kleaf/constants.bzl
@@ -11,13 +11,15 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+"""
+Utility public constants.
+"""
 
 load(
     "//build/kernel/kleaf/impl:constants.bzl",
     "AARCH64_IMAGES",
     "GKI_ARTIFACTS_AARCH64_OUTS",
     "MODULES_STAGING_ARCHIVE",
-    "MODULE_OUTS_FILE_OUTPUT_GROUP",
     "MODULE_OUTS_FILE_SUFFIX",
     "SYSTEM_DLKM_OUTS",
     "TOOLCHAIN_VERSION_FILENAME",
diff --git a/kleaf/impl/kernel_build.bzl b/kleaf/impl/kernel_build.bzl
index d62cbd7..4a0e473 100644
--- a/kleaf/impl/kernel_build.bzl
+++ b/kleaf/impl/kernel_build.bzl
@@ -11,6 +11,9 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+"""
+Defines a kernel build target.
+"""
 
 load("@bazel_skylib//lib:paths.bzl", "paths")
 load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
@@ -61,6 +64,7 @@
         name,
         build_config,
         outs,
+        keep_module_symvers = None,
         srcs = None,
         module_outs = None,
         implicit_outs = None,
@@ -100,6 +104,10 @@
         name: The final kernel target name, e.g. `"kernel_aarch64"`.
         build_config: Label of the build.config file, e.g. `"build.config.gki.aarch64"`.
         kconfig_ext: Label of an external Kconfig.ext file sourced by the GKI kernel.
+        keep_module_symvers: If set to True, a copy of the default output `Module.symvers` is kept.
+          * To avoid collisions in mixed build distribution packages, the file is renamed
+            as `$(name)_Module.symvers`.
+          * Default is False.
         srcs: The kernel sources (a `glob()`). If unspecified or `None`, it is the following:
           ```
           glob(
@@ -257,10 +265,10 @@
 
           Labels are created for each item in `module_implicit_outs` as in `outs`.
 
-        kmi_symbol_list: A label referring to the main KMI symbol list file. See `additional_kmi_symbol_list`.
+        kmi_symbol_list: A label referring to the main KMI symbol list file. See `additional_kmi_symbol_lists`.
 
           This is the Bazel equivalent of `ADDTIONAL_KMI_SYMBOL_LISTS`.
-        additional_kmi_symbol_list: A list of labels referring to additional KMI symbol list files.
+        additional_kmi_symbol_lists: A list of labels referring to additional KMI symbol list files.
 
           This is the Bazel equivalent of `ADDTIONAL_KMI_SYMBOL_LISTS`.
 
@@ -323,7 +331,8 @@
           If the value is `"false"`; or the value is `"auto"` and
           `--kbuild_symtypes` is not specified, then `KBUILD_SYMTYPES=`.
         toolchain_version: The toolchain version to depend on.
-        kwargs: Additional attributes to the internal rule, e.g.
+        dtstree: Device tree support.
+        **kwargs: Additional attributes to the internal rule, e.g.
           [`visibility`](https://docs.bazel.build/versions/main/visibility.html).
           See complete list
           [here](https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes).
@@ -349,7 +358,7 @@
         )
 
     internal_kwargs = dict(kwargs)
-    internal_kwargs.pop("visibility", default = None)
+    internal_kwargs.pop("visibility", None)
 
     kwargs_with_manual = dict(kwargs)
     kwargs_with_manual["tags"] = ["manual"]
@@ -414,6 +423,7 @@
     _kernel_build(
         name = name,
         config = config_target_name,
+        keep_module_symvers = keep_module_symvers,
         srcs = srcs,
         outs = kernel_utils.transform_kernel_build_outs(name, "outs", outs),
         module_outs = kernel_utils.transform_kernel_build_outs(name, "module_outs", module_outs),
@@ -697,6 +707,20 @@
             symtypes_dir = symtypes_dir.path,
         )
 
+    copy_module_symvers_cmd = ""
+    module_symvers_copy = None
+    if ctx.attr.keep_module_symvers:
+        module_symvers_copy = ctx.actions.declare_file("{}/{}_Module.symvers".format(
+            ctx.label.name,
+            ctx.label.name,
+        ))
+        command_outputs.append(module_symvers_copy)
+        copy_module_symvers_cmd = """
+           cp -f ${{OUT_DIR}}/Module.symvers {module_symvers_copy}
+        """.format(
+            module_symvers_copy = module_symvers_copy.path,
+        )
+
     command += """
          # Actual kernel build
            {interceptor_command_prefix} make -C ${{KERNEL_DIR}} ${{TOOL_ARGS}} O=${{OUT_DIR}} ${{MAKE_GOALS}}
@@ -725,6 +749,8 @@
            {grab_intree_modules_cmd}
          # Grab unstripped in-tree modules
            {grab_unstripped_intree_modules_cmd}
+         # Make a copy of Module.symvers
+           {copy_module_symvers_cmd}
          # Check if there are remaining *.ko files
            remaining_ko_files=$({check_declared_output_list} \\
                 --declared $(cat {all_module_names_file} {base_kernel_all_module_names_file_path}) \\
@@ -758,6 +784,7 @@
         out_dir_kernel_headers_tar = out_dir_kernel_headers_tar.path,
         interceptor_command_prefix = interceptor_command_prefix,
         label = ctx.label,
+        copy_module_symvers_cmd = copy_module_symvers_cmd,
     )
 
     debug.print_scripts(ctx, command)
@@ -851,6 +878,8 @@
     default_info_files.append(all_module_names_file)
     if kmi_strict_mode_out:
         default_info_files.append(kmi_strict_mode_out)
+    if ctx.attr.keep_module_symvers:
+        default_info_files.append(module_symvers_copy)
     default_info = DefaultInfo(
         files = depset(default_info_files),
         # For kernel_build_test
@@ -880,6 +909,9 @@
             aspects = [kernel_toolchain_aspect],
             doc = "the kernel_config target",
         ),
+        "keep_module_symvers": attr.bool(
+            doc = "If true, a copy of `Module.symvers` is kept, with the name `{name}_Module.symvers`",
+        ),
         "srcs": attr.label_list(mandatory = True, doc = "kernel sources", allow_files = True),
         "outs": attr.string_list(),
         "module_outs": attr.string_list(doc = "output *.ko files"),
@@ -930,6 +962,7 @@
     },
 )
 
+#buildifier: disable=return-value
 def _kernel_build_check_toolchain(ctx):
     """
     Check toolchain_version is the same as base_kernel.
@@ -941,6 +974,7 @@
     base_toolchain_file = utils.getoptattr(base_kernel[KernelToolchainInfo], "toolchain_version_file")
 
     if base_toolchain == None and base_toolchain_file == None:
+        # buildifier: disable=print
         print(("\nWARNING: {this_label}: No check is performed between the toolchain " +
                "version of the base build ({base_kernel}) and the toolchain version of " +
                "{this_name} ({this_toolchain}), because the toolchain version of {base_kernel} " +