blob: be5e5124006b589449ea1f30067cf21eb526d080 [file] [log] [blame]
Colin Crossfb6d7812019-01-09 22:17:55 -08001// 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
15package java
16
17import (
Colin Crossfb6d7812019-01-09 22:17:55 -080018 "fmt"
19 "path/filepath"
Colin Cross98fd5742019-01-09 23:04:25 -080020 "sort"
Colin Crossfb6d7812019-01-09 22:17:55 -080021 "strconv"
22 "strings"
Colin Cross3047fa22019-04-18 10:56:44 -070023
Jaewoong Jung9befb0c2020-01-18 10:33:43 -080024 "android/soong/android"
25 "android/soong/java/config"
26
Colin Cross3047fa22019-04-18 10:56:44 -070027 "github.com/google/blueprint/pathtools"
Colin Crossfb6d7812019-01-09 22:17:55 -080028)
29
Colin Cross98fd5742019-01-09 23:04:25 -080030func init() {
Colin Cross3047fa22019-04-18 10:56:44 -070031 android.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
32 android.RegisterSingletonType("sdk", sdkSingletonFactory)
Colin Cross10932872019-04-18 14:27:12 -070033 android.RegisterMakeVarsProvider(pctx, sdkMakeVars)
Colin Cross98fd5742019-01-09 23:04:25 -080034}
35
Colin Cross3047fa22019-04-18 10:56:44 -070036var sdkVersionsKey = android.NewOnceKey("sdkVersionsKey")
37var sdkFrameworkAidlPathKey = android.NewOnceKey("sdkFrameworkAidlPathKey")
Anton Hansson85c151c2020-04-09 13:29:59 +010038var nonUpdatableFrameworkAidlPathKey = android.NewOnceKey("nonUpdatableFrameworkAidlPathKey")
Colin Cross10932872019-04-18 14:27:12 -070039var apiFingerprintPathKey = android.NewOnceKey("apiFingerprintPathKey")
Colin Cross98fd5742019-01-09 23:04:25 -080040
Colin Crossfb6d7812019-01-09 22:17:55 -080041type sdkContext interface {
Jiyong Park6a927c42020-01-21 02:03:43 +090042 // sdkVersion returns sdkSpec that corresponds to the sdk_version property of the current module
43 sdkVersion() sdkSpec
Paul Duffine25c6442019-10-11 13:50:28 +010044 // systemModules returns the system_modules property of the current module, or an empty string if it is not set.
45 systemModules() string
Jiyong Park6a927c42020-01-21 02:03:43 +090046 // minSdkVersion returns sdkSpec that corresponds to the min_sdk_version property of the current module,
47 // or from sdk_version if it is not set.
48 minSdkVersion() sdkSpec
49 // targetSdkVersion returns the sdkSpec that corresponds to the target_sdk_version property of the current module,
50 // or from sdk_version if it is not set.
51 targetSdkVersion() sdkSpec
Colin Crossfb6d7812019-01-09 22:17:55 -080052}
53
Nikita Ioffe934c4f22020-03-02 16:58:11 +000054func UseApiFingerprint(ctx android.BaseModuleContext) bool {
55 if ctx.Config().UnbundledBuild() &&
Baligh Uddinf6201372020-01-24 23:15:44 +000056 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
57 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
58 return true
59 }
60 return false
61}
62
Jiyong Park6a927c42020-01-21 02:03:43 +090063// sdkKind represents a particular category of an SDK spec like public, system, test, etc.
64type sdkKind int
65
66const (
67 sdkInvalid sdkKind = iota
68 sdkNone
69 sdkCore
70 sdkCorePlatform
71 sdkPublic
72 sdkSystem
73 sdkTest
Jiyong Park50146e92020-01-30 18:00:15 +090074 sdkModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +090075 sdkSystemServer
Jiyong Park6a927c42020-01-21 02:03:43 +090076 sdkPrivate
77)
78
79// String returns the string representation of this sdkKind
80func (k sdkKind) String() string {
81 switch k {
82 case sdkPrivate:
83 return "private"
84 case sdkNone:
85 return "none"
86 case sdkPublic:
87 return "public"
88 case sdkSystem:
89 return "system"
90 case sdkTest:
91 return "test"
92 case sdkCore:
93 return "core"
94 case sdkCorePlatform:
95 return "core_platform"
Jiyong Park50146e92020-01-30 18:00:15 +090096 case sdkModule:
97 return "module"
Jiyong Parkaae9bd12020-02-12 04:36:43 +090098 case sdkSystemServer:
99 return "system_server"
Colin Crossfb6d7812019-01-09 22:17:55 -0800100 default:
Jiyong Park6a927c42020-01-21 02:03:43 +0900101 return "invalid"
Colin Crossfb6d7812019-01-09 22:17:55 -0800102 }
103}
104
Jiyong Park6a927c42020-01-21 02:03:43 +0900105// sdkVersion represents a specific version number of an SDK spec of a particular kind
106type sdkVersion int
107
108const (
109 // special version number for a not-yet-frozen SDK
110 sdkVersionCurrent sdkVersion = sdkVersion(android.FutureApiLevel)
111 // special version number to be used for SDK specs where version number doesn't
112 // make sense, e.g. "none", "", etc.
113 sdkVersionNone sdkVersion = sdkVersion(0)
114)
115
116// isCurrent checks if the sdkVersion refers to the not-yet-published version of an sdkKind
117func (v sdkVersion) isCurrent() bool {
118 return v == sdkVersionCurrent
119}
120
121// isNumbered checks if the sdkVersion refers to the published (a.k.a numbered) version of an sdkKind
122func (v sdkVersion) isNumbered() bool {
123 return !v.isCurrent() && v != sdkVersionNone
124}
125
126// String returns the string representation of this sdkVersion.
127func (v sdkVersion) String() string {
128 if v.isCurrent() {
129 return "current"
130 } else if v.isNumbered() {
131 return strconv.Itoa(int(v))
132 }
133 return "(no version)"
134}
135
136// asNumberString directly converts the numeric value of this sdk version as a string.
137// When isNumbered() is true, this method is the same as String(). However, for sdkVersionCurrent
138// and sdkVersionNone, this returns 10000 and 0 while String() returns "current" and "(no version"),
139// respectively.
140func (v sdkVersion) asNumberString() string {
141 return strconv.Itoa(int(v))
142}
143
144// sdkSpec represents the kind and the version of an SDK for a module to build against
145type sdkSpec struct {
146 kind sdkKind
147 version sdkVersion
148 raw string
149}
150
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100151func (s sdkSpec) String() string {
152 return fmt.Sprintf("%s_%s", s.kind, s.version)
153}
154
Jiyong Park6a927c42020-01-21 02:03:43 +0900155// valid checks if this sdkSpec is well-formed. Note however that true doesn't mean that the
156// specified SDK actually exists.
157func (s sdkSpec) valid() bool {
158 return s.kind != sdkInvalid
159}
160
161// specified checks if this sdkSpec is well-formed and is not "".
162func (s sdkSpec) specified() bool {
163 return s.valid() && s.kind != sdkPrivate
164}
165
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100166// whether the API surface is managed and versioned, i.e. has .txt file that
167// get frozen on SDK freeze and changes get reviewed by API council.
168func (s sdkSpec) stable() bool {
169 if !s.specified() {
170 return false
171 }
172 switch s.kind {
173 case sdkCore, sdkPublic, sdkSystem, sdkModule, sdkSystemServer:
174 return true
175 case sdkNone, sdkCorePlatform, sdkTest, sdkPrivate:
176 return false
177 default:
178 panic(fmt.Errorf("unknown sdkKind=%v", s.kind))
179 }
180 return false
181}
182
Jiyong Park6a927c42020-01-21 02:03:43 +0900183// prebuiltSdkAvailableForUnbundledBuilt tells whether this sdkSpec can have a prebuilt SDK
184// that can be used for unbundled builds.
185func (s sdkSpec) prebuiltSdkAvailableForUnbundledBuild() bool {
186 // "", "none", and "core_platform" are not available for unbundled build
187 // as we don't/can't have prebuilt stub for the versions
188 return s.kind != sdkPrivate && s.kind != sdkNone && s.kind != sdkCorePlatform
189}
190
191// forPdkBuild converts this sdkSpec into another sdkSpec that is for the PDK builds.
192func (s sdkSpec) forPdkBuild(ctx android.EarlyModuleContext) sdkSpec {
193 // For PDK builds, use the latest SDK version instead of "current" or ""
194 if s.kind == sdkPrivate || s.kind == sdkPublic {
195 kind := s.kind
196 if kind == sdkPrivate {
197 // We don't have prebuilt SDK for private APIs, so use the public SDK
198 // instead. This looks odd, but that's how it has been done.
199 // TODO(b/148271073): investigate the need for this.
200 kind = sdkPublic
Colin Crossfb6d7812019-01-09 22:17:55 -0800201 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900202 version := sdkVersion(LatestSdkVersionInt(ctx))
203 return sdkSpec{kind, version, s.raw}
Colin Crossfb6d7812019-01-09 22:17:55 -0800204 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900205 return s
Colin Crossfb6d7812019-01-09 22:17:55 -0800206}
207
Jiyong Park6a927c42020-01-21 02:03:43 +0900208// usePrebuilt determines whether prebuilt SDK should be used for this sdkSpec with the given context.
209func (s sdkSpec) usePrebuilt(ctx android.EarlyModuleContext) bool {
210 if s.version.isCurrent() {
211 // "current" can be built from source and be from prebuilt SDK
212 return ctx.Config().UnbundledBuildUsePrebuiltSdks()
213 } else if s.version.isNumbered() {
214 // sanity check
215 if s.kind != sdkPublic && s.kind != sdkSystem && s.kind != sdkTest {
216 panic(fmt.Errorf("prebuilt SDK is not not available for sdkKind=%q", s.kind))
217 return false
218 }
219 // numbered SDKs are always from prebuilt
220 return true
Colin Crossfb6d7812019-01-09 22:17:55 -0800221 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900222 // "", "none", "core_platform" fall here
223 return false
224}
225
226// effectiveVersion converts an sdkSpec into the concrete sdkVersion that the module
227// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
228// it returns android.FutureApiLevel(10000).
229func (s sdkSpec) effectiveVersion(ctx android.EarlyModuleContext) (sdkVersion, error) {
230 if !s.valid() {
231 return s.version, fmt.Errorf("invalid sdk version %q", s.raw)
232 }
233 if ctx.Config().IsPdkBuild() {
234 s = s.forPdkBuild(ctx)
235 }
236 if s.version.isNumbered() {
237 return s.version, nil
238 }
239 return sdkVersion(ctx.Config().DefaultAppTargetSdkInt()), nil
240}
241
242// effectiveVersionString converts an sdkSpec into the concrete version string that the module
243// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
244// it returns the codename (P, Q, R, etc.)
245func (s sdkSpec) effectiveVersionString(ctx android.EarlyModuleContext) (string, error) {
246 ver, err := s.effectiveVersion(ctx)
247 if err == nil && int(ver) == ctx.Config().DefaultAppTargetSdkInt() {
248 return ctx.Config().DefaultAppTargetSdk(), nil
249 }
250 return ver.String(), err
251}
252
253func sdkSpecFrom(str string) sdkSpec {
254 switch str {
255 // special cases first
256 case "":
257 return sdkSpec{sdkPrivate, sdkVersionNone, str}
258 case "none":
259 return sdkSpec{sdkNone, sdkVersionNone, str}
260 case "core_platform":
261 return sdkSpec{sdkCorePlatform, sdkVersionNone, str}
262 default:
263 // the syntax is [kind_]version
264 sep := strings.LastIndex(str, "_")
265
266 var kindString string
267 if sep == 0 {
268 return sdkSpec{sdkInvalid, sdkVersionNone, str}
269 } else if sep == -1 {
270 kindString = ""
271 } else {
272 kindString = str[0:sep]
273 }
274 versionString := str[sep+1 : len(str)]
275
276 var kind sdkKind
277 switch kindString {
278 case "":
279 kind = sdkPublic
280 case "core":
281 kind = sdkCore
282 case "system":
283 kind = sdkSystem
284 case "test":
285 kind = sdkTest
Jiyong Park50146e92020-01-30 18:00:15 +0900286 case "module":
287 kind = sdkModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900288 case "system_server":
289 kind = sdkSystemServer
Jiyong Park6a927c42020-01-21 02:03:43 +0900290 default:
291 return sdkSpec{sdkInvalid, sdkVersionNone, str}
292 }
293
294 var version sdkVersion
295 if versionString == "current" {
296 version = sdkVersionCurrent
297 } else if i, err := strconv.Atoi(versionString); err == nil {
298 version = sdkVersion(i)
299 } else {
300 return sdkSpec{sdkInvalid, sdkVersionNone, str}
301 }
302
303 return sdkSpec{kind, version, str}
304 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800305}
306
Colin Cross1184b642019-12-30 18:43:07 -0800307func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext sdkContext) sdkDep {
Jiyong Park6a927c42020-01-21 02:03:43 +0900308 sdkVersion := sdkContext.sdkVersion()
309 if !sdkVersion.valid() {
310 ctx.PropertyErrorf("sdk_version", "invalid version %q", sdkVersion.raw)
Colin Crossfb6d7812019-01-09 22:17:55 -0800311 return sdkDep{}
312 }
313
Jiyong Park6a927c42020-01-21 02:03:43 +0900314 if ctx.Config().IsPdkBuild() {
315 sdkVersion = sdkVersion.forPdkBuild(ctx)
316 }
317
318 if sdkVersion.usePrebuilt(ctx) {
319 dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), sdkVersion.kind.String())
Colin Crossfb6d7812019-01-09 22:17:55 -0800320 jar := filepath.Join(dir, "android.jar")
321 // There's no aidl for other SDKs yet.
322 // TODO(77525052): Add aidl files for other SDKs too.
Jiyong Park6a927c42020-01-21 02:03:43 +0900323 public_dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), "public")
Colin Crossfb6d7812019-01-09 22:17:55 -0800324 aidl := filepath.Join(public_dir, "framework.aidl")
325 jarPath := android.ExistentPathForSource(ctx, jar)
326 aidlPath := android.ExistentPathForSource(ctx, aidl)
327 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
328
329 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
330 return sdkDep{
331 invalidVersion: true,
Jiyong Park6a927c42020-01-21 02:03:43 +0900332 bootclasspath: []string{fmt.Sprintf("sdk_%s_%s_android", sdkVersion.kind, sdkVersion.version.String())},
Colin Crossfb6d7812019-01-09 22:17:55 -0800333 }
334 }
335
336 if !jarPath.Valid() {
Jiyong Park6a927c42020-01-21 02:03:43 +0900337 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, jar)
Colin Crossfb6d7812019-01-09 22:17:55 -0800338 return sdkDep{}
339 }
340
341 if !aidlPath.Valid() {
Jiyong Park6a927c42020-01-21 02:03:43 +0900342 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, aidl)
Colin Crossfb6d7812019-01-09 22:17:55 -0800343 return sdkDep{}
344 }
345
346 return sdkDep{
347 useFiles: true,
348 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Cross3047fa22019-04-18 10:56:44 -0700349 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800350 }
351 }
352
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900353 toModule := func(modules []string, res string, aidl android.Path) sdkDep {
Colin Cross6cef4812019-10-17 14:23:50 -0700354 return sdkDep{
Colin Crossfb6d7812019-01-09 22:17:55 -0800355 useModule: true,
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900356 bootclasspath: append(modules, config.DefaultLambdaStubsLibrary),
Colin Cross6cef4812019-10-17 14:23:50 -0700357 systemModules: "core-current-stubs-system-modules",
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900358 java9Classpath: modules,
359 frameworkResModule: res,
Colin Cross3047fa22019-04-18 10:56:44 -0700360 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800361 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800362 }
363
Colin Cross98fd5742019-01-09 23:04:25 -0800364 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
365 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
Jiyong Park6a927c42020-01-21 02:03:43 +0900366 if sdkVersion.kind == sdkSystem && sdkVersion.version.isNumbered() {
Colin Cross98fd5742019-01-09 23:04:25 -0800367 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
368 if ctx.DeviceSpecific() || ctx.SocSpecific() {
369 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
370 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
371 }
372 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900373 if len(allowed_versions) > 0 && !android.InList(sdkVersion.version.String(), allowed_versions) {
Colin Cross98fd5742019-01-09 23:04:25 -0800374 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
Jiyong Park6a927c42020-01-21 02:03:43 +0900375 sdkVersion.raw, allowed_versions)
Colin Cross98fd5742019-01-09 23:04:25 -0800376 }
377 }
378
Jiyong Park6a927c42020-01-21 02:03:43 +0900379 switch sdkVersion.kind {
380 case sdkPrivate:
Colin Crossfb6d7812019-01-09 22:17:55 -0800381 return sdkDep{
382 useDefaultLibs: true,
383 frameworkResModule: "framework-res",
384 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900385 case sdkNone:
Paul Duffine25c6442019-10-11 13:50:28 +0100386 systemModules := sdkContext.systemModules()
387 if systemModules == "" {
388 ctx.PropertyErrorf("sdk_version",
389 `system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`)
390 } else if systemModules == "none" {
Colin Cross6d8d8c62019-10-28 15:10:03 -0700391 return sdkDep{
392 noStandardLibs: true,
393 }
Paul Duffine25c6442019-10-11 13:50:28 +0100394 }
395
Paul Duffin52d398a2019-06-11 12:31:14 +0100396 return sdkDep{
Colin Cross6d8d8c62019-10-28 15:10:03 -0700397 useModule: true,
Paul Duffin52d398a2019-06-11 12:31:14 +0100398 noStandardLibs: true,
Paul Duffine25c6442019-10-11 13:50:28 +0100399 systemModules: systemModules,
Colin Cross6cef4812019-10-17 14:23:50 -0700400 bootclasspath: []string{systemModules},
Paul Duffin52d398a2019-06-11 12:31:14 +0100401 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900402 case sdkCorePlatform:
Paul Duffin50c217c2019-06-12 13:25:22 +0100403 return sdkDep{
404 useDefaultLibs: true,
405 frameworkResModule: "framework-res",
406 noFrameworksLibs: true,
407 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900408 case sdkPublic:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900409 return toModule([]string{"android_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900410 case sdkSystem:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900411 return toModule([]string{"android_system_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900412 case sdkTest:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900413 return toModule([]string{"android_test_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900414 case sdkCore:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900415 return toModule([]string{"core.current.stubs"}, "", nil)
Jiyong Park50146e92020-01-30 18:00:15 +0900416 case sdkModule:
417 // TODO(146757305): provide .apk and .aidl that have more APIs for modules
Anton Hansson85c151c2020-04-09 13:29:59 +0100418 return toModule([]string{"android_module_lib_stubs_current"}, "framework-res", nonUpdatableFrameworkAidlPath(ctx))
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900419 case sdkSystemServer:
420 // TODO(146757305): provide .apk and .aidl that have more APIs for modules
Anton Hanssonbbd78552020-03-19 15:23:38 +0000421 return toModule([]string{"android_system_server_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800422 default:
Jiyong Park6a927c42020-01-21 02:03:43 +0900423 panic(fmt.Errorf("invalid sdk %q", sdkVersion.raw))
Colin Crossfb6d7812019-01-09 22:17:55 -0800424 }
425}
Colin Cross98fd5742019-01-09 23:04:25 -0800426
Colin Cross3047fa22019-04-18 10:56:44 -0700427func sdkPreSingletonFactory() android.Singleton {
428 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800429}
430
Colin Cross3047fa22019-04-18 10:56:44 -0700431type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800432
Colin Cross3047fa22019-04-18 10:56:44 -0700433func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800434 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
435 if err != nil {
436 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
437 }
438
439 var sdkVersions []int
440 for _, sdkJar := range sdkJars {
441 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
442 v, err := strconv.Atoi(dir)
443 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
444 continue
445 } else if err != nil {
446 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
447 }
448 sdkVersions = append(sdkVersions, v)
449 }
450
451 sort.Ints(sdkVersions)
452
Colin Cross3047fa22019-04-18 10:56:44 -0700453 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
454}
455
Jiyong Park6a927c42020-01-21 02:03:43 +0900456func LatestSdkVersionInt(ctx android.EarlyModuleContext) int {
457 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
458 latestSdkVersion := 0
459 if len(sdkVersions) > 0 {
460 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
461 }
462 return latestSdkVersion
463}
464
Colin Cross3047fa22019-04-18 10:56:44 -0700465func sdkSingletonFactory() android.Singleton {
466 return sdkSingleton{}
467}
468
469type sdkSingleton struct{}
470
471func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross10932872019-04-18 14:27:12 -0700472 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700473 return
474 }
475
Colin Cross10932872019-04-18 14:27:12 -0700476 createSdkFrameworkAidl(ctx)
Anton Hansson85c151c2020-04-09 13:29:59 +0100477 createNonUpdatableFrameworkAidl(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700478 createAPIFingerprint(ctx)
479}
Colin Cross3047fa22019-04-18 10:56:44 -0700480
Colin Cross10932872019-04-18 14:27:12 -0700481// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
482func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross3047fa22019-04-18 10:56:44 -0700483 stubsModules := []string{
484 "android_stubs_current",
485 "android_test_stubs_current",
486 "android_system_stubs_current",
487 }
488
Anton Hansson85c151c2020-04-09 13:29:59 +0100489 combinedAidl := sdkFrameworkAidlPath(ctx)
490 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
491
492 rule := createFrameworkAidl(stubsModules, tempPath, ctx)
493
494 commitChangeForRestat(rule, tempPath, combinedAidl)
495
496 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
497}
498
499// Creates a version of framework.aidl for the non-updatable part of the platform.
500func createNonUpdatableFrameworkAidl(ctx android.SingletonContext) {
501 stubsModules := []string{"android_module_lib_stubs_current"}
502
503 combinedAidl := nonUpdatableFrameworkAidlPath(ctx)
504 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
505
506 rule := createFrameworkAidl(stubsModules, tempPath, ctx)
507
508 commitChangeForRestat(rule, tempPath, combinedAidl)
509
510 rule.Build(pctx, ctx, "framework_non_updatable_aidl", "generate framework_non_updatable.aidl")
511}
512
513func createFrameworkAidl(stubsModules []string, path android.OutputPath, ctx android.SingletonContext) *android.RuleBuilder {
Colin Cross3047fa22019-04-18 10:56:44 -0700514 stubsJars := make([]android.Paths, len(stubsModules))
515
516 ctx.VisitAllModules(func(module android.Module) {
517 // Collect dex jar paths for the modules listed above.
518 if j, ok := module.(Dependency); ok {
519 name := ctx.ModuleName(module)
520 if i := android.IndexList(name, stubsModules); i != -1 {
521 stubsJars[i] = j.HeaderJars()
522 }
523 }
524 })
525
526 var missingDeps []string
527
528 for i := range stubsJars {
529 if stubsJars[i] == nil {
530 if ctx.Config().AllowMissingDependencies() {
531 missingDeps = append(missingDeps, stubsModules[i])
532 } else {
Anton Hansson85c151c2020-04-09 13:29:59 +0100533 ctx.Errorf("failed to find dex jar path for module %q", stubsModules[i])
Colin Cross3047fa22019-04-18 10:56:44 -0700534 }
535 }
536 }
537
538 rule := android.NewRuleBuilder()
539 rule.MissingDeps(missingDeps)
540
541 var aidls android.Paths
542 for _, jars := range stubsJars {
543 for _, jar := range jars {
544 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
545
546 rule.Command().
547 Text("rm -f").Output(aidl)
548 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700549 BuiltTool(ctx, "sdkparcelables").
Colin Cross3047fa22019-04-18 10:56:44 -0700550 Input(jar).
551 Output(aidl)
552
553 aidls = append(aidls, aidl)
554 }
555 }
556
Colin Cross3047fa22019-04-18 10:56:44 -0700557 rule.Command().
Anton Hansson85c151c2020-04-09 13:29:59 +0100558 Text("rm -f").Output(path)
Colin Cross3047fa22019-04-18 10:56:44 -0700559 rule.Command().
560 Text("cat").
561 Inputs(aidls).
562 Text("| sort -u >").
Anton Hansson85c151c2020-04-09 13:29:59 +0100563 Output(path)
Colin Cross3047fa22019-04-18 10:56:44 -0700564
Anton Hansson85c151c2020-04-09 13:29:59 +0100565 return rule
Colin Cross3047fa22019-04-18 10:56:44 -0700566}
567
568func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
569 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
570 return android.PathForOutput(ctx, "framework.aidl")
571 }).(android.OutputPath)
572}
573
Anton Hansson85c151c2020-04-09 13:29:59 +0100574func nonUpdatableFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
575 return ctx.Config().Once(nonUpdatableFrameworkAidlPathKey, func() interface{} {
576 return android.PathForOutput(ctx, "framework_non_updatable.aidl")
577 }).(android.OutputPath)
578}
579
Colin Cross10932872019-04-18 14:27:12 -0700580// Create api_fingerprint.txt
581func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Park71b519d2019-04-18 17:25:49 +0900582 out := ApiFingerprintPath(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700583
584 rule := android.NewRuleBuilder()
585
586 rule.Command().
587 Text("rm -f").Output(out)
588 cmd := rule.Command()
589
590 if ctx.Config().PlatformSdkCodename() == "REL" {
591 cmd.Text("echo REL >").Output(out)
592 } else if ctx.Config().IsPdkBuild() {
593 // TODO: get this from the PDK artifacts?
594 cmd.Text("echo PDK >").Output(out)
595 } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
596 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
597 if err != nil {
598 ctx.Errorf("error globbing API files: %s", err)
599 }
600
601 cmd.Text("cat").
602 Inputs(android.PathsForSource(ctx, in)).
Elliott Hughes34b49d12019-09-06 14:42:24 -0700603 Text("| md5sum | cut -d' ' -f1 >").
Colin Cross10932872019-04-18 14:27:12 -0700604 Output(out)
605 } else {
606 // Unbundled build
607 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
608 cmd.Text("echo").
609 Flag(ctx.Config().PlatformPreviewSdkVersion()).
610 Text(">").
611 Output(out)
612 }
613
614 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
615}
616
Jiyong Park71b519d2019-04-18 17:25:49 +0900617func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross10932872019-04-18 14:27:12 -0700618 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
619 return android.PathForOutput(ctx, "api_fingerprint.txt")
620 }).(android.OutputPath)
621}
622
623func sdkMakeVars(ctx android.MakeVarsContext) {
624 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700625 return
626 }
627
628 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Park71b519d2019-04-18 17:25:49 +0900629 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800630}