blob: 2dbcf4a65a684d4c0701025bb2d67d0573f04ba2 [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 {
Paul Duffin250e6192019-06-07 10:44:37 +010041 // sdkVersion returns the sdk_version property of the current module, or an empty string if it is not set.
Colin Crossfb6d7812019-01-09 22:17:55 -080042 sdkVersion() string
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
Colin Crossfb6d7812019-01-09 22:17:55 -080045 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
46 minSdkVersion() string
47 // targetSdkVersion returns the target_sdk_version property of the current module, or sdkVersion() if it is not set.
48 targetSdkVersion() string
49}
50
Baligh Uddinf6201372020-01-24 23:15:44 +000051func UseApiFingerprint(ctx android.BaseModuleContext, v string) bool {
52 if v == ctx.Config().PlatformSdkCodename() &&
53 ctx.Config().UnbundledBuild() &&
54 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
55 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
56 return true
57 }
58 return false
59}
60
Colin Cross0ea8ba82019-06-06 14:33:29 -070061func sdkVersionOrDefault(ctx android.BaseModuleContext, v string) string {
Baligh Uddinf6201372020-01-24 23:15:44 +000062 var sdkVersion string
Colin Crossfb6d7812019-01-09 22:17:55 -080063 switch v {
Paul Duffin50c217c2019-06-12 13:25:22 +010064 case "", "none", "current", "test_current", "system_current", "core_current", "core_platform":
Baligh Uddinf6201372020-01-24 23:15:44 +000065 sdkVersion = ctx.Config().DefaultAppTargetSdk()
Colin Crossfb6d7812019-01-09 22:17:55 -080066 default:
Baligh Uddinf6201372020-01-24 23:15:44 +000067 sdkVersion = v
Colin Crossfb6d7812019-01-09 22:17:55 -080068 }
Baligh Uddinf6201372020-01-24 23:15:44 +000069 if UseApiFingerprint(ctx, sdkVersion) {
70 apiFingerprint := ApiFingerprintPath(ctx)
71 sdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
72 }
73 return sdkVersion
Colin Crossfb6d7812019-01-09 22:17:55 -080074}
75
76// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
77// it returns android.FutureApiLevel (10000).
Colin Cross1184b642019-12-30 18:43:07 -080078func sdkVersionToNumber(ctx android.EarlyModuleContext, v string) (int, error) {
Colin Crossfb6d7812019-01-09 22:17:55 -080079 switch v {
Paul Duffin50c217c2019-06-12 13:25:22 +010080 case "", "none", "current", "test_current", "system_current", "core_current", "core_platform":
Colin Crossfb6d7812019-01-09 22:17:55 -080081 return ctx.Config().DefaultAppTargetSdkInt(), nil
82 default:
83 n := android.GetNumericSdkVersion(v)
84 if i, err := strconv.Atoi(n); err != nil {
85 return -1, fmt.Errorf("invalid sdk version %q", n)
86 } else {
87 return i, nil
88 }
89 }
90}
91
Colin Cross1184b642019-12-30 18:43:07 -080092func sdkVersionToNumberAsString(ctx android.EarlyModuleContext, v string) (string, error) {
Colin Crossfb6d7812019-01-09 22:17:55 -080093 n, err := sdkVersionToNumber(ctx, v)
94 if err != nil {
95 return "", err
96 }
97 return strconv.Itoa(n), nil
98}
99
Colin Cross1184b642019-12-30 18:43:07 -0800100func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext sdkContext) sdkDep {
Colin Crossfb6d7812019-01-09 22:17:55 -0800101 v := sdkContext.sdkVersion()
Paul Duffin5c2f9632019-06-12 14:21:31 +0100102
Colin Cross98fd5742019-01-09 23:04:25 -0800103 // For PDK builds, use the latest SDK version instead of "current"
104 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700105 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800106 latestSdkVersion := 0
107 if len(sdkVersions) > 0 {
108 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
109 }
110 v = strconv.Itoa(latestSdkVersion)
111 }
112
Colin Crossff0daf42019-04-02 16:10:56 -0700113 numericSdkVersion, err := sdkVersionToNumber(ctx, v)
Colin Crossfb6d7812019-01-09 22:17:55 -0800114 if err != nil {
115 ctx.PropertyErrorf("sdk_version", "%s", err)
116 return sdkDep{}
117 }
118
Colin Crossfb6d7812019-01-09 22:17:55 -0800119 toPrebuilt := func(sdk string) sdkDep {
120 var api, v string
121 if strings.Contains(sdk, "_") {
122 t := strings.Split(sdk, "_")
123 api = t[0]
124 v = t[1]
125 } else {
126 api = "public"
127 v = sdk
128 }
129 dir := filepath.Join("prebuilts", "sdk", v, api)
130 jar := filepath.Join(dir, "android.jar")
131 // There's no aidl for other SDKs yet.
132 // TODO(77525052): Add aidl files for other SDKs too.
133 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
134 aidl := filepath.Join(public_dir, "framework.aidl")
135 jarPath := android.ExistentPathForSource(ctx, jar)
136 aidlPath := android.ExistentPathForSource(ctx, aidl)
137 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
138
139 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
140 return sdkDep{
141 invalidVersion: true,
Colin Cross6cef4812019-10-17 14:23:50 -0700142 bootclasspath: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Crossfb6d7812019-01-09 22:17:55 -0800143 }
144 }
145
146 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800147 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdk, jar)
Colin Crossfb6d7812019-01-09 22:17:55 -0800148 return sdkDep{}
149 }
150
151 if !aidlPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800152 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdk, aidl)
Colin Crossfb6d7812019-01-09 22:17:55 -0800153 return sdkDep{}
154 }
155
156 return sdkDep{
157 useFiles: true,
158 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Cross3047fa22019-04-18 10:56:44 -0700159 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800160 }
161 }
162
Colin Cross3047fa22019-04-18 10:56:44 -0700163 toModule := func(m, r string, aidl android.Path) sdkDep {
Colin Cross6cef4812019-10-17 14:23:50 -0700164 return sdkDep{
Colin Crossfb6d7812019-01-09 22:17:55 -0800165 useModule: true,
Colin Cross6cef4812019-10-17 14:23:50 -0700166 bootclasspath: []string{m, config.DefaultLambdaStubsLibrary},
167 systemModules: "core-current-stubs-system-modules",
168 java9Classpath: []string{m},
Colin Crossfb6d7812019-01-09 22:17:55 -0800169 frameworkResModule: r,
Colin Cross3047fa22019-04-18 10:56:44 -0700170 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800171 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800172 }
173
Colin Cross98fd5742019-01-09 23:04:25 -0800174 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
175 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
Colin Crossff0daf42019-04-02 16:10:56 -0700176 if strings.HasPrefix(v, "system_") && numericSdkVersion != android.FutureApiLevel {
Colin Cross98fd5742019-01-09 23:04:25 -0800177 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
178 if ctx.DeviceSpecific() || ctx.SocSpecific() {
179 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
180 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
181 }
182 }
Colin Crossff0daf42019-04-02 16:10:56 -0700183 if len(allowed_versions) > 0 && !android.InList(strconv.Itoa(numericSdkVersion), allowed_versions) {
Colin Cross98fd5742019-01-09 23:04:25 -0800184 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
185 v, allowed_versions)
186 }
187 }
188
Paul Duffin50c217c2019-06-12 13:25:22 +0100189 if ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
190 v != "" && v != "none" && v != "core_platform" {
Colin Crossfb6d7812019-01-09 22:17:55 -0800191 return toPrebuilt(v)
192 }
193
194 switch v {
195 case "":
196 return sdkDep{
197 useDefaultLibs: true,
198 frameworkResModule: "framework-res",
199 }
Paul Duffin52d398a2019-06-11 12:31:14 +0100200 case "none":
Paul Duffine25c6442019-10-11 13:50:28 +0100201 systemModules := sdkContext.systemModules()
202 if systemModules == "" {
203 ctx.PropertyErrorf("sdk_version",
204 `system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`)
205 } else if systemModules == "none" {
Colin Cross6d8d8c62019-10-28 15:10:03 -0700206 return sdkDep{
207 noStandardLibs: true,
208 }
Paul Duffine25c6442019-10-11 13:50:28 +0100209 }
210
Paul Duffin52d398a2019-06-11 12:31:14 +0100211 return sdkDep{
Colin Cross6d8d8c62019-10-28 15:10:03 -0700212 useModule: true,
Paul Duffin52d398a2019-06-11 12:31:14 +0100213 noStandardLibs: true,
Paul Duffine25c6442019-10-11 13:50:28 +0100214 systemModules: systemModules,
Colin Cross6cef4812019-10-17 14:23:50 -0700215 bootclasspath: []string{systemModules},
Paul Duffin52d398a2019-06-11 12:31:14 +0100216 }
Paul Duffin50c217c2019-06-12 13:25:22 +0100217 case "core_platform":
218 return sdkDep{
219 useDefaultLibs: true,
220 frameworkResModule: "framework-res",
221 noFrameworksLibs: true,
222 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800223 case "current":
Colin Cross3047fa22019-04-18 10:56:44 -0700224 return toModule("android_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800225 case "system_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700226 return toModule("android_system_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800227 case "test_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700228 return toModule("android_test_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800229 case "core_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700230 return toModule("core.current.stubs", "", nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800231 default:
232 return toPrebuilt(v)
233 }
234}
Colin Cross98fd5742019-01-09 23:04:25 -0800235
Colin Cross3047fa22019-04-18 10:56:44 -0700236func sdkPreSingletonFactory() android.Singleton {
237 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800238}
239
Colin Cross3047fa22019-04-18 10:56:44 -0700240type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800241
Colin Cross3047fa22019-04-18 10:56:44 -0700242func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800243 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
244 if err != nil {
245 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
246 }
247
248 var sdkVersions []int
249 for _, sdkJar := range sdkJars {
250 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
251 v, err := strconv.Atoi(dir)
252 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
253 continue
254 } else if err != nil {
255 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
256 }
257 sdkVersions = append(sdkVersions, v)
258 }
259
260 sort.Ints(sdkVersions)
261
Colin Cross3047fa22019-04-18 10:56:44 -0700262 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
263}
264
265func sdkSingletonFactory() android.Singleton {
266 return sdkSingleton{}
267}
268
269type sdkSingleton struct{}
270
271func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross10932872019-04-18 14:27:12 -0700272 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700273 return
274 }
275
Colin Cross10932872019-04-18 14:27:12 -0700276 createSdkFrameworkAidl(ctx)
277 createAPIFingerprint(ctx)
278}
Colin Cross3047fa22019-04-18 10:56:44 -0700279
Colin Cross10932872019-04-18 14:27:12 -0700280// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
281func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross3047fa22019-04-18 10:56:44 -0700282 stubsModules := []string{
283 "android_stubs_current",
284 "android_test_stubs_current",
285 "android_system_stubs_current",
286 }
287
288 stubsJars := make([]android.Paths, len(stubsModules))
289
290 ctx.VisitAllModules(func(module android.Module) {
291 // Collect dex jar paths for the modules listed above.
292 if j, ok := module.(Dependency); ok {
293 name := ctx.ModuleName(module)
294 if i := android.IndexList(name, stubsModules); i != -1 {
295 stubsJars[i] = j.HeaderJars()
296 }
297 }
298 })
299
300 var missingDeps []string
301
302 for i := range stubsJars {
303 if stubsJars[i] == nil {
304 if ctx.Config().AllowMissingDependencies() {
305 missingDeps = append(missingDeps, stubsModules[i])
306 } else {
307 ctx.Errorf("failed to find dex jar path for module %q",
308 stubsModules[i])
309 }
310 }
311 }
312
313 rule := android.NewRuleBuilder()
314 rule.MissingDeps(missingDeps)
315
316 var aidls android.Paths
317 for _, jars := range stubsJars {
318 for _, jar := range jars {
319 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
320
321 rule.Command().
322 Text("rm -f").Output(aidl)
323 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700324 BuiltTool(ctx, "sdkparcelables").
Colin Cross3047fa22019-04-18 10:56:44 -0700325 Input(jar).
326 Output(aidl)
327
328 aidls = append(aidls, aidl)
329 }
330 }
331
332 combinedAidl := sdkFrameworkAidlPath(ctx)
333 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
334
335 rule.Command().
336 Text("rm -f").Output(tempPath)
337 rule.Command().
338 Text("cat").
339 Inputs(aidls).
340 Text("| sort -u >").
341 Output(tempPath)
342
343 commitChangeForRestat(rule, tempPath, combinedAidl)
344
345 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
346}
347
348func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
349 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
350 return android.PathForOutput(ctx, "framework.aidl")
351 }).(android.OutputPath)
352}
353
Colin Cross10932872019-04-18 14:27:12 -0700354// Create api_fingerprint.txt
355func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Park71b519d2019-04-18 17:25:49 +0900356 out := ApiFingerprintPath(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700357
358 rule := android.NewRuleBuilder()
359
360 rule.Command().
361 Text("rm -f").Output(out)
362 cmd := rule.Command()
363
364 if ctx.Config().PlatformSdkCodename() == "REL" {
365 cmd.Text("echo REL >").Output(out)
366 } else if ctx.Config().IsPdkBuild() {
367 // TODO: get this from the PDK artifacts?
368 cmd.Text("echo PDK >").Output(out)
369 } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
370 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
371 if err != nil {
372 ctx.Errorf("error globbing API files: %s", err)
373 }
374
375 cmd.Text("cat").
376 Inputs(android.PathsForSource(ctx, in)).
Elliott Hughes34b49d12019-09-06 14:42:24 -0700377 Text("| md5sum | cut -d' ' -f1 >").
Colin Cross10932872019-04-18 14:27:12 -0700378 Output(out)
379 } else {
380 // Unbundled build
381 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
382 cmd.Text("echo").
383 Flag(ctx.Config().PlatformPreviewSdkVersion()).
384 Text(">").
385 Output(out)
386 }
387
388 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
389}
390
Jiyong Park71b519d2019-04-18 17:25:49 +0900391func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross10932872019-04-18 14:27:12 -0700392 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
393 return android.PathForOutput(ctx, "api_fingerprint.txt")
394 }).(android.OutputPath)
395}
396
397func sdkMakeVars(ctx android.MakeVarsContext) {
398 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700399 return
400 }
401
402 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Park71b519d2019-04-18 17:25:49 +0900403 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800404}