Make system linker.config.pb match make

Make has different logic for generating the linker.config.pb than
what we've been using in soong up until now, switch soong to match.

Bug: 384091387
Test: diff out/target/product/vsoc_x86_64/system/etc/linker.config.pb out/soong/.intermediates/build/soong/fsgen/aosp_cf_x86_64_phone_generated_system_image/android_common/system/system/etc/linker.config.pb
Change-Id: I581b75b62a28dcf477d60c5f4488d10faaa286cf
diff --git a/cc/llndk_library.go b/cc/llndk_library.go
index 162dd54..8cc3852 100644
--- a/cc/llndk_library.go
+++ b/cc/llndk_library.go
@@ -63,23 +63,52 @@
 }
 
 func makeLlndkVars(ctx android.MakeVarsContext) {
-	// Make uses LLNDK_MOVED_TO_APEX_LIBRARIES to generate the linker config.
-	movedToApexLlndkLibraries := make(map[string]bool)
-	ctx.VisitAllModules(func(module android.Module) {
-		if library := moduleLibraryInterface(module); library != nil && library.hasLLNDKStubs() {
-			if library.isLLNDKMovedToApex() {
-				name := library.implementationModuleName(module.(*Module).BaseModuleName())
-				movedToApexLlndkLibraries[name] = true
-			}
-		}
-	})
 
-	ctx.Strict("LLNDK_MOVED_TO_APEX_LIBRARIES",
-		strings.Join(android.SortedKeys(movedToApexLlndkLibraries), " "))
 }
 
 func init() {
 	RegisterLlndkLibraryTxtType(android.InitRegistrationContext)
+	android.RegisterParallelSingletonType("movedToApexLlndkLibraries", movedToApexLlndkLibrariesFactory)
+}
+
+func movedToApexLlndkLibrariesFactory() android.Singleton {
+	return &movedToApexLlndkLibraries{}
+}
+
+type movedToApexLlndkLibraries struct {
+	movedToApexLlndkLibraries []string
+}
+
+func (s *movedToApexLlndkLibraries) GenerateBuildActions(ctx android.SingletonContext) {
+	// Make uses LLNDK_MOVED_TO_APEX_LIBRARIES to generate the linker config.
+	movedToApexLlndkLibrariesMap := make(map[string]bool)
+	ctx.VisitAllModules(func(module android.Module) {
+		if library := moduleLibraryInterface(module); library != nil && library.hasLLNDKStubs() {
+			if library.isLLNDKMovedToApex() {
+				name := library.implementationModuleName(module.(*Module).BaseModuleName())
+				movedToApexLlndkLibrariesMap[name] = true
+			}
+		}
+	})
+	s.movedToApexLlndkLibraries = android.SortedKeys(movedToApexLlndkLibrariesMap)
+
+	var sb strings.Builder
+	for i, l := range s.movedToApexLlndkLibraries {
+		if i > 0 {
+			sb.WriteRune(' ')
+		}
+		sb.WriteString(l)
+		sb.WriteString(".so")
+	}
+	android.WriteFileRule(ctx, MovedToApexLlndkLibrariesFile(ctx), sb.String())
+}
+
+func MovedToApexLlndkLibrariesFile(ctx android.PathContext) android.WritablePath {
+	return android.PathForIntermediates(ctx, "moved_to_apex_llndk_libraries.txt")
+}
+
+func (s *movedToApexLlndkLibraries) MakeVars(ctx android.MakeVarsContext) {
+	ctx.Strict("LLNDK_MOVED_TO_APEX_LIBRARIES", strings.Join(s.movedToApexLlndkLibraries, " "))
 }
 
 func RegisterLlndkLibraryTxtType(ctx android.RegistrationContext) {
diff --git a/cc/makevars.go b/cc/makevars.go
index 4cb98e7..ca97b76 100644
--- a/cc/makevars.go
+++ b/cc/makevars.go
@@ -186,8 +186,6 @@
 	if len(deviceTargets) > 1 {
 		makeVarsToolchain(ctx, "2ND_", deviceTargets[1])
 	}
-
-	makeLlndkVars(ctx)
 }
 
 func makeVarsToolchain(ctx android.MakeVarsContext, secondPrefix string,
diff --git a/cc/stub_library.go b/cc/stub_library.go
index e746a33..5911be0 100644
--- a/cc/stub_library.go
+++ b/cc/stub_library.go
@@ -26,9 +26,13 @@
 	android.RegisterParallelSingletonType("stublibraries", stubLibrariesSingleton)
 }
 
+func stubLibrariesSingleton() android.Singleton {
+	return &stubLibraries{}
+}
+
 type stubLibraries struct {
-	stubLibraryMap       map[string]bool
-	stubVendorLibraryMap map[string]bool
+	stubLibraries       []string
+	vendorStubLibraries []string
 
 	apiListCoverageXmlPaths []string
 }
@@ -51,13 +55,15 @@
 
 func (s *stubLibraries) GenerateBuildActions(ctx android.SingletonContext) {
 	// Visit all generated soong modules and store stub library file names.
+	stubLibraryMap := make(map[string]bool)
+	vendorStubLibraryMap := make(map[string]bool)
 	ctx.VisitAllModules(func(module android.Module) {
 		if m, ok := module.(*Module); ok {
 			if IsStubTarget(m) {
 				if name := getInstalledFileName(ctx, m); name != "" {
-					s.stubLibraryMap[name] = true
+					stubLibraryMap[name] = true
 					if m.InVendor() {
-						s.stubVendorLibraryMap[name] = true
+						vendorStubLibraryMap[name] = true
 					}
 				}
 			}
@@ -68,19 +74,20 @@
 			}
 		}
 	})
+	s.stubLibraries = android.SortedKeys(stubLibraryMap)
+	s.vendorStubLibraries = android.SortedKeys(vendorStubLibraryMap)
+
+	android.WriteFileRule(ctx, StubLibrariesFile(ctx), strings.Join(s.stubLibraries, " "))
 }
 
-func stubLibrariesSingleton() android.Singleton {
-	return &stubLibraries{
-		stubLibraryMap:       make(map[string]bool),
-		stubVendorLibraryMap: make(map[string]bool),
-	}
+func StubLibrariesFile(ctx android.PathContext) android.WritablePath {
+	return android.PathForIntermediates(ctx, "stub_libraries.txt")
 }
 
 func (s *stubLibraries) MakeVars(ctx android.MakeVarsContext) {
 	// Convert stub library file names into Makefile variable.
-	ctx.Strict("STUB_LIBRARIES", strings.Join(android.SortedKeys(s.stubLibraryMap), " "))
-	ctx.Strict("SOONG_STUB_VENDOR_LIBRARIES", strings.Join(android.SortedKeys(s.stubVendorLibraryMap), " "))
+	ctx.Strict("STUB_LIBRARIES", strings.Join(s.stubLibraries, " "))
+	ctx.Strict("SOONG_STUB_VENDOR_LIBRARIES", strings.Join(s.vendorStubLibraries, " "))
 
 	// Export the list of API XML files to Make.
 	sort.Strings(s.apiListCoverageXmlPaths)