From 47f6315f01f7c81cfd6a494f0a65426bb97a97c5 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Thu, 9 Apr 2020 17:15:44 +0100 Subject: [PATCH] Create prebuilt api modules in load hook not mutator An attempt to reference one of the prebuilt modules directly from an Android.bp file highlighted a bug. The prebuilt_apis module used a mutator to create filegroup and java_import modules for a set of api versions. That mutator ran after the "prebuilts" mutator which handled the renaming of prebuilt modules when the matching source module was not present. That meant that the only way to reference the module was by explicitly adding the prefix "prebuilt_". This change fixed that bug by creating the modules in a load hook rather that a mutator. This ensures that the prebuilt modules are present well before the "prebuilts" mutator is run. Removing the mutator also removes an unnecessary traversal of the whole dependency tree. It also updated the documentation to explain that it creates java_import modules as well as the filegroup modules. Bug: 153649372 Test: m droid Change-Id: Id44dce8ca9968ae903345df6ef1c4f1be9cb76c5 --- java/prebuilt_apis.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/java/prebuilt_apis.go b/java/prebuilt_apis.go index cb17feedd..86eddb172 100644 --- a/java/prebuilt_apis.go +++ b/java/prebuilt_apis.go @@ -28,10 +28,6 @@ func init() { func RegisterPrebuiltApisBuildComponents(ctx android.RegistrationContext) { ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory) - - ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) { - ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel() - }) } type prebuiltApisProperties struct { @@ -48,7 +44,7 @@ func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContex // no need to implement } -func parseJarPath(ctx android.BaseModuleContext, path string) (module string, apiver string, scope string) { +func parseJarPath(path string) (module string, apiver string, scope string) { elements := strings.Split(path, "/") apiver = elements[0] @@ -58,7 +54,7 @@ func parseJarPath(ctx android.BaseModuleContext, path string) (module string, ap return } -func parseApiFilePath(ctx android.BaseModuleContext, path string) (module string, apiver string, scope string) { +func parseApiFilePath(ctx android.LoadHookContext, path string) (module string, apiver string, scope string) { elements := strings.Split(path, "/") apiver = elements[0] @@ -73,7 +69,7 @@ func parseApiFilePath(ctx android.BaseModuleContext, path string) (module string return } -func createImport(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) { +func createImport(mctx android.LoadHookContext, module string, scope string, apiver string, path string) { props := struct { Name *string Jars []string @@ -89,7 +85,7 @@ func createImport(mctx android.TopDownMutatorContext, module string, scope strin mctx.CreateModule(ImportFactory, &props) } -func createFilegroup(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) { +func createFilegroup(mctx android.LoadHookContext, module string, scope string, apiver string, path string) { fgName := module + ".api." + scope + "." + apiver filegroupProps := struct { Name *string @@ -100,7 +96,7 @@ func createFilegroup(mctx android.TopDownMutatorContext, module string, scope st mctx.CreateModule(android.FileGroupFactory, &filegroupProps) } -func getPrebuiltFiles(mctx android.TopDownMutatorContext, name string) []string { +func getPrebuiltFiles(mctx android.LoadHookContext, name string) []string { mydir := mctx.ModuleDir() + "/" var files []string for _, apiver := range mctx.Module().(*prebuiltApis).properties.Api_dirs { @@ -115,7 +111,7 @@ func getPrebuiltFiles(mctx android.TopDownMutatorContext, name string) []string return files } -func prebuiltSdkStubs(mctx android.TopDownMutatorContext) { +func prebuiltSdkStubs(mctx android.LoadHookContext) { mydir := mctx.ModuleDir() + "/" // //.jar files := getPrebuiltFiles(mctx, "*.jar") @@ -123,12 +119,12 @@ func prebuiltSdkStubs(mctx android.TopDownMutatorContext) { for _, f := range files { // create a Import module for each jar file localPath := strings.TrimPrefix(f, mydir) - module, apiver, scope := parseJarPath(mctx, localPath) + module, apiver, scope := parseJarPath(localPath) createImport(mctx, module, scope, apiver, localPath) } } -func prebuiltApiFiles(mctx android.TopDownMutatorContext) { +func prebuiltApiFiles(mctx android.LoadHookContext) { mydir := mctx.ModuleDir() + "/" // //api/.txt files := getPrebuiltFiles(mctx, "api/*.txt") @@ -178,7 +174,7 @@ func prebuiltApiFiles(mctx android.TopDownMutatorContext) { } } -func PrebuiltApisMutator(mctx android.TopDownMutatorContext) { +func createPrebuiltApiModules(mctx android.LoadHookContext) { if _, ok := mctx.Module().(*prebuiltApis); ok { prebuiltApiFiles(mctx) prebuiltSdkStubs(mctx) @@ -191,9 +187,15 @@ func PrebuiltApisMutator(mctx android.TopDownMutatorContext) { // generates a filegroup module named -api... // // It also creates -api..latest for the latest . +// +// Similarly, it generates a java_import for all API .jar files found under the +// directory where the Android.bp is located. Specifically, an API file located +// at .///api/.jar generates a java_import module named +// .... func PrebuiltApisFactory() android.Module { module := &prebuiltApis{} module.AddProperties(&module.properties) android.InitAndroidModule(module) + android.AddLoadHook(module, createPrebuiltApiModules) return module }