Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame^] | 1 | // Copyright 2019 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/java/config" |
| 20 | "fmt" |
| 21 | "path/filepath" |
| 22 | "strconv" |
| 23 | "strings" |
| 24 | ) |
| 25 | |
| 26 | type sdkContext interface { |
| 27 | // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set. |
| 28 | sdkVersion() string |
| 29 | // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set. |
| 30 | minSdkVersion() string |
| 31 | // targetSdkVersion returns the target_sdk_version property of the current module, or sdkVersion() if it is not set. |
| 32 | targetSdkVersion() string |
| 33 | } |
| 34 | |
| 35 | func sdkVersionOrDefault(ctx android.BaseContext, v string) string { |
| 36 | switch v { |
| 37 | case "", "current", "system_current", "test_current", "core_current": |
| 38 | return ctx.Config().DefaultAppTargetSdk() |
| 39 | default: |
| 40 | return v |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number) |
| 45 | // it returns android.FutureApiLevel (10000). |
| 46 | func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) { |
| 47 | switch v { |
| 48 | case "", "current", "test_current", "system_current", "core_current": |
| 49 | return ctx.Config().DefaultAppTargetSdkInt(), nil |
| 50 | default: |
| 51 | n := android.GetNumericSdkVersion(v) |
| 52 | if i, err := strconv.Atoi(n); err != nil { |
| 53 | return -1, fmt.Errorf("invalid sdk version %q", n) |
| 54 | } else { |
| 55 | return i, nil |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) { |
| 61 | n, err := sdkVersionToNumber(ctx, v) |
| 62 | if err != nil { |
| 63 | return "", err |
| 64 | } |
| 65 | return strconv.Itoa(n), nil |
| 66 | } |
| 67 | |
| 68 | func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep { |
| 69 | v := sdkContext.sdkVersion() |
| 70 | i, err := sdkVersionToNumber(ctx, v) |
| 71 | if err != nil { |
| 72 | ctx.PropertyErrorf("sdk_version", "%s", err) |
| 73 | return sdkDep{} |
| 74 | } |
| 75 | |
| 76 | // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks) |
| 77 | // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set) |
| 78 | if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel { |
| 79 | allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions() |
| 80 | if ctx.DeviceSpecific() || ctx.SocSpecific() { |
| 81 | if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 { |
| 82 | allowed_versions = ctx.DeviceConfig().SystemSdkVersions() |
| 83 | } |
| 84 | } |
| 85 | version := strings.TrimPrefix(v, "system_") |
| 86 | if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) { |
| 87 | ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q", |
| 88 | v, allowed_versions) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | toPrebuilt := func(sdk string) sdkDep { |
| 93 | var api, v string |
| 94 | if strings.Contains(sdk, "_") { |
| 95 | t := strings.Split(sdk, "_") |
| 96 | api = t[0] |
| 97 | v = t[1] |
| 98 | } else { |
| 99 | api = "public" |
| 100 | v = sdk |
| 101 | } |
| 102 | dir := filepath.Join("prebuilts", "sdk", v, api) |
| 103 | jar := filepath.Join(dir, "android.jar") |
| 104 | // There's no aidl for other SDKs yet. |
| 105 | // TODO(77525052): Add aidl files for other SDKs too. |
| 106 | public_dir := filepath.Join("prebuilts", "sdk", v, "public") |
| 107 | aidl := filepath.Join(public_dir, "framework.aidl") |
| 108 | jarPath := android.ExistentPathForSource(ctx, jar) |
| 109 | aidlPath := android.ExistentPathForSource(ctx, aidl) |
| 110 | lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath) |
| 111 | |
| 112 | if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() { |
| 113 | return sdkDep{ |
| 114 | invalidVersion: true, |
| 115 | modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)}, |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if !jarPath.Valid() { |
| 120 | ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar) |
| 121 | return sdkDep{} |
| 122 | } |
| 123 | |
| 124 | if !aidlPath.Valid() { |
| 125 | ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl) |
| 126 | return sdkDep{} |
| 127 | } |
| 128 | |
| 129 | return sdkDep{ |
| 130 | useFiles: true, |
| 131 | jars: android.Paths{jarPath.Path(), lambdaStubsPath}, |
| 132 | aidl: aidlPath.Path(), |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | toModule := func(m, r string) sdkDep { |
| 137 | ret := sdkDep{ |
| 138 | useModule: true, |
| 139 | modules: []string{m, config.DefaultLambdaStubsLibrary}, |
| 140 | systemModules: m + "_system_modules", |
| 141 | frameworkResModule: r, |
| 142 | } |
| 143 | if m == "core.current.stubs" { |
| 144 | ret.systemModules = "core-system-modules" |
| 145 | } else if m == "core.platform.api.stubs" { |
| 146 | ret.systemModules = "core-platform-api-stubs-system-modules" |
| 147 | } |
| 148 | return ret |
| 149 | } |
| 150 | |
| 151 | if ctx.Config().UnbundledBuildPrebuiltSdks() && v != "" { |
| 152 | return toPrebuilt(v) |
| 153 | } |
| 154 | |
| 155 | switch v { |
| 156 | case "": |
| 157 | return sdkDep{ |
| 158 | useDefaultLibs: true, |
| 159 | frameworkResModule: "framework-res", |
| 160 | } |
| 161 | case "current": |
| 162 | return toModule("android_stubs_current", "framework-res") |
| 163 | case "system_current": |
| 164 | return toModule("android_system_stubs_current", "framework-res") |
| 165 | case "test_current": |
| 166 | return toModule("android_test_stubs_current", "framework-res") |
| 167 | case "core_current": |
| 168 | return toModule("core.current.stubs", "") |
| 169 | default: |
| 170 | return toPrebuilt(v) |
| 171 | } |
| 172 | } |