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/java/dexpreopt.go b/java/dexpreopt.go
index b5830c7..ac00592 100644
--- a/java/dexpreopt.go
+++ b/java/dexpreopt.go
@@ -30,6 +30,7 @@
 	installPath         android.InstallPath
 	uncompressedDex     bool
 	isSDKLibrary        bool
+	isApp               bool
 	isTest              bool
 	isPresignedPrebuilt bool
 
@@ -38,6 +39,11 @@
 	classLoaderContexts dexpreopt.ClassLoaderContextMap
 
 	builtInstalled string
+
+	// A path to a dexpreopt.config file generated by Soong for libraries that may be used as a
+	// <uses-library> by Make modules. The path is passed to Make via LOCAL_SOONG_DEXPREOPT_CONFIG
+	// variable. If the path is nil, no config is generated (which is the case for apps and tests).
+	configPath android.WritablePath
 }
 
 type DexpreoptProperties struct {
@@ -117,7 +123,40 @@
 	// the dexpreopter struct hasn't been fully initialized before we're called,
 	// e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively
 	// disabled, even if installable is true.
-	if d.dexpreoptDisabled(ctx) || d.installPath.Base() == "." {
+	if d.installPath.Base() == "." {
+		return
+	}
+
+	dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
+
+	buildPath := android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath
+
+	providesUsesLib := ctx.ModuleName()
+	if ulib, ok := ctx.Module().(ProvidesUsesLib); ok {
+		name := ulib.ProvidesUsesLib()
+		if name != nil {
+			providesUsesLib = *name
+		}
+	}
+
+	if !d.isApp && !d.isTest {
+		// Slim dexpreopt config is serialized to dexpreopt.config files and used by
+		// dex_preopt_config_merger.py to get information about <uses-library> dependencies.
+		// Note that it might be needed even if dexpreopt is disabled for this module.
+		slimDexpreoptConfig := &dexpreopt.ModuleConfig{
+			Name:                 ctx.ModuleName(),
+			DexLocation:          dexLocation,
+			BuildPath:            buildPath,
+			EnforceUsesLibraries: d.enforceUsesLibs,
+			ProvidesUsesLibrary:  providesUsesLib,
+			ClassLoaderContexts:  d.classLoaderContexts,
+			// The rest of the fields are not needed.
+		}
+		d.configPath = android.PathForModuleOut(ctx, "dexpreopt", "dexpreopt.config")
+		dexpreopt.WriteSlimModuleConfigForMake(ctx, slimDexpreoptConfig, d.configPath)
+	}
+
+	if d.dexpreoptDisabled(ctx) {
 		return
 	}
 
@@ -157,8 +196,6 @@
 	// The image locations for all Android variants are identical.
 	imageLocations := bootImage.getAnyAndroidVariant().imageLocations()
 
-	dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
-
 	var profileClassListing android.OptionalPath
 	var profileBootListing android.OptionalPath
 	profileIsTextListing := false
@@ -177,10 +214,11 @@
 		}
 	}
 
+	// Full dexpreopt config, used to create dexpreopt build rules.
 	dexpreoptConfig := &dexpreopt.ModuleConfig{
 		Name:            ctx.ModuleName(),
 		DexLocation:     dexLocation,
-		BuildPath:       android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
+		BuildPath:       buildPath,
 		DexPath:         dexJarFile,
 		ManifestPath:    d.manifestFile,
 		UncompressedDex: d.uncompressedDex,
@@ -192,6 +230,7 @@
 		ProfileBootListing:   profileBootListing,
 
 		EnforceUsesLibraries: d.enforceUsesLibs,
+		ProvidesUsesLibrary:  providesUsesLib,
 		ClassLoaderContexts:  d.classLoaderContexts,
 
 		Archs:                   archs,