Also generate api lists based on symbol files for cc_library rule during build time.

Test: build/soong/soong_ui.bash --make-mode SKIP_ABI_CHECKS=true TARGET_PRODUCT=aosp_cf_x86_phone TARGET_BUILD_VARIANT=userdebug droid dist DIST_DIR=/usr/local/google/home/sophiez/my_dist_dir EMMA_INSTRUMENT=true EMMA_INSTRUMENT_FRAMEWORK=true CLANG_COVERAGE=true SKIP_BOOT_JARS_CHECK=true

Change-Id: Ic857c14c5c258b8f4d150cc71ce9eabce33d7d54
diff --git a/cc/androidmk.go b/cc/androidmk.go
index bda1006..ffd9539 100644
--- a/cc/androidmk.go
+++ b/cc/androidmk.go
@@ -294,6 +294,9 @@
 			if library.buildStubs() {
 				entries.SetBool("LOCAL_NO_NOTICE_FILE", true)
 			}
+			if library.apiListCoverageXmlPath.String() != "" {
+				entries.SetString("SOONG_CC_API_XML", "$(SOONG_CC_API_XML) "+library.apiListCoverageXmlPath.String())
+			}
 		})
 	}
 	// If a library providing a stub is included in an APEX, the private APIs of the library
diff --git a/cc/coverage.go b/cc/coverage.go
index baf4226..8dd2db1 100644
--- a/cc/coverage.go
+++ b/cc/coverage.go
@@ -244,3 +244,19 @@
 		m[1].(Coverage).EnableCoverageIfNeeded()
 	}
 }
+
+func parseSymbolFileForAPICoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath {
+	apiLevelsJson := android.GetApiLevelsJson(ctx)
+	symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
+	outputFile := ctx.baseModuleName() + ".xml"
+	parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFile)
+	rule := android.NewRuleBuilder(pctx, ctx)
+	rule.Command().
+		BuiltTool("ndk_api_coverage_parser").
+		Input(symbolFilePath).
+		Output(parsedApiCoveragePath).
+		Implicit(apiLevelsJson).
+		FlagWithArg("--api-map ", apiLevelsJson.String())
+	rule.Build("native_library_api_list", "Generate native API list based on symbol files for coverage measurement")
+	return parsedApiCoveragePath
+}
diff --git a/cc/library.go b/cc/library.go
index 8f302fc..7392151 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -550,6 +550,8 @@
 	*baseInstaller
 
 	collectedSnapshotHeaders android.Paths
+
+	apiListCoverageXmlPath android.ModuleOutPath
 }
 
 type ccLibraryBazelHandler struct {
@@ -965,6 +967,12 @@
 		objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
 		library.versionScriptPath = android.OptionalPathForPath(
 			nativeAbiResult.versionScript)
+
+		// Parse symbol file to get API list for coverage
+		if library.stubsVersion() == "current" && ctx.PrimaryArch() {
+			library.apiListCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile)
+		}
+
 		return objs
 	}
 
diff --git a/cc/ndk_library.go b/cc/ndk_library.go
index 63e8261..51cdddf 100644
--- a/cc/ndk_library.go
+++ b/cc/ndk_library.go
@@ -29,7 +29,6 @@
 
 func init() {
 	pctx.HostBinToolVariable("ndkStubGenerator", "ndkstubgen")
-	pctx.HostBinToolVariable("ndk_api_coverage_parser", "ndk_api_coverage_parser")
 	pctx.HostBinToolVariable("abidiff", "abidiff")
 	pctx.HostBinToolVariable("abitidy", "abitidy")
 	pctx.HostBinToolVariable("abidw", "abidw")
@@ -43,12 +42,6 @@
 			CommandDeps: []string{"$ndkStubGenerator"},
 		}, "arch", "apiLevel", "apiMap", "flags")
 
-	parseNdkApiRule = pctx.AndroidStaticRule("parseNdkApiRule",
-		blueprint.RuleParams{
-			Command:     "$ndk_api_coverage_parser $in $out --api-map $apiMap",
-			CommandDeps: []string{"$ndk_api_coverage_parser"},
-		}, "apiMap")
-
 	abidw = pctx.AndroidStaticRule("abidw",
 		blueprint.RuleParams{
 			Command: "$abidw --type-id-style hash --no-corpus-path " +
@@ -276,24 +269,6 @@
 		android.Paths{src}, nil, nil)
 }
 
-func parseSymbolFileForCoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath {
-	apiLevelsJson := android.GetApiLevelsJson(ctx)
-	symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
-	outputFileName := strings.Split(symbolFilePath.Base(), ".")[0]
-	parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFileName+".xml")
-	ctx.Build(pctx, android.BuildParams{
-		Rule:        parseNdkApiRule,
-		Description: "parse ndk api symbol file for api coverage: " + symbolFilePath.Rel(),
-		Outputs:     []android.WritablePath{parsedApiCoveragePath},
-		Input:       symbolFilePath,
-		Implicits:   []android.Path{apiLevelsJson},
-		Args: map[string]string{
-			"apiMap": apiLevelsJson.String(),
-		},
-	})
-	return parsedApiCoveragePath
-}
-
 func (this *stubDecorator) findImplementationLibrary(ctx ModuleContext) android.Path {
 	dep := ctx.GetDirectDepWithTag(strings.TrimSuffix(ctx.ModuleName(), ndkLibrarySuffix),
 		stubImplementation)
@@ -454,7 +429,7 @@
 		}
 	}
 	if c.apiLevel.IsCurrent() && ctx.PrimaryArch() {
-		c.parsedCoverageXmlPath = parseSymbolFileForCoverage(ctx, symbolFile)
+		c.parsedCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile)
 	}
 	return objs
 }