android_system_image that generates linker.config.pb

android_system_image module type is a specialization of the
android_filesystem module type. Currently, it adds a build rule for
creating linker.config.pb from the information about all the other files
in the filesystem and includes linker.config.pb to the filesystem as
well.

To do so, the filesystem module now provides a function pointer which
subtype modules like android_system_image can implement to pass extra
files that they want to package in the filesystem.

In addition, the linkerconfig package is revised to make it possible to
build linker.config.pb file outside of the package.

Bug: 185391776
Test: m microdroid and inspect etc/linker.config.pb in it.

Change-Id: Id89c40b519213062860d7306029b8413d8d36a2d
diff --git a/filesystem/filesystem.go b/filesystem/filesystem.go
index cf98717..b2caa51 100644
--- a/filesystem/filesystem.go
+++ b/filesystem/filesystem.go
@@ -31,6 +31,7 @@
 
 func registerBuildComponents(ctx android.RegistrationContext) {
 	ctx.RegisterModuleType("android_filesystem", filesystemFactory)
+	ctx.RegisterModuleType("android_system_image", systemImageFactory)
 }
 
 type filesystem struct {
@@ -39,6 +40,9 @@
 
 	properties filesystemProperties
 
+	// Function that builds extra files under the root directory and returns the files
+	buildExtraFiles func(ctx android.ModuleContext, root android.OutputPath) android.OutputPaths
+
 	output     android.OutputPath
 	installDir android.InstallPath
 }
@@ -87,10 +91,14 @@
 // partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
 func filesystemFactory() android.Module {
 	module := &filesystem{}
+	initFilesystemModule(module)
+	return module
+}
+
+func initFilesystemModule(module *filesystem) {
 	module.AddProperties(&module.properties)
 	android.InitPackageModule(module)
 	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
-	return module
 }
 
 var dependencyTag = struct {
@@ -148,7 +156,7 @@
 	ctx.InstallFile(f.installDir, f.installFileName(), f.output)
 }
 
-// root zip will contain stuffs like dirs or symlinks.
+// root zip will contain extra files/dirs that are not from the `deps` property.
 func (f *filesystem) buildRootZip(ctx android.ModuleContext) android.OutputPath {
 	rootDir := android.PathForModuleGen(ctx, "root").OutputPath
 	builder := android.NewRuleBuilder(pctx, ctx)
@@ -182,15 +190,34 @@
 		builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
 	}
 
-	zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath
+	// create extra files if there's any
+	rootForExtraFiles := android.PathForModuleGen(ctx, "root-extra").OutputPath
+	var extraFiles android.OutputPaths
+	if f.buildExtraFiles != nil {
+		extraFiles = f.buildExtraFiles(ctx, rootForExtraFiles)
+		for _, f := range extraFiles {
+			rel, _ := filepath.Rel(rootForExtraFiles.String(), f.String())
+			if strings.HasPrefix(rel, "..") {
+				panic(fmt.Errorf("%q is not under %q\n", f, rootForExtraFiles))
+			}
+		}
+	}
 
-	builder.Command().
-		BuiltTool("soong_zip").
-		FlagWithOutput("-o ", zipOut).
+	// Zip them all
+	zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath
+	zipCommand := builder.Command().BuiltTool("soong_zip")
+	zipCommand.FlagWithOutput("-o ", zipOut).
 		FlagWithArg("-C ", rootDir.String()).
 		Flag("-L 0"). // no compression because this will be unzipped soon
 		FlagWithArg("-D ", rootDir.String()).
 		Flag("-d") // include empty directories
+	if len(extraFiles) > 0 {
+		zipCommand.FlagWithArg("-C ", rootForExtraFiles.String())
+		for _, f := range extraFiles {
+			zipCommand.FlagWithInput("-f ", f)
+		}
+	}
+
 	builder.Command().Text("rm -rf").Text(rootDir.String())
 
 	builder.Build("zip_root", fmt.Sprintf("zipping root contents for %s", ctx.ModuleName()))