Add GSI support for android_system_image

GSI's system.img includes system_ext and product artifacts. This patches
android_system_image module so it can include such artifacts when
building GSI.

Bug: 370351758
Test: m android_gsi
Change-Id: Id29678b1101e787e88dbedae38cdbf6d82d1cb95
diff --git a/filesystem/filesystem.go b/filesystem/filesystem.go
index 78e24e2..29e5ec3 100644
--- a/filesystem/filesystem.go
+++ b/filesystem/filesystem.go
@@ -67,6 +67,10 @@
 	BuildLinkerConfigFile(ctx android.ModuleContext, builder *android.RuleBuilder, rebasedDir android.OutputPath)
 	// Function that filters PackagingSpec in PackagingBase.GatherPackagingSpecs()
 	FilterPackagingSpec(spec android.PackagingSpec) bool
+	// Function that modifies PackagingSpec in PackagingBase.GatherPackagingSpecs() to customize.
+	// For example, GSI system.img contains system_ext and product artifacts and their
+	// relPathInPackage need to be rebased to system/system_ext and system/system_product.
+	ModifyPackagingSpec(spec *android.PackagingSpec)
 }
 
 var _ filesystemBuilder = (*filesystem)(nil)
@@ -302,6 +306,10 @@
 	return true
 }
 
+func (f *filesystem) ModifyPackagingSpec(ps *android.PackagingSpec) {
+	// do nothing by default
+}
+
 var pctx = android.NewPackageContext("android/soong/filesystem")
 
 func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
@@ -792,7 +800,7 @@
 // Note that "apex" module installs its contents to "apex"(fake partition) as well
 // for symbol lookup by imitating "activated" paths.
 func (f *filesystem) gatherFilteredPackagingSpecs(ctx android.ModuleContext) map[string]android.PackagingSpec {
-	specs := f.PackagingBase.GatherPackagingSpecsWithFilter(ctx, f.filesystemBuilder.FilterPackagingSpec)
+	specs := f.PackagingBase.GatherPackagingSpecsWithFilterAndModifier(ctx, f.filesystemBuilder.FilterPackagingSpec, f.filesystemBuilder.ModifyPackagingSpec)
 	return specs
 }
 
@@ -818,13 +826,7 @@
 	android.ModuleBase
 	android.DefaultsModuleBase
 
-	properties filesystemDefaultsProperties
-}
-
-type filesystemDefaultsProperties struct {
-	// Identifies which partition this is for //visibility:any_system_image (and others) visibility
-	// checks, and will be used in the future for API surface checks.
-	Partition_type *string
+	properties FilesystemProperties
 }
 
 // android_filesystem_defaults is a default module for android_filesystem and android_system_image
diff --git a/filesystem/system_image.go b/filesystem/system_image.go
index 672458c..4d176ae 100644
--- a/filesystem/system_image.go
+++ b/filesystem/system_image.go
@@ -18,6 +18,9 @@
 	"android/soong/android"
 	"android/soong/linkerconfig"
 
+	"path/filepath"
+	"strings"
+
 	"github.com/google/blueprint/proptools"
 )
 
@@ -58,5 +61,14 @@
 // for symbol lookup by imitating "activated" paths.
 func (s *systemImage) FilterPackagingSpec(ps android.PackagingSpec) bool {
 	return !ps.SkipInstall() &&
-		(ps.Partition() == "system" || ps.Partition() == "root")
+		(ps.Partition() == "system" || ps.Partition() == "root" ||
+			strings.HasPrefix(ps.Partition(), "system/"))
+}
+
+func (s *systemImage) ModifyPackagingSpec(ps *android.PackagingSpec) {
+	if strings.HasPrefix(ps.Partition(), "system/") {
+		subPartition := strings.TrimPrefix(ps.Partition(), "system/")
+		ps.SetPartition("system")
+		ps.SetRelPathInPackage(filepath.Join(subPartition, ps.RelPathInPackage()))
+	}
 }