Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 1 | // Copyright 2021 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 android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strconv" |
| 20 | "strings" |
| 21 | ) |
| 22 | |
| 23 | type SdkContext interface { |
| 24 | // SdkVersion returns SdkSpec that corresponds to the sdk_version property of the current module |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 25 | SdkVersion(ctx EarlyModuleContext) SdkSpec |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 26 | // SystemModules returns the system_modules property of the current module, or an empty string if it is not set. |
| 27 | SystemModules() string |
| 28 | // MinSdkVersion returns SdkSpec that corresponds to the min_sdk_version property of the current module, |
| 29 | // or from sdk_version if it is not set. |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 30 | MinSdkVersion(ctx EarlyModuleContext) SdkSpec |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 31 | // ReplaceMaxSdkVersionPlaceholder returns SdkSpec to replace the maxSdkVersion property of permission and |
| 32 | // uses-permission tags if it is set. |
| 33 | ReplaceMaxSdkVersionPlaceholder(ctx EarlyModuleContext) SdkSpec |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 34 | // TargetSdkVersion returns the SdkSpec that corresponds to the target_sdk_version property of the current module, |
| 35 | // or from sdk_version if it is not set. |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 36 | TargetSdkVersion(ctx EarlyModuleContext) SdkSpec |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // SdkKind represents a particular category of an SDK spec like public, system, test, etc. |
| 40 | type SdkKind int |
| 41 | |
| 42 | const ( |
| 43 | SdkInvalid SdkKind = iota |
| 44 | SdkNone |
| 45 | SdkCore |
| 46 | SdkCorePlatform |
Spandan Das | 0b555e3 | 2022-11-28 18:48:51 +0000 | [diff] [blame] | 47 | SdkIntraCore // API surface provided by one core module to another |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 48 | SdkPublic |
| 49 | SdkSystem |
| 50 | SdkTest |
| 51 | SdkModule |
| 52 | SdkSystemServer |
| 53 | SdkPrivate |
Spandan Das | 4ac2aed | 2022-12-28 01:54:29 +0000 | [diff] [blame^] | 54 | SdkToolchain // API surface provided by ART to compile other API domains |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 55 | ) |
| 56 | |
| 57 | // String returns the string representation of this SdkKind |
| 58 | func (k SdkKind) String() string { |
| 59 | switch k { |
| 60 | case SdkPrivate: |
| 61 | return "private" |
| 62 | case SdkNone: |
| 63 | return "none" |
| 64 | case SdkPublic: |
| 65 | return "public" |
| 66 | case SdkSystem: |
| 67 | return "system" |
| 68 | case SdkTest: |
| 69 | return "test" |
| 70 | case SdkCore: |
| 71 | return "core" |
| 72 | case SdkCorePlatform: |
| 73 | return "core_platform" |
Spandan Das | 0b555e3 | 2022-11-28 18:48:51 +0000 | [diff] [blame] | 74 | case SdkIntraCore: |
| 75 | return "intracore" |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 76 | case SdkModule: |
| 77 | return "module-lib" |
| 78 | case SdkSystemServer: |
| 79 | return "system-server" |
Spandan Das | 4ac2aed | 2022-12-28 01:54:29 +0000 | [diff] [blame^] | 80 | case SdkToolchain: |
| 81 | return "toolchain" |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 82 | default: |
| 83 | return "invalid" |
| 84 | } |
| 85 | } |
| 86 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 87 | // SdkSpec represents the kind and the version of an SDK for a module to build against |
| 88 | type SdkSpec struct { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 89 | Kind SdkKind |
| 90 | ApiLevel ApiLevel |
| 91 | Raw string |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func (s SdkSpec) String() string { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 95 | return fmt.Sprintf("%s_%s", s.Kind, s.ApiLevel) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Valid checks if this SdkSpec is well-formed. Note however that true doesn't mean that the |
| 99 | // specified SDK actually exists. |
| 100 | func (s SdkSpec) Valid() bool { |
| 101 | return s.Kind != SdkInvalid |
| 102 | } |
| 103 | |
| 104 | // Specified checks if this SdkSpec is well-formed and is not "". |
| 105 | func (s SdkSpec) Specified() bool { |
| 106 | return s.Valid() && s.Kind != SdkPrivate |
| 107 | } |
| 108 | |
| 109 | // whether the API surface is managed and versioned, i.e. has .txt file that |
| 110 | // get frozen on SDK freeze and changes get reviewed by API council. |
| 111 | func (s SdkSpec) Stable() bool { |
| 112 | if !s.Specified() { |
| 113 | return false |
| 114 | } |
| 115 | switch s.Kind { |
| 116 | case SdkNone: |
| 117 | // there is nothing to manage and version in this case; de facto stable API. |
| 118 | return true |
| 119 | case SdkCore, SdkPublic, SdkSystem, SdkModule, SdkSystemServer: |
| 120 | return true |
| 121 | case SdkCorePlatform, SdkTest, SdkPrivate: |
| 122 | return false |
| 123 | default: |
| 124 | panic(fmt.Errorf("unknown SdkKind=%v", s.Kind)) |
| 125 | } |
| 126 | return false |
| 127 | } |
| 128 | |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 129 | // PrebuiltSdkAvailableForUnbundledBuild tells whether this SdkSpec can have a prebuilt SDK |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 130 | // that can be used for unbundled builds. |
| 131 | func (s SdkSpec) PrebuiltSdkAvailableForUnbundledBuild() bool { |
| 132 | // "", "none", and "core_platform" are not available for unbundled build |
| 133 | // as we don't/can't have prebuilt stub for the versions |
| 134 | return s.Kind != SdkPrivate && s.Kind != SdkNone && s.Kind != SdkCorePlatform |
| 135 | } |
| 136 | |
| 137 | func (s SdkSpec) ForVendorPartition(ctx EarlyModuleContext) SdkSpec { |
| 138 | // If BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES has a numeric value, |
| 139 | // use it instead of "current" for the vendor partition. |
| 140 | currentSdkVersion := ctx.DeviceConfig().CurrentApiLevelForVendorModules() |
| 141 | if currentSdkVersion == "current" { |
| 142 | return s |
| 143 | } |
| 144 | |
| 145 | if s.Kind == SdkPublic || s.Kind == SdkSystem { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 146 | if s.ApiLevel.IsCurrent() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 147 | if i, err := strconv.Atoi(currentSdkVersion); err == nil { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 148 | apiLevel := uncheckedFinalApiLevel(i) |
| 149 | return SdkSpec{s.Kind, apiLevel, s.Raw} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 150 | } |
| 151 | panic(fmt.Errorf("BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES must be either \"current\" or a number, but was %q", currentSdkVersion)) |
| 152 | } |
| 153 | } |
| 154 | return s |
| 155 | } |
| 156 | |
| 157 | // UsePrebuilt determines whether prebuilt SDK should be used for this SdkSpec with the given context. |
| 158 | func (s SdkSpec) UsePrebuilt(ctx EarlyModuleContext) bool { |
Jiyong Park | c702204 | 2021-04-15 16:53:05 +0900 | [diff] [blame] | 159 | switch s { |
| 160 | case SdkSpecNone, SdkSpecCorePlatform, SdkSpecPrivate: |
| 161 | return false |
| 162 | } |
| 163 | |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 164 | if s.ApiLevel.IsCurrent() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 165 | // "current" can be built from source and be from prebuilt SDK |
| 166 | return ctx.Config().AlwaysUsePrebuiltSdks() |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 167 | } else if !s.ApiLevel.IsPreview() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 168 | // validation check |
Paul Duffin | 12e311d | 2021-10-28 17:42:16 +0100 | [diff] [blame] | 169 | if s.Kind != SdkPublic && s.Kind != SdkSystem && s.Kind != SdkTest && s.Kind != SdkModule && s.Kind != SdkSystemServer { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 170 | panic(fmt.Errorf("prebuilt SDK is not not available for SdkKind=%q", s.Kind)) |
| 171 | return false |
| 172 | } |
| 173 | // numbered SDKs are always from prebuilt |
| 174 | return true |
| 175 | } |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 176 | return false |
| 177 | } |
| 178 | |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 179 | // EffectiveVersion converts an SdkSpec into the concrete ApiLevel that the module should use. For |
| 180 | // modules targeting an unreleased SDK (meaning it does not yet have a number) it returns |
| 181 | // FutureApiLevel(10000). |
| 182 | func (s SdkSpec) EffectiveVersion(ctx EarlyModuleContext) (ApiLevel, error) { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 183 | if !s.Valid() { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 184 | return s.ApiLevel, fmt.Errorf("invalid sdk version %q", s.Raw) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if ctx.DeviceSpecific() || ctx.SocSpecific() { |
| 188 | s = s.ForVendorPartition(ctx) |
| 189 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 190 | if !s.ApiLevel.IsPreview() { |
| 191 | return s.ApiLevel, nil |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 192 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 193 | ret := ctx.Config().DefaultAppTargetSdk(ctx) |
| 194 | if ret.IsPreview() { |
| 195 | return FutureApiLevel, nil |
| 196 | } |
| 197 | return ret, nil |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // EffectiveVersionString converts an SdkSpec into the concrete version string that the module |
| 201 | // should use. For modules targeting an unreleased SDK (meaning it does not yet have a number) |
| 202 | // it returns the codename (P, Q, R, etc.) |
| 203 | func (s SdkSpec) EffectiveVersionString(ctx EarlyModuleContext) (string, error) { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 204 | if !s.Valid() { |
| 205 | return s.ApiLevel.String(), fmt.Errorf("invalid sdk version %q", s.Raw) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 206 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 207 | |
| 208 | if ctx.DeviceSpecific() || ctx.SocSpecific() { |
| 209 | s = s.ForVendorPartition(ctx) |
| 210 | } |
| 211 | if !s.ApiLevel.IsPreview() { |
| 212 | return s.ApiLevel.String(), nil |
| 213 | } |
| 214 | return ctx.Config().DefaultAppTargetSdk(ctx).String(), nil |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 217 | var ( |
Jiyong Park | c702204 | 2021-04-15 16:53:05 +0900 | [diff] [blame] | 218 | SdkSpecNone = SdkSpec{SdkNone, NoneApiLevel, "(no version)"} |
| 219 | SdkSpecPrivate = SdkSpec{SdkPrivate, FutureApiLevel, ""} |
| 220 | SdkSpecCorePlatform = SdkSpec{SdkCorePlatform, FutureApiLevel, "core_platform"} |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 221 | ) |
| 222 | |
| 223 | func SdkSpecFrom(ctx EarlyModuleContext, str string) SdkSpec { |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 224 | return SdkSpecFromWithConfig(ctx.Config(), str) |
| 225 | } |
| 226 | |
| 227 | func SdkSpecFromWithConfig(config Config, str string) SdkSpec { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 228 | switch str { |
| 229 | // special cases first |
| 230 | case "": |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 231 | return SdkSpecPrivate |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 232 | case "none": |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 233 | return SdkSpecNone |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 234 | case "core_platform": |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 235 | return SdkSpecCorePlatform |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 236 | default: |
| 237 | // the syntax is [kind_]version |
| 238 | sep := strings.LastIndex(str, "_") |
| 239 | |
| 240 | var kindString string |
| 241 | if sep == 0 { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 242 | return SdkSpec{SdkInvalid, NoneApiLevel, str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 243 | } else if sep == -1 { |
| 244 | kindString = "" |
| 245 | } else { |
| 246 | kindString = str[0:sep] |
| 247 | } |
| 248 | versionString := str[sep+1 : len(str)] |
| 249 | |
| 250 | var kind SdkKind |
| 251 | switch kindString { |
| 252 | case "": |
| 253 | kind = SdkPublic |
| 254 | case "core": |
| 255 | kind = SdkCore |
| 256 | case "system": |
| 257 | kind = SdkSystem |
| 258 | case "test": |
| 259 | kind = SdkTest |
| 260 | case "module": |
| 261 | kind = SdkModule |
| 262 | case "system_server": |
| 263 | kind = SdkSystemServer |
| 264 | default: |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 265 | return SdkSpec{SdkInvalid, NoneApiLevel, str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 266 | } |
| 267 | |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 268 | apiLevel, err := ApiLevelFromUserWithConfig(config, versionString) |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 269 | if err != nil { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 270 | return SdkSpec{SdkInvalid, apiLevel, str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 271 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 272 | return SdkSpec{kind, apiLevel, str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | func (s SdkSpec) ValidateSystemSdk(ctx EarlyModuleContext) bool { |
| 277 | // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module) |
| 278 | // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29, |
| 279 | // sdk_version of the modules in vendor/product that use system sdk must be either system_28, system_29 or system_current |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 280 | if s.Kind != SdkSystem || s.ApiLevel.IsPreview() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 281 | return true |
| 282 | } |
| 283 | allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions() |
| 284 | if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) { |
| 285 | systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions() |
| 286 | if len(systemSdkVersions) > 0 { |
| 287 | allowedVersions = systemSdkVersions |
| 288 | } |
| 289 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 290 | if len(allowedVersions) > 0 && !InList(s.ApiLevel.String(), allowedVersions) { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 291 | ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q", |
| 292 | s.Raw, allowedVersions) |
| 293 | return false |
| 294 | } |
| 295 | return true |
| 296 | } |