Autogenerate a system_dlkm android_filesystem

This .img file will package the .ko files returned by
`BOARD_SYSTEM_KERNEL_MODULES` (plus some other files). The .ko files
will be clustered by directory, and a `prebuilt_kernel_modules` will
be automatically created for each cluster. These will then be added to
the deps of the top-level system_dlkm android_filesystem.

Bug: 376726109
Test: m out/soong/.intermediates/build/soong/fsgen/soong_filesystem_creator/android_common/diff_test_aosp_cf_x86_64_phone_generated_system_dlkm_image.txt
KATI only installed file(s):
  etc/NOTICE.xml.gz
  etc/build.prop
  etc/fs_config_dirs
  etc/fs_config_files

Change-Id: I96669ad9595379c4de4c67331fc56bfa8d93e036
diff --git a/fsgen/filesystem_creator.go b/fsgen/filesystem_creator.go
index 06e154a..7ef7d99 100644
--- a/fsgen/filesystem_creator.go
+++ b/fsgen/filesystem_creator.go
@@ -25,6 +25,7 @@
 
 	"android/soong/android"
 	"android/soong/filesystem"
+	"android/soong/kernel"
 
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/parser"
@@ -115,6 +116,9 @@
 		if ctx.DeviceConfig().BuildingOdmImage() && ctx.DeviceConfig().OdmPath() == "odm" {
 			generatedPartitions = append(generatedPartitions, "odm")
 		}
+		if partitionVars.BuildingSystemDlkmImage {
+			generatedPartitions = append(generatedPartitions, "system_dlkm")
+		}
 
 		return &FsGenState{
 			depCandidates: candidates,
@@ -160,6 +164,7 @@
 					"com.android.vndk.v33": defaultDepCandidateProps(ctx.Config()),
 					"com.android.vndk.v34": defaultDepCandidateProps(ctx.Config()),
 				},
+				"system_dlkm": {},
 			},
 			soongGeneratedPartitions:  generatedPartitions,
 			fsDepsMutex:               sync.Mutex{},
@@ -500,9 +505,19 @@
 		fsProps.Linkerconfig.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
 	}
 
+	if partitionType == "system_dlkm" {
+		kernelModules := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules
+		f.createPrebuiltKernelModules(ctx, partitionType, kernelModules)
+	}
+
 	var module android.Module
 	if partitionType == "system" {
 		module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
+	} else if partitionType == "system_dlkm" {
+		// Do not set partition_type. build/soong/android/paths#modulePartition currently does not support dlkm
+		// partitions. Since `android_filesystem` uses a partition based filter, setting the partition here
+		// would result in missing in entries.
+		module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
 	} else {
 		// Explicitly set the partition.
 		fsProps.Partition_type = proptools.StringPtr(partitionType)
@@ -531,6 +546,41 @@
 	return true
 }
 
+// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
+// autogenerated *_dlkm filsystem modules.
+// The input `kernelModules` is a space separated list of .ko files in the workspace. This will be partitioned per directory
+// and a `prebuilt_kernel_modules` will be created per partition.
+// These autogenerated modules will be subsequently added to the deps of the top level *_dlkm android_filesystem
+func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string, kernelModules []string) {
+	// Partition the files per directory
+	dirToFiles := map[string][]string{}
+	for _, kernelModule := range kernelModules {
+		dir := filepath.Dir(kernelModule)
+		base := filepath.Base(kernelModule)
+		dirToFiles[dir] = append(dirToFiles[dir], base)
+	}
+	// Create a prebuilt_kernel_modules module per partition
+	fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
+	for index, dir := range android.SortedKeys(dirToFiles) {
+		name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules-%s", partitionType, strconv.Itoa(index)))
+		props := &struct {
+			Name *string
+			Srcs []string
+		}{
+			Name: proptools.StringPtr(name),
+			Srcs: dirToFiles[dir],
+		}
+		kernelModule := ctx.CreateModuleInDirectory(
+			kernel.PrebuiltKernelModulesFactory,
+			dir,
+			props,
+		)
+		kernelModule.HideFromMake()
+		// Add to deps
+		(*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
+	}
+}
+
 // createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
 // 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
 // 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)