Collect metrics from inside soong_build

Collect the number of modules and variants and some basic statistics
on memory usage inside soong_build by writing out a proto that is
read back in by soong_ui.

Test: examine soong.log
Change-Id: I6926876377a4f6229cf41fdbf166ae03c885ea55
diff --git a/cmd/soong_build/Android.bp b/cmd/soong_build/Android.bp
index 2536a53..6b79823 100644
--- a/cmd/soong_build/Android.bp
+++ b/cmd/soong_build/Android.bp
@@ -17,9 +17,11 @@
     deps: [
         "blueprint",
         "blueprint-bootstrap",
+        "golang-protobuf-proto",
         "soong",
         "soong-android",
         "soong-env",
+        "soong-ui-metrics_proto",
     ],
     srcs: [
         "main.go",
diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go
index 30381e0..bd1a9ed 100644
--- a/cmd/soong_build/main.go
+++ b/cmd/soong_build/main.go
@@ -131,4 +131,15 @@
 			os.Exit(1)
 		}
 	}
+
+	// TODO(ccross): make this a command line argument.  Requires plumbing through blueprint
+	//  to affect the command line of the primary builder.
+	if docFile == "" {
+		metricsFile := filepath.Join(bootstrap.BuildDir, "soong_build_metrics.pb")
+		err = android.WriteMetrics(configuration, metricsFile)
+		if err != nil {
+			fmt.Fprintf(os.Stderr, "error writing soong_build metrics %s: %s", metricsFile, err)
+			os.Exit(1)
+		}
+	}
 }