aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Duffin <paulduffin@google.com>2020-05-20 11:52:25 +0100
committerPaul Duffin <paulduffin@google.com>2020-05-25 10:40:39 +0100
commit5ae30792b192e256008f1f5bec5977c2d1d0a5c8 (patch)
tree7c3ce835dc95536f0eb7d4c9f33d955fb46d12e1
parent4762436a8dbb3d791d91c1b9e575c79e8df2180f (diff)
downloadbuild_soong-5ae30792b192e256008f1f5bec5977c2d1d0a5c8.tar.gz
build_soong-5ae30792b192e256008f1f5bec5977c2d1d0a5c8.tar.bz2
build_soong-5ae30792b192e256008f1f5bec5977c2d1d0a5c8.zip
Retry: "java_sdk_library: Add redirection to module-lib stubs"
Previously, when using sdk_version: "module_current" any direct reference to an sdk library would use the public not module-lib stubs. This change corrects that. Prior to the addition of the module-lib api scope almost all java_sdk_library instances supported all the scopes to which a request for jars could be redirected, i.e. public, system and test. The exceptions to that are a few special instances that were used with sdk_version: "none" and so were either caught by the java_sdk_library special cases or dropped through to public. The addition of module-lib, plus the flexible control over which scopes are generated means that is no longer true. It is possible for a java_sdk_library to be requested for a scope it does not have which would have resulted in an empty set of paths being returned leading to confusing compilation errors. To avoid that this change also adds support for using the inheritance hierarchy defined by the apiScope.extends field to fall back to the closest available surface. Test: m nothing Bug: 155164730 (cherry picked from commit 803a9565cd3d85ba22fb00f870b8fffabc3ab30d) Change-Id: I89a0abf4033209f9a104285c990096bc0b4a0255
-rw-r--r--java/java_test.go39
-rw-r--r--java/sdk_library.go49
2 files changed, 83 insertions, 5 deletions
diff --git a/java/java_test.go b/java/java_test.go
index 01ddccfc..b8abacb5 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -1261,6 +1261,45 @@ func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
`)
}
+func TestJavaSdkLibrary_MissingScope(t *testing.T) {
+ testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
+ java_sdk_library {
+ name: "foo",
+ srcs: ["a.java"],
+ public: {
+ enabled: false,
+ },
+ }
+
+ java_library {
+ name: "baz",
+ srcs: ["a.java"],
+ libs: ["foo"],
+ sdk_version: "module_current",
+ }
+ `)
+}
+
+func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
+ testJava(t, `
+ java_sdk_library {
+ name: "foo",
+ srcs: ["a.java"],
+ system: {
+ enabled: true,
+ },
+ }
+
+ java_library {
+ name: "baz",
+ srcs: ["a.java"],
+ libs: ["foo"],
+ // foo does not have module-lib scope so it should fallback to system
+ sdk_version: "module_current",
+ }
+ `)
+}
+
var compilerFlagsTestCases = []struct {
in string
out bool
diff --git a/java/sdk_library.go b/java/sdk_library.go
index e46460e5..1fff3ba3 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -551,7 +551,7 @@ func (c *commonToSdkLibraryAndImport) apiModuleName(apiScope *apiScope) string {
return c.namingScheme.apiModuleName(apiScope, c.moduleBase.BaseModuleName())
}
-func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths {
+func (c *commonToSdkLibraryAndImport) getScopePathsCreateIfNeeded(scope *apiScope) *scopePaths {
if c.scopePaths == nil {
c.scopePaths = make(map[*apiScope]*scopePaths)
}
@@ -564,6 +564,28 @@ func (c *commonToSdkLibraryAndImport) getScopePaths(scope *apiScope) *scopePaths
return paths
}
+func (c *commonToSdkLibraryAndImport) findScopePaths(scope *apiScope) *scopePaths {
+ if c.scopePaths == nil {
+ return nil
+ }
+
+ return c.scopePaths[scope]
+}
+
+// If this does not support the requested api scope then find the closest available
+// scope it does support. Returns nil if no such scope is available.
+func (c *commonToSdkLibraryAndImport) findClosestScopePath(scope *apiScope) *scopePaths {
+ for s := scope; s != nil; s = s.extends {
+ if paths := c.findScopePaths(s); paths != nil {
+ return paths
+ }
+ }
+
+ // This should never happen outside tests as public should be the base scope for every
+ // scope and is enabled by default.
+ return nil
+}
+
func (c *commonToSdkLibraryAndImport) sdkJarsCommon(ctx android.BaseModuleContext, sdkVersion sdkSpec, headerJars bool) android.Paths {
// If a specific numeric version has been requested then use prebuilt versions of the sdk.
@@ -575,13 +597,26 @@ func (c *commonToSdkLibraryAndImport) sdkJarsCommon(ctx android.BaseModuleContex
switch sdkVersion.kind {
case sdkSystem:
apiScope = apiScopeSystem
+ case sdkModule:
+ apiScope = apiScopeModuleLib
case sdkTest:
apiScope = apiScopeTest
default:
apiScope = apiScopePublic
}
- paths := c.getScopePaths(apiScope)
+ paths := c.findClosestScopePath(apiScope)
+ if paths == nil {
+ var scopes []string
+ for _, s := range allApiScopes {
+ if c.findScopePaths(s) != nil {
+ scopes = append(scopes, s.name)
+ }
+ }
+ ctx.ModuleErrorf("requires api scope %s from %s but it only has %q available", apiScope.name, c.moduleBase.BaseModuleName(), scopes)
+ return nil
+ }
+
if headerJars {
return paths.stubsHeaderPath
} else {
@@ -704,7 +739,7 @@ func (module *SdkLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext)
// Extract information from any of the scope specific dependencies.
if scopeTag, ok := tag.(scopeDependencyTag); ok {
apiScope := scopeTag.apiScope
- scopePaths := module.getScopePaths(apiScope)
+ scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
// Extract information from the dependency. The exact information extracted
// is determined by the nature of the dependency which is determined by the tag.
@@ -1460,7 +1495,7 @@ func (module *sdkLibraryImport) GenerateAndroidBuildActions(ctx android.ModuleCo
if lib, ok := to.(Dependency); ok {
if scopeTag, ok := tag.(scopeDependencyTag); ok {
apiScope := scopeTag.apiScope
- scopePaths := module.getScopePaths(apiScope)
+ scopePaths := module.getScopePathsCreateIfNeeded(apiScope)
scopePaths.stubsHeaderPath = lib.HeaderJars()
}
}
@@ -1645,7 +1680,11 @@ func (s *sdkLibrarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMembe
s.Scopes = make(map[*apiScope]scopeProperties)
for _, apiScope := range allApiScopes {
- paths := sdk.getScopePaths(apiScope)
+ paths := sdk.findScopePaths(apiScope)
+ if paths == nil {
+ continue
+ }
+
jars := paths.stubsImplPath
if len(jars) > 0 {
properties := scopeProperties{}