blob: 1c047a353ecbc8bddfa6a74140be904d3b5049fa [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 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"
Colin Crossfb6d7812019-01-09 22:17:55 -080097 default:
Jiyong Park6a927c42020-01-21 02:03:43 +090098 return "invalid"
Colin Crossfb6d7812019-01-09 22:17:55 -080099 }
100}
101
Jiyong Park6a927c42020-01-21 02:03:43 +0900102// sdkVersion represents a specific version number of an SDK spec of a particular kind
103type sdkVersion int
104
105const (
106 // special version number for a not-yet-frozen SDK
107 sdkVersionCurrent sdkVersion = sdkVersion(android.FutureApiLevel)
108 // special version number to be used for SDK specs where version number doesn't
109 // make sense, e.g. "none", "", etc.
110 sdkVersionNone sdkVersion = sdkVersion(0)
111)
112
113// isCurrent checks if the sdkVersion refers to the not-yet-published version of an sdkKind
114func (v sdkVersion) isCurrent() bool {
115 return v == sdkVersionCurrent
116}
117
118// isNumbered checks if the sdkVersion refers to the published (a.k.a numbered) version of an sdkKind
119func (v sdkVersion) isNumbered() bool {
120 return !v.isCurrent() && v != sdkVersionNone
121}
122
123// String returns the string representation of this sdkVersion.
124func (v sdkVersion) String() string {
125 if v.isCurrent() {
126 return "current"
127 } else if v.isNumbered() {
128 return strconv.Itoa(int(v))
129 }
130 return "(no version)"
131}
132
133// asNumberString directly converts the numeric value of this sdk version as a string.
134// When isNumbered() is true, this method is the same as String(). However, for sdkVersionCurrent
135// and sdkVersionNone, this returns 10000 and 0 while String() returns "current" and "(no version"),
136// respectively.
137func (v sdkVersion) asNumberString() string {
138 return strconv.Itoa(int(v))
139}
140
141// sdkSpec represents the kind and the version of an SDK for a module to build against
142type sdkSpec struct {
143 kind sdkKind
144 version sdkVersion
145 raw string
146}
147
148// valid checks if this sdkSpec is well-formed. Note however that true doesn't mean that the
149// specified SDK actually exists.
150func (s sdkSpec) valid() bool {
151 return s.kind != sdkInvalid
152}
153
154// specified checks if this sdkSpec is well-formed and is not "".
155func (s sdkSpec) specified() bool {
156 return s.valid() && s.kind != sdkPrivate
157}
158
159// prebuiltSdkAvailableForUnbundledBuilt tells whether this sdkSpec can have a prebuilt SDK
160// that can be used for unbundled builds.
161func (s sdkSpec) prebuiltSdkAvailableForUnbundledBuild() bool {
162 // "", "none", and "core_platform" are not available for unbundled build
163 // as we don't/can't have prebuilt stub for the versions
164 return s.kind != sdkPrivate && s.kind != sdkNone && s.kind != sdkCorePlatform
165}
166
167// forPdkBuild converts this sdkSpec into another sdkSpec that is for the PDK builds.
168func (s sdkSpec) forPdkBuild(ctx android.EarlyModuleContext) sdkSpec {
169 // For PDK builds, use the latest SDK version instead of "current" or ""
170 if s.kind == sdkPrivate || s.kind == sdkPublic {
171 kind := s.kind
172 if kind == sdkPrivate {
173 // We don't have prebuilt SDK for private APIs, so use the public SDK
174 // instead. This looks odd, but that's how it has been done.
175 // TODO(b/148271073): investigate the need for this.
176 kind = sdkPublic
Colin Crossfb6d7812019-01-09 22:17:55 -0800177 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900178 version := sdkVersion(LatestSdkVersionInt(ctx))
179 return sdkSpec{kind, version, s.raw}
Colin Crossfb6d7812019-01-09 22:17:55 -0800180 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900181 return s
Colin Crossfb6d7812019-01-09 22:17:55 -0800182}
183
Jiyong Park6a927c42020-01-21 02:03:43 +0900184// usePrebuilt determines whether prebuilt SDK should be used for this sdkSpec with the given context.
185func (s sdkSpec) usePrebuilt(ctx android.EarlyModuleContext) bool {
186 if s.version.isCurrent() {
187 // "current" can be built from source and be from prebuilt SDK
188 return ctx.Config().UnbundledBuildUsePrebuiltSdks()
189 } else if s.version.isNumbered() {
190 // sanity check
191 if s.kind != sdkPublic && s.kind != sdkSystem && s.kind != sdkTest {
192 panic(fmt.Errorf("prebuilt SDK is not not available for sdkKind=%q", s.kind))
193 return false
194 }
195 // numbered SDKs are always from prebuilt
196 return true
Colin Crossfb6d7812019-01-09 22:17:55 -0800197 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900198 // "", "none", "core_platform" fall here
199 return false
200}
201
202// effectiveVersion converts an sdkSpec into the concrete sdkVersion that the module
203// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
204// it returns android.FutureApiLevel(10000).
205func (s sdkSpec) effectiveVersion(ctx android.EarlyModuleContext) (sdkVersion, error) {
206 if !s.valid() {
207 return s.version, fmt.Errorf("invalid sdk version %q", s.raw)
208 }
209 if ctx.Config().IsPdkBuild() {
210 s = s.forPdkBuild(ctx)
211 }
212 if s.version.isNumbered() {
213 return s.version, nil
214 }
215 return sdkVersion(ctx.Config().DefaultAppTargetSdkInt()), nil
216}
217
218// effectiveVersionString converts an sdkSpec into the concrete version string that the module
219// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
220// it returns the codename (P, Q, R, etc.)
221func (s sdkSpec) effectiveVersionString(ctx android.EarlyModuleContext) (string, error) {
222 ver, err := s.effectiveVersion(ctx)
223 if err == nil && int(ver) == ctx.Config().DefaultAppTargetSdkInt() {
224 return ctx.Config().DefaultAppTargetSdk(), nil
225 }
226 return ver.String(), err
227}
228
229func sdkSpecFrom(str string) sdkSpec {
230 switch str {
231 // special cases first
232 case "":
233 return sdkSpec{sdkPrivate, sdkVersionNone, str}
234 case "none":
235 return sdkSpec{sdkNone, sdkVersionNone, str}
236 case "core_platform":
237 return sdkSpec{sdkCorePlatform, sdkVersionNone, str}
238 default:
239 // the syntax is [kind_]version
240 sep := strings.LastIndex(str, "_")
241
242 var kindString string
243 if sep == 0 {
244 return sdkSpec{sdkInvalid, sdkVersionNone, str}
245 } else if sep == -1 {
246 kindString = ""
247 } else {
248 kindString = str[0:sep]
249 }
250 versionString := str[sep+1 : len(str)]
251
252 var kind sdkKind
253 switch kindString {
254 case "":
255 kind = sdkPublic
256 case "core":
257 kind = sdkCore
258 case "system":
259 kind = sdkSystem
260 case "test":
261 kind = sdkTest
Jiyong Park50146e92020-01-30 18:00:15 +0900262 case "module":
263 kind = sdkModule
Jiyong Park6a927c42020-01-21 02:03:43 +0900264 default:
265 return sdkSpec{sdkInvalid, sdkVersionNone, str}
266 }
267
268 var version sdkVersion
269 if versionString == "current" {
270 version = sdkVersionCurrent
271 } else if i, err := strconv.Atoi(versionString); err == nil {
272 version = sdkVersion(i)
273 } else {
274 return sdkSpec{sdkInvalid, sdkVersionNone, str}
275 }
276
277 return sdkSpec{kind, version, str}
278 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800279}
280
Colin Cross1184b642019-12-30 18:43:07 -0800281func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext sdkContext) sdkDep {
Jiyong Park6a927c42020-01-21 02:03:43 +0900282 sdkVersion := sdkContext.sdkVersion()
283 if !sdkVersion.valid() {
284 ctx.PropertyErrorf("sdk_version", "invalid version %q", sdkVersion.raw)
Colin Crossfb6d7812019-01-09 22:17:55 -0800285 return sdkDep{}
286 }
287
Jiyong Park6a927c42020-01-21 02:03:43 +0900288 if ctx.Config().IsPdkBuild() {
289 sdkVersion = sdkVersion.forPdkBuild(ctx)
290 }
291
292 if sdkVersion.usePrebuilt(ctx) {
293 dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), sdkVersion.kind.String())
Colin Crossfb6d7812019-01-09 22:17:55 -0800294 jar := filepath.Join(dir, "android.jar")
295 // There's no aidl for other SDKs yet.
296 // TODO(77525052): Add aidl files for other SDKs too.
Jiyong Park6a927c42020-01-21 02:03:43 +0900297 public_dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), "public")
Colin Crossfb6d7812019-01-09 22:17:55 -0800298 aidl := filepath.Join(public_dir, "framework.aidl")
299 jarPath := android.ExistentPathForSource(ctx, jar)
300 aidlPath := android.ExistentPathForSource(ctx, aidl)
301 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
302
303 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
304 return sdkDep{
305 invalidVersion: true,
Jiyong Park6a927c42020-01-21 02:03:43 +0900306 bootclasspath: []string{fmt.Sprintf("sdk_%s_%s_android", sdkVersion.kind, sdkVersion.version.String())},
Colin Crossfb6d7812019-01-09 22:17:55 -0800307 }
308 }
309
310 if !jarPath.Valid() {
Jiyong Park6a927c42020-01-21 02:03:43 +0900311 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, jar)
Colin Crossfb6d7812019-01-09 22:17:55 -0800312 return sdkDep{}
313 }
314
315 if !aidlPath.Valid() {
Jiyong Park6a927c42020-01-21 02:03:43 +0900316 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, aidl)
Colin Crossfb6d7812019-01-09 22:17:55 -0800317 return sdkDep{}
318 }
319
320 return sdkDep{
321 useFiles: true,
322 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Cross3047fa22019-04-18 10:56:44 -0700323 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800324 }
325 }
326
Colin Cross3047fa22019-04-18 10:56:44 -0700327 toModule := func(m, r string, aidl android.Path) sdkDep {
Colin Cross6cef4812019-10-17 14:23:50 -0700328 return sdkDep{
Colin Crossfb6d7812019-01-09 22:17:55 -0800329 useModule: true,
Colin Cross6cef4812019-10-17 14:23:50 -0700330 bootclasspath: []string{m, config.DefaultLambdaStubsLibrary},
331 systemModules: "core-current-stubs-system-modules",
332 java9Classpath: []string{m},
Colin Crossfb6d7812019-01-09 22:17:55 -0800333 frameworkResModule: r,
Colin Cross3047fa22019-04-18 10:56:44 -0700334 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800335 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800336 }
337
Colin Cross98fd5742019-01-09 23:04:25 -0800338 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
339 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
Jiyong Park6a927c42020-01-21 02:03:43 +0900340 if sdkVersion.kind == sdkSystem && sdkVersion.version.isNumbered() {
Colin Cross98fd5742019-01-09 23:04:25 -0800341 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
342 if ctx.DeviceSpecific() || ctx.SocSpecific() {
343 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
344 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
345 }
346 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900347 if len(allowed_versions) > 0 && !android.InList(sdkVersion.version.String(), allowed_versions) {
Colin Cross98fd5742019-01-09 23:04:25 -0800348 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
Jiyong Park6a927c42020-01-21 02:03:43 +0900349 sdkVersion.raw, allowed_versions)
Colin Cross98fd5742019-01-09 23:04:25 -0800350 }
351 }
352
Jiyong Park6a927c42020-01-21 02:03:43 +0900353 switch sdkVersion.kind {
354 case sdkPrivate:
Colin Crossfb6d7812019-01-09 22:17:55 -0800355 return sdkDep{
356 useDefaultLibs: true,
357 frameworkResModule: "framework-res",
358 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900359 case sdkNone:
Paul Duffine25c6442019-10-11 13:50:28 +0100360 systemModules := sdkContext.systemModules()
361 if systemModules == "" {
362 ctx.PropertyErrorf("sdk_version",
363 `system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`)
364 } else if systemModules == "none" {
Colin Cross6d8d8c62019-10-28 15:10:03 -0700365 return sdkDep{
366 noStandardLibs: true,
367 }
Paul Duffine25c6442019-10-11 13:50:28 +0100368 }
369
Paul Duffin52d398a2019-06-11 12:31:14 +0100370 return sdkDep{
Colin Cross6d8d8c62019-10-28 15:10:03 -0700371 useModule: true,
Paul Duffin52d398a2019-06-11 12:31:14 +0100372 noStandardLibs: true,
Paul Duffine25c6442019-10-11 13:50:28 +0100373 systemModules: systemModules,
Colin Cross6cef4812019-10-17 14:23:50 -0700374 bootclasspath: []string{systemModules},
Paul Duffin52d398a2019-06-11 12:31:14 +0100375 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900376 case sdkCorePlatform:
Paul Duffin50c217c2019-06-12 13:25:22 +0100377 return sdkDep{
378 useDefaultLibs: true,
379 frameworkResModule: "framework-res",
380 noFrameworksLibs: true,
381 }
Jiyong Park6a927c42020-01-21 02:03:43 +0900382 case sdkPublic:
Colin Cross3047fa22019-04-18 10:56:44 -0700383 return toModule("android_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900384 case sdkSystem:
Colin Cross3047fa22019-04-18 10:56:44 -0700385 return toModule("android_system_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900386 case sdkTest:
Colin Cross3047fa22019-04-18 10:56:44 -0700387 return toModule("android_test_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Jiyong Park6a927c42020-01-21 02:03:43 +0900388 case sdkCore:
Colin Cross3047fa22019-04-18 10:56:44 -0700389 return toModule("core.current.stubs", "", nil)
Jiyong Park50146e92020-01-30 18:00:15 +0900390 case sdkModule:
391 // TODO(146757305): provide .apk and .aidl that have more APIs for modules
392 return toModule("android_module_lib_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800393 default:
Jiyong Park6a927c42020-01-21 02:03:43 +0900394 panic(fmt.Errorf("invalid sdk %q", sdkVersion.raw))
Colin Crossfb6d7812019-01-09 22:17:55 -0800395 }
396}
Colin Cross98fd5742019-01-09 23:04:25 -0800397
Colin Cross3047fa22019-04-18 10:56:44 -0700398func sdkPreSingletonFactory() android.Singleton {
399 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800400}
401
Colin Cross3047fa22019-04-18 10:56:44 -0700402type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800403
Colin Cross3047fa22019-04-18 10:56:44 -0700404func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800405 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
406 if err != nil {
407 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
408 }
409
410 var sdkVersions []int
411 for _, sdkJar := range sdkJars {
412 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
413 v, err := strconv.Atoi(dir)
414 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
415 continue
416 } else if err != nil {
417 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
418 }
419 sdkVersions = append(sdkVersions, v)
420 }
421
422 sort.Ints(sdkVersions)
423
Colin Cross3047fa22019-04-18 10:56:44 -0700424 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
425}
426
Jiyong Park6a927c42020-01-21 02:03:43 +0900427func LatestSdkVersionInt(ctx android.EarlyModuleContext) int {
428 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
429 latestSdkVersion := 0
430 if len(sdkVersions) > 0 {
431 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
432 }
433 return latestSdkVersion
434}
435
Colin Cross3047fa22019-04-18 10:56:44 -0700436func sdkSingletonFactory() android.Singleton {
437 return sdkSingleton{}
438}
439
440type sdkSingleton struct{}
441
442func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross10932872019-04-18 14:27:12 -0700443 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700444 return
445 }
446
Colin Cross10932872019-04-18 14:27:12 -0700447 createSdkFrameworkAidl(ctx)
448 createAPIFingerprint(ctx)
449}
Colin Cross3047fa22019-04-18 10:56:44 -0700450
Colin Cross10932872019-04-18 14:27:12 -0700451// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
452func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross3047fa22019-04-18 10:56:44 -0700453 stubsModules := []string{
454 "android_stubs_current",
455 "android_test_stubs_current",
456 "android_system_stubs_current",
457 }
458
459 stubsJars := make([]android.Paths, len(stubsModules))
460
461 ctx.VisitAllModules(func(module android.Module) {
462 // Collect dex jar paths for the modules listed above.
463 if j, ok := module.(Dependency); ok {
464 name := ctx.ModuleName(module)
465 if i := android.IndexList(name, stubsModules); i != -1 {
466 stubsJars[i] = j.HeaderJars()
467 }
468 }
469 })
470
471 var missingDeps []string
472
473 for i := range stubsJars {
474 if stubsJars[i] == nil {
475 if ctx.Config().AllowMissingDependencies() {
476 missingDeps = append(missingDeps, stubsModules[i])
477 } else {
478 ctx.Errorf("failed to find dex jar path for module %q",
479 stubsModules[i])
480 }
481 }
482 }
483
484 rule := android.NewRuleBuilder()
485 rule.MissingDeps(missingDeps)
486
487 var aidls android.Paths
488 for _, jars := range stubsJars {
489 for _, jar := range jars {
490 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
491
492 rule.Command().
493 Text("rm -f").Output(aidl)
494 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700495 BuiltTool(ctx, "sdkparcelables").
Colin Cross3047fa22019-04-18 10:56:44 -0700496 Input(jar).
497 Output(aidl)
498
499 aidls = append(aidls, aidl)
500 }
501 }
502
503 combinedAidl := sdkFrameworkAidlPath(ctx)
504 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
505
506 rule.Command().
507 Text("rm -f").Output(tempPath)
508 rule.Command().
509 Text("cat").
510 Inputs(aidls).
511 Text("| sort -u >").
512 Output(tempPath)
513
514 commitChangeForRestat(rule, tempPath, combinedAidl)
515
516 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
517}
518
519func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
520 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
521 return android.PathForOutput(ctx, "framework.aidl")
522 }).(android.OutputPath)
523}
524
Colin Cross10932872019-04-18 14:27:12 -0700525// Create api_fingerprint.txt
526func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Park71b519d2019-04-18 17:25:49 +0900527 out := ApiFingerprintPath(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700528
529 rule := android.NewRuleBuilder()
530
531 rule.Command().
532 Text("rm -f").Output(out)
533 cmd := rule.Command()
534
535 if ctx.Config().PlatformSdkCodename() == "REL" {
536 cmd.Text("echo REL >").Output(out)
537 } else if ctx.Config().IsPdkBuild() {
538 // TODO: get this from the PDK artifacts?
539 cmd.Text("echo PDK >").Output(out)
540 } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
541 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
542 if err != nil {
543 ctx.Errorf("error globbing API files: %s", err)
544 }
545
546 cmd.Text("cat").
547 Inputs(android.PathsForSource(ctx, in)).
Elliott Hughes34b49d12019-09-06 14:42:24 -0700548 Text("| md5sum | cut -d' ' -f1 >").
Colin Cross10932872019-04-18 14:27:12 -0700549 Output(out)
550 } else {
551 // Unbundled build
552 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
553 cmd.Text("echo").
554 Flag(ctx.Config().PlatformPreviewSdkVersion()).
555 Text(">").
556 Output(out)
557 }
558
559 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
560}
561
Jiyong Park71b519d2019-04-18 17:25:49 +0900562func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross10932872019-04-18 14:27:12 -0700563 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
564 return android.PathForOutput(ctx, "api_fingerprint.txt")
565 }).(android.OutputPath)
566}
567
568func sdkMakeVars(ctx android.MakeVarsContext) {
569 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700570 return
571 }
572
573 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Park71b519d2019-04-18 17:25:49 +0900574 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800575}