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" |
Jiyong Park | 7416d67 | 2024-01-04 23:20:42 +0000 | [diff] [blame] | 19 | "reflect" |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 20 | "strconv" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
| 24 | type SdkContext interface { |
| 25 | // 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] | 26 | SdkVersion(ctx EarlyModuleContext) SdkSpec |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 27 | // SystemModules returns the system_modules property of the current module, or an empty string if it is not set. |
| 28 | SystemModules() string |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 29 | // MinSdkVersion returns ApiLevel that corresponds to the min_sdk_version property of the current module, |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 30 | // or from sdk_version if it is not set. |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 31 | MinSdkVersion(ctx EarlyModuleContext) ApiLevel |
Spandan Das | a26eda7 | 2023-03-02 00:56:06 +0000 | [diff] [blame] | 32 | // ReplaceMaxSdkVersionPlaceholder returns Apilevel to replace the maxSdkVersion property of permission and |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 33 | // uses-permission tags if it is set. |
Spandan Das | a26eda7 | 2023-03-02 00:56:06 +0000 | [diff] [blame] | 34 | ReplaceMaxSdkVersionPlaceholder(ctx EarlyModuleContext) ApiLevel |
Spandan Das | ca70fc4 | 2023-03-01 23:38:49 +0000 | [diff] [blame] | 35 | // TargetSdkVersion returns the ApiLevel that corresponds to the target_sdk_version property of the current module, |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 36 | // or from sdk_version if it is not set. |
Spandan Das | ca70fc4 | 2023-03-01 23:38:49 +0000 | [diff] [blame] | 37 | TargetSdkVersion(ctx EarlyModuleContext) ApiLevel |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // SdkKind represents a particular category of an SDK spec like public, system, test, etc. |
| 41 | type SdkKind int |
| 42 | |
| 43 | const ( |
| 44 | SdkInvalid SdkKind = iota |
| 45 | SdkNone |
| 46 | SdkCore |
| 47 | SdkCorePlatform |
Spandan Das | 0b555e3 | 2022-11-28 18:48:51 +0000 | [diff] [blame] | 48 | SdkIntraCore // API surface provided by one core module to another |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 49 | SdkPublic |
| 50 | SdkSystem |
| 51 | SdkTest |
Mark White | 9421c4c | 2023-08-10 00:07:03 +0000 | [diff] [blame] | 52 | SdkTestFrameworksCore |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 53 | SdkModule |
| 54 | SdkSystemServer |
| 55 | SdkPrivate |
Spandan Das | 4ac2aed | 2022-12-28 01:54:29 +0000 | [diff] [blame] | 56 | SdkToolchain // API surface provided by ART to compile other API domains |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 57 | ) |
| 58 | |
| 59 | // String returns the string representation of this SdkKind |
| 60 | func (k SdkKind) String() string { |
| 61 | switch k { |
| 62 | case SdkPrivate: |
| 63 | return "private" |
| 64 | case SdkNone: |
| 65 | return "none" |
| 66 | case SdkPublic: |
| 67 | return "public" |
| 68 | case SdkSystem: |
| 69 | return "system" |
| 70 | case SdkTest: |
| 71 | return "test" |
Mark White | 9421c4c | 2023-08-10 00:07:03 +0000 | [diff] [blame] | 72 | case SdkTestFrameworksCore: |
| 73 | return "test_frameworks_core" |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 74 | case SdkCore: |
| 75 | return "core" |
| 76 | case SdkCorePlatform: |
| 77 | return "core_platform" |
Spandan Das | 0b555e3 | 2022-11-28 18:48:51 +0000 | [diff] [blame] | 78 | case SdkIntraCore: |
| 79 | return "intracore" |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 80 | case SdkModule: |
| 81 | return "module-lib" |
| 82 | case SdkSystemServer: |
| 83 | return "system-server" |
Spandan Das | 4ac2aed | 2022-12-28 01:54:29 +0000 | [diff] [blame] | 84 | case SdkToolchain: |
| 85 | return "toolchain" |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 86 | default: |
| 87 | return "invalid" |
| 88 | } |
| 89 | } |
| 90 | |
Jihoon Kang | 5b065bb | 2023-04-05 21:08:08 +0000 | [diff] [blame] | 91 | func (k SdkKind) DefaultJavaLibraryName() string { |
Spandan Das | 626a8ad | 2023-03-20 18:52:50 +0000 | [diff] [blame] | 92 | switch k { |
| 93 | case SdkPublic: |
| 94 | return "android_stubs_current" |
| 95 | case SdkSystem: |
| 96 | return "android_system_stubs_current" |
| 97 | case SdkTest: |
| 98 | return "android_test_stubs_current" |
Mark White | 9421c4c | 2023-08-10 00:07:03 +0000 | [diff] [blame] | 99 | case SdkTestFrameworksCore: |
| 100 | return "android_test_frameworks_core_stubs_current" |
Spandan Das | 626a8ad | 2023-03-20 18:52:50 +0000 | [diff] [blame] | 101 | case SdkCore: |
| 102 | return "core.current.stubs" |
| 103 | case SdkModule: |
| 104 | return "android_module_lib_stubs_current" |
| 105 | case SdkSystemServer: |
| 106 | return "android_system_server_stubs_current" |
| 107 | default: |
| 108 | panic(fmt.Errorf("APIs of API surface %v cannot be provided by a single Soong module\n", k)) |
| 109 | } |
| 110 | } |
| 111 | |
Jihoon Kang | bd09345 | 2023-12-26 19:08:01 +0000 | [diff] [blame] | 112 | func (k SdkKind) DefaultExportableJavaLibraryName() string { |
| 113 | switch k { |
| 114 | case SdkPublic, SdkSystem, SdkTest, SdkModule, SdkSystemServer: |
| 115 | return k.DefaultJavaLibraryName() + "_exportable" |
| 116 | case SdkCore: |
| 117 | return k.DefaultJavaLibraryName() + ".exportable" |
| 118 | default: |
| 119 | panic(fmt.Errorf("API surface %v does not provide exportable stubs", k)) |
| 120 | } |
| 121 | } |
| 122 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 123 | // SdkSpec represents the kind and the version of an SDK for a module to build against |
| 124 | type SdkSpec struct { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 125 | Kind SdkKind |
| 126 | ApiLevel ApiLevel |
| 127 | Raw string |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | func (s SdkSpec) String() string { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 131 | return fmt.Sprintf("%s_%s", s.Kind, s.ApiLevel) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Valid checks if this SdkSpec is well-formed. Note however that true doesn't mean that the |
| 135 | // specified SDK actually exists. |
| 136 | func (s SdkSpec) Valid() bool { |
| 137 | return s.Kind != SdkInvalid |
| 138 | } |
| 139 | |
| 140 | // Specified checks if this SdkSpec is well-formed and is not "". |
| 141 | func (s SdkSpec) Specified() bool { |
| 142 | return s.Valid() && s.Kind != SdkPrivate |
| 143 | } |
| 144 | |
| 145 | // whether the API surface is managed and versioned, i.e. has .txt file that |
| 146 | // get frozen on SDK freeze and changes get reviewed by API council. |
| 147 | func (s SdkSpec) Stable() bool { |
| 148 | if !s.Specified() { |
| 149 | return false |
| 150 | } |
| 151 | switch s.Kind { |
| 152 | case SdkNone: |
| 153 | // there is nothing to manage and version in this case; de facto stable API. |
| 154 | return true |
| 155 | case SdkCore, SdkPublic, SdkSystem, SdkModule, SdkSystemServer: |
| 156 | return true |
Mark White | 9421c4c | 2023-08-10 00:07:03 +0000 | [diff] [blame] | 157 | case SdkCorePlatform, SdkTest, SdkTestFrameworksCore, SdkPrivate: |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 158 | return false |
| 159 | default: |
| 160 | panic(fmt.Errorf("unknown SdkKind=%v", s.Kind)) |
| 161 | } |
| 162 | return false |
| 163 | } |
| 164 | |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 165 | // PrebuiltSdkAvailableForUnbundledBuild tells whether this SdkSpec can have a prebuilt SDK |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 166 | // that can be used for unbundled builds. |
| 167 | func (s SdkSpec) PrebuiltSdkAvailableForUnbundledBuild() bool { |
| 168 | // "", "none", and "core_platform" are not available for unbundled build |
| 169 | // as we don't/can't have prebuilt stub for the versions |
| 170 | return s.Kind != SdkPrivate && s.Kind != SdkNone && s.Kind != SdkCorePlatform |
| 171 | } |
| 172 | |
| 173 | func (s SdkSpec) ForVendorPartition(ctx EarlyModuleContext) SdkSpec { |
| 174 | // If BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES has a numeric value, |
| 175 | // use it instead of "current" for the vendor partition. |
| 176 | currentSdkVersion := ctx.DeviceConfig().CurrentApiLevelForVendorModules() |
Jiyong Park | 7416d67 | 2024-01-04 23:20:42 +0000 | [diff] [blame] | 177 | // b/314011075: special case for Java modules in vendor partition. They can no longer use |
| 178 | // SDK 35 or later. Their maximum API level is limited to 34 (Android U). This is to |
| 179 | // discourage the use of Java APIs in the vendor partition which hasn't been officially |
| 180 | // supported since the Project Treble back in Android 10. We would like to eventually |
| 181 | // evacuate all Java modules from the partition, but that shall be done progressively. |
| 182 | // Note that the check for the availability of SDK 34 is to not break existing tests where |
| 183 | // any of the frozen SDK version is unavailable. |
| 184 | if isJava(ctx.Module()) && isSdkVersion34AvailableIn(ctx.Config()) { |
| 185 | currentSdkVersion = "34" |
| 186 | } |
| 187 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 188 | if currentSdkVersion == "current" { |
| 189 | return s |
| 190 | } |
| 191 | |
| 192 | if s.Kind == SdkPublic || s.Kind == SdkSystem { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 193 | if s.ApiLevel.IsCurrent() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 194 | if i, err := strconv.Atoi(currentSdkVersion); err == nil { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 195 | apiLevel := uncheckedFinalApiLevel(i) |
| 196 | return SdkSpec{s.Kind, apiLevel, s.Raw} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 197 | } |
| 198 | panic(fmt.Errorf("BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES must be either \"current\" or a number, but was %q", currentSdkVersion)) |
| 199 | } |
| 200 | } |
| 201 | return s |
| 202 | } |
| 203 | |
| 204 | // UsePrebuilt determines whether prebuilt SDK should be used for this SdkSpec with the given context. |
| 205 | func (s SdkSpec) UsePrebuilt(ctx EarlyModuleContext) bool { |
Jiyong Park | c702204 | 2021-04-15 16:53:05 +0900 | [diff] [blame] | 206 | switch s { |
| 207 | case SdkSpecNone, SdkSpecCorePlatform, SdkSpecPrivate: |
| 208 | return false |
| 209 | } |
| 210 | |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 211 | if s.ApiLevel.IsCurrent() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 212 | // "current" can be built from source and be from prebuilt SDK |
| 213 | return ctx.Config().AlwaysUsePrebuiltSdks() |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 214 | } else if !s.ApiLevel.IsPreview() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 215 | // validation check |
Mark White | 9421c4c | 2023-08-10 00:07:03 +0000 | [diff] [blame] | 216 | if s.Kind != SdkPublic && s.Kind != SdkSystem && s.Kind != SdkTest && |
| 217 | s.Kind != SdkTestFrameworksCore && s.Kind != SdkModule && s.Kind != SdkSystemServer { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 218 | panic(fmt.Errorf("prebuilt SDK is not not available for SdkKind=%q", s.Kind)) |
| 219 | return false |
| 220 | } |
| 221 | // numbered SDKs are always from prebuilt |
| 222 | return true |
| 223 | } |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 224 | return false |
| 225 | } |
| 226 | |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 227 | // EffectiveVersion converts an SdkSpec into the concrete ApiLevel that the module should use. For |
| 228 | // modules targeting an unreleased SDK (meaning it does not yet have a number) it returns |
| 229 | // FutureApiLevel(10000). |
| 230 | func (s SdkSpec) EffectiveVersion(ctx EarlyModuleContext) (ApiLevel, error) { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 231 | if !s.Valid() { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 232 | return s.ApiLevel, fmt.Errorf("invalid sdk version %q", s.Raw) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | if ctx.DeviceSpecific() || ctx.SocSpecific() { |
| 236 | s = s.ForVendorPartition(ctx) |
| 237 | } |
Spandan Das | dd7057c | 2023-01-05 01:03:47 +0000 | [diff] [blame] | 238 | return s.ApiLevel.EffectiveVersion(ctx) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // EffectiveVersionString converts an SdkSpec into the concrete version string that the module |
| 242 | // should use. For modules targeting an unreleased SDK (meaning it does not yet have a number) |
| 243 | // it returns the codename (P, Q, R, etc.) |
| 244 | func (s SdkSpec) EffectiveVersionString(ctx EarlyModuleContext) (string, error) { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 245 | if !s.Valid() { |
| 246 | return s.ApiLevel.String(), fmt.Errorf("invalid sdk version %q", s.Raw) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 247 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 248 | |
| 249 | if ctx.DeviceSpecific() || ctx.SocSpecific() { |
| 250 | s = s.ForVendorPartition(ctx) |
| 251 | } |
Spandan Das | dd7057c | 2023-01-05 01:03:47 +0000 | [diff] [blame] | 252 | return s.ApiLevel.EffectiveVersionString(ctx) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 253 | } |
| 254 | |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 255 | var ( |
Jiyong Park | c702204 | 2021-04-15 16:53:05 +0900 | [diff] [blame] | 256 | SdkSpecNone = SdkSpec{SdkNone, NoneApiLevel, "(no version)"} |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame] | 257 | SdkSpecPrivate = SdkSpec{SdkPrivate, PrivateApiLevel, ""} |
Jiyong Park | c702204 | 2021-04-15 16:53:05 +0900 | [diff] [blame] | 258 | SdkSpecCorePlatform = SdkSpec{SdkCorePlatform, FutureApiLevel, "core_platform"} |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 259 | ) |
| 260 | |
| 261 | func SdkSpecFrom(ctx EarlyModuleContext, str string) SdkSpec { |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 262 | return SdkSpecFromWithConfig(ctx.Config(), str) |
| 263 | } |
| 264 | |
| 265 | func SdkSpecFromWithConfig(config Config, str string) SdkSpec { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 266 | switch str { |
| 267 | // special cases first |
| 268 | case "": |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 269 | return SdkSpecPrivate |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 270 | case "none": |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 271 | return SdkSpecNone |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 272 | case "core_platform": |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 273 | return SdkSpecCorePlatform |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 274 | default: |
| 275 | // the syntax is [kind_]version |
| 276 | sep := strings.LastIndex(str, "_") |
| 277 | |
| 278 | var kindString string |
| 279 | if sep == 0 { |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame] | 280 | return SdkSpec{SdkInvalid, NewInvalidApiLevel(str), str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 281 | } else if sep == -1 { |
| 282 | kindString = "" |
| 283 | } else { |
| 284 | kindString = str[0:sep] |
| 285 | } |
| 286 | versionString := str[sep+1 : len(str)] |
| 287 | |
| 288 | var kind SdkKind |
| 289 | switch kindString { |
| 290 | case "": |
| 291 | kind = SdkPublic |
| 292 | case "core": |
| 293 | kind = SdkCore |
| 294 | case "system": |
| 295 | kind = SdkSystem |
| 296 | case "test": |
| 297 | kind = SdkTest |
Mark White | 9421c4c | 2023-08-10 00:07:03 +0000 | [diff] [blame] | 298 | case "test_frameworks_core": |
| 299 | kind = SdkTestFrameworksCore |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 300 | case "module": |
| 301 | kind = SdkModule |
| 302 | case "system_server": |
| 303 | kind = SdkSystemServer |
| 304 | default: |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 305 | return SdkSpec{SdkInvalid, NoneApiLevel, str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 306 | } |
| 307 | |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 308 | apiLevel, err := ApiLevelFromUserWithConfig(config, versionString) |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 309 | if err != nil { |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame] | 310 | return SdkSpec{SdkInvalid, NewInvalidApiLevel(versionString), str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 311 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 312 | return SdkSpec{kind, apiLevel, str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Jiyong Park | 7416d67 | 2024-01-04 23:20:42 +0000 | [diff] [blame] | 316 | // Checks if the use of this SDK `s` is valid for the given module context `ctx`. |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 317 | func (s SdkSpec) ValidateSystemSdk(ctx EarlyModuleContext) bool { |
Jiyong Park | 7416d67 | 2024-01-04 23:20:42 +0000 | [diff] [blame] | 318 | // Do some early checks. This check is currently only for Java modules. And our only concern |
| 319 | // is the use of "system" SDKs. |
Jiyong Park | 3bb9924 | 2024-01-04 23:21:26 +0000 | [diff] [blame] | 320 | if !isJava(ctx.Module()) || s.Kind != SdkSystem || ctx.DeviceConfig().BuildBrokenDontCheckSystemSdk() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 321 | return true |
| 322 | } |
Jiyong Park | 7416d67 | 2024-01-04 23:20:42 +0000 | [diff] [blame] | 323 | |
| 324 | inVendor := ctx.DeviceSpecific() || ctx.SocSpecific() |
| 325 | inProduct := ctx.ProductSpecific() |
| 326 | isProductUnbundled := ctx.Config().EnforceProductPartitionInterface() |
| 327 | inApex := false |
| 328 | if am, ok := ctx.Module().(ApexModule); ok { |
| 329 | inApex = am.InAnyApex() |
| 330 | } |
| 331 | isUnbundled := inVendor || (inProduct && isProductUnbundled) || inApex |
| 332 | |
| 333 | // Bundled modules can use any SDK |
| 334 | if !isUnbundled { |
| 335 | return true |
| 336 | } |
| 337 | |
| 338 | // Unbundled modules are allowed to use BOARD_SYSTEMSDK_VERSIONS |
| 339 | supportedVersions := ctx.DeviceConfig().SystemSdkVersions() |
| 340 | |
| 341 | // b/314011075: special case for vendor modules. Java modules in the vendor partition can |
| 342 | // not use SDK 35 or later. This is to discourage the use of Java APIs in the vendor |
| 343 | // partition which hasn't been officially supported since the Project Treble back in Android |
| 344 | // 10. We would like to eventually evacuate all Java modules from the partition, but that |
| 345 | // shall be done progressively. |
| 346 | if inVendor { |
| 347 | // 28 was the API when BOARD_SYSTEMSDK_VERSIONS was introduced, so that's the oldest |
| 348 | // we should allow. |
| 349 | supportedVersions = []string{} |
| 350 | for v := 28; v <= 34; v++ { |
| 351 | supportedVersions = append(supportedVersions, strconv.Itoa(v)) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 352 | } |
| 353 | } |
Jiyong Park | 7416d67 | 2024-01-04 23:20:42 +0000 | [diff] [blame] | 354 | |
| 355 | // APEXes in the system partition are still considered as part of the platform, thus can use |
| 356 | // more SDKs from PLATFORM_SYSTEMSDK_VERSIONS |
| 357 | if inApex && !inVendor { |
| 358 | supportedVersions = ctx.DeviceConfig().PlatformSystemSdkVersions() |
| 359 | } |
| 360 | |
| 361 | thisVer, err := s.EffectiveVersion(ctx) |
| 362 | if err != nil { |
| 363 | ctx.PropertyErrorf("sdk_version", "invalid sdk version %q", s.Raw) |
| 364 | return false |
| 365 | } |
| 366 | |
| 367 | thisVerString := strconv.Itoa(thisVer.FinalOrPreviewInt()) |
| 368 | if thisVer.IsPreview() { |
| 369 | thisVerString = *ctx.Config().productVariables.Platform_sdk_version_or_codename |
| 370 | } |
| 371 | |
| 372 | if !InList(thisVerString, supportedVersions) { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 373 | ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q", |
Jiyong Park | 7416d67 | 2024-01-04 23:20:42 +0000 | [diff] [blame] | 374 | s.Raw, supportedVersions) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 375 | return false |
| 376 | } |
| 377 | return true |
| 378 | } |
Spandan Das | 6b73fcb | 2023-03-20 20:20:58 +0000 | [diff] [blame] | 379 | |
Jiyong Park | 7416d67 | 2024-01-04 23:20:42 +0000 | [diff] [blame] | 380 | func isJava(m Module) bool { |
| 381 | moduleType := reflect.TypeOf(m).String() |
| 382 | return strings.HasPrefix(moduleType, "*java.") |
| 383 | } |
| 384 | |
| 385 | func isSdkVersion34AvailableIn(c Config) bool { |
| 386 | return c.PlatformSdkVersion().FinalInt() >= 34 |
| 387 | } |
| 388 | |
Spandan Das | 6b73fcb | 2023-03-20 20:20:58 +0000 | [diff] [blame] | 389 | func init() { |
| 390 | RegisterMakeVarsProvider(pctx, javaSdkMakeVars) |
| 391 | } |
| 392 | |
| 393 | // Export the name of the soong modules representing the various Java API surfaces. |
| 394 | func javaSdkMakeVars(ctx MakeVarsContext) { |
Jihoon Kang | 91c8395 | 2023-05-30 19:12:28 +0000 | [diff] [blame] | 395 | ctx.Strict("ANDROID_PUBLIC_STUBS", SdkPublic.DefaultJavaLibraryName()) |
| 396 | ctx.Strict("ANDROID_SYSTEM_STUBS", SdkSystem.DefaultJavaLibraryName()) |
| 397 | ctx.Strict("ANDROID_TEST_STUBS", SdkTest.DefaultJavaLibraryName()) |
| 398 | ctx.Strict("ANDROID_MODULE_LIB_STUBS", SdkModule.DefaultJavaLibraryName()) |
| 399 | ctx.Strict("ANDROID_SYSTEM_SERVER_STUBS", SdkSystemServer.DefaultJavaLibraryName()) |
| 400 | ctx.Strict("ANDROID_CORE_STUBS", SdkCore.DefaultJavaLibraryName()) |
Spandan Das | 6b73fcb | 2023-03-20 20:20:58 +0000 | [diff] [blame] | 401 | } |