blob: d230ccd96248aba8b7fe6b4bff45fb0cc6c860ba [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")
Colin Cross10932872019-04-18 14:27:12 -070038var apiFingerprintPathKey = android.NewOnceKey("apiFingerprintPathKey")
Colin Cross98fd5742019-01-09 23:04:25 -080039
Colin Crossfb6d7812019-01-09 22:17:55 -080040type sdkContext interface {
Jiyong Park6a927c42020-01-21 02:03:43 +090041 // sdkVersion returns sdkSpec that corresponds to the sdk_version property of the current module
42 sdkVersion() sdkSpec
Paul Duffine25c6442019-10-11 13:50:28 +010043 // systemModules returns the system_modules property of the current module, or an empty string if it is not set.
44 systemModules() string
Jiyong Park6a927c42020-01-21 02:03:43 +090045 // 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 Crossfb6d7812019-01-09 22:17:55 -080051}
52
Baligh Uddinf6201372020-01-24 23:15:44 +000053func UseApiFingerprint(ctx android.BaseModuleContext, v string) bool {
54 if v == ctx.Config().PlatformSdkCodename() &&
55 ctx.Config().UnbundledBuild() &&
56 !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
151// valid checks if this sdkSpec is well-formed. Note however that true doesn't mean that the
152// specified SDK actually exists.
153func (s sdkSpec) valid() bool {
154 return s.kind != sdkInvalid
155}
156
157// specified checks if this sdkSpec is well-formed and is not "".
158func (s sdkSpec) specified() bool {
159 return s.valid() && s.kind != sdkPrivate
160}
161
162// prebuiltSdkAvailableForUnbundledBuilt tells whether this sdkSpec can have a prebuilt SDK
163// that can be used for unbundled builds.
164func (s sdkSpec) prebuiltSdkAvailableForUnbundledBuild() bool {
165 // "", "none", and "core_platform" are not available for unbundled build
166 // as we don't/can't have prebuilt stub for the versions
167 return s.kind != sdkPrivate && s.kind != sdkNone && s.kind != sdkCorePlatform
168}
169
170// forPdkBuild converts this sdkSpec into another sdkSpec that is for the PDK builds.
171func (s sdkSpec) forPdkBuild(ctx android.EarlyModuleContext) sdkSpec {
172 // For PDK builds, use the latest SDK version instead of "current" or ""
173 if s.kind == sdkPrivate || s.kind == sdkPublic {
174 kind := s.kind
175 if kind == sdkPrivate {
176 // We don't have prebuilt SDK for private APIs, so use the public SDK
177 // instead. This looks odd, but that's how it has been done.
178 // TODO(b/148271073): investigate the need for this.
179 kind = sdkPublic
Colin Crossfb6d7812019-01-09 22:17:55 -0800180 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900181 version := sdkVersion(LatestSdkVersionInt(ctx))
182 return sdkSpec{kind, version, s.raw}
Colin Crossfb6d7812019-01-09 22:17:55 -0800183 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900184 return s
Colin Crossfb6d7812019-01-09 22:17:55 -0800185}
186
Jiyong Park6a927c42020-01-21 02:03:43 +0900187// usePrebuilt determines whether prebuilt SDK should be used for this sdkSpec with the given context.
188func (s sdkSpec) usePrebuilt(ctx android.EarlyModuleContext) bool {
189 if s.version.isCurrent() {
190 // "current" can be built from source and be from prebuilt SDK
191 return ctx.Config().UnbundledBuildUsePrebuiltSdks()
192 } else if s.version.isNumbered() {
193 // sanity check
194 if s.kind != sdkPublic && s.kind != sdkSystem && s.kind != sdkTest {
195 panic(fmt.Errorf("prebuilt SDK is not not available for sdkKind=%q", s.kind))
196 return false
197 }
198 // numbered SDKs are always from prebuilt
199 return true
Colin Crossfb6d7812019-01-09 22:17:55 -0800200 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900201 // "", "none", "core_platform" fall here
202 return false
203}
204
205// effectiveVersion converts an sdkSpec into the concrete sdkVersion that the module
206// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
207// it returns android.FutureApiLevel(10000).
208func (s sdkSpec) effectiveVersion(ctx android.EarlyModuleContext) (sdkVersion, error) {
209 if !s.valid() {
210 return s.version, fmt.Errorf("invalid sdk version %q", s.raw)
211 }
212 if ctx.Config().IsPdkBuild() {
213 s = s.forPdkBuild(ctx)
214 }
215 if s.version.isNumbered() {
216 return s.version, nil
217 }
218 return sdkVersion(ctx.Config().DefaultAppTargetSdkInt()), nil
219}
220
221// effectiveVersionString converts an sdkSpec into the concrete version string that the module
222// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
223// it returns the codename (P, Q, R, etc.)
224func (s sdkSpec) effectiveVersionString(ctx android.EarlyModuleContext) (string, error) {
225 ver, err := s.effectiveVersion(ctx)
226 if err == nil && int(ver) == ctx.Config().DefaultAppTargetSdkInt() {
227 return ctx.Config().DefaultAppTargetSdk(), nil
228 }
229 return ver.String(), err
230}
231
232func sdkSpecFrom(str string) sdkSpec {
233 switch str {
234 // special cases first
235 case "":
236 return sdkSpec{sdkPrivate, sdkVersionNone, str}
237 case "none":
238 return sdkSpec{sdkNone, sdkVersionNone, str}
239 case "core_platform":
240 return sdkSpec{sdkCorePlatform, sdkVersionNone, str}
241 default:
242 // the syntax is [kind_]version
243 sep := strings.LastIndex(str, "_")
244
245 var kindString string
246 if sep == 0 {
247 return sdkSpec{sdkInvalid, sdkVersionNone, str}
248 } else if sep == -1 {
249 kindString = ""
250 } else {
251 kindString = str[0:sep]
252 }
253 versionString := str[sep+1 : len(str)]
254
255 var kind sdkKind
256 switch kindString {
257 case "":
258 kind = sdkPublic
259 case "core":
260 kind = sdkCore
261 case "system":
262 kind = sdkSystem
263 case "test":
264 kind = sdkTest
Jiyong Park50146e92020-01-30 18:00:15 +0900265 case "module":
266 kind = sdkModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900267 case "system_server":
268 kind = sdkSystemServer
Jiyong Park6a927c42020-01-21 02:03:43 +0900269 default:
270 return sdkSpec{sdkInvalid, sdkVersionNone, str}
271 }
272
273 var version sdkVersion
274 if versionString == "current" {
275 version = sdkVersionCurrent
276 } else if i, err := strconv.Atoi(versionString); err == nil {
277 version = sdkVersion(i)
278 } else {
279 return sdkSpec{sdkInvalid, sdkVersionNone, str}
280 }
281
282 return sdkSpec{kind, version, str}
283 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800284}
285
Jeongik Cha7c708312020-01-28 13:52:36 +0900286func (s sdkSpec) validateSystemSdk(ctx android.EarlyModuleContext) bool {
287 // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module)
288 // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29,
289 // sdk_version of the modules in vendor/product that use system sdk must be either system_28, system_29 or system_current
290 if s.kind != sdkSystem || !s.version.isNumbered() {
291 return true
292 }
293 allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions()
294 if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
295 systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions()
296 if len(systemSdkVersions) > 0 {
297 allowedVersions = systemSdkVersions
298 }
299 }
300 if len(allowedVersions) > 0 && !android.InList(s.version.String(), allowedVersions) {
301 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
302 s.raw, allowedVersions)
303 return false
304 }
305 return true
306}
307
Colin Cross1184b642019-12-30 18:43:07 -0800308func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext sdkContext) sdkDep {
Jiyong Park6a927c42020-01-21 02:03:43 +0900309 sdkVersion := sdkContext.sdkVersion()
310 if !sdkVersion.valid() {
311 ctx.PropertyErrorf("sdk_version", "invalid version %q", sdkVersion.raw)
Colin Crossfb6d7812019-01-09 22:17:55 -0800312 return sdkDep{}
313 }
314
Jiyong Park6a927c42020-01-21 02:03:43 +0900315 if ctx.Config().IsPdkBuild() {
316 sdkVersion = sdkVersion.forPdkBuild(ctx)
317 }
Jeongik Cha7c708312020-01-28 13:52:36 +0900318 if !sdkVersion.validateSystemSdk(ctx) {
319 return sdkDep{}
320 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900321
322 if sdkVersion.usePrebuilt(ctx) {
323 dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), sdkVersion.kind.String())
Colin Crossfb6d7812019-01-09 22:17:55 -0800324 jar := filepath.Join(dir, "android.jar")
325 // There's no aidl for other SDKs yet.
326 // TODO(77525052): Add aidl files for other SDKs too.
Jiyong Park6a927c42020-01-21 02:03:43 +0900327 public_dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), "public")
Colin Crossfb6d7812019-01-09 22:17:55 -0800328 aidl := filepath.Join(public_dir, "framework.aidl")
329 jarPath := android.ExistentPathForSource(ctx, jar)
330 aidlPath := android.ExistentPathForSource(ctx, aidl)
331 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
332
333 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
334 return sdkDep{
335 invalidVersion: true,
Jiyong Park6a927c42020-01-21 02:03:43 +0900336 bootclasspath: []string{fmt.Sprintf("sdk_%s_%s_android", sdkVersion.kind, sdkVersion.version.String())},
Colin Crossfb6d7812019-01-09 22:17:55 -0800337 }
338 }
339
340 if !jarPath.Valid() {
Jiyong Park6a927c42020-01-21 02:03:43 +0900341 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, jar)
Colin Crossfb6d7812019-01-09 22:17:55 -0800342 return sdkDep{}
343 }
344
345 if !aidlPath.Valid() {
Jiyong Park6a927c42020-01-21 02:03:43 +0900346 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, aidl)
Colin Crossfb6d7812019-01-09 22:17:55 -0800347 return sdkDep{}
348 }
349
350 return sdkDep{
351 useFiles: true,
352 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Cross3047fa22019-04-18 10:56:44 -0700353 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800354 }
355 }
356
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900357 toModule := func(modules []string, res string, aidl android.Path) sdkDep {
Colin Cross6cef4812019-10-17 14:23:50 -0700358 return sdkDep{
Colin Crossfb6d7812019-01-09 22:17:55 -0800359 useModule: true,
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900360 bootclasspath: append(modules, config.DefaultLambdaStubsLibrary),
Colin Cross6cef4812019-10-17 14:23:50 -0700361 systemModules: "core-current-stubs-system-modules",
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900362 java9Classpath: modules,
363 frameworkResModule: res,
Colin Cross3047fa22019-04-18 10:56:44 -0700364 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800365 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800366 }
367
Jiyong Park6a927c42020-01-21 02:03:43 +0900368 switch sdkVersion.kind {
369 case sdkPrivate:
Colin Crossfb6d7812019-01-09 22:17:55 -0800370 return sdkDep{
371 useDefaultLibs: true,
372 frameworkResModule: "framework-res",
373 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900374 case sdkNone:
Paul Duffine25c6442019-10-11 13:50:28 +0100375 systemModules := sdkContext.systemModules()
376 if systemModules == "" {
377 ctx.PropertyErrorf("sdk_version",
378 `system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`)
379 } else if systemModules == "none" {
Colin Cross6d8d8c62019-10-28 15:10:03 -0700380 return sdkDep{
381 noStandardLibs: true,
382 }
Paul Duffine25c6442019-10-11 13:50:28 +0100383 }
384
Paul Duffin52d398a2019-06-11 12:31:14 +0100385 return sdkDep{
Colin Cross6d8d8c62019-10-28 15:10:03 -0700386 useModule: true,
Paul Duffin52d398a2019-06-11 12:31:14 +0100387 noStandardLibs: true,
Paul Duffine25c6442019-10-11 13:50:28 +0100388 systemModules: systemModules,
Colin Cross6cef4812019-10-17 14:23:50 -0700389 bootclasspath: []string{systemModules},
Paul Duffin52d398a2019-06-11 12:31:14 +0100390 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900391 case sdkCorePlatform:
Paul Duffin50c217c2019-06-12 13:25:22 +0100392 return sdkDep{
393 useDefaultLibs: true,
394 frameworkResModule: "framework-res",
395 noFrameworksLibs: true,
396 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900397 case sdkPublic:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900398 return toModule([]string{"android_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900399 case sdkSystem:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900400 return toModule([]string{"android_system_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900401 case sdkTest:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900402 return toModule([]string{"android_test_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900403 case sdkCore:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900404 return toModule([]string{"core.current.stubs"}, "", nil)
Jiyong Park50146e92020-01-30 18:00:15 +0900405 case sdkModule:
406 // TODO(146757305): provide .apk and .aidl that have more APIs for modules
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900407 return toModule([]string{"android_module_lib_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
408 case sdkSystemServer:
409 // TODO(146757305): provide .apk and .aidl that have more APIs for modules
410 return toModule([]string{"android_module_lib_stubs_current", "services-stubs"}, "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800411 default:
Jiyong Park6a927c42020-01-21 02:03:43 +0900412 panic(fmt.Errorf("invalid sdk %q", sdkVersion.raw))
Colin Crossfb6d7812019-01-09 22:17:55 -0800413 }
414}
Colin Cross98fd5742019-01-09 23:04:25 -0800415
Colin Cross3047fa22019-04-18 10:56:44 -0700416func sdkPreSingletonFactory() android.Singleton {
417 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800418}
419
Colin Cross3047fa22019-04-18 10:56:44 -0700420type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800421
Colin Cross3047fa22019-04-18 10:56:44 -0700422func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800423 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
424 if err != nil {
425 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
426 }
427
428 var sdkVersions []int
429 for _, sdkJar := range sdkJars {
430 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
431 v, err := strconv.Atoi(dir)
432 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
433 continue
434 } else if err != nil {
435 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
436 }
437 sdkVersions = append(sdkVersions, v)
438 }
439
440 sort.Ints(sdkVersions)
441
Colin Cross3047fa22019-04-18 10:56:44 -0700442 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
443}
444
Jiyong Park6a927c42020-01-21 02:03:43 +0900445func LatestSdkVersionInt(ctx android.EarlyModuleContext) int {
446 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
447 latestSdkVersion := 0
448 if len(sdkVersions) > 0 {
449 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
450 }
451 return latestSdkVersion
452}
453
Colin Cross3047fa22019-04-18 10:56:44 -0700454func sdkSingletonFactory() android.Singleton {
455 return sdkSingleton{}
456}
457
458type sdkSingleton struct{}
459
460func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross10932872019-04-18 14:27:12 -0700461 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700462 return
463 }
464
Colin Cross10932872019-04-18 14:27:12 -0700465 createSdkFrameworkAidl(ctx)
466 createAPIFingerprint(ctx)
467}
Colin Cross3047fa22019-04-18 10:56:44 -0700468
Colin Cross10932872019-04-18 14:27:12 -0700469// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
470func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross3047fa22019-04-18 10:56:44 -0700471 stubsModules := []string{
472 "android_stubs_current",
473 "android_test_stubs_current",
474 "android_system_stubs_current",
475 }
476
477 stubsJars := make([]android.Paths, len(stubsModules))
478
479 ctx.VisitAllModules(func(module android.Module) {
480 // Collect dex jar paths for the modules listed above.
481 if j, ok := module.(Dependency); ok {
482 name := ctx.ModuleName(module)
483 if i := android.IndexList(name, stubsModules); i != -1 {
484 stubsJars[i] = j.HeaderJars()
485 }
486 }
487 })
488
489 var missingDeps []string
490
491 for i := range stubsJars {
492 if stubsJars[i] == nil {
493 if ctx.Config().AllowMissingDependencies() {
494 missingDeps = append(missingDeps, stubsModules[i])
495 } else {
496 ctx.Errorf("failed to find dex jar path for module %q",
497 stubsModules[i])
498 }
499 }
500 }
501
502 rule := android.NewRuleBuilder()
503 rule.MissingDeps(missingDeps)
504
505 var aidls android.Paths
506 for _, jars := range stubsJars {
507 for _, jar := range jars {
508 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
509
510 rule.Command().
511 Text("rm -f").Output(aidl)
512 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700513 BuiltTool(ctx, "sdkparcelables").
Colin Cross3047fa22019-04-18 10:56:44 -0700514 Input(jar).
515 Output(aidl)
516
517 aidls = append(aidls, aidl)
518 }
519 }
520
521 combinedAidl := sdkFrameworkAidlPath(ctx)
522 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
523
524 rule.Command().
525 Text("rm -f").Output(tempPath)
526 rule.Command().
527 Text("cat").
528 Inputs(aidls).
529 Text("| sort -u >").
530 Output(tempPath)
531
532 commitChangeForRestat(rule, tempPath, combinedAidl)
533
534 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
535}
536
537func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
538 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
539 return android.PathForOutput(ctx, "framework.aidl")
540 }).(android.OutputPath)
541}
542
Colin Cross10932872019-04-18 14:27:12 -0700543// Create api_fingerprint.txt
544func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Park71b519d2019-04-18 17:25:49 +0900545 out := ApiFingerprintPath(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700546
547 rule := android.NewRuleBuilder()
548
549 rule.Command().
550 Text("rm -f").Output(out)
551 cmd := rule.Command()
552
553 if ctx.Config().PlatformSdkCodename() == "REL" {
554 cmd.Text("echo REL >").Output(out)
555 } else if ctx.Config().IsPdkBuild() {
556 // TODO: get this from the PDK artifacts?
557 cmd.Text("echo PDK >").Output(out)
558 } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
559 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
560 if err != nil {
561 ctx.Errorf("error globbing API files: %s", err)
562 }
563
564 cmd.Text("cat").
565 Inputs(android.PathsForSource(ctx, in)).
Elliott Hughes34b49d12019-09-06 14:42:24 -0700566 Text("| md5sum | cut -d' ' -f1 >").
Colin Cross10932872019-04-18 14:27:12 -0700567 Output(out)
568 } else {
569 // Unbundled build
570 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
571 cmd.Text("echo").
572 Flag(ctx.Config().PlatformPreviewSdkVersion()).
573 Text(">").
574 Output(out)
575 }
576
577 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
578}
579
Jiyong Park71b519d2019-04-18 17:25:49 +0900580func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross10932872019-04-18 14:27:12 -0700581 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
582 return android.PathForOutput(ctx, "api_fingerprint.txt")
583 }).(android.OutputPath)
584}
585
586func sdkMakeVars(ctx android.MakeVarsContext) {
587 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700588 return
589 }
590
591 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Park71b519d2019-04-18 17:25:49 +0900592 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800593}