Reapply "Add super.img to android_device"

This reverts commit 4173c5bed6853af9a6ca44b1dcfafb6bb5848b43.

This resubmission moves the check that all partitions are specified
to execution time, because on aosp-main-future-without-vendor
BUILDING_VENDOR_IMAGE is false while
BOARD_GOOGLE_DYNAMIC_PARTITIONS_PARTITION_LIST still lists the vendor
partition.

Bug: 376727180
Test: Presubmits
Change-Id: I485574c98ba78f7eea3878135ff71fd6da94587e
diff --git a/fsgen/filesystem_creator.go b/fsgen/filesystem_creator.go
index a070e01..9d61a60 100644
--- a/fsgen/filesystem_creator.go
+++ b/fsgen/filesystem_creator.go
@@ -161,13 +161,14 @@
 		f.properties.Vbmeta_partition_names = append(f.properties.Vbmeta_partition_names, x.partitionName)
 	}
 
+	var superImageSubpartitions []string
 	if buildingSuperImage(partitionVars) {
-		createSuperImage(ctx, finalSoongGeneratedPartitions, partitionVars)
+		superImageSubpartitions = createSuperImage(ctx, finalSoongGeneratedPartitions, partitionVars)
 		f.properties.Super_image = ":" + generatedModuleNameForPartition(ctx.Config(), "super")
 	}
 
 	ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions = finalSoongGeneratedPartitions
-	f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names)
+	f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names, superImageSubpartitions)
 }
 
 func generatedModuleName(cfg android.Config, suffix string) string {
@@ -206,6 +207,7 @@
 	ctx android.LoadHookContext,
 	generatedPartitionTypes []string,
 	vbmetaPartitions []string,
+	superImageSubPartitions []string,
 ) {
 	baseProps := &struct {
 		Name         *string
@@ -217,19 +219,22 @@
 
 	// Currently, only the system and system_ext partition module is created.
 	partitionProps := &filesystem.PartitionNameProperties{}
-	if android.InList("system", generatedPartitionTypes) {
+	if f.properties.Super_image != "" {
+		partitionProps.Super_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "super"))
+	}
+	if android.InList("system", generatedPartitionTypes) && !android.InList("system", superImageSubPartitions) {
 		partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
 	}
-	if android.InList("system_ext", generatedPartitionTypes) {
+	if android.InList("system_ext", generatedPartitionTypes) && !android.InList("system_ext", superImageSubPartitions) {
 		partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
 	}
-	if android.InList("vendor", generatedPartitionTypes) {
+	if android.InList("vendor", generatedPartitionTypes) && !android.InList("vendor", superImageSubPartitions) {
 		partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
 	}
-	if android.InList("product", generatedPartitionTypes) {
+	if android.InList("product", generatedPartitionTypes) && !android.InList("product", superImageSubPartitions) {
 		partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
 	}
-	if android.InList("odm", generatedPartitionTypes) {
+	if android.InList("odm", generatedPartitionTypes) && !android.InList("odm", superImageSubPartitions) {
 		partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
 	}
 	if android.InList("userdata", f.properties.Generated_partition_types) {
@@ -238,13 +243,13 @@
 	if android.InList("recovery", f.properties.Generated_partition_types) {
 		partitionProps.Recovery_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "recovery"))
 	}
-	if android.InList("system_dlkm", f.properties.Generated_partition_types) {
+	if android.InList("system_dlkm", f.properties.Generated_partition_types) && !android.InList("system_dlkm", superImageSubPartitions) {
 		partitionProps.System_dlkm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_dlkm"))
 	}
-	if android.InList("vendor_dlkm", f.properties.Generated_partition_types) {
+	if android.InList("vendor_dlkm", f.properties.Generated_partition_types) && !android.InList("vendor_dlkm", superImageSubPartitions) {
 		partitionProps.Vendor_dlkm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor_dlkm"))
 	}
-	if android.InList("odm_dlkm", f.properties.Generated_partition_types) {
+	if android.InList("odm_dlkm", f.properties.Generated_partition_types) && !android.InList("odm_dlkm", superImageSubPartitions) {
 		partitionProps.Odm_dlkm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm_dlkm"))
 	}
 	if f.properties.Boot_image != "" {
diff --git a/fsgen/super_img.go b/fsgen/super_img.go
index a36f614..5994fb6 100644
--- a/fsgen/super_img.go
+++ b/fsgen/super_img.go
@@ -27,7 +27,7 @@
 	return partitionVars.ProductBuildSuperPartition
 }
 
-func createSuperImage(ctx android.LoadHookContext, partitions []string, partitionVars android.PartitionVariables) {
+func createSuperImage(ctx android.LoadHookContext, partitions []string, partitionVars android.PartitionVariables) []string {
 	baseProps := &struct {
 		Name *string
 	}{
@@ -59,34 +59,45 @@
 	}
 	superImageProps.Partition_groups = partitionGroupsInfo
 
+	var superImageSubpartitions []string
 	partitionNameProps := &filesystem.SuperImagePartitionNameProperties{}
 	if android.InList("system", partitions) {
 		partitionNameProps.System_partition = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
+		superImageSubpartitions = append(superImageSubpartitions, "system")
 	}
 	if android.InList("system_ext", partitions) {
 		partitionNameProps.System_ext_partition = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
+		superImageSubpartitions = append(superImageSubpartitions, "system_ext")
 	}
 	if android.InList("system_dlkm", partitions) {
 		partitionNameProps.System_dlkm_partition = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_dlkm"))
+		superImageSubpartitions = append(superImageSubpartitions, "system_dlkm")
 	}
 	if android.InList("system_other", partitions) {
 		partitionNameProps.System_other_partition = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_other"))
+		superImageSubpartitions = append(superImageSubpartitions, "system_other")
 	}
 	if android.InList("product", partitions) {
 		partitionNameProps.Product_partition = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
+		superImageSubpartitions = append(superImageSubpartitions, "product")
 	}
 	if android.InList("vendor", partitions) {
 		partitionNameProps.Vendor_partition = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
+		superImageSubpartitions = append(superImageSubpartitions, "vendor")
 	}
 	if android.InList("vendor_dlkm", partitions) {
 		partitionNameProps.Vendor_dlkm_partition = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor_dlkm"))
+		superImageSubpartitions = append(superImageSubpartitions, "vendor_dlkm")
 	}
 	if android.InList("odm", partitions) {
 		partitionNameProps.Odm_partition = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
+		superImageSubpartitions = append(superImageSubpartitions, "odm")
 	}
 	if android.InList("odm_dlkm", partitions) {
 		partitionNameProps.Odm_dlkm_partition = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm_dlkm"))
+		superImageSubpartitions = append(superImageSubpartitions, "odm_dlkm")
 	}
 
 	ctx.CreateModule(filesystem.SuperImageFactory, baseProps, superImageProps, partitionNameProps)
+	return superImageSubpartitions
 }