Use partition intead of "root" to assemble filesystem artifacts

The artifacts for filesystem packaging are currently assembled in
```
$intermediates/$module/android_common/root/$base_dir
```

This CL changes this to
```
$intermediates/$module/android_common/$partition/$base_dir
e.g.
$intermediates/$module/android_common/system/system (system)
$intermediates/$module/android_common/system_ext (system_ext)
```

The motivatiton for this change is to fix diffs in
BuildManifestSystemExt.apk. The build_manifest.pb of this apk is
generated from the base of $PRODUCT_OUT. This means that the paths of
the artifacts contains the partition prefix. diff https://diff.googleplex.com/#key=9xuSx7nvP251

An alternative would be to use partition as the `Base_dir`, but this
would create an additional top-level directory in the soong img files
(make img files do not have a top-level partition directory for non
system images)

Test: 383144733
Bug: Built and mounted make and soong system_ext.img for AOSP CP. NOTICE
is the last diff

Change-Id: Iee53113a7e00dcda9d987f2aabf9605740bbe7fe
diff --git a/filesystem/filesystem.go b/filesystem/filesystem.go
index b9cb076..81afd02 100644
--- a/filesystem/filesystem.go
+++ b/filesystem/filesystem.go
@@ -457,7 +457,7 @@
 }
 
 func (f *filesystem) appendToEntry(ctx android.ModuleContext, installedFile android.Path) {
-	partitionBaseDir := android.PathForModuleOut(ctx, "root", proptools.String(f.properties.Base_dir)).String() + "/"
+	partitionBaseDir := android.PathForModuleOut(ctx, f.rootDirString(), proptools.String(f.properties.Base_dir)).String() + "/"
 
 	relPath, inTargetPartition := strings.CutPrefix(installedFile.String(), partitionBaseDir)
 	if inTargetPartition {
@@ -547,8 +547,12 @@
 	builder.Command().Textf("cp -prf %s/* %s", rebasedDir, installPath)
 }
 
+func (f *filesystem) rootDirString() string {
+	return f.partitionName()
+}
+
 func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.Path {
-	rootDir := android.PathForModuleOut(ctx, "root").OutputPath
+	rootDir := android.PathForModuleOut(ctx, f.rootDirString()).OutputPath
 	rebasedDir := rootDir
 	if f.properties.Base_dir != nil {
 		rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
@@ -775,7 +779,7 @@
 		ctx.PropertyErrorf("include_make_built_files", "include_make_built_files is not supported for compressed cpio image.")
 	}
 
-	rootDir := android.PathForModuleOut(ctx, "root").OutputPath
+	rootDir := android.PathForModuleOut(ctx, f.rootDirString()).OutputPath
 	rebasedDir := rootDir
 	if f.properties.Base_dir != nil {
 		rebasedDir = rootDir.Join(ctx, *f.properties.Base_dir)
diff --git a/filesystem/filesystem_test.go b/filesystem/filesystem_test.go
index 0ed3870..2dcb23d 100644
--- a/filesystem/filesystem_test.go
+++ b/filesystem/filesystem_test.go
@@ -181,7 +181,7 @@
 	`)
 
 	module := result.ModuleForTests("myfilesystem", "android_common")
-	output := module.Output("out/soong/.intermediates/myfilesystem/android_common/root/system/etc/linker.config.pb")
+	output := module.Output("out/soong/.intermediates/myfilesystem/android_common/myfilesystem/system/etc/linker.config.pb")
 
 	fullCommand := output.RuleParams.Command
 	startIndex := strings.Index(fullCommand, "conv_linker_config")
diff --git a/filesystem/fsverity_metadata.go b/filesystem/fsverity_metadata.go
index 6372c5e..91b8c57 100644
--- a/filesystem/fsverity_metadata.go
+++ b/filesystem/fsverity_metadata.go
@@ -85,6 +85,18 @@
 		f.appendToEntry(ctx, destPath)
 	}
 
+	fsVerityBaseDir := rootDir.String()
+	if f.PartitionType() == "system_ext" {
+		// Use the equivalent of $PRODUCT_OUT as the base dir.
+		// This ensures that the paths in build_manifest.pb contain on-device paths
+		// e.g. system_ext/framework/javalib.jar
+		// and not framework/javalib.jar.
+		//
+		// Although base-dir is outside the rootdir provided for packaging, this action
+		// is hermetic since it uses `manifestGeneratorListPath` to filter the files to be written to build_manifest.pb
+		fsVerityBaseDir = filepath.Dir(rootDir.String())
+	}
+
 	// STEP 2: generate signed BuildManifest.apk
 	// STEP 2-1: generate build_manifest.pb
 	manifestGeneratorListPath := android.PathForModuleOut(ctx, "fsverity_manifest.list")
@@ -96,7 +108,7 @@
 	builder.Command().
 		BuiltTool("fsverity_manifest_generator").
 		FlagWithInput("--fsverity-path ", fsverityPath).
-		FlagWithArg("--base-dir ", rootDir.String()).
+		FlagWithArg("--base-dir ", fsVerityBaseDir).
 		FlagWithArg("--output ", manifestPbPath.String()).
 		FlagWithInput("@", manifestGeneratorListPath)