NDK library: extract ABI representations with STG

Replace the abidw->stg pipeline by just stg for extraction directly from
ELF/DWARF. This is to migrate to STG entirely.

As a migration tooling, keep the existing method active via
`legacy_use_abidw` as a flag on ndk_library.

Bug: 156513478
Change-Id: Ideaa9908b31591f49f9a167cfa3f3d5c95d8b198
Signed-off-by: Matthias Maennich <maennich@google.com>
diff --git a/cc/ndk_library.go b/cc/ndk_library.go
index e10e70d..c0c6eef 100644
--- a/cc/ndk_library.go
+++ b/cc/ndk_library.go
@@ -44,6 +44,7 @@
 			CommandDeps: []string{"$ndkStubGenerator"},
 		}, "arch", "apiLevel", "apiMap", "flags")
 
+	// TODO(b/156513478): remove once migration to STG is complete
 	abidw = pctx.AndroidStaticRule("abidw",
 		blueprint.RuleParams{
 			Command: "$abidw --type-id-style hash --no-corpus-path " +
@@ -52,6 +53,13 @@
 			CommandDeps: []string{"$abidw"},
 		}, "symbolList")
 
+	stg = pctx.AndroidStaticRule("stg",
+		blueprint.RuleParams{
+			Command:     "$stg -S :$symbolList --elf $in -o $out",
+			CommandDeps: []string{"$stg"},
+		}, "symbolList")
+
+	// TODO(b/156513478): remove once migration to STG is complete
 	xml2stg = pctx.AndroidStaticRule("xml2stg",
 		blueprint.RuleParams{
 			Command:     "$stg --abi -i $in -o $out",
@@ -109,6 +117,10 @@
 
 	// Headers presented by this library to the Public API Surface
 	Export_header_libs []string
+
+	// TODO(b/156513478): remove once migration to STG is complete
+	// Fall back to the legacy abidw ABI extraction pipeline
+	Legacy_use_abidw *bool
 }
 
 type stubDecorator struct {
@@ -351,7 +363,8 @@
 	return false
 }
 
-func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) {
+// TODO(b/156513478): remove once migration to STG is complete
+func (this *stubDecorator) dumpAbiLegacy(ctx ModuleContext, symbolList android.Path) {
 	implementationLibrary := this.findImplementationLibrary(ctx)
 	abiRawPath := getNdkAbiDumpInstallBase(ctx).Join(ctx,
 		this.apiLevel.String(), ctx.Arch().ArchType.String(),
@@ -378,6 +391,23 @@
 	})
 }
 
+func (this *stubDecorator) dumpAbi(ctx ModuleContext, symbolList android.Path) {
+	implementationLibrary := this.findImplementationLibrary(ctx)
+	this.abiDumpPath = getNdkAbiDumpInstallBase(ctx).Join(ctx,
+		this.apiLevel.String(), ctx.Arch().ArchType.String(),
+		this.libraryName(ctx), "abi.stg")
+	ctx.Build(pctx, android.BuildParams{
+		Rule:        stg,
+		Description: fmt.Sprintf("stg %s", implementationLibrary),
+		Input:       implementationLibrary,
+		Implicit:    symbolList,
+		Output:      this.abiDumpPath,
+		Args: map[string]string{
+			"symbolList": symbolList.String(),
+		},
+	})
+}
+
 func findNextApiLevel(ctx ModuleContext, apiLevel android.ApiLevel) *android.ApiLevel {
 	apiLevels := append(ctx.Config().AllSupportedApiLevels(),
 		android.FutureApiLevel)
@@ -476,7 +506,11 @@
 	objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
 	c.versionScriptPath = nativeAbiResult.versionScript
 	if canDumpAbi(ctx.Config()) {
-		c.dumpAbi(ctx, nativeAbiResult.symbolList)
+		if proptools.BoolDefault(c.properties.Legacy_use_abidw, false) {
+			c.dumpAbiLegacy(ctx, nativeAbiResult.symbolList)
+		} else {
+			c.dumpAbi(ctx, nativeAbiResult.symbolList)
+		}
 		if canDiffAbi() {
 			c.diffAbi(ctx)
 		}