Merge "Add module-lib to droidstubs' supported sdk types" am: 2937eb5bb4 am: bba5a387d6 am: ae4d32592a

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1844312

Change-Id: I2c7b67e1b455a6a2df7b8558a3dc5c0f3a893408
This commit is contained in:
Pedro Loureiro
2021-10-07 10:59:11 +00:00
committed by Automerger Merge Worker
2 changed files with 57 additions and 19 deletions

View File

@@ -25,6 +25,9 @@ import (
"android/soong/remoteexec" "android/soong/remoteexec"
) )
// The values allowed for Droidstubs' Api_levels_sdk_type
var allowedApiLevelSdkTypes = []string{"public", "system", "module-lib"}
func init() { func init() {
RegisterStubsBuildComponents(android.InitRegistrationContext) RegisterStubsBuildComponents(android.InitRegistrationContext)
} }
@@ -134,7 +137,7 @@ type DroidstubsProperties struct {
// the dirs which Metalava extracts API levels annotations from. // the dirs which Metalava extracts API levels annotations from.
Api_levels_annotations_dirs []string Api_levels_annotations_dirs []string
// the sdk kind which Metalava extracts API levels annotations from. Supports 'public' and 'system' for now; defaults to public. // the sdk kind which Metalava extracts API levels annotations from. Supports 'public', 'system' and 'module-lib' for now; defaults to public.
Api_levels_sdk_type *string Api_levels_sdk_type *string
// the filename which Metalava extracts API levels annotations from. Defaults to android.jar. // the filename which Metalava extracts API levels annotations from. Defaults to android.jar.
@@ -404,19 +407,24 @@ func (d *Droidstubs) apiLevelsAnnotationsFlags(ctx android.ModuleContext, cmd *a
// When parsing a stub jar for a specific version, Metalava picks the first pattern that defines // When parsing a stub jar for a specific version, Metalava picks the first pattern that defines
// an actual file present on disk (in the order the patterns were passed). For system APIs for // an actual file present on disk (in the order the patterns were passed). For system APIs for
// privileged apps that are only defined since API level 21 (Lollipop), fallback to public stubs // privileged apps that are only defined since API level 21 (Lollipop), fallback to public stubs
// for older releases. // for older releases. Similarly, module-lib falls back to system API.
if sdkType := proptools.StringDefault(d.properties.Api_levels_sdk_type, "public"); sdkType != "public" { var sdkDirs []string
if sdkType != "system" { switch proptools.StringDefault(d.properties.Api_levels_sdk_type, "public") {
ctx.PropertyErrorf("api_levels_sdk_type", "only 'public' and 'system' are supported") case "module-lib":
} sdkDirs = []string{"module-lib", "system", "public"}
// If building non public stubs, add all sdkType patterns first... case "system":
for _, dir := range dirs { sdkDirs = []string{"system", "public"}
cmd.FlagWithArg("--android-jar-pattern ", fmt.Sprintf("%s/%%/%s/%s", dir, sdkType, filename)) case "public":
} sdkDirs = []string{"public"}
default:
ctx.PropertyErrorf("api_levels_sdk_type", "needs to be one of %v", allowedApiLevelSdkTypes)
return
} }
for _, dir := range dirs {
// ... and fallback to public ones, for Metalava to use if needed. for _, sdkDir := range sdkDirs {
cmd.FlagWithArg("--android-jar-pattern ", fmt.Sprintf("%s/%%/%s/%s", dir, "public", filename)) for _, dir := range dirs {
cmd.FlagWithArg("--android-jar-pattern ", fmt.Sprintf("%s/%%/%s/%s", dir, sdkDir, filename))
}
} }
} }

View File

@@ -15,6 +15,7 @@
package java package java
import ( import (
"fmt"
"reflect" "reflect"
"regexp" "regexp"
"strings" "strings"
@@ -82,8 +83,10 @@ func TestDroidstubs(t *testing.T) {
} }
} }
func TestSystemDroidstubs(t *testing.T) { // runs a test for droidstubs with a customizable sdkType argument and returns
ctx, _ := testJavaWithFS(t, ` // the list of jar patterns that is passed as `--android-jar-pattern`
func getAndroidJarPatternsForDroidstubs(t *testing.T, sdkType string) []string {
ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
droiddoc_exported_dir { droiddoc_exported_dir {
name: "some-exported-dir", name: "some-exported-dir",
path: "somedir", path: "somedir",
@@ -102,9 +105,9 @@ func TestSystemDroidstubs(t *testing.T) {
"some-other-exported-dir", "some-other-exported-dir",
], ],
api_levels_annotations_enabled: true, api_levels_annotations_enabled: true,
api_levels_sdk_type: "system", api_levels_sdk_type: "%s",
} }
`, `, sdkType),
map[string][]byte{ map[string][]byte{
"foo-doc/a.java": nil, "foo-doc/a.java": nil,
}) })
@@ -113,13 +116,40 @@ func TestSystemDroidstubs(t *testing.T) {
manifest := m.Output("metalava.sbox.textproto") manifest := m.Output("metalava.sbox.textproto")
cmd := String(android.RuleBuilderSboxProtoForTests(t, manifest).Commands[0].Command) cmd := String(android.RuleBuilderSboxProtoForTests(t, manifest).Commands[0].Command)
r := regexp.MustCompile(`--android-jar-pattern [^ ]+/android.jar`) r := regexp.MustCompile(`--android-jar-pattern [^ ]+/android.jar`)
matches := r.FindAllString(cmd, -1) return r.FindAllString(cmd, -1)
}
func TestPublicDroidstubs(t *testing.T) {
patterns := getAndroidJarPatternsForDroidstubs(t, "public")
android.AssertArrayString(t, "order of patterns", []string{
"--android-jar-pattern somedir/%/public/android.jar",
"--android-jar-pattern someotherdir/%/public/android.jar",
}, patterns)
}
func TestSystemDroidstubs(t *testing.T) {
patterns := getAndroidJarPatternsForDroidstubs(t, "system")
android.AssertArrayString(t, "order of patterns", []string{ android.AssertArrayString(t, "order of patterns", []string{
"--android-jar-pattern somedir/%/system/android.jar", "--android-jar-pattern somedir/%/system/android.jar",
"--android-jar-pattern someotherdir/%/system/android.jar", "--android-jar-pattern someotherdir/%/system/android.jar",
"--android-jar-pattern somedir/%/public/android.jar", "--android-jar-pattern somedir/%/public/android.jar",
"--android-jar-pattern someotherdir/%/public/android.jar", "--android-jar-pattern someotherdir/%/public/android.jar",
}, matches) }, patterns)
}
func TestModuleLibDroidstubs(t *testing.T) {
patterns := getAndroidJarPatternsForDroidstubs(t, "module-lib")
android.AssertArrayString(t, "order of patterns", []string{
"--android-jar-pattern somedir/%/module-lib/android.jar",
"--android-jar-pattern someotherdir/%/module-lib/android.jar",
"--android-jar-pattern somedir/%/system/android.jar",
"--android-jar-pattern someotherdir/%/system/android.jar",
"--android-jar-pattern somedir/%/public/android.jar",
"--android-jar-pattern someotherdir/%/public/android.jar",
}, patterns)
} }
func TestDroidstubsSandbox(t *testing.T) { func TestDroidstubsSandbox(t *testing.T) {