Write module dexpreopt.config for Make.

This is needed for Java libraries that are <uses-library> dependencies
of Java libraries and apps defined as Make modules. Each dexpreopted
module in Make generates a dexpreopt.config file, which incorporates
information from its dependencies' dexpreopt.config files. For
dependencies that are Make modules their dexpreopt.config files are
generated by Make, and for Soong modules they are generated by Soong.
Since Soong doesn't know which libraries are used by Make, it generates
build rules for a superset of the necessary libraries.

Bug: 132357300
Test: lunch aosp_cf_x86_phone-userdebug && m
Change-Id: I325b1037658736ee3c02450b08c00eca1a175962
diff --git a/dexpreopt/class_loader_context.go b/dexpreopt/class_loader_context.go
index bf610ef..ec62eb3 100644
--- a/dexpreopt/class_loader_context.go
+++ b/dexpreopt/class_loader_context.go
@@ -534,3 +534,26 @@
 	}
 	return clcs
 }
+
+// Convert Soong CLC map to JSON representation for Make.
+func toJsonClassLoaderContext(clcMap ClassLoaderContextMap) jsonClassLoaderContextMap {
+	jClcMap := make(jsonClassLoaderContextMap)
+	for sdkVer, clcs := range clcMap {
+		sdkVerStr := fmt.Sprintf("%d", sdkVer)
+		jClcMap[sdkVerStr] = toJsonClassLoaderContextRec(clcs)
+	}
+	return jClcMap
+}
+
+// Recursive helper for toJsonClassLoaderContext.
+func toJsonClassLoaderContextRec(clcs []*ClassLoaderContext) map[string]*jsonClassLoaderContext {
+	jClcs := make(map[string]*jsonClassLoaderContext, len(clcs))
+	for _, clc := range clcs {
+		jClcs[clc.Name] = &jsonClassLoaderContext{
+			Host:        clc.Host.String(),
+			Device:      clc.Device,
+			Subcontexts: toJsonClassLoaderContextRec(clc.Subcontexts),
+		}
+	}
+	return jClcs
+}
diff --git a/dexpreopt/config.go b/dexpreopt/config.go
index 867ece6..d55204b 100644
--- a/dexpreopt/config.go
+++ b/dexpreopt/config.go
@@ -114,6 +114,7 @@
 	ProfileBootListing   android.OptionalPath
 
 	EnforceUsesLibraries bool
+	ProvidesUsesLibrary  string // the name of the <uses-library> (usually the same as its module)
 	ClassLoaderContexts  ClassLoaderContextMap
 
 	Archs                   []android.ArchType
@@ -290,6 +291,42 @@
 	return config.ModuleConfig, nil
 }
 
+// WriteSlimModuleConfigForMake serializes a subset of ModuleConfig into a per-module
+// dexpreopt.config JSON file. It is a way to pass dexpreopt information about Soong modules to
+// Make, which is needed when a Make module has a <uses-library> dependency on a Soong module.
+func WriteSlimModuleConfigForMake(ctx android.ModuleContext, config *ModuleConfig, path android.WritablePath) {
+	if path == nil {
+		return
+	}
+
+	// JSON representation of the slim module dexpreopt.config.
+	type slimModuleJSONConfig struct {
+		Name                 string
+		DexLocation          string
+		BuildPath            string
+		EnforceUsesLibraries bool
+		ProvidesUsesLibrary  string
+		ClassLoaderContexts  jsonClassLoaderContextMap
+	}
+
+	jsonConfig := &slimModuleJSONConfig{
+		Name:                 config.Name,
+		DexLocation:          config.DexLocation,
+		BuildPath:            config.BuildPath.String(),
+		EnforceUsesLibraries: config.EnforceUsesLibraries,
+		ProvidesUsesLibrary:  config.ProvidesUsesLibrary,
+		ClassLoaderContexts:  toJsonClassLoaderContext(config.ClassLoaderContexts),
+	}
+
+	data, err := json.MarshalIndent(jsonConfig, "", "    ")
+	if err != nil {
+		ctx.ModuleErrorf("failed to JSON marshal module dexpreopt.config: %v", err)
+		return
+	}
+
+	android.WriteFileRule(ctx, path, string(data))
+}
+
 // dex2oatModuleName returns the name of the module to use for the dex2oat host
 // tool. It should be a binary module with public visibility that is compiled
 // and installed for host.