Use Soong modules for the NDK's extra STL libraries

 * The extra STL libs are:

       libc++abi.a [needed for ndk_libc++_static]
       libandroid_support.a [always needed in NDK r16]
       libunwind.a [needed for ARM32]

 * The existing STL-dependency logic in linkShared only applies to shared
   libraries. By moving it to STL deps, the extra STL libs are linked into
   both shared libraries and executables.

 * Remove the ndk_prebuilt_library/ndkPrebuiltLibraryFactory module type,
   which is unused now.

 * Reuse the ndk_prebuilt_static_stl module type to describe the extra
   static libraries that are linked with both the static and shared libc++
   STLs.

Bug: b/73133405
Test: manual
Change-Id: I3f73e4f882d39e6efa470073bb4fc8c42dff8253
diff --git a/cc/cc.go b/cc/cc.go
index b9c589a..721f4b1 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -1052,10 +1052,6 @@
 		// These are always allowed
 		return
 	}
-	if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
-		// These are allowed, but they don't set sdk_version
-		return
-	}
 	if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
 		// These are allowed, but they don't set sdk_version
 		return
diff --git a/cc/library.go b/cc/library.go
index 76f8a8c..98a9673 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -601,25 +601,6 @@
 	sharedLibs := deps.SharedLibs
 	sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
 
-	// TODO(danalbert): Clean this up when soong supports prebuilts.
-	if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
-		libDir := getNdkStlLibDir(ctx, "libc++")
-
-		if strings.HasSuffix(ctx.selectedStl(), "_shared") {
-			deps.StaticLibs = append(deps.StaticLibs,
-				libDir.Join(ctx, "libandroid_support.a"))
-		} else {
-			deps.StaticLibs = append(deps.StaticLibs,
-				libDir.Join(ctx, "libc++abi.a"),
-				libDir.Join(ctx, "libandroid_support.a"))
-		}
-
-		if ctx.Arch().ArchType == android.Arm {
-			deps.StaticLibs = append(deps.StaticLibs,
-				libDir.Join(ctx, "libunwind.a"))
-		}
-	}
-
 	linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
 	linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
 	linkerDeps = append(linkerDeps, objs.tidyFiles...)
diff --git a/cc/ndk_prebuilt.go b/cc/ndk_prebuilt.go
index 7e8d989..4c633c2 100644
--- a/cc/ndk_prebuilt.go
+++ b/cc/ndk_prebuilt.go
@@ -23,7 +23,6 @@
 )
 
 func init() {
-	android.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory)
 	android.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory)
 	android.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
 	android.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
@@ -80,66 +79,32 @@
 	deps PathDeps, objs Objects) android.Path {
 	// A null build step, but it sets up the output path.
 	if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
-		ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
+		ctx.ModuleErrorf("NDK prebuilt objects must have an ndk_crt prefixed name")
 	}
 
 	return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
 }
 
-type ndkPrebuiltLibraryLinker struct {
+type ndkPrebuiltStlLinker struct {
 	*libraryDecorator
 }
 
-func (ndk *ndkPrebuiltLibraryLinker) linkerProps() []interface{} {
+func (ndk *ndkPrebuiltStlLinker) linkerProps() []interface{} {
 	return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties)
 }
 
-func (*ndkPrebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
+func (*ndkPrebuiltStlLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
 	// NDK libraries can't have any dependencies
 	return deps
 }
 
-func ndkPrebuiltLibraryFactory() android.Module {
-	module, library := NewLibrary(android.DeviceSupported)
-	library.BuildOnlyShared()
-	linker := &ndkPrebuiltLibraryLinker{
-		libraryDecorator: library,
-	}
-	module.compiler = nil
-	module.linker = linker
-	module.installer = nil
-	module.stl = nil
-	module.Properties.HideFromMake = true
-	return module.Init()
-}
-
-func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
-	deps PathDeps, objs Objects) android.Path {
-	// A null build step, but it sets up the output path.
-	ndk.exportIncludes(ctx, "-isystem")
-
-	return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
-		ctx.sdkVersion())
-}
-
-// The NDK STLs are slightly different from the prebuilt system libraries:
-//     * Are not specific to each platform version.
-//     * The libraries are not in a predictable location for each STL.
-
-type ndkPrebuiltStlLinker struct {
-	ndkPrebuiltLibraryLinker
-}
-
 func ndkPrebuiltSharedStlFactory() android.Module {
 	module, library := NewLibrary(android.DeviceSupported)
 	library.BuildOnlyShared()
-	linker := &ndkPrebuiltStlLinker{
-		ndkPrebuiltLibraryLinker: ndkPrebuiltLibraryLinker{
-			libraryDecorator: library,
-		},
-	}
 	module.compiler = nil
-	module.linker = linker
+	module.linker = &ndkPrebuiltStlLinker{
+		libraryDecorator: library,
+	}
 	module.installer = nil
 	minVersionString := "minimum"
 	noStlString := "none"
@@ -151,29 +116,25 @@
 func ndkPrebuiltStaticStlFactory() android.Module {
 	module, library := NewLibrary(android.DeviceSupported)
 	library.BuildOnlyStatic()
-	linker := &ndkPrebuiltStlLinker{
-		ndkPrebuiltLibraryLinker: ndkPrebuiltLibraryLinker{
-			libraryDecorator: library,
-		},
-	}
 	module.compiler = nil
-	module.linker = linker
+	module.linker = &ndkPrebuiltStlLinker{
+		libraryDecorator: library,
+	}
 	module.installer = nil
 	module.Properties.HideFromMake = true
 	return module.Init()
 }
 
-func getNdkStlLibDir(ctx android.ModuleContext, stl string) android.SourcePath {
-	libDir := "cxx-stl/llvm-libc++/libs"
-	ndkSrcRoot := "prebuilts/ndk/current/sources"
-	return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
+func getNdkStlLibDir(ctx android.ModuleContext) android.SourcePath {
+	libDir := "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs"
+	return android.PathForSource(ctx, libDir).Join(ctx, ctx.Arch().Abi[0])
 }
 
 func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
 	deps PathDeps, objs Objects) android.Path {
 	// A null build step, but it sets up the output path.
 	if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
-		ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
+		ctx.ModuleErrorf("NDK prebuilt libraries must have an ndk_lib prefixed name")
 	}
 
 	ndk.exportIncludes(ctx, "-isystem")
@@ -184,8 +145,6 @@
 		libExt = staticLibraryExtension
 	}
 
-	stlName := strings.TrimSuffix(libName, "_shared")
-	stlName = strings.TrimSuffix(stlName, "_static")
-	libDir := getNdkStlLibDir(ctx, stlName)
+	libDir := getNdkStlLibDir(ctx)
 	return libDir.Join(ctx, libName+libExt)
 }
diff --git a/cc/stl.go b/cc/stl.go
index 2da6471..6f63835 100644
--- a/cc/stl.go
+++ b/cc/stl.go
@@ -129,10 +129,16 @@
 		// The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have
 		// its own includes. The includes are handled in CCBase.Flags().
 		deps.SharedLibs = append([]string{"libstdc++"}, deps.SharedLibs...)
-	case "ndk_libc++_shared":
-		deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
-	case "ndk_libc++_static":
-		deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl)
+	case "ndk_libc++_shared", "ndk_libc++_static":
+		if stl.Properties.SelectedStl == "ndk_libc++_shared" {
+			deps.SharedLibs = append(deps.SharedLibs, stl.Properties.SelectedStl)
+		} else {
+			deps.StaticLibs = append(deps.StaticLibs, stl.Properties.SelectedStl, "ndk_libc++abi")
+		}
+		deps.StaticLibs = append(deps.StaticLibs, "ndk_libandroid_support")
+		if ctx.Arch().ArchType == android.Arm {
+			deps.StaticLibs = append(deps.StaticLibs, "ndk_libunwind")
+		}
 	default:
 		panic(fmt.Errorf("Unknown stl: %q", stl.Properties.SelectedStl))
 	}