Merge "Add test & documentation for PropertiesToApply"
diff --git a/apex/apex.go b/apex/apex.go
index 82d7ecf..88d93af 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -237,6 +237,23 @@
 	}
 }
 
+type apexArchBundleProperties struct {
+	Arch struct {
+		Arm struct {
+			ApexNativeDependencies
+		}
+		Arm64 struct {
+			ApexNativeDependencies
+		}
+		X86 struct {
+			ApexNativeDependencies
+		}
+		X86_64 struct {
+			ApexNativeDependencies
+		}
+	}
+}
+
 // These properties can be used in override_apex to override the corresponding properties in the
 // base apex.
 type overridableProperties struct {
@@ -273,6 +290,7 @@
 	// Properties
 	properties            apexBundleProperties
 	targetProperties      apexTargetBundleProperties
+	archProperties        apexArchBundleProperties
 	overridableProperties overridableProperties
 	vndkProperties        apexVndkProperties // only for apex_vndk modules
 
@@ -653,6 +671,20 @@
 			}
 		}
 
+		// Add native modules targeting a specific arch variant
+		switch target.Arch.ArchType {
+		case android.Arm:
+			depsList = append(depsList, a.archProperties.Arch.Arm.ApexNativeDependencies)
+		case android.Arm64:
+			depsList = append(depsList, a.archProperties.Arch.Arm64.ApexNativeDependencies)
+		case android.X86:
+			depsList = append(depsList, a.archProperties.Arch.X86.ApexNativeDependencies)
+		case android.X86_64:
+			depsList = append(depsList, a.archProperties.Arch.X86_64.ApexNativeDependencies)
+		default:
+			panic(fmt.Errorf("unsupported arch %v\n", ctx.Arch().ArchType))
+		}
+
 		for _, d := range depsList {
 			addDependenciesForNativeModules(ctx, d, target, imageVariation)
 		}
@@ -1931,6 +1963,7 @@
 
 	module.AddProperties(&module.properties)
 	module.AddProperties(&module.targetProperties)
+	module.AddProperties(&module.archProperties)
 	module.AddProperties(&module.overridableProperties)
 
 	android.InitAndroidMultiTargetsArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
diff --git a/apex/apex_test.go b/apex/apex_test.go
index 506c6fe..69b1dbb 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -3891,6 +3891,64 @@
 	ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
 }
 
+func TestApexWithArch(t *testing.T) {
+	ctx, _ := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			arch: {
+				arm64: {
+					native_shared_libs: ["mylib.arm64"],
+				},
+				x86_64: {
+					native_shared_libs: ["mylib.x64"],
+				},
+			}
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		cc_library {
+			name: "mylib.arm64",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			stl: "none",
+			// TODO: remove //apex_available:platform
+			apex_available: [
+				"//apex_available:platform",
+				"myapex",
+			],
+		}
+
+		cc_library {
+			name: "mylib.x64",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			stl: "none",
+			// TODO: remove //apex_available:platform
+			apex_available: [
+				"//apex_available:platform",
+				"myapex",
+			],
+		}
+	`)
+
+	apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
+	copyCmds := apexRule.Args["copy_commands"]
+
+	// Ensure that apex variant is created for the direct dep
+	ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
+	ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
+
+	// Ensure that both direct and indirect deps are copied into apex
+	ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
+	ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
+}
+
 func TestApexWithShBinary(t *testing.T) {
 	ctx, _ := testApex(t, `
 		apex {
diff --git a/cc/cc.go b/cc/cc.go
index 89f32f1..240080f 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -2999,6 +2999,9 @@
 	}
 }
 
+var _ android.ApexModule = (*Module)(nil)
+
+// Implements android.ApexModule
 func (c *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
 	depTag := ctx.OtherModuleDependencyTag(dep)
 	libDepTag, isLibDepTag := depTag.(libraryDependencyTag)
@@ -3032,6 +3035,7 @@
 	return true
 }
 
+// Implements android.ApexModule
 func (c *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
 	sdkVersion android.ApiLevel) error {
 	// We ignore libclang_rt.* prebuilt libs since they declare sdk_version: 14(b/121358700)
diff --git a/cmd/soong_ui/main.go b/cmd/soong_ui/main.go
index bd1d450..74ede68 100644
--- a/cmd/soong_ui/main.go
+++ b/cmd/soong_ui/main.go
@@ -174,7 +174,7 @@
 
 	build.SetupOutDir(buildCtx, config)
 
-	if config.UseBazel() {
+	if config.UseBazel() && config.Dist() {
 		defer populateExternalDistDir(buildCtx, config)
 	}
 
@@ -547,6 +547,12 @@
 		return
 	}
 
+	// Make sure the internal DIST_DIR actually exists before trying to read from it
+	if _, err = os.Stat(internalDistDirPath); os.IsNotExist(err) {
+		ctx.Println("Skipping Bazel dist dir migration - nothing to do!")
+		return
+	}
+
 	// Make sure the external DIST_DIR actually exists before trying to write to it
 	if err = os.MkdirAll(externalDistDirPath, 0755); err != nil {
 		ctx.Fatalf("Unable to make directory %s: %s", externalDistDirPath, err)
diff --git a/genrule/genrule.go b/genrule/genrule.go
index 9054017..594d253 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -547,6 +547,9 @@
 	}
 }
 
+var _ android.ApexModule = (*Module)(nil)
+
+// Implements android.ApexModule
 func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
 	sdkVersion android.ApiLevel) error {
 	// Because generated outputs are checked by client modules(e.g. cc_library, ...)
diff --git a/java/aar.go b/java/aar.go
index 3750f72..dfcd956 100644
--- a/java/aar.go
+++ b/java/aar.go
@@ -851,10 +851,14 @@
 	return nil, nil
 }
 
+var _ android.ApexModule = (*AARImport)(nil)
+
+// Implements android.ApexModule
 func (a *AARImport) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
 	return a.depIsInSameApex(ctx, dep)
 }
 
+// Implements android.ApexModule
 func (g *AARImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
 	sdkVersion android.ApiLevel) error {
 	return nil
diff --git a/java/app.go b/java/app.go
index e6d9550..c0ac9c2 100755
--- a/java/app.go
+++ b/java/app.go
@@ -1653,6 +1653,9 @@
 	return sdkSpecFrom("")
 }
 
+var _ android.ApexModule = (*AndroidAppImport)(nil)
+
+// Implements android.ApexModule
 func (j *AndroidAppImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
 	sdkVersion android.ApiLevel) error {
 	// Do not check for prebuilts against the min_sdk_version of enclosing APEX
diff --git a/java/java.go b/java/java.go
index d44719e..02d78f2 100644
--- a/java/java.go
+++ b/java/java.go
@@ -2021,10 +2021,12 @@
 	return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
 }
 
+// Implements android.ApexModule
 func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
 	return j.depIsInSameApex(ctx, dep)
 }
 
+// Implements android.ApexModule
 func (j *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
 	sdkVersion android.ApiLevel) error {
 	sdkSpec := j.minSdkVersion()
@@ -2070,6 +2072,8 @@
 	InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
 }
 
+var _ android.ApexModule = (*Library)(nil)
+
 // Provides access to the list of permitted packages from updatable boot jars.
 type PermittedPackagesForUpdatableBootJars interface {
 	PermittedPackagesForUpdatableBootJars() []string
@@ -2934,10 +2938,14 @@
 	return nil, nil
 }
 
+var _ android.ApexModule = (*Import)(nil)
+
+// Implements android.ApexModule
 func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
 	return j.depIsInSameApex(ctx, dep)
 }
 
+// Implements android.ApexModule
 func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
 	sdkVersion android.ApiLevel) error {
 	// Do not check for prebuilts against the min_sdk_version of enclosing APEX
@@ -3129,6 +3137,9 @@
 	return j.dexJarFile
 }
 
+var _ android.ApexModule = (*DexImport)(nil)
+
+// Implements android.ApexModule
 func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
 	sdkVersion android.ApiLevel) error {
 	// we don't check prebuilt modules for sdk_version
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 4e33d74..2e10f9c 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -1925,6 +1925,9 @@
 	}
 }
 
+var _ android.ApexModule = (*SdkLibraryImport)(nil)
+
+// Implements android.ApexModule
 func (module *SdkLibraryImport) DepIsInSameApex(mctx android.BaseModuleContext, dep android.Module) bool {
 	depTag := mctx.OtherModuleDependencyTag(dep)
 	if depTag == xmlPermissionsFileTag {
@@ -1936,6 +1939,7 @@
 	return false
 }
 
+// Implements android.ApexModule
 func (module *SdkLibraryImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
 	sdkVersion android.ApiLevel) error {
 	// we don't check prebuilt modules for sdk_version
@@ -2141,6 +2145,9 @@
 	// do nothing
 }
 
+var _ android.ApexModule = (*sdkLibraryXml)(nil)
+
+// Implements android.ApexModule
 func (module *sdkLibraryXml) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
 	sdkVersion android.ApiLevel) error {
 	// sdkLibraryXml doesn't need to be checked separately because java_sdk_library is checked
diff --git a/rust/rust.go b/rust/rust.go
index 3d70121..21da5ae 100644
--- a/rust/rust.go
+++ b/rust/rust.go
@@ -1065,6 +1065,9 @@
 	return String(mod.Properties.Min_sdk_version)
 }
 
+var _ android.ApexModule = (*Module)(nil)
+
+// Implements android.ApexModule
 func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
 	minSdkVersion := mod.minSdkVersion()
 	if minSdkVersion == "apex_inherit" {
@@ -1087,6 +1090,7 @@
 	return nil
 }
 
+// Implements android.ApexModule
 func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
 	depTag := ctx.OtherModuleDependencyTag(dep)
 
diff --git a/sysprop/sysprop_library.go b/sysprop/sysprop_library.go
index 6a53414..93a3fe0 100644
--- a/sysprop/sysprop_library.go
+++ b/sysprop/sysprop_library.go
@@ -322,6 +322,9 @@
 		}}
 }
 
+var _ android.ApexModule = (*syspropLibrary)(nil)
+
+// Implements android.ApexModule
 func (m *syspropLibrary) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
 	sdkVersion android.ApiLevel) error {
 	return fmt.Errorf("sysprop_library is not supposed to be part of apex modules")
diff --git a/ui/metrics/metrics.go b/ui/metrics/metrics.go
index 2a09461..efb572c 100644
--- a/ui/metrics/metrics.go
+++ b/ui/metrics/metrics.go
@@ -12,8 +12,30 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+// Package metrics represents the metrics system for Android Platform Build Systems.
 package metrics
 
+// This is the main heart of the metrics system for Android Platform Build Systems.
+// The starting of the soong_ui (cmd/soong_ui/main.go), the metrics system is
+// initialized by the invocation of New and is then stored in the context
+// (ui/build/context.go) to be used throughout the system. During the build
+// initialization phase, several functions in this file are invoked to store
+// information such as the environment, build configuration and build metadata.
+// There are several scoped code that has Begin() and defer End() functions
+// that captures the metrics and is them added as a perfInfo into the set
+// of the collected metrics. Finally, when soong_ui has finished the build,
+// the defer Dump function is invoked to store the collected metrics to the
+// raw protobuf file in the $OUT directory.
+//
+// There is one additional step that occurs after the raw protobuf file is written.
+// If the configuration environment variable ANDROID_ENABLE_METRICS_UPLOAD is
+// set with the path, the raw protobuf file is uploaded to the destination. See
+// ui/build/upload.go for more details. The filename of the raw protobuf file
+// and the list of files to be uploaded is defined in cmd/soong_ui/main.go.
+//
+// See ui/metrics/event.go for the explanation of what an event is and how
+// the metrics system is a stack based system.
+
 import (
 	"io/ioutil"
 	"os"
@@ -23,25 +45,41 @@
 
 	"github.com/golang/protobuf/proto"
 
-	soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
+	"android/soong/ui/metrics/metrics_proto"
 )
 
 const (
-	PrimaryNinja    = "ninja"
-	RunKati         = "kati"
+	// Below is a list of names passed in to the Begin tracing functions. These
+	// names are used to group a set of metrics.
+
+	// Setup and tear down of the build systems.
 	RunSetupTool    = "setup"
 	RunShutdownTool = "shutdown"
-	RunSoong        = "soong"
-	RunBazel        = "bazel"
 	TestRun         = "test"
-	Total           = "total"
+
+	// List of build system tools.
+	RunSoong     = "soong"
+	PrimaryNinja = "ninja"
+	RunKati      = "kati"
+	RunBazel     = "bazel"
+
+	// Overall build from building the graph to building the target.
+	Total = "total"
 )
 
+// Metrics is a struct that stores collected metrics during the course
+// of a build which later is dumped to a MetricsBase protobuf file.
+// See ui/metrics/metrics_proto/metrics.proto for further details
+// on what information is collected.
 type Metrics struct {
-	metrics     soong_metrics_proto.MetricsBase
+	// The protobuf message that is later written to the file.
+	metrics soong_metrics_proto.MetricsBase
+
+	// A list of pending build events.
 	EventTracer *EventTracer
 }
 
+// New returns a pointer of Metrics to store a set of metrics.
 func New() (metrics *Metrics) {
 	m := &Metrics{
 		metrics:     soong_metrics_proto.MetricsBase{},
@@ -50,6 +88,8 @@
 	return m
 }
 
+// SetTimeMetrics stores performance information from an executed block of
+// code.
 func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
 	switch perf.GetName() {
 	case RunKati:
@@ -60,19 +100,26 @@
 		m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf)
 	case PrimaryNinja:
 		m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
+	case RunSetupTool:
+		m.metrics.SetupTools = append(m.metrics.SetupTools, &perf)
 	case Total:
 		m.metrics.Total = &perf
 	}
 }
 
+// BuildConfig stores information about the build configuration.
 func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) {
 	m.metrics.BuildConfig = b
 }
 
+// SystemResourceInfo stores information related to the host system such
+// as total CPU and memory.
 func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) {
 	m.metrics.SystemResourceInfo = b
 }
 
+// SetMetadataMetrics sets information about the build such as the target
+// product, host architecture and out directory.
 func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
 	for k, v := range metadata {
 		switch k {
@@ -92,15 +139,15 @@
 				m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
 			}
 		case "TARGET_ARCH":
-			m.metrics.TargetArch = m.getArch(v)
+			m.metrics.TargetArch = arch(v)
 		case "TARGET_ARCH_VARIANT":
 			m.metrics.TargetArchVariant = proto.String(v)
 		case "TARGET_CPU_VARIANT":
 			m.metrics.TargetCpuVariant = proto.String(v)
 		case "HOST_ARCH":
-			m.metrics.HostArch = m.getArch(v)
+			m.metrics.HostArch = arch(v)
 		case "HOST_2ND_ARCH":
-			m.metrics.Host_2NdArch = m.getArch(v)
+			m.metrics.Host_2NdArch = arch(v)
 		case "HOST_OS_EXTRA":
 			m.metrics.HostOsExtra = proto.String(v)
 		case "HOST_CROSS_OS":
@@ -115,8 +162,10 @@
 	}
 }
 
-func (m *Metrics) getArch(arch string) *soong_metrics_proto.MetricsBase_Arch {
-	switch arch {
+// arch returns the corresponding MetricsBase_Arch based on the string
+// parameter.
+func arch(a string) *soong_metrics_proto.MetricsBase_Arch {
+	switch a {
 	case "arm":
 		return soong_metrics_proto.MetricsBase_ARM.Enum()
 	case "arm64":
@@ -130,37 +179,51 @@
 	}
 }
 
+// SetBuildDateTime sets the build date and time. The value written
+// to the protobuf file is in seconds.
 func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
 	m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
 }
 
+// SetBuildCommand adds the build command specified by the user to the
+// list of collected metrics.
 func (m *Metrics) SetBuildCommand(cmd []string) {
 	m.metrics.BuildCommand = proto.String(strings.Join(cmd, " "))
 }
 
-// exports the output to the file at outputPath
-func (m *Metrics) Dump(outputPath string) error {
+// Dump exports the collected metrics from the executed build to the file at
+// out path.
+func (m *Metrics) Dump(out string) error {
 	// ignore the error if the hostname could not be retrieved as it
 	// is not a critical metric to extract.
 	if hostname, err := os.Hostname(); err == nil {
 		m.metrics.Hostname = proto.String(hostname)
 	}
 	m.metrics.HostOs = proto.String(runtime.GOOS)
-	return writeMessageToFile(&m.metrics, outputPath)
+
+	return save(&m.metrics, out)
 }
 
+// SetSoongBuildMetrics sets the metrics collected from the soong_build
+// execution.
 func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
 	m.metrics.SoongBuildMetrics = metrics
 }
 
+// A CriticalUserJourneysMetrics is a struct that contains critical user journey
+// metrics. These critical user journeys are defined under cuj/cuj.go file.
 type CriticalUserJourneysMetrics struct {
+	// A list of collected CUJ metrics.
 	cujs soong_metrics_proto.CriticalUserJourneysMetrics
 }
 
+// NewCriticalUserJourneyMetrics returns a pointer of CriticalUserJourneyMetrics
+// to capture CUJs metrics.
 func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics {
 	return &CriticalUserJourneysMetrics{}
 }
 
+// Add adds a set of collected metrics from an executed critical user journey.
 func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) {
 	c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{
 		Name:    proto.String(name),
@@ -168,22 +231,25 @@
 	})
 }
 
-func (c *CriticalUserJourneysMetrics) Dump(outputPath string) (err error) {
-	return writeMessageToFile(&c.cujs, outputPath)
+// Dump saves the collected CUJs metrics to the raw protobuf file.
+func (c *CriticalUserJourneysMetrics) Dump(filename string) (err error) {
+	return save(&c.cujs, filename)
 }
 
-func writeMessageToFile(pb proto.Message, outputPath string) (err error) {
+// save takes a protobuf message, marshals to an array of bytes
+// and is then saved to a file.
+func save(pb proto.Message, filename string) (err error) {
 	data, err := proto.Marshal(pb)
 	if err != nil {
 		return err
 	}
-	tempPath := outputPath + ".tmp"
-	err = ioutil.WriteFile(tempPath, []byte(data), 0644)
-	if err != nil {
+
+	tempFilename := filename + ".tmp"
+	if err := ioutil.WriteFile(tempFilename, []byte(data), 0644 /* rw-r--r-- */); err != nil {
 		return err
 	}
-	err = os.Rename(tempPath, outputPath)
-	if err != nil {
+
+	if err := os.Rename(tempFilename, filename); err != nil {
 		return err
 	}