From 802ae0fdd3b6f9975f0bb4c4b18ada5603c7aa63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Kongstad?= Date: Wed, 27 Jul 2022 13:47:32 +0200 Subject: [PATCH] Pass --sdk-extensions-{root,info} to metalava Teach soong to pass metalava the two new command line arguments --sdk-extensions-root and --sdk-extensions-info when generating the API levels file (--generate-api-levels api-versions.xml). The directory hierarcy in a droiddoc_exported_dir module has special meaning, e.g. extensions/1/public/*-stubs.jar are the mainline module stubs containing the public API for SdkExt version 1. Update the logic where the directories are scanned for android.jar files to locate the SDK extension jars (extensions//public/*-stubs.jar). Also introduce a new field on droidstubs properties (Extensions_info_file) to set the value of --sdk-extensions-info. Note: if Extensions_info_file is not set, neither --sdk-extensions-root or --sdk-extensions-info will be passed to metalava. Bug: 228828986 Test: go test -run TestDroidstubsWithSdkExtensions ./java Test: m sdk dist && grep -e from= -e module= out/dist/data/api-versions.xml # needs APIs to be listed in the info file Change-Id: I682e34d328fc93d3eded8565ffee40961307901a --- java/droidstubs.go | 26 ++++++++++++++++++++++++++ java/droidstubs_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/java/droidstubs.go b/java/droidstubs.go index 12590ca50..d9efb408f 100644 --- a/java/droidstubs.go +++ b/java/droidstubs.go @@ -17,6 +17,7 @@ package java import ( "fmt" "path/filepath" + "regexp" "strings" "github.com/google/blueprint/proptools" @@ -142,6 +143,10 @@ type DroidstubsProperties struct { // if set to true, collect the values used by the Dev tools and // write them in files packaged with the SDK. Defaults to false. Write_sdk_values *bool + + // path or filegroup to file defining extension an SDK name <-> numerical ID mapping and + // what APIs exist in which SDKs; passed to metalava via --sdk-extensions-info + Extensions_info_file *string `android:"path"` } // Used by xsd_config @@ -398,9 +403,20 @@ func (d *Droidstubs) apiLevelsGenerationFlags(ctx android.ModuleContext, cmd *an filename := proptools.StringDefault(d.properties.Api_levels_jar_filename, "android.jar") var dirs []string + var extensions_dir string ctx.VisitDirectDepsWithTag(metalavaAPILevelsAnnotationsDirTag, func(m android.Module) { if t, ok := m.(*ExportedDroiddocDir); ok { + extRegex := regexp.MustCompile(t.dir.String() + `/extensions/[0-9]+/public/.*\.jar`) + + // Grab the first extensions_dir and we find while scanning ExportedDroiddocDir.deps; + // ideally this should be read from prebuiltApis.properties.Extensions_* for _, dep := range t.deps { + if extRegex.MatchString(dep.String()) && d.properties.Extensions_info_file != nil { + if extensions_dir == "" { + extensions_dir = t.dir.String() + "/extensions" + } + cmd.Implicit(dep) + } if dep.Base() == filename { cmd.Implicit(dep) } @@ -445,6 +461,16 @@ func (d *Droidstubs) apiLevelsGenerationFlags(ctx android.ModuleContext, cmd *an cmd.FlagWithArg("--android-jar-pattern ", fmt.Sprintf("%s/%%/%s/%s", dir, sdkDir, filename)) } } + + if d.properties.Extensions_info_file != nil { + if extensions_dir == "" { + ctx.ModuleErrorf("extensions_info_file set, but no SDK extension dirs found") + } + info_file := android.PathForModuleSrc(ctx, *d.properties.Extensions_info_file) + cmd.Implicit(info_file) + cmd.FlagWithArg("--sdk-extensions-root ", extensions_dir) + cmd.FlagWithArg("--sdk-extensions-info ", info_file.String()) + } } func metalavaUseRbe(ctx android.ModuleContext) bool { diff --git a/java/droidstubs_test.go b/java/droidstubs_test.go index 9fdfddeb1..24436921c 100644 --- a/java/droidstubs_test.go +++ b/java/droidstubs_test.go @@ -259,3 +259,33 @@ func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, m t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars) } } + +func TestDroidstubsWithSdkExtensions(t *testing.T) { + ctx, _ := testJavaWithFS(t, ` + droiddoc_exported_dir { + name: "sdk-dir", + path: "sdk", + } + + droidstubs { + name: "baz-stubs", + api_levels_annotations_dirs: ["sdk-dir"], + api_levels_annotations_enabled: true, + extensions_info_file: ":info-file", + } + + filegroup { + name: "info-file", + srcs: ["sdk/extensions/info.txt"], + } + `, + map[string][]byte{ + "sdk/extensions/1/public/some-mainline-module-stubs.jar": nil, + "sdk/extensions/info.txt": nil, + }) + m := ctx.ModuleForTests("baz-stubs", "android_common") + manifest := m.Output("metalava.sbox.textproto") + cmdline := String(android.RuleBuilderSboxProtoForTests(t, manifest).Commands[0].Command) + android.AssertStringDoesContain(t, "sdk-extensions-root present", cmdline, "--sdk-extensions-root sdk/extensions") + android.AssertStringDoesContain(t, "sdk-extensions-info present", cmdline, "--sdk-extensions-info sdk/extensions/info.txt") +}