Prototype changes for multitree

This change contains a prototype implementation for multitree. Several
interfaces and modules are added.

1. Imported/Exported
Modules implementing Exportable interface can export artifacts to other
components. "imported_filegroup" modules can import generated artifacts from other exported modules.

2. Multitree metadata
It contains information about imported/exported modules in each
component, and can be generated via "m update-meta".

3. cc library stub
It's based on prototype stub libraries. It uses imported/exported
mechanism to expose a C API, with a map.txt file and header files.

Bug: 230448564
Test: m
Change-Id: Id7ff7618e2c630c5617a564d8b23b60a1cc9c8e8
diff --git a/multitree/Android.bp b/multitree/Android.bp
new file mode 100644
index 0000000..9b16d20
--- /dev/null
+++ b/multitree/Android.bp
@@ -0,0 +1,19 @@
+package {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+bootstrap_go_package {
+    name: "soong-multitree",
+    pkgPath: "android/soong/multitree",
+    deps: [
+        "blueprint",
+        "soong-android",
+    ],
+    srcs: [
+        "api_surface.go",
+        "export.go",
+        "metadata.go",
+        "import.go",
+    ],
+    pluginFor: ["soong_build"],
+}
diff --git a/multitree/api_surface.go b/multitree/api_surface.go
new file mode 100644
index 0000000..f739a24
--- /dev/null
+++ b/multitree/api_surface.go
@@ -0,0 +1,119 @@
+// Copyright 2021 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package multitree
+
+import (
+	"android/soong/android"
+	"fmt"
+
+	"github.com/google/blueprint"
+)
+
+var (
+	pctx = android.NewPackageContext("android/soong/multitree")
+)
+
+func init() {
+	RegisterApiSurfaceBuildComponents(android.InitRegistrationContext)
+}
+
+var PrepareForTestWithApiSurface = android.FixtureRegisterWithContext(RegisterApiSurfaceBuildComponents)
+
+func RegisterApiSurfaceBuildComponents(ctx android.RegistrationContext) {
+	ctx.RegisterModuleType("api_surface", ApiSurfaceFactory)
+}
+
+type ApiSurface struct {
+	android.ModuleBase
+	ExportableModuleBase
+	properties apiSurfaceProperties
+
+	allOutputs    android.Paths
+	taggedOutputs map[string]android.Paths
+}
+
+type apiSurfaceProperties struct {
+	Contributions []string
+}
+
+func ApiSurfaceFactory() android.Module {
+	module := &ApiSurface{}
+	module.AddProperties(&module.properties)
+	android.InitAndroidModule(module)
+	InitExportableModule(module)
+	return module
+}
+
+func (surface *ApiSurface) DepsMutator(ctx android.BottomUpMutatorContext) {
+	if surface.properties.Contributions != nil {
+		ctx.AddVariationDependencies(nil, nil, surface.properties.Contributions...)
+	}
+
+}
+func (surface *ApiSurface) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	contributionFiles := make(map[string]android.Paths)
+	var allOutputs android.Paths
+	ctx.WalkDeps(func(child, parent android.Module) bool {
+		if contribution, ok := child.(ApiContribution); ok {
+			copied := contribution.CopyFilesWithTag(ctx)
+			for tag, files := range copied {
+				contributionFiles[child.Name()+"#"+tag] = files
+			}
+			for _, paths := range copied {
+				allOutputs = append(allOutputs, paths...)
+			}
+			return false // no transitive dependencies
+		}
+		return false
+	})
+
+	// phony target
+	ctx.Build(pctx, android.BuildParams{
+		Rule:   blueprint.Phony,
+		Output: android.PathForPhony(ctx, ctx.ModuleName()),
+		Inputs: allOutputs,
+	})
+
+	surface.allOutputs = allOutputs
+	surface.taggedOutputs = contributionFiles
+}
+
+func (surface *ApiSurface) OutputFiles(tag string) (android.Paths, error) {
+	if tag != "" {
+		return nil, fmt.Errorf("unknown tag: %q", tag)
+	}
+	return surface.allOutputs, nil
+}
+
+func (surface *ApiSurface) TaggedOutputs() map[string]android.Paths {
+	return surface.taggedOutputs
+}
+
+func (surface *ApiSurface) Exportable() bool {
+	return true
+}
+
+var _ android.OutputFileProducer = (*ApiSurface)(nil)
+var _ Exportable = (*ApiSurface)(nil)
+
+type ApiContribution interface {
+	// copy files necessaryt to construct an API surface
+	// For C, it will be map.txt and .h files
+	// For Java, it will be api.txt
+	CopyFilesWithTag(ctx android.ModuleContext) map[string]android.Paths // output paths
+
+	// Generate Android.bp in out/ to use the exported .txt files
+	// GenerateBuildFiles(ctx ModuleContext) Paths //output paths
+}
diff --git a/multitree/export.go b/multitree/export.go
new file mode 100644
index 0000000..aecade5
--- /dev/null
+++ b/multitree/export.go
@@ -0,0 +1,67 @@
+// Copyright 2022 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package multitree
+
+import (
+	"android/soong/android"
+
+	"github.com/google/blueprint/proptools"
+)
+
+type moduleExportProperty struct {
+	// True if the module is exported to the other components in a multi-tree.
+	// Any components in the multi-tree can import this module to use.
+	Export *bool
+}
+
+type ExportableModuleBase struct {
+	properties moduleExportProperty
+}
+
+type Exportable interface {
+	// Properties for the exporable module.
+	exportableModuleProps() *moduleExportProperty
+
+	// Check if this module can be exported.
+	// If this returns false, the module will not be exported regardless of the 'export' value.
+	Exportable() bool
+
+	// Returns 'true' if this module has 'export: true'
+	// This module will not be exported if it returns 'false' to 'Exportable()' interface even if
+	// it has 'export: true'.
+	IsExported() bool
+
+	// Map from tags to outputs.
+	// Each module can tag their outputs for convenience.
+	TaggedOutputs() map[string]android.Paths
+}
+
+type ExportableModule interface {
+	android.Module
+	android.OutputFileProducer
+	Exportable
+}
+
+func InitExportableModule(module ExportableModule) {
+	module.AddProperties(module.exportableModuleProps())
+}
+
+func (m *ExportableModuleBase) exportableModuleProps() *moduleExportProperty {
+	return &m.properties
+}
+
+func (m *ExportableModuleBase) IsExported() bool {
+	return proptools.Bool(m.properties.Export)
+}
diff --git a/multitree/import.go b/multitree/import.go
new file mode 100644
index 0000000..1e5c421
--- /dev/null
+++ b/multitree/import.go
@@ -0,0 +1,96 @@
+// Copyright 2022 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package multitree
+
+import (
+	"android/soong/android"
+)
+
+var (
+	nameSuffix = ".imported"
+)
+
+type MultitreeImportedModuleInterface interface {
+	GetMultitreeImportedModuleName() string
+}
+
+func init() {
+	android.RegisterModuleType("imported_filegroup", importedFileGroupFactory)
+
+	android.PreArchMutators(RegisterMultitreePreArchMutators)
+}
+
+type importedFileGroupProperties struct {
+	// Imported modules from the other components in a multi-tree
+	Imported []string
+}
+
+type importedFileGroup struct {
+	android.ModuleBase
+
+	properties importedFileGroupProperties
+	srcs       android.Paths
+}
+
+func (ifg *importedFileGroup) Name() string {
+	return ifg.BaseModuleName() + nameSuffix
+}
+
+func importedFileGroupFactory() android.Module {
+	module := &importedFileGroup{}
+	module.AddProperties(&module.properties)
+
+	android.InitAndroidModule(module)
+	return module
+}
+
+var _ MultitreeImportedModuleInterface = (*importedFileGroup)(nil)
+
+func (ifg *importedFileGroup) GetMultitreeImportedModuleName() string {
+	// The base module name of the imported filegroup is used as the imported module name
+	return ifg.BaseModuleName()
+}
+
+var _ android.SourceFileProducer = (*importedFileGroup)(nil)
+
+func (ifg *importedFileGroup) Srcs() android.Paths {
+	return ifg.srcs
+}
+
+func (ifg *importedFileGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+	// srcs from this module must not be used. Adding a dot path to avoid the empty
+	// source failure. Still soong returns error when a module wants to build against
+	// this source, which is intended.
+	ifg.srcs = android.PathsForModuleSrc(ctx, []string{"."})
+}
+
+func RegisterMultitreePreArchMutators(ctx android.RegisterMutatorsContext) {
+	ctx.BottomUp("multitree_imported_rename", MultitreeImportedRenameMutator).Parallel()
+}
+
+func MultitreeImportedRenameMutator(ctx android.BottomUpMutatorContext) {
+	if m, ok := ctx.Module().(MultitreeImportedModuleInterface); ok {
+		name := m.GetMultitreeImportedModuleName()
+		if !ctx.OtherModuleExists(name) {
+			// Provide an empty filegroup not to break the build while updating the metadata.
+			// In other cases, soong will report an error to guide users to run 'm update-meta'
+			// first.
+			if !ctx.Config().TargetMultitreeUpdateMeta() {
+				ctx.ModuleErrorf("\"%s\" filegroup must be imported.\nRun 'm update-meta' first to import the filegroup.", name)
+			}
+			ctx.Rename(name)
+		}
+	}
+}
diff --git a/multitree/metadata.go b/multitree/metadata.go
new file mode 100644
index 0000000..3fd7215
--- /dev/null
+++ b/multitree/metadata.go
@@ -0,0 +1,74 @@
+// Copyright 2022 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package multitree
+
+import (
+	"android/soong/android"
+	"encoding/json"
+)
+
+func init() {
+	android.RegisterSingletonType("update-meta", UpdateMetaSingleton)
+}
+
+func UpdateMetaSingleton() android.Singleton {
+	return &updateMetaSingleton{}
+}
+
+type jsonImported struct {
+	FileGroups map[string][]string `json:",omitempty"`
+}
+
+type metadataJsonFlags struct {
+	Imported jsonImported        `json:",omitempty"`
+	Exported map[string][]string `json:",omitempty"`
+}
+
+type updateMetaSingleton struct {
+	importedModules       []string
+	generatedMetadataFile android.OutputPath
+}
+
+func (s *updateMetaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
+	metadata := metadataJsonFlags{
+		Imported: jsonImported{
+			FileGroups: make(map[string][]string),
+		},
+		Exported: make(map[string][]string),
+	}
+	ctx.VisitAllModules(func(module android.Module) {
+		if ifg, ok := module.(*importedFileGroup); ok {
+			metadata.Imported.FileGroups[ifg.BaseModuleName()] = ifg.properties.Imported
+		}
+		if e, ok := module.(ExportableModule); ok {
+			if e.IsExported() && e.Exportable() {
+				for tag, files := range e.TaggedOutputs() {
+					// TODO(b/219846705): refactor this to a dictionary
+					metadata.Exported[e.Name()+":"+tag] = append(metadata.Exported[e.Name()+":"+tag], files.Strings()...)
+				}
+			}
+		}
+	})
+	jsonStr, err := json.Marshal(metadata)
+	if err != nil {
+		ctx.Errorf(err.Error())
+	}
+	s.generatedMetadataFile = android.PathForOutput(ctx, "multitree", "metadata.json")
+	android.WriteFileRule(ctx, s.generatedMetadataFile, string(jsonStr))
+}
+
+func (s *updateMetaSingleton) MakeVars(ctx android.MakeVarsContext) {
+	ctx.Strict("MULTITREE_METADATA", s.generatedMetadataFile.String())
+}