blob: b44cd8e1a8d430c8686a36939f330fc81c939ac7 [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 Hansson3f07ab22020-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 Ioffe1f4f3452020-03-02 16:58:11 +000054func UseApiFingerprint(ctx android.BaseModuleContext) bool {
55 if ctx.Config().UnbundledBuild() &&
Jeongik Cha816a23a2020-07-08 01:09:23 +090056 !ctx.Config().AlwaysUsePrebuiltSdks() &&
Baligh Uddinf6201372020-01-24 23:15:44 +000057 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:
Jiyong Park49062c82020-06-17 12:11:14 +090097 return "module-lib"
Jiyong Parkaae9bd12020-02-12 04:36:43 +090098 case sdkSystemServer:
Jiyong Park49062c82020-06-17 12:11:14 +090099 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 Satayev2db1c3f2020-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 Satayev2db1c3f2020-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 {
Artur Satayev8cf899a2020-04-15 17:29:42 +0100173 case sdkNone:
174 // there is nothing to manage and version in this case; de facto stable API.
175 return true
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100176 case sdkCore, sdkPublic, sdkSystem, sdkModule, sdkSystemServer:
177 return true
Artur Satayev8cf899a2020-04-15 17:29:42 +0100178 case sdkCorePlatform, sdkTest, sdkPrivate:
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100179 return false
180 default:
181 panic(fmt.Errorf("unknown sdkKind=%v", s.kind))
182 }
183 return false
184}
185
Jiyong Park6a927c42020-01-21 02:03:43 +0900186// prebuiltSdkAvailableForUnbundledBuilt tells whether this sdkSpec can have a prebuilt SDK
187// that can be used for unbundled builds.
188func (s sdkSpec) prebuiltSdkAvailableForUnbundledBuild() bool {
189 // "", "none", and "core_platform" are not available for unbundled build
190 // as we don't/can't have prebuilt stub for the versions
191 return s.kind != sdkPrivate && s.kind != sdkNone && s.kind != sdkCorePlatform
192}
193
Jiyong Park6a927c42020-01-21 02:03:43 +0900194// usePrebuilt determines whether prebuilt SDK should be used for this sdkSpec with the given context.
195func (s sdkSpec) usePrebuilt(ctx android.EarlyModuleContext) bool {
196 if s.version.isCurrent() {
197 // "current" can be built from source and be from prebuilt SDK
Jeongik Cha816a23a2020-07-08 01:09:23 +0900198 return ctx.Config().AlwaysUsePrebuiltSdks()
Jiyong Park6a927c42020-01-21 02:03:43 +0900199 } else if s.version.isNumbered() {
Patrice Arrudab481b872020-07-28 18:30:44 +0000200 // validation check
Jiyong Park6a927c42020-01-21 02:03:43 +0900201 if s.kind != sdkPublic && s.kind != sdkSystem && s.kind != sdkTest {
202 panic(fmt.Errorf("prebuilt SDK is not not available for sdkKind=%q", s.kind))
203 return false
204 }
205 // numbered SDKs are always from prebuilt
206 return true
Colin Crossfb6d7812019-01-09 22:17:55 -0800207 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900208 // "", "none", "core_platform" fall here
209 return false
210}
211
212// effectiveVersion converts an sdkSpec into the concrete sdkVersion that the module
213// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
214// it returns android.FutureApiLevel(10000).
215func (s sdkSpec) effectiveVersion(ctx android.EarlyModuleContext) (sdkVersion, error) {
216 if !s.valid() {
217 return s.version, fmt.Errorf("invalid sdk version %q", s.raw)
218 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900219 if s.version.isNumbered() {
220 return s.version, nil
221 }
222 return sdkVersion(ctx.Config().DefaultAppTargetSdkInt()), nil
223}
224
225// effectiveVersionString converts an sdkSpec into the concrete version string that the module
226// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
227// it returns the codename (P, Q, R, etc.)
228func (s sdkSpec) effectiveVersionString(ctx android.EarlyModuleContext) (string, error) {
229 ver, err := s.effectiveVersion(ctx)
230 if err == nil && int(ver) == ctx.Config().DefaultAppTargetSdkInt() {
231 return ctx.Config().DefaultAppTargetSdk(), nil
232 }
233 return ver.String(), err
234}
235
Colin Cross17dec172020-05-14 18:05:32 -0700236func (s sdkSpec) defaultJavaLanguageVersion(ctx android.EarlyModuleContext) javaVersion {
237 sdk, err := s.effectiveVersion(ctx)
238 if err != nil {
239 ctx.PropertyErrorf("sdk_version", "%s", err)
240 }
241 if sdk <= 23 {
242 return JAVA_VERSION_7
243 } else if sdk <= 29 {
244 return JAVA_VERSION_8
245 } else {
246 return JAVA_VERSION_9
247 }
248}
249
Jiyong Park6a927c42020-01-21 02:03:43 +0900250func sdkSpecFrom(str string) sdkSpec {
251 switch str {
252 // special cases first
253 case "":
254 return sdkSpec{sdkPrivate, sdkVersionNone, str}
255 case "none":
256 return sdkSpec{sdkNone, sdkVersionNone, str}
257 case "core_platform":
258 return sdkSpec{sdkCorePlatform, sdkVersionNone, str}
259 default:
260 // the syntax is [kind_]version
261 sep := strings.LastIndex(str, "_")
262
263 var kindString string
264 if sep == 0 {
265 return sdkSpec{sdkInvalid, sdkVersionNone, str}
266 } else if sep == -1 {
267 kindString = ""
268 } else {
269 kindString = str[0:sep]
270 }
271 versionString := str[sep+1 : len(str)]
272
273 var kind sdkKind
274 switch kindString {
275 case "":
276 kind = sdkPublic
277 case "core":
278 kind = sdkCore
279 case "system":
280 kind = sdkSystem
281 case "test":
282 kind = sdkTest
Jiyong Park50146e92020-01-30 18:00:15 +0900283 case "module":
284 kind = sdkModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900285 case "system_server":
286 kind = sdkSystemServer
Jiyong Park6a927c42020-01-21 02:03:43 +0900287 default:
288 return sdkSpec{sdkInvalid, sdkVersionNone, str}
289 }
290
291 var version sdkVersion
292 if versionString == "current" {
293 version = sdkVersionCurrent
294 } else if i, err := strconv.Atoi(versionString); err == nil {
295 version = sdkVersion(i)
296 } else {
297 return sdkSpec{sdkInvalid, sdkVersionNone, str}
298 }
299
300 return sdkSpec{kind, version, str}
301 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800302}
303
Jeongik Cha7c708312020-01-28 13:52:36 +0900304func (s sdkSpec) validateSystemSdk(ctx android.EarlyModuleContext) bool {
305 // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module)
306 // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29,
307 // sdk_version of the modules in vendor/product that use system sdk must be either system_28, system_29 or system_current
308 if s.kind != sdkSystem || !s.version.isNumbered() {
309 return true
310 }
311 allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions()
312 if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
313 systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions()
314 if len(systemSdkVersions) > 0 {
315 allowedVersions = systemSdkVersions
316 }
317 }
318 if len(allowedVersions) > 0 && !android.InList(s.version.String(), allowedVersions) {
319 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
320 s.raw, allowedVersions)
321 return false
322 }
323 return true
324}
325
Colin Cross1184b642019-12-30 18:43:07 -0800326func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext sdkContext) sdkDep {
Jiyong Park6a927c42020-01-21 02:03:43 +0900327 sdkVersion := sdkContext.sdkVersion()
328 if !sdkVersion.valid() {
329 ctx.PropertyErrorf("sdk_version", "invalid version %q", sdkVersion.raw)
Colin Crossfb6d7812019-01-09 22:17:55 -0800330 return sdkDep{}
331 }
332
Jeongik Cha7c708312020-01-28 13:52:36 +0900333 if !sdkVersion.validateSystemSdk(ctx) {
334 return sdkDep{}
335 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900336
337 if sdkVersion.usePrebuilt(ctx) {
338 dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), sdkVersion.kind.String())
Colin Crossfb6d7812019-01-09 22:17:55 -0800339 jar := filepath.Join(dir, "android.jar")
340 // There's no aidl for other SDKs yet.
341 // TODO(77525052): Add aidl files for other SDKs too.
Jiyong Park6a927c42020-01-21 02:03:43 +0900342 public_dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), "public")
Colin Crossfb6d7812019-01-09 22:17:55 -0800343 aidl := filepath.Join(public_dir, "framework.aidl")
344 jarPath := android.ExistentPathForSource(ctx, jar)
345 aidlPath := android.ExistentPathForSource(ctx, aidl)
346 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
347
348 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
349 return sdkDep{
350 invalidVersion: true,
Jiyong Park6a927c42020-01-21 02:03:43 +0900351 bootclasspath: []string{fmt.Sprintf("sdk_%s_%s_android", sdkVersion.kind, sdkVersion.version.String())},
Colin Crossfb6d7812019-01-09 22:17:55 -0800352 }
353 }
354
355 if !jarPath.Valid() {
Jiyong Park6a927c42020-01-21 02:03:43 +0900356 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, jar)
Colin Crossfb6d7812019-01-09 22:17:55 -0800357 return sdkDep{}
358 }
359
360 if !aidlPath.Valid() {
Jiyong Park6a927c42020-01-21 02:03:43 +0900361 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, aidl)
Colin Crossfb6d7812019-01-09 22:17:55 -0800362 return sdkDep{}
363 }
364
Colin Cross17dec172020-05-14 18:05:32 -0700365 var systemModules string
366 if sdkVersion.defaultJavaLanguageVersion(ctx).usesJavaModules() {
367 systemModules = "sdk_public_" + sdkVersion.version.String() + "_system_modules"
368 }
369
Colin Crossfb6d7812019-01-09 22:17:55 -0800370 return sdkDep{
Colin Cross17dec172020-05-14 18:05:32 -0700371 useFiles: true,
372 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
373 aidl: android.OptionalPathForPath(aidlPath.Path()),
374 systemModules: systemModules,
Colin Crossfb6d7812019-01-09 22:17:55 -0800375 }
376 }
377
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900378 toModule := func(modules []string, res string, aidl android.Path) sdkDep {
Colin Cross6cef4812019-10-17 14:23:50 -0700379 return sdkDep{
Colin Crossfb6d7812019-01-09 22:17:55 -0800380 useModule: true,
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900381 bootclasspath: append(modules, config.DefaultLambdaStubsLibrary),
Colin Cross6cef4812019-10-17 14:23:50 -0700382 systemModules: "core-current-stubs-system-modules",
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900383 java9Classpath: modules,
384 frameworkResModule: res,
Colin Cross3047fa22019-04-18 10:56:44 -0700385 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800386 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800387 }
388
Jiyong Park6a927c42020-01-21 02:03:43 +0900389 switch sdkVersion.kind {
390 case sdkPrivate:
Colin Crossfb6d7812019-01-09 22:17:55 -0800391 return sdkDep{
Pete Gilline3d44b22020-06-29 11:28:51 +0100392 useModule: true,
Pete Gillin84c38072020-07-09 18:03:41 +0100393 systemModules: corePlatformSystemModules(ctx),
394 bootclasspath: corePlatformBootclasspathLibraries(ctx),
Pete Gilline3d44b22020-06-29 11:28:51 +0100395 classpath: config.FrameworkLibraries,
Colin Crossfb6d7812019-01-09 22:17:55 -0800396 frameworkResModule: "framework-res",
397 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900398 case sdkNone:
Paul Duffine25c6442019-10-11 13:50:28 +0100399 systemModules := sdkContext.systemModules()
400 if systemModules == "" {
401 ctx.PropertyErrorf("sdk_version",
402 `system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`)
403 } else if systemModules == "none" {
Colin Cross6d8d8c62019-10-28 15:10:03 -0700404 return sdkDep{
405 noStandardLibs: true,
406 }
Paul Duffine25c6442019-10-11 13:50:28 +0100407 }
408
Paul Duffin52d398a2019-06-11 12:31:14 +0100409 return sdkDep{
Colin Cross6d8d8c62019-10-28 15:10:03 -0700410 useModule: true,
Paul Duffin52d398a2019-06-11 12:31:14 +0100411 noStandardLibs: true,
Paul Duffine25c6442019-10-11 13:50:28 +0100412 systemModules: systemModules,
Colin Cross6cef4812019-10-17 14:23:50 -0700413 bootclasspath: []string{systemModules},
Paul Duffin52d398a2019-06-11 12:31:14 +0100414 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900415 case sdkCorePlatform:
Paul Duffin50c217c2019-06-12 13:25:22 +0100416 return sdkDep{
Pete Gillin7b0bdce2020-07-01 13:05:32 +0100417 useModule: true,
Pete Gillin84c38072020-07-09 18:03:41 +0100418 systemModules: corePlatformSystemModules(ctx),
419 bootclasspath: corePlatformBootclasspathLibraries(ctx),
Pete Gillin7b0bdce2020-07-01 13:05:32 +0100420 noFrameworksLibs: true,
Paul Duffin50c217c2019-06-12 13:25:22 +0100421 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900422 case sdkPublic:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900423 return toModule([]string{"android_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900424 case sdkSystem:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900425 return toModule([]string{"android_system_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900426 case sdkTest:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900427 return toModule([]string{"android_test_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900428 case sdkCore:
Pete Gillin880f9642020-07-01 13:17:16 +0100429 return sdkDep{
430 useModule: true,
431 bootclasspath: []string{"core.current.stubs", config.DefaultLambdaStubsLibrary},
432 systemModules: "core-current-stubs-system-modules",
433 noFrameworksLibs: true,
434 }
Jiyong Park50146e92020-01-30 18:00:15 +0900435 case sdkModule:
436 // TODO(146757305): provide .apk and .aidl that have more APIs for modules
Anton Hansson3f07ab22020-04-09 13:29:59 +0100437 return toModule([]string{"android_module_lib_stubs_current"}, "framework-res", nonUpdatableFrameworkAidlPath(ctx))
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900438 case sdkSystemServer:
439 // TODO(146757305): provide .apk and .aidl that have more APIs for modules
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000440 return toModule([]string{"android_system_server_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800441 default:
Jiyong Park6a927c42020-01-21 02:03:43 +0900442 panic(fmt.Errorf("invalid sdk %q", sdkVersion.raw))
Colin Crossfb6d7812019-01-09 22:17:55 -0800443 }
444}
Colin Cross98fd5742019-01-09 23:04:25 -0800445
Colin Cross3047fa22019-04-18 10:56:44 -0700446func sdkPreSingletonFactory() android.Singleton {
447 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800448}
449
Colin Cross3047fa22019-04-18 10:56:44 -0700450type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800451
Colin Cross3047fa22019-04-18 10:56:44 -0700452func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800453 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
454 if err != nil {
455 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
456 }
457
458 var sdkVersions []int
459 for _, sdkJar := range sdkJars {
460 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
461 v, err := strconv.Atoi(dir)
462 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
463 continue
464 } else if err != nil {
465 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
466 }
467 sdkVersions = append(sdkVersions, v)
468 }
469
470 sort.Ints(sdkVersions)
471
Colin Cross3047fa22019-04-18 10:56:44 -0700472 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
473}
474
Jiyong Park6a927c42020-01-21 02:03:43 +0900475func LatestSdkVersionInt(ctx android.EarlyModuleContext) int {
476 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
477 latestSdkVersion := 0
478 if len(sdkVersions) > 0 {
479 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
480 }
481 return latestSdkVersion
482}
483
Colin Cross3047fa22019-04-18 10:56:44 -0700484func sdkSingletonFactory() android.Singleton {
485 return sdkSingleton{}
486}
487
488type sdkSingleton struct{}
489
490func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Dan Willemsen9f435972020-05-28 15:28:00 -0700491 if ctx.Config().AlwaysUsePrebuiltSdks() {
Colin Cross3047fa22019-04-18 10:56:44 -0700492 return
493 }
494
Colin Cross10932872019-04-18 14:27:12 -0700495 createSdkFrameworkAidl(ctx)
Anton Hansson3f07ab22020-04-09 13:29:59 +0100496 createNonUpdatableFrameworkAidl(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700497 createAPIFingerprint(ctx)
498}
Colin Cross3047fa22019-04-18 10:56:44 -0700499
Colin Cross10932872019-04-18 14:27:12 -0700500// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
501func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross3047fa22019-04-18 10:56:44 -0700502 stubsModules := []string{
503 "android_stubs_current",
504 "android_test_stubs_current",
505 "android_system_stubs_current",
506 }
507
Anton Hansson3f07ab22020-04-09 13:29:59 +0100508 combinedAidl := sdkFrameworkAidlPath(ctx)
509 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
510
511 rule := createFrameworkAidl(stubsModules, tempPath, ctx)
512
513 commitChangeForRestat(rule, tempPath, combinedAidl)
514
515 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
516}
517
518// Creates a version of framework.aidl for the non-updatable part of the platform.
519func createNonUpdatableFrameworkAidl(ctx android.SingletonContext) {
520 stubsModules := []string{"android_module_lib_stubs_current"}
521
522 combinedAidl := nonUpdatableFrameworkAidlPath(ctx)
523 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
524
525 rule := createFrameworkAidl(stubsModules, tempPath, ctx)
526
527 commitChangeForRestat(rule, tempPath, combinedAidl)
528
529 rule.Build(pctx, ctx, "framework_non_updatable_aidl", "generate framework_non_updatable.aidl")
530}
531
532func createFrameworkAidl(stubsModules []string, path android.OutputPath, ctx android.SingletonContext) *android.RuleBuilder {
Colin Cross3047fa22019-04-18 10:56:44 -0700533 stubsJars := make([]android.Paths, len(stubsModules))
534
535 ctx.VisitAllModules(func(module android.Module) {
536 // Collect dex jar paths for the modules listed above.
537 if j, ok := module.(Dependency); ok {
538 name := ctx.ModuleName(module)
539 if i := android.IndexList(name, stubsModules); i != -1 {
540 stubsJars[i] = j.HeaderJars()
541 }
542 }
543 })
544
545 var missingDeps []string
546
547 for i := range stubsJars {
548 if stubsJars[i] == nil {
549 if ctx.Config().AllowMissingDependencies() {
550 missingDeps = append(missingDeps, stubsModules[i])
551 } else {
Anton Hansson3f07ab22020-04-09 13:29:59 +0100552 ctx.Errorf("failed to find dex jar path for module %q", stubsModules[i])
Colin Cross3047fa22019-04-18 10:56:44 -0700553 }
554 }
555 }
556
557 rule := android.NewRuleBuilder()
558 rule.MissingDeps(missingDeps)
559
560 var aidls android.Paths
561 for _, jars := range stubsJars {
562 for _, jar := range jars {
563 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
564
565 rule.Command().
566 Text("rm -f").Output(aidl)
567 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700568 BuiltTool(ctx, "sdkparcelables").
Colin Cross3047fa22019-04-18 10:56:44 -0700569 Input(jar).
570 Output(aidl)
571
572 aidls = append(aidls, aidl)
573 }
574 }
575
Colin Cross3047fa22019-04-18 10:56:44 -0700576 rule.Command().
Anton Hansson3f07ab22020-04-09 13:29:59 +0100577 Text("rm -f").Output(path)
Colin Cross3047fa22019-04-18 10:56:44 -0700578 rule.Command().
579 Text("cat").
580 Inputs(aidls).
581 Text("| sort -u >").
Anton Hansson3f07ab22020-04-09 13:29:59 +0100582 Output(path)
Colin Cross3047fa22019-04-18 10:56:44 -0700583
Anton Hansson3f07ab22020-04-09 13:29:59 +0100584 return rule
Colin Cross3047fa22019-04-18 10:56:44 -0700585}
586
587func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
588 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
589 return android.PathForOutput(ctx, "framework.aidl")
590 }).(android.OutputPath)
591}
592
Anton Hansson3f07ab22020-04-09 13:29:59 +0100593func nonUpdatableFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
594 return ctx.Config().Once(nonUpdatableFrameworkAidlPathKey, func() interface{} {
595 return android.PathForOutput(ctx, "framework_non_updatable.aidl")
596 }).(android.OutputPath)
597}
598
Colin Cross10932872019-04-18 14:27:12 -0700599// Create api_fingerprint.txt
600func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Park71b519d2019-04-18 17:25:49 +0900601 out := ApiFingerprintPath(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700602
603 rule := android.NewRuleBuilder()
604
605 rule.Command().
606 Text("rm -f").Output(out)
607 cmd := rule.Command()
608
609 if ctx.Config().PlatformSdkCodename() == "REL" {
610 cmd.Text("echo REL >").Output(out)
Jeongik Cha816a23a2020-07-08 01:09:23 +0900611 } else if !ctx.Config().AlwaysUsePrebuiltSdks() {
Colin Cross10932872019-04-18 14:27:12 -0700612 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
613 if err != nil {
614 ctx.Errorf("error globbing API files: %s", err)
615 }
616
617 cmd.Text("cat").
618 Inputs(android.PathsForSource(ctx, in)).
Elliott Hughes34b49d12019-09-06 14:42:24 -0700619 Text("| md5sum | cut -d' ' -f1 >").
Colin Cross10932872019-04-18 14:27:12 -0700620 Output(out)
621 } else {
622 // Unbundled build
623 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
624 cmd.Text("echo").
625 Flag(ctx.Config().PlatformPreviewSdkVersion()).
626 Text(">").
627 Output(out)
628 }
629
630 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
631}
632
Jiyong Park71b519d2019-04-18 17:25:49 +0900633func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross10932872019-04-18 14:27:12 -0700634 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
635 return android.PathForOutput(ctx, "api_fingerprint.txt")
636 }).(android.OutputPath)
637}
638
639func sdkMakeVars(ctx android.MakeVarsContext) {
Dan Willemsen9f435972020-05-28 15:28:00 -0700640 if ctx.Config().AlwaysUsePrebuiltSdks() {
Colin Cross3047fa22019-04-18 10:56:44 -0700641 return
642 }
643
644 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Park71b519d2019-04-18 17:25:49 +0900645 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800646}