From 6a927c4e6ad5c53dcca7b0db2fec129d1f0d3f82 Mon Sep 17 00:00:00 2001 From: Jiyong Park Date: Tue, 21 Jan 2020 02:03:43 +0900 Subject: Abstract sdk_version string using sdkSpec type The value format that sdk_version (and min_sdk_version, etc.) can have has consistently evolved and is quite complicated. Furthermore, with the Mainline module effort, we are expected to have more sdk_versions like 'module-app-current', 'module-lib-current', etc. The goal of this change is to abstract the various sdk versions, which are currently represented in string and is parsed in various places, into a type called sdkSpec, so that adding new sdk veresions becomes easier than before. The sdk_version string is now parsed in only one place 'SdkSpecFrom', in which it is converted into the sdkSpec struct. The struct type provides several methods that again converts sdkSpec into context-specific information such as the effective version number, etc. Bug: 146757305 Bug: 147879031 Test: m Change-Id: I252f3706544f00ea71c61c23460f07561dd28ab0 --- java/java.go | 64 ++++++++++++++++++++++++------------------------------------ 1 file changed, 25 insertions(+), 39 deletions(-) (limited to 'java/java.go') diff --git a/java/java.go b/java/java.go index 320cb7b55..a4f191d94 100644 --- a/java/java.go +++ b/java/java.go @@ -90,7 +90,7 @@ func (j *Module) checkSdkVersion(ctx android.ModuleContext) { if j.SocSpecific() || j.DeviceSpecific() || (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) { if sc, ok := ctx.Module().(sdkContext); ok { - if sc.sdkVersion() == "" { + if !sc.sdkVersion().specified() { ctx.PropertyErrorf("sdk_version", "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).") } @@ -101,12 +101,11 @@ func (j *Module) checkSdkVersion(ctx android.ModuleContext) { func (j *Module) checkPlatformAPI(ctx android.ModuleContext) { if sc, ok := ctx.Module().(sdkContext); ok { usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis) - if usePlatformAPI != (sc.sdkVersion() == "") { - if usePlatformAPI { - ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.") - } else { - ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.") - } + sdkVersionSpecified := sc.sdkVersion().specified() + if usePlatformAPI && sdkVersionSpecified { + ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.") + } else if !usePlatformAPI && !sdkVersionSpecified { + ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.") } } @@ -455,8 +454,8 @@ type Dependency interface { } type SdkLibraryDependency interface { - SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths - SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths + SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths + SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths } type xref interface { @@ -557,24 +556,24 @@ func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool { ctx.Config().UnbundledBuild()) } -func (j *Module) sdkVersion() string { - return String(j.deviceProperties.Sdk_version) +func (j *Module) sdkVersion() sdkSpec { + return sdkSpecFrom(String(j.deviceProperties.Sdk_version)) } func (j *Module) systemModules() string { return proptools.String(j.deviceProperties.System_modules) } -func (j *Module) minSdkVersion() string { +func (j *Module) minSdkVersion() sdkSpec { if j.deviceProperties.Min_sdk_version != nil { - return *j.deviceProperties.Min_sdk_version + return sdkSpecFrom(*j.deviceProperties.Min_sdk_version) } return j.sdkVersion() } -func (j *Module) targetSdkVersion() string { +func (j *Module) targetSdkVersion() sdkSpec { if j.deviceProperties.Target_sdk_version != nil { - return *j.deviceProperties.Target_sdk_version + return sdkSpecFrom(*j.deviceProperties.Target_sdk_version) } return j.sdkVersion() } @@ -780,26 +779,25 @@ func (m *Module) getLinkType(name string) (ret linkType, stubs bool) { name == "stub-annotations" || name == "private-stub-annotations-jar" || name == "core-lambda-stubs" || name == "core-generated-annotation-stubs": return javaCore, true - case ver == "core_current": + case ver.kind == sdkCore: return javaCore, false case name == "android_system_stubs_current": return javaSystem, true - case strings.HasPrefix(ver, "system_"): + case ver.kind == sdkSystem: return javaSystem, false case name == "android_test_stubs_current": return javaSystem, true - case strings.HasPrefix(ver, "test_"): + case ver.kind == sdkTest: return javaPlatform, false case name == "android_stubs_current": return javaSdk, true - case ver == "current": + case ver.kind == sdkPublic: return javaSdk, false - case ver == "" || ver == "none" || ver == "core_platform": + case ver.kind == sdkPrivate || ver.kind == sdkNone || ver.kind == sdkCorePlatform: return javaPlatform, false + case !ver.valid(): + panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw)) default: - if _, err := strconv.Atoi(ver); err != nil { - panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver)) - } return javaSdk, false } } @@ -996,19 +994,7 @@ func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) { } func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion { - v := sdkContext.sdkVersion() - // For PDK builds, use the latest SDK version instead of "current" - if ctx.Config().IsPdkBuild() && - (v == "" || v == "none" || v == "core_platform" || v == "current") { - sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int) - latestSdkVersion := 0 - if len(sdkVersions) > 0 { - latestSdkVersion = sdkVersions[len(sdkVersions)-1] - } - v = strconv.Itoa(latestSdkVersion) - } - - sdk, err := sdkVersionToNumber(ctx, v) + sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx) if err != nil { ctx.PropertyErrorf("sdk_version", "%s", err) } @@ -2297,11 +2283,11 @@ type Import struct { exportedSdkLibs []string } -func (j *Import) sdkVersion() string { - return String(j.properties.Sdk_version) +func (j *Import) sdkVersion() sdkSpec { + return sdkSpecFrom(String(j.properties.Sdk_version)) } -func (j *Import) minSdkVersion() string { +func (j *Import) minSdkVersion() sdkSpec { return j.sdkVersion() } -- cgit v1.2.3-59-g8ed1b