blob: 0e132d399531229e9314dd6a4a749e1c5774c138 [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
Nikita Ioffe1f4f3452020-03-02 16:58:11 +000053func UseApiFingerprint(ctx android.BaseModuleContext) bool {
54 if ctx.Config().UnbundledBuild() &&
Baligh Uddinf6201372020-01-24 23:15:44 +000055 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
56 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
57 return true
58 }
59 return false
60}
61
Jiyong Park6a927c42020-01-21 02:03:43 +090062// sdkKind represents a particular category of an SDK spec like public, system, test, etc.
63type sdkKind int
64
65const (
66 sdkInvalid sdkKind = iota
67 sdkNone
68 sdkCore
69 sdkCorePlatform
70 sdkPublic
71 sdkSystem
72 sdkTest
Jiyong Park50146e92020-01-30 18:00:15 +090073 sdkModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +090074 sdkSystemServer
Jiyong Park6a927c42020-01-21 02:03:43 +090075 sdkPrivate
76)
77
78// String returns the string representation of this sdkKind
79func (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 Park50146e92020-01-30 18:00:15 +090095 case sdkModule:
96 return "module"
Jiyong Parkaae9bd12020-02-12 04:36:43 +090097 case sdkSystemServer:
98 return "system_server"
Colin Crossfb6d7812019-01-09 22:17:55 -080099 default:
Jiyong Park6a927c42020-01-21 02:03:43 +0900100 return "invalid"
Colin Crossfb6d7812019-01-09 22:17:55 -0800101 }
102}
103
Jiyong Park6a927c42020-01-21 02:03:43 +0900104// sdkVersion represents a specific version number of an SDK spec of a particular kind
105type sdkVersion int
106
107const (
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
116func (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
121func (v sdkVersion) isNumbered() bool {
122 return !v.isCurrent() && v != sdkVersionNone
123}
124
125// String returns the string representation of this sdkVersion.
126func (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.
139func (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
144type sdkSpec struct {
145 kind sdkKind
146 version sdkVersion
147 raw string
148}
149
150// valid checks if this sdkSpec is well-formed. Note however that true doesn't mean that the
151// specified SDK actually exists.
152func (s sdkSpec) valid() bool {
153 return s.kind != sdkInvalid
154}
155
156// specified checks if this sdkSpec is well-formed and is not "".
157func (s sdkSpec) specified() bool {
158 return s.valid() && s.kind != sdkPrivate
159}
160
161// prebuiltSdkAvailableForUnbundledBuilt tells whether this sdkSpec can have a prebuilt SDK
162// that can be used for unbundled builds.
163func (s sdkSpec) prebuiltSdkAvailableForUnbundledBuild() bool {
164 // "", "none", and "core_platform" are not available for unbundled build
165 // as we don't/can't have prebuilt stub for the versions
166 return s.kind != sdkPrivate && s.kind != sdkNone && s.kind != sdkCorePlatform
167}
168
169// forPdkBuild converts this sdkSpec into another sdkSpec that is for the PDK builds.
170func (s sdkSpec) forPdkBuild(ctx android.EarlyModuleContext) sdkSpec {
171 // For PDK builds, use the latest SDK version instead of "current" or ""
172 if s.kind == sdkPrivate || s.kind == sdkPublic {
173 kind := s.kind
174 if kind == sdkPrivate {
175 // We don't have prebuilt SDK for private APIs, so use the public SDK
176 // instead. This looks odd, but that's how it has been done.
177 // TODO(b/148271073): investigate the need for this.
178 kind = sdkPublic
Colin Crossfb6d7812019-01-09 22:17:55 -0800179 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900180 version := sdkVersion(LatestSdkVersionInt(ctx))
181 return sdkSpec{kind, version, s.raw}
Colin Crossfb6d7812019-01-09 22:17:55 -0800182 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900183 return s
Colin Crossfb6d7812019-01-09 22:17:55 -0800184}
185
Jiyong Park6a927c42020-01-21 02:03:43 +0900186// usePrebuilt determines whether prebuilt SDK should be used for this sdkSpec with the given context.
187func (s sdkSpec) usePrebuilt(ctx android.EarlyModuleContext) bool {
188 if s.version.isCurrent() {
189 // "current" can be built from source and be from prebuilt SDK
190 return ctx.Config().UnbundledBuildUsePrebuiltSdks()
191 } else if s.version.isNumbered() {
192 // sanity check
193 if s.kind != sdkPublic && s.kind != sdkSystem && s.kind != sdkTest {
194 panic(fmt.Errorf("prebuilt SDK is not not available for sdkKind=%q", s.kind))
195 return false
196 }
197 // numbered SDKs are always from prebuilt
198 return true
Colin Crossfb6d7812019-01-09 22:17:55 -0800199 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900200 // "", "none", "core_platform" fall here
201 return false
202}
203
204// effectiveVersion converts an sdkSpec into the concrete sdkVersion that the module
205// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
206// it returns android.FutureApiLevel(10000).
207func (s sdkSpec) effectiveVersion(ctx android.EarlyModuleContext) (sdkVersion, error) {
208 if !s.valid() {
209 return s.version, fmt.Errorf("invalid sdk version %q", s.raw)
210 }
211 if ctx.Config().IsPdkBuild() {
212 s = s.forPdkBuild(ctx)
213 }
214 if s.version.isNumbered() {
215 return s.version, nil
216 }
217 return sdkVersion(ctx.Config().DefaultAppTargetSdkInt()), nil
218}
219
220// effectiveVersionString converts an sdkSpec into the concrete version string that the module
221// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
222// it returns the codename (P, Q, R, etc.)
223func (s sdkSpec) effectiveVersionString(ctx android.EarlyModuleContext) (string, error) {
224 ver, err := s.effectiveVersion(ctx)
225 if err == nil && int(ver) == ctx.Config().DefaultAppTargetSdkInt() {
226 return ctx.Config().DefaultAppTargetSdk(), nil
227 }
228 return ver.String(), err
229}
230
231func sdkSpecFrom(str string) sdkSpec {
232 switch str {
233 // special cases first
234 case "":
235 return sdkSpec{sdkPrivate, sdkVersionNone, str}
236 case "none":
237 return sdkSpec{sdkNone, sdkVersionNone, str}
238 case "core_platform":
239 return sdkSpec{sdkCorePlatform, sdkVersionNone, str}
240 default:
241 // the syntax is [kind_]version
242 sep := strings.LastIndex(str, "_")
243
244 var kindString string
245 if sep == 0 {
246 return sdkSpec{sdkInvalid, sdkVersionNone, str}
247 } else if sep == -1 {
248 kindString = ""
249 } else {
250 kindString = str[0:sep]
251 }
252 versionString := str[sep+1 : len(str)]
253
254 var kind sdkKind
255 switch kindString {
256 case "":
257 kind = sdkPublic
258 case "core":
259 kind = sdkCore
260 case "system":
261 kind = sdkSystem
262 case "test":
263 kind = sdkTest
Jiyong Park50146e92020-01-30 18:00:15 +0900264 case "module":
265 kind = sdkModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900266 case "system_server":
267 kind = sdkSystemServer
Jiyong Park6a927c42020-01-21 02:03:43 +0900268 default:
269 return sdkSpec{sdkInvalid, sdkVersionNone, str}
270 }
271
272 var version sdkVersion
273 if versionString == "current" {
274 version = sdkVersionCurrent
275 } else if i, err := strconv.Atoi(versionString); err == nil {
276 version = sdkVersion(i)
277 } else {
278 return sdkSpec{sdkInvalid, sdkVersionNone, str}
279 }
280
281 return sdkSpec{kind, version, str}
282 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800283}
284
Jeongik Cha7c708312020-01-28 13:52:36 +0900285func (s sdkSpec) validateSystemSdk(ctx android.EarlyModuleContext) bool {
286 // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module)
287 // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29,
288 // sdk_version of the modules in vendor/product that use system sdk must be either system_28, system_29 or system_current
289 if s.kind != sdkSystem || !s.version.isNumbered() {
290 return true
291 }
292 allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions()
293 if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
294 systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions()
295 if len(systemSdkVersions) > 0 {
296 allowedVersions = systemSdkVersions
297 }
298 }
299 if len(allowedVersions) > 0 && !android.InList(s.version.String(), allowedVersions) {
300 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
301 s.raw, allowedVersions)
302 return false
303 }
304 return true
305}
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 }
Jeongik Cha7c708312020-01-28 13:52:36 +0900317 if !sdkVersion.validateSystemSdk(ctx) {
318 return sdkDep{}
319 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900320
321 if sdkVersion.usePrebuilt(ctx) {
322 dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), sdkVersion.kind.String())
Colin Crossfb6d7812019-01-09 22:17:55 -0800323 jar := filepath.Join(dir, "android.jar")
324 // There's no aidl for other SDKs yet.
325 // TODO(77525052): Add aidl files for other SDKs too.
Jiyong Park6a927c42020-01-21 02:03:43 +0900326 public_dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), "public")
Colin Crossfb6d7812019-01-09 22:17:55 -0800327 aidl := filepath.Join(public_dir, "framework.aidl")
328 jarPath := android.ExistentPathForSource(ctx, jar)
329 aidlPath := android.ExistentPathForSource(ctx, aidl)
330 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
331
332 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
333 return sdkDep{
334 invalidVersion: true,
Jiyong Park6a927c42020-01-21 02:03:43 +0900335 bootclasspath: []string{fmt.Sprintf("sdk_%s_%s_android", sdkVersion.kind, sdkVersion.version.String())},
Colin Crossfb6d7812019-01-09 22:17:55 -0800336 }
337 }
338
339 if !jarPath.Valid() {
Jiyong Park6a927c42020-01-21 02:03:43 +0900340 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, jar)
Colin Crossfb6d7812019-01-09 22:17:55 -0800341 return sdkDep{}
342 }
343
344 if !aidlPath.Valid() {
Jiyong Park6a927c42020-01-21 02:03:43 +0900345 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, aidl)
Colin Crossfb6d7812019-01-09 22:17:55 -0800346 return sdkDep{}
347 }
348
349 return sdkDep{
350 useFiles: true,
351 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Cross3047fa22019-04-18 10:56:44 -0700352 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800353 }
354 }
355
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900356 toModule := func(modules []string, res string, aidl android.Path) sdkDep {
Colin Cross6cef4812019-10-17 14:23:50 -0700357 return sdkDep{
Colin Crossfb6d7812019-01-09 22:17:55 -0800358 useModule: true,
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900359 bootclasspath: append(modules, config.DefaultLambdaStubsLibrary),
Colin Cross6cef4812019-10-17 14:23:50 -0700360 systemModules: "core-current-stubs-system-modules",
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900361 java9Classpath: modules,
362 frameworkResModule: res,
Colin Cross3047fa22019-04-18 10:56:44 -0700363 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800364 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800365 }
366
Jiyong Park6a927c42020-01-21 02:03:43 +0900367 switch sdkVersion.kind {
368 case sdkPrivate:
Colin Crossfb6d7812019-01-09 22:17:55 -0800369 return sdkDep{
370 useDefaultLibs: true,
371 frameworkResModule: "framework-res",
372 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900373 case sdkNone:
Paul Duffine25c6442019-10-11 13:50:28 +0100374 systemModules := sdkContext.systemModules()
375 if systemModules == "" {
376 ctx.PropertyErrorf("sdk_version",
377 `system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`)
378 } else if systemModules == "none" {
Colin Cross6d8d8c62019-10-28 15:10:03 -0700379 return sdkDep{
380 noStandardLibs: true,
381 }
Paul Duffine25c6442019-10-11 13:50:28 +0100382 }
383
Paul Duffin52d398a2019-06-11 12:31:14 +0100384 return sdkDep{
Colin Cross6d8d8c62019-10-28 15:10:03 -0700385 useModule: true,
Paul Duffin52d398a2019-06-11 12:31:14 +0100386 noStandardLibs: true,
Paul Duffine25c6442019-10-11 13:50:28 +0100387 systemModules: systemModules,
Colin Cross6cef4812019-10-17 14:23:50 -0700388 bootclasspath: []string{systemModules},
Paul Duffin52d398a2019-06-11 12:31:14 +0100389 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900390 case sdkCorePlatform:
Paul Duffin50c217c2019-06-12 13:25:22 +0100391 return sdkDep{
392 useDefaultLibs: true,
393 frameworkResModule: "framework-res",
394 noFrameworksLibs: true,
395 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900396 case sdkPublic:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900397 return toModule([]string{"android_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900398 case sdkSystem:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900399 return toModule([]string{"android_system_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900400 case sdkTest:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900401 return toModule([]string{"android_test_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900402 case sdkCore:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900403 return toModule([]string{"core.current.stubs"}, "", nil)
Jiyong Park50146e92020-01-30 18:00:15 +0900404 case sdkModule:
405 // TODO(146757305): provide .apk and .aidl that have more APIs for modules
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900406 return toModule([]string{"android_module_lib_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
407 case sdkSystemServer:
408 // TODO(146757305): provide .apk and .aidl that have more APIs for modules
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000409 return toModule([]string{"android_system_server_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800410 default:
Jiyong Park6a927c42020-01-21 02:03:43 +0900411 panic(fmt.Errorf("invalid sdk %q", sdkVersion.raw))
Colin Crossfb6d7812019-01-09 22:17:55 -0800412 }
413}
Colin Cross98fd5742019-01-09 23:04:25 -0800414
Colin Cross3047fa22019-04-18 10:56:44 -0700415func sdkPreSingletonFactory() android.Singleton {
416 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800417}
418
Colin Cross3047fa22019-04-18 10:56:44 -0700419type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800420
Colin Cross3047fa22019-04-18 10:56:44 -0700421func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800422 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
423 if err != nil {
424 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
425 }
426
427 var sdkVersions []int
428 for _, sdkJar := range sdkJars {
429 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
430 v, err := strconv.Atoi(dir)
431 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
432 continue
433 } else if err != nil {
434 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
435 }
436 sdkVersions = append(sdkVersions, v)
437 }
438
439 sort.Ints(sdkVersions)
440
Colin Cross3047fa22019-04-18 10:56:44 -0700441 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
442}
443
Jiyong Park6a927c42020-01-21 02:03:43 +0900444func LatestSdkVersionInt(ctx android.EarlyModuleContext) int {
445 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
446 latestSdkVersion := 0
447 if len(sdkVersions) > 0 {
448 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
449 }
450 return latestSdkVersion
451}
452
Colin Cross3047fa22019-04-18 10:56:44 -0700453func sdkSingletonFactory() android.Singleton {
454 return sdkSingleton{}
455}
456
457type sdkSingleton struct{}
458
459func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross10932872019-04-18 14:27:12 -0700460 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700461 return
462 }
463
Colin Cross10932872019-04-18 14:27:12 -0700464 createSdkFrameworkAidl(ctx)
465 createAPIFingerprint(ctx)
466}
Colin Cross3047fa22019-04-18 10:56:44 -0700467
Colin Cross10932872019-04-18 14:27:12 -0700468// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
469func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross3047fa22019-04-18 10:56:44 -0700470 stubsModules := []string{
471 "android_stubs_current",
472 "android_test_stubs_current",
473 "android_system_stubs_current",
474 }
475
476 stubsJars := make([]android.Paths, len(stubsModules))
477
478 ctx.VisitAllModules(func(module android.Module) {
479 // Collect dex jar paths for the modules listed above.
480 if j, ok := module.(Dependency); ok {
481 name := ctx.ModuleName(module)
482 if i := android.IndexList(name, stubsModules); i != -1 {
483 stubsJars[i] = j.HeaderJars()
484 }
485 }
486 })
487
488 var missingDeps []string
489
490 for i := range stubsJars {
491 if stubsJars[i] == nil {
492 if ctx.Config().AllowMissingDependencies() {
493 missingDeps = append(missingDeps, stubsModules[i])
494 } else {
495 ctx.Errorf("failed to find dex jar path for module %q",
496 stubsModules[i])
497 }
498 }
499 }
500
501 rule := android.NewRuleBuilder()
502 rule.MissingDeps(missingDeps)
503
504 var aidls android.Paths
505 for _, jars := range stubsJars {
506 for _, jar := range jars {
507 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
508
509 rule.Command().
510 Text("rm -f").Output(aidl)
511 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700512 BuiltTool(ctx, "sdkparcelables").
Colin Cross3047fa22019-04-18 10:56:44 -0700513 Input(jar).
514 Output(aidl)
515
516 aidls = append(aidls, aidl)
517 }
518 }
519
520 combinedAidl := sdkFrameworkAidlPath(ctx)
521 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
522
523 rule.Command().
524 Text("rm -f").Output(tempPath)
525 rule.Command().
526 Text("cat").
527 Inputs(aidls).
528 Text("| sort -u >").
529 Output(tempPath)
530
531 commitChangeForRestat(rule, tempPath, combinedAidl)
532
533 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
534}
535
536func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
537 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
538 return android.PathForOutput(ctx, "framework.aidl")
539 }).(android.OutputPath)
540}
541
Colin Cross10932872019-04-18 14:27:12 -0700542// Create api_fingerprint.txt
543func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Park71b519d2019-04-18 17:25:49 +0900544 out := ApiFingerprintPath(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700545
546 rule := android.NewRuleBuilder()
547
548 rule.Command().
549 Text("rm -f").Output(out)
550 cmd := rule.Command()
551
552 if ctx.Config().PlatformSdkCodename() == "REL" {
553 cmd.Text("echo REL >").Output(out)
554 } else if ctx.Config().IsPdkBuild() {
555 // TODO: get this from the PDK artifacts?
556 cmd.Text("echo PDK >").Output(out)
557 } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
558 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
559 if err != nil {
560 ctx.Errorf("error globbing API files: %s", err)
561 }
562
563 cmd.Text("cat").
564 Inputs(android.PathsForSource(ctx, in)).
Elliott Hughes34b49d12019-09-06 14:42:24 -0700565 Text("| md5sum | cut -d' ' -f1 >").
Colin Cross10932872019-04-18 14:27:12 -0700566 Output(out)
567 } else {
568 // Unbundled build
569 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
570 cmd.Text("echo").
571 Flag(ctx.Config().PlatformPreviewSdkVersion()).
572 Text(">").
573 Output(out)
574 }
575
576 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
577}
578
Jiyong Park71b519d2019-04-18 17:25:49 +0900579func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross10932872019-04-18 14:27:12 -0700580 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
581 return android.PathForOutput(ctx, "api_fingerprint.txt")
582 }).(android.OutputPath)
583}
584
585func sdkMakeVars(ctx android.MakeVarsContext) {
586 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700587 return
588 }
589
590 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Park71b519d2019-04-18 17:25:49 +0900591 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800592}