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 ( |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 18 | "fmt" |
| 19 | "path/filepath" |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 20 | "sort" |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 21 | "strconv" |
| 22 | "strings" |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 23 | |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 24 | "android/soong/android" |
| 25 | "android/soong/java/config" |
| 26 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint/pathtools" |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 30 | func init() { |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 31 | android.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory) |
| 32 | android.RegisterSingletonType("sdk", sdkSingletonFactory) |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 33 | android.RegisterMakeVarsProvider(pctx, sdkMakeVars) |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 36 | var sdkVersionsKey = android.NewOnceKey("sdkVersionsKey") |
| 37 | var sdkFrameworkAidlPathKey = android.NewOnceKey("sdkFrameworkAidlPathKey") |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 38 | var apiFingerprintPathKey = android.NewOnceKey("apiFingerprintPathKey") |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 39 | |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 40 | type sdkContext interface { |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 41 | // sdkVersion returns sdkSpec that corresponds to the sdk_version property of the current module |
| 42 | sdkVersion() sdkSpec |
Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 43 | // systemModules returns the system_modules property of the current module, or an empty string if it is not set. |
| 44 | systemModules() string |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 45 | // minSdkVersion returns sdkSpec that corresponds to the min_sdk_version property of the current module, |
| 46 | // or from sdk_version if it is not set. |
| 47 | minSdkVersion() sdkSpec |
| 48 | // targetSdkVersion returns the sdkSpec that corresponds to the target_sdk_version property of the current module, |
| 49 | // or from sdk_version if it is not set. |
| 50 | targetSdkVersion() sdkSpec |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Nikita Ioffe | 1f4f345 | 2020-03-02 16:58:11 +0000 | [diff] [blame] | 53 | func UseApiFingerprint(ctx android.BaseModuleContext) bool { |
| 54 | if ctx.Config().UnbundledBuild() && |
Baligh Uddin | f620137 | 2020-01-24 23:15:44 +0000 | [diff] [blame] | 55 | !ctx.Config().UnbundledBuildUsePrebuiltSdks() && |
| 56 | ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") { |
| 57 | return true |
| 58 | } |
| 59 | return false |
| 60 | } |
| 61 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 62 | // sdkKind represents a particular category of an SDK spec like public, system, test, etc. |
| 63 | type sdkKind int |
| 64 | |
| 65 | const ( |
| 66 | sdkInvalid sdkKind = iota |
| 67 | sdkNone |
| 68 | sdkCore |
| 69 | sdkCorePlatform |
| 70 | sdkPublic |
| 71 | sdkSystem |
| 72 | sdkTest |
Jiyong Park | 50146e9 | 2020-01-30 18:00:15 +0900 | [diff] [blame] | 73 | sdkModule |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 74 | sdkSystemServer |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 75 | sdkPrivate |
| 76 | ) |
| 77 | |
| 78 | // String returns the string representation of this sdkKind |
| 79 | func (k sdkKind) String() string { |
| 80 | switch k { |
| 81 | case sdkPrivate: |
| 82 | return "private" |
| 83 | case sdkNone: |
| 84 | return "none" |
| 85 | case sdkPublic: |
| 86 | return "public" |
| 87 | case sdkSystem: |
| 88 | return "system" |
| 89 | case sdkTest: |
| 90 | return "test" |
| 91 | case sdkCore: |
| 92 | return "core" |
| 93 | case sdkCorePlatform: |
| 94 | return "core_platform" |
Jiyong Park | 50146e9 | 2020-01-30 18:00:15 +0900 | [diff] [blame] | 95 | case sdkModule: |
| 96 | return "module" |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 97 | case sdkSystemServer: |
| 98 | return "system_server" |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 99 | default: |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 100 | return "invalid" |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 104 | // sdkVersion represents a specific version number of an SDK spec of a particular kind |
| 105 | type sdkVersion int |
| 106 | |
| 107 | const ( |
| 108 | // special version number for a not-yet-frozen SDK |
| 109 | sdkVersionCurrent sdkVersion = sdkVersion(android.FutureApiLevel) |
| 110 | // special version number to be used for SDK specs where version number doesn't |
| 111 | // make sense, e.g. "none", "", etc. |
| 112 | sdkVersionNone sdkVersion = sdkVersion(0) |
| 113 | ) |
| 114 | |
| 115 | // isCurrent checks if the sdkVersion refers to the not-yet-published version of an sdkKind |
| 116 | func (v sdkVersion) isCurrent() bool { |
| 117 | return v == sdkVersionCurrent |
| 118 | } |
| 119 | |
| 120 | // isNumbered checks if the sdkVersion refers to the published (a.k.a numbered) version of an sdkKind |
| 121 | func (v sdkVersion) isNumbered() bool { |
| 122 | return !v.isCurrent() && v != sdkVersionNone |
| 123 | } |
| 124 | |
| 125 | // String returns the string representation of this sdkVersion. |
| 126 | func (v sdkVersion) String() string { |
| 127 | if v.isCurrent() { |
| 128 | return "current" |
| 129 | } else if v.isNumbered() { |
| 130 | return strconv.Itoa(int(v)) |
| 131 | } |
| 132 | return "(no version)" |
| 133 | } |
| 134 | |
| 135 | // asNumberString directly converts the numeric value of this sdk version as a string. |
| 136 | // When isNumbered() is true, this method is the same as String(). However, for sdkVersionCurrent |
| 137 | // and sdkVersionNone, this returns 10000 and 0 while String() returns "current" and "(no version"), |
| 138 | // respectively. |
| 139 | func (v sdkVersion) asNumberString() string { |
| 140 | return strconv.Itoa(int(v)) |
| 141 | } |
| 142 | |
| 143 | // sdkSpec represents the kind and the version of an SDK for a module to build against |
| 144 | type sdkSpec struct { |
| 145 | kind sdkKind |
| 146 | version sdkVersion |
| 147 | raw string |
| 148 | } |
| 149 | |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 150 | func (s sdkSpec) String() string { |
| 151 | return fmt.Sprintf("%s_%s", s.kind, s.version) |
| 152 | } |
| 153 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 154 | // valid checks if this sdkSpec is well-formed. Note however that true doesn't mean that the |
| 155 | // specified SDK actually exists. |
| 156 | func (s sdkSpec) valid() bool { |
| 157 | return s.kind != sdkInvalid |
| 158 | } |
| 159 | |
| 160 | // specified checks if this sdkSpec is well-formed and is not "". |
| 161 | func (s sdkSpec) specified() bool { |
| 162 | return s.valid() && s.kind != sdkPrivate |
| 163 | } |
| 164 | |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 165 | // whether the API surface is managed and versioned, i.e. has .txt file that |
| 166 | // get frozen on SDK freeze and changes get reviewed by API council. |
| 167 | func (s sdkSpec) stable() bool { |
| 168 | if !s.specified() { |
| 169 | return false |
| 170 | } |
| 171 | switch s.kind { |
Artur Satayev | 8cf899a | 2020-04-15 17:29:42 +0100 | [diff] [blame^] | 172 | case sdkNone: |
| 173 | // there is nothing to manage and version in this case; de facto stable API. |
| 174 | return true |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 175 | case sdkCore, sdkPublic, sdkSystem, sdkModule, sdkSystemServer: |
| 176 | return true |
Artur Satayev | 8cf899a | 2020-04-15 17:29:42 +0100 | [diff] [blame^] | 177 | case sdkCorePlatform, sdkTest, sdkPrivate: |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 178 | return false |
| 179 | default: |
| 180 | panic(fmt.Errorf("unknown sdkKind=%v", s.kind)) |
| 181 | } |
| 182 | return false |
| 183 | } |
| 184 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 185 | // prebuiltSdkAvailableForUnbundledBuilt tells whether this sdkSpec can have a prebuilt SDK |
| 186 | // that can be used for unbundled builds. |
| 187 | func (s sdkSpec) prebuiltSdkAvailableForUnbundledBuild() bool { |
| 188 | // "", "none", and "core_platform" are not available for unbundled build |
| 189 | // as we don't/can't have prebuilt stub for the versions |
| 190 | return s.kind != sdkPrivate && s.kind != sdkNone && s.kind != sdkCorePlatform |
| 191 | } |
| 192 | |
| 193 | // forPdkBuild converts this sdkSpec into another sdkSpec that is for the PDK builds. |
| 194 | func (s sdkSpec) forPdkBuild(ctx android.EarlyModuleContext) sdkSpec { |
| 195 | // For PDK builds, use the latest SDK version instead of "current" or "" |
| 196 | if s.kind == sdkPrivate || s.kind == sdkPublic { |
| 197 | kind := s.kind |
| 198 | if kind == sdkPrivate { |
| 199 | // We don't have prebuilt SDK for private APIs, so use the public SDK |
| 200 | // instead. This looks odd, but that's how it has been done. |
| 201 | // TODO(b/148271073): investigate the need for this. |
| 202 | kind = sdkPublic |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 203 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 204 | version := sdkVersion(LatestSdkVersionInt(ctx)) |
| 205 | return sdkSpec{kind, version, s.raw} |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 206 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 207 | return s |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 210 | // usePrebuilt determines whether prebuilt SDK should be used for this sdkSpec with the given context. |
| 211 | func (s sdkSpec) usePrebuilt(ctx android.EarlyModuleContext) bool { |
| 212 | if s.version.isCurrent() { |
| 213 | // "current" can be built from source and be from prebuilt SDK |
| 214 | return ctx.Config().UnbundledBuildUsePrebuiltSdks() |
| 215 | } else if s.version.isNumbered() { |
| 216 | // sanity check |
| 217 | if s.kind != sdkPublic && s.kind != sdkSystem && s.kind != sdkTest { |
| 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 |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 223 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 224 | // "", "none", "core_platform" fall here |
| 225 | return false |
| 226 | } |
| 227 | |
| 228 | // effectiveVersion converts an sdkSpec into the concrete sdkVersion that the module |
| 229 | // should use. For modules targeting an unreleased SDK (meaning it does not yet have a number) |
| 230 | // it returns android.FutureApiLevel(10000). |
| 231 | func (s sdkSpec) effectiveVersion(ctx android.EarlyModuleContext) (sdkVersion, error) { |
| 232 | if !s.valid() { |
| 233 | return s.version, fmt.Errorf("invalid sdk version %q", s.raw) |
| 234 | } |
| 235 | if ctx.Config().IsPdkBuild() { |
| 236 | s = s.forPdkBuild(ctx) |
| 237 | } |
| 238 | if s.version.isNumbered() { |
| 239 | return s.version, nil |
| 240 | } |
| 241 | return sdkVersion(ctx.Config().DefaultAppTargetSdkInt()), nil |
| 242 | } |
| 243 | |
| 244 | // effectiveVersionString converts an sdkSpec into the concrete version string that the module |
| 245 | // should use. For modules targeting an unreleased SDK (meaning it does not yet have a number) |
| 246 | // it returns the codename (P, Q, R, etc.) |
| 247 | func (s sdkSpec) effectiveVersionString(ctx android.EarlyModuleContext) (string, error) { |
| 248 | ver, err := s.effectiveVersion(ctx) |
| 249 | if err == nil && int(ver) == ctx.Config().DefaultAppTargetSdkInt() { |
| 250 | return ctx.Config().DefaultAppTargetSdk(), nil |
| 251 | } |
| 252 | return ver.String(), err |
| 253 | } |
| 254 | |
| 255 | func sdkSpecFrom(str string) sdkSpec { |
| 256 | switch str { |
| 257 | // special cases first |
| 258 | case "": |
| 259 | return sdkSpec{sdkPrivate, sdkVersionNone, str} |
| 260 | case "none": |
| 261 | return sdkSpec{sdkNone, sdkVersionNone, str} |
| 262 | case "core_platform": |
| 263 | return sdkSpec{sdkCorePlatform, sdkVersionNone, str} |
| 264 | default: |
| 265 | // the syntax is [kind_]version |
| 266 | sep := strings.LastIndex(str, "_") |
| 267 | |
| 268 | var kindString string |
| 269 | if sep == 0 { |
| 270 | return sdkSpec{sdkInvalid, sdkVersionNone, str} |
| 271 | } else if sep == -1 { |
| 272 | kindString = "" |
| 273 | } else { |
| 274 | kindString = str[0:sep] |
| 275 | } |
| 276 | versionString := str[sep+1 : len(str)] |
| 277 | |
| 278 | var kind sdkKind |
| 279 | switch kindString { |
| 280 | case "": |
| 281 | kind = sdkPublic |
| 282 | case "core": |
| 283 | kind = sdkCore |
| 284 | case "system": |
| 285 | kind = sdkSystem |
| 286 | case "test": |
| 287 | kind = sdkTest |
Jiyong Park | 50146e9 | 2020-01-30 18:00:15 +0900 | [diff] [blame] | 288 | case "module": |
| 289 | kind = sdkModule |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 290 | case "system_server": |
| 291 | kind = sdkSystemServer |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 292 | default: |
| 293 | return sdkSpec{sdkInvalid, sdkVersionNone, str} |
| 294 | } |
| 295 | |
| 296 | var version sdkVersion |
| 297 | if versionString == "current" { |
| 298 | version = sdkVersionCurrent |
| 299 | } else if i, err := strconv.Atoi(versionString); err == nil { |
| 300 | version = sdkVersion(i) |
| 301 | } else { |
| 302 | return sdkSpec{sdkInvalid, sdkVersionNone, str} |
| 303 | } |
| 304 | |
| 305 | return sdkSpec{kind, version, str} |
| 306 | } |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Jeongik Cha | 7c70831 | 2020-01-28 13:52:36 +0900 | [diff] [blame] | 309 | func (s sdkSpec) validateSystemSdk(ctx android.EarlyModuleContext) bool { |
| 310 | // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module) |
| 311 | // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29, |
| 312 | // sdk_version of the modules in vendor/product that use system sdk must be either system_28, system_29 or system_current |
| 313 | if s.kind != sdkSystem || !s.version.isNumbered() { |
| 314 | return true |
| 315 | } |
| 316 | allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions() |
| 317 | if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) { |
| 318 | systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions() |
| 319 | if len(systemSdkVersions) > 0 { |
| 320 | allowedVersions = systemSdkVersions |
| 321 | } |
| 322 | } |
| 323 | if len(allowedVersions) > 0 && !android.InList(s.version.String(), allowedVersions) { |
| 324 | ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q", |
| 325 | s.raw, allowedVersions) |
| 326 | return false |
| 327 | } |
| 328 | return true |
| 329 | } |
| 330 | |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 331 | func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext sdkContext) sdkDep { |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 332 | sdkVersion := sdkContext.sdkVersion() |
| 333 | if !sdkVersion.valid() { |
| 334 | ctx.PropertyErrorf("sdk_version", "invalid version %q", sdkVersion.raw) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 335 | return sdkDep{} |
| 336 | } |
| 337 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 338 | if ctx.Config().IsPdkBuild() { |
| 339 | sdkVersion = sdkVersion.forPdkBuild(ctx) |
| 340 | } |
Jeongik Cha | 7c70831 | 2020-01-28 13:52:36 +0900 | [diff] [blame] | 341 | if !sdkVersion.validateSystemSdk(ctx) { |
| 342 | return sdkDep{} |
| 343 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 344 | |
| 345 | if sdkVersion.usePrebuilt(ctx) { |
| 346 | dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), sdkVersion.kind.String()) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 347 | jar := filepath.Join(dir, "android.jar") |
| 348 | // There's no aidl for other SDKs yet. |
| 349 | // TODO(77525052): Add aidl files for other SDKs too. |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 350 | public_dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), "public") |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 351 | aidl := filepath.Join(public_dir, "framework.aidl") |
| 352 | jarPath := android.ExistentPathForSource(ctx, jar) |
| 353 | aidlPath := android.ExistentPathForSource(ctx, aidl) |
| 354 | lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath) |
| 355 | |
| 356 | if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() { |
| 357 | return sdkDep{ |
| 358 | invalidVersion: true, |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 359 | bootclasspath: []string{fmt.Sprintf("sdk_%s_%s_android", sdkVersion.kind, sdkVersion.version.String())}, |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
| 363 | if !jarPath.Valid() { |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 364 | ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, jar) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 365 | return sdkDep{} |
| 366 | } |
| 367 | |
| 368 | if !aidlPath.Valid() { |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 369 | ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, aidl) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 370 | return sdkDep{} |
| 371 | } |
| 372 | |
| 373 | return sdkDep{ |
| 374 | useFiles: true, |
| 375 | jars: android.Paths{jarPath.Path(), lambdaStubsPath}, |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 376 | aidl: android.OptionalPathForPath(aidlPath.Path()), |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 380 | toModule := func(modules []string, res string, aidl android.Path) sdkDep { |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 381 | return sdkDep{ |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 382 | useModule: true, |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 383 | bootclasspath: append(modules, config.DefaultLambdaStubsLibrary), |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 384 | systemModules: "core-current-stubs-system-modules", |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 385 | java9Classpath: modules, |
| 386 | frameworkResModule: res, |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 387 | aidl: android.OptionalPathForPath(aidl), |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 388 | } |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 391 | switch sdkVersion.kind { |
| 392 | case sdkPrivate: |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 393 | return sdkDep{ |
| 394 | useDefaultLibs: true, |
| 395 | frameworkResModule: "framework-res", |
| 396 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 397 | case sdkNone: |
Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 398 | systemModules := sdkContext.systemModules() |
| 399 | if systemModules == "" { |
| 400 | ctx.PropertyErrorf("sdk_version", |
| 401 | `system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`) |
| 402 | } else if systemModules == "none" { |
Colin Cross | 6d8d8c6 | 2019-10-28 15:10:03 -0700 | [diff] [blame] | 403 | return sdkDep{ |
| 404 | noStandardLibs: true, |
| 405 | } |
Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 408 | return sdkDep{ |
Colin Cross | 6d8d8c6 | 2019-10-28 15:10:03 -0700 | [diff] [blame] | 409 | useModule: true, |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 410 | noStandardLibs: true, |
Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 411 | systemModules: systemModules, |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 412 | bootclasspath: []string{systemModules}, |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 413 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 414 | case sdkCorePlatform: |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 415 | return sdkDep{ |
| 416 | useDefaultLibs: true, |
| 417 | frameworkResModule: "framework-res", |
| 418 | noFrameworksLibs: true, |
| 419 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 420 | case sdkPublic: |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 421 | return toModule([]string{"android_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx)) |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 422 | case sdkSystem: |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 423 | return toModule([]string{"android_system_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx)) |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 424 | case sdkTest: |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 425 | return toModule([]string{"android_test_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx)) |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 426 | case sdkCore: |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 427 | return toModule([]string{"core.current.stubs"}, "", nil) |
Jiyong Park | 50146e9 | 2020-01-30 18:00:15 +0900 | [diff] [blame] | 428 | case sdkModule: |
| 429 | // TODO(146757305): provide .apk and .aidl that have more APIs for modules |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 430 | return toModule([]string{"android_module_lib_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx)) |
| 431 | case sdkSystemServer: |
| 432 | // TODO(146757305): provide .apk and .aidl that have more APIs for modules |
Anton Hansson | ba6ab2e | 2020-03-19 15:23:38 +0000 | [diff] [blame] | 433 | return toModule([]string{"android_system_server_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx)) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 434 | default: |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 435 | panic(fmt.Errorf("invalid sdk %q", sdkVersion.raw)) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 436 | } |
| 437 | } |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 438 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 439 | func sdkPreSingletonFactory() android.Singleton { |
| 440 | return sdkPreSingleton{} |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 443 | type sdkPreSingleton struct{} |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 444 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 445 | func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 446 | sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil) |
| 447 | if err != nil { |
| 448 | ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error()) |
| 449 | } |
| 450 | |
| 451 | var sdkVersions []int |
| 452 | for _, sdkJar := range sdkJars { |
| 453 | dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar))) |
| 454 | v, err := strconv.Atoi(dir) |
| 455 | if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax { |
| 456 | continue |
| 457 | } else if err != nil { |
| 458 | ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error()) |
| 459 | } |
| 460 | sdkVersions = append(sdkVersions, v) |
| 461 | } |
| 462 | |
| 463 | sort.Ints(sdkVersions) |
| 464 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 465 | ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions }) |
| 466 | } |
| 467 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 468 | func LatestSdkVersionInt(ctx android.EarlyModuleContext) int { |
| 469 | sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int) |
| 470 | latestSdkVersion := 0 |
| 471 | if len(sdkVersions) > 0 { |
| 472 | latestSdkVersion = sdkVersions[len(sdkVersions)-1] |
| 473 | } |
| 474 | return latestSdkVersion |
| 475 | } |
| 476 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 477 | func sdkSingletonFactory() android.Singleton { |
| 478 | return sdkSingleton{} |
| 479 | } |
| 480 | |
| 481 | type sdkSingleton struct{} |
| 482 | |
| 483 | func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 484 | if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() { |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 485 | return |
| 486 | } |
| 487 | |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 488 | createSdkFrameworkAidl(ctx) |
| 489 | createAPIFingerprint(ctx) |
| 490 | } |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 491 | |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 492 | // Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules. |
| 493 | func createSdkFrameworkAidl(ctx android.SingletonContext) { |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 494 | stubsModules := []string{ |
| 495 | "android_stubs_current", |
| 496 | "android_test_stubs_current", |
| 497 | "android_system_stubs_current", |
| 498 | } |
| 499 | |
| 500 | stubsJars := make([]android.Paths, len(stubsModules)) |
| 501 | |
| 502 | ctx.VisitAllModules(func(module android.Module) { |
| 503 | // Collect dex jar paths for the modules listed above. |
| 504 | if j, ok := module.(Dependency); ok { |
| 505 | name := ctx.ModuleName(module) |
| 506 | if i := android.IndexList(name, stubsModules); i != -1 { |
| 507 | stubsJars[i] = j.HeaderJars() |
| 508 | } |
| 509 | } |
| 510 | }) |
| 511 | |
| 512 | var missingDeps []string |
| 513 | |
| 514 | for i := range stubsJars { |
| 515 | if stubsJars[i] == nil { |
| 516 | if ctx.Config().AllowMissingDependencies() { |
| 517 | missingDeps = append(missingDeps, stubsModules[i]) |
| 518 | } else { |
| 519 | ctx.Errorf("failed to find dex jar path for module %q", |
| 520 | stubsModules[i]) |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | rule := android.NewRuleBuilder() |
| 526 | rule.MissingDeps(missingDeps) |
| 527 | |
| 528 | var aidls android.Paths |
| 529 | for _, jars := range stubsJars { |
| 530 | for _, jar := range jars { |
| 531 | aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl")) |
| 532 | |
| 533 | rule.Command(). |
| 534 | Text("rm -f").Output(aidl) |
| 535 | rule.Command(). |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 536 | BuiltTool(ctx, "sdkparcelables"). |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 537 | Input(jar). |
| 538 | Output(aidl) |
| 539 | |
| 540 | aidls = append(aidls, aidl) |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | combinedAidl := sdkFrameworkAidlPath(ctx) |
| 545 | tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp") |
| 546 | |
| 547 | rule.Command(). |
| 548 | Text("rm -f").Output(tempPath) |
| 549 | rule.Command(). |
| 550 | Text("cat"). |
| 551 | Inputs(aidls). |
| 552 | Text("| sort -u >"). |
| 553 | Output(tempPath) |
| 554 | |
| 555 | commitChangeForRestat(rule, tempPath, combinedAidl) |
| 556 | |
| 557 | rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl") |
| 558 | } |
| 559 | |
| 560 | func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath { |
| 561 | return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} { |
| 562 | return android.PathForOutput(ctx, "framework.aidl") |
| 563 | }).(android.OutputPath) |
| 564 | } |
| 565 | |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 566 | // Create api_fingerprint.txt |
| 567 | func createAPIFingerprint(ctx android.SingletonContext) { |
Jiyong Park | 71b519d | 2019-04-18 17:25:49 +0900 | [diff] [blame] | 568 | out := ApiFingerprintPath(ctx) |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 569 | |
| 570 | rule := android.NewRuleBuilder() |
| 571 | |
| 572 | rule.Command(). |
| 573 | Text("rm -f").Output(out) |
| 574 | cmd := rule.Command() |
| 575 | |
| 576 | if ctx.Config().PlatformSdkCodename() == "REL" { |
| 577 | cmd.Text("echo REL >").Output(out) |
| 578 | } else if ctx.Config().IsPdkBuild() { |
| 579 | // TODO: get this from the PDK artifacts? |
| 580 | cmd.Text("echo PDK >").Output(out) |
| 581 | } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() { |
| 582 | in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil) |
| 583 | if err != nil { |
| 584 | ctx.Errorf("error globbing API files: %s", err) |
| 585 | } |
| 586 | |
| 587 | cmd.Text("cat"). |
| 588 | Inputs(android.PathsForSource(ctx, in)). |
Elliott Hughes | 34b49d1 | 2019-09-06 14:42:24 -0700 | [diff] [blame] | 589 | Text("| md5sum | cut -d' ' -f1 >"). |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 590 | Output(out) |
| 591 | } else { |
| 592 | // Unbundled build |
| 593 | // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one |
| 594 | cmd.Text("echo"). |
| 595 | Flag(ctx.Config().PlatformPreviewSdkVersion()). |
| 596 | Text(">"). |
| 597 | Output(out) |
| 598 | } |
| 599 | |
| 600 | rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt") |
| 601 | } |
| 602 | |
Jiyong Park | 71b519d | 2019-04-18 17:25:49 +0900 | [diff] [blame] | 603 | func ApiFingerprintPath(ctx android.PathContext) android.OutputPath { |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 604 | return ctx.Config().Once(apiFingerprintPathKey, func() interface{} { |
| 605 | return android.PathForOutput(ctx, "api_fingerprint.txt") |
| 606 | }).(android.OutputPath) |
| 607 | } |
| 608 | |
| 609 | func sdkMakeVars(ctx android.MakeVarsContext) { |
| 610 | if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() { |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 611 | return |
| 612 | } |
| 613 | |
| 614 | ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String()) |
Jiyong Park | 71b519d | 2019-04-18 17:25:49 +0900 | [diff] [blame] | 615 | ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String()) |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 616 | } |