blob: 73b7dcf78eb4dac20794e47a48a3d7d05fb6d36c [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 (
18 "android/soong/android"
19 "android/soong/java/config"
20 "fmt"
21 "path/filepath"
Colin Cross98fd5742019-01-09 23:04:25 -080022 "sort"
Colin Crossfb6d7812019-01-09 22:17:55 -080023 "strconv"
24 "strings"
Colin Cross3047fa22019-04-18 10:56:44 -070025
26 "github.com/google/blueprint/pathtools"
Colin Crossfb6d7812019-01-09 22:17:55 -080027)
28
Colin Cross98fd5742019-01-09 23:04:25 -080029func init() {
Colin Cross3047fa22019-04-18 10:56:44 -070030 android.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
31 android.RegisterSingletonType("sdk", sdkSingletonFactory)
Colin Cross10932872019-04-18 14:27:12 -070032 android.RegisterMakeVarsProvider(pctx, sdkMakeVars)
Colin Cross98fd5742019-01-09 23:04:25 -080033}
34
Colin Cross3047fa22019-04-18 10:56:44 -070035var sdkVersionsKey = android.NewOnceKey("sdkVersionsKey")
36var sdkFrameworkAidlPathKey = android.NewOnceKey("sdkFrameworkAidlPathKey")
Colin Cross10932872019-04-18 14:27:12 -070037var apiFingerprintPathKey = android.NewOnceKey("apiFingerprintPathKey")
Colin Cross98fd5742019-01-09 23:04:25 -080038
Colin Crossfb6d7812019-01-09 22:17:55 -080039type sdkContext interface {
Paul Duffin250e6192019-06-07 10:44:37 +010040 // 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 -080041 sdkVersion() string
Paul Duffine25c6442019-10-11 13:50:28 +010042 // systemModules returns the system_modules property of the current module, or an empty string if it is not set.
43 systemModules() string
Colin Crossfb6d7812019-01-09 22:17:55 -080044 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
45 minSdkVersion() string
46 // targetSdkVersion returns the target_sdk_version property of the current module, or sdkVersion() if it is not set.
47 targetSdkVersion() string
48}
49
Baligh Uddinf6201372020-01-24 23:15:44 +000050func UseApiFingerprint(ctx android.BaseModuleContext, v string) bool {
51 if v == ctx.Config().PlatformSdkCodename() &&
52 ctx.Config().UnbundledBuild() &&
53 !ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
54 ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") {
55 return true
56 }
57 return false
58}
59
Colin Cross0ea8ba82019-06-06 14:33:29 -070060func sdkVersionOrDefault(ctx android.BaseModuleContext, v string) string {
Baligh Uddinf6201372020-01-24 23:15:44 +000061 var sdkVersion string
Colin Crossfb6d7812019-01-09 22:17:55 -080062 switch v {
Paul Duffin50c217c2019-06-12 13:25:22 +010063 case "", "none", "current", "test_current", "system_current", "core_current", "core_platform":
Baligh Uddinf6201372020-01-24 23:15:44 +000064 sdkVersion = ctx.Config().DefaultAppTargetSdk()
Colin Crossfb6d7812019-01-09 22:17:55 -080065 default:
Baligh Uddinf6201372020-01-24 23:15:44 +000066 sdkVersion = v
Colin Crossfb6d7812019-01-09 22:17:55 -080067 }
Baligh Uddinf6201372020-01-24 23:15:44 +000068 if UseApiFingerprint(ctx, sdkVersion) {
69 apiFingerprint := ApiFingerprintPath(ctx)
70 sdkVersion += fmt.Sprintf(".$$(cat %s)", apiFingerprint.String())
71 }
72 return sdkVersion
Colin Crossfb6d7812019-01-09 22:17:55 -080073}
74
75// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
76// it returns android.FutureApiLevel (10000).
Colin Cross1184b642019-12-30 18:43:07 -080077func sdkVersionToNumber(ctx android.EarlyModuleContext, v string) (int, error) {
Colin Crossfb6d7812019-01-09 22:17:55 -080078 switch v {
Paul Duffin50c217c2019-06-12 13:25:22 +010079 case "", "none", "current", "test_current", "system_current", "core_current", "core_platform":
Colin Crossfb6d7812019-01-09 22:17:55 -080080 return ctx.Config().DefaultAppTargetSdkInt(), nil
81 default:
82 n := android.GetNumericSdkVersion(v)
83 if i, err := strconv.Atoi(n); err != nil {
84 return -1, fmt.Errorf("invalid sdk version %q", n)
85 } else {
86 return i, nil
87 }
88 }
89}
90
Colin Cross1184b642019-12-30 18:43:07 -080091func sdkVersionToNumberAsString(ctx android.EarlyModuleContext, v string) (string, error) {
Colin Crossfb6d7812019-01-09 22:17:55 -080092 n, err := sdkVersionToNumber(ctx, v)
93 if err != nil {
94 return "", err
95 }
96 return strconv.Itoa(n), nil
97}
98
Colin Cross1184b642019-12-30 18:43:07 -080099func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext sdkContext) sdkDep {
Colin Crossfb6d7812019-01-09 22:17:55 -0800100 v := sdkContext.sdkVersion()
Paul Duffin5c2f9632019-06-12 14:21:31 +0100101
Colin Cross98fd5742019-01-09 23:04:25 -0800102 // For PDK builds, use the latest SDK version instead of "current"
103 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700104 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800105 latestSdkVersion := 0
106 if len(sdkVersions) > 0 {
107 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
108 }
109 v = strconv.Itoa(latestSdkVersion)
110 }
111
Colin Crossff0daf42019-04-02 16:10:56 -0700112 numericSdkVersion, err := sdkVersionToNumber(ctx, v)
Colin Crossfb6d7812019-01-09 22:17:55 -0800113 if err != nil {
114 ctx.PropertyErrorf("sdk_version", "%s", err)
115 return sdkDep{}
116 }
117
Colin Crossfb6d7812019-01-09 22:17:55 -0800118 toPrebuilt := func(sdk string) sdkDep {
119 var api, v string
120 if strings.Contains(sdk, "_") {
121 t := strings.Split(sdk, "_")
122 api = t[0]
123 v = t[1]
124 } else {
125 api = "public"
126 v = sdk
127 }
128 dir := filepath.Join("prebuilts", "sdk", v, api)
129 jar := filepath.Join(dir, "android.jar")
130 // There's no aidl for other SDKs yet.
131 // TODO(77525052): Add aidl files for other SDKs too.
132 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
133 aidl := filepath.Join(public_dir, "framework.aidl")
134 jarPath := android.ExistentPathForSource(ctx, jar)
135 aidlPath := android.ExistentPathForSource(ctx, aidl)
136 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
137
138 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
139 return sdkDep{
140 invalidVersion: true,
Colin Cross6cef4812019-10-17 14:23:50 -0700141 bootclasspath: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Crossfb6d7812019-01-09 22:17:55 -0800142 }
143 }
144
145 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800146 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdk, jar)
Colin Crossfb6d7812019-01-09 22:17:55 -0800147 return sdkDep{}
148 }
149
150 if !aidlPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800151 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdk, aidl)
Colin Crossfb6d7812019-01-09 22:17:55 -0800152 return sdkDep{}
153 }
154
155 return sdkDep{
156 useFiles: true,
157 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Cross3047fa22019-04-18 10:56:44 -0700158 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800159 }
160 }
161
Colin Cross3047fa22019-04-18 10:56:44 -0700162 toModule := func(m, r string, aidl android.Path) sdkDep {
Colin Cross6cef4812019-10-17 14:23:50 -0700163 return sdkDep{
Colin Crossfb6d7812019-01-09 22:17:55 -0800164 useModule: true,
Colin Cross6cef4812019-10-17 14:23:50 -0700165 bootclasspath: []string{m, config.DefaultLambdaStubsLibrary},
166 systemModules: "core-current-stubs-system-modules",
167 java9Classpath: []string{m},
Colin Crossfb6d7812019-01-09 22:17:55 -0800168 frameworkResModule: r,
Colin Cross3047fa22019-04-18 10:56:44 -0700169 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800170 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800171 }
172
Colin Cross98fd5742019-01-09 23:04:25 -0800173 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
174 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
Colin Crossff0daf42019-04-02 16:10:56 -0700175 if strings.HasPrefix(v, "system_") && numericSdkVersion != android.FutureApiLevel {
Colin Cross98fd5742019-01-09 23:04:25 -0800176 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
177 if ctx.DeviceSpecific() || ctx.SocSpecific() {
178 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
179 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
180 }
181 }
Colin Crossff0daf42019-04-02 16:10:56 -0700182 if len(allowed_versions) > 0 && !android.InList(strconv.Itoa(numericSdkVersion), allowed_versions) {
Colin Cross98fd5742019-01-09 23:04:25 -0800183 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
184 v, allowed_versions)
185 }
186 }
187
Paul Duffin50c217c2019-06-12 13:25:22 +0100188 if ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
189 v != "" && v != "none" && v != "core_platform" {
Colin Crossfb6d7812019-01-09 22:17:55 -0800190 return toPrebuilt(v)
191 }
192
193 switch v {
194 case "":
195 return sdkDep{
196 useDefaultLibs: true,
197 frameworkResModule: "framework-res",
198 }
Paul Duffin52d398a2019-06-11 12:31:14 +0100199 case "none":
Paul Duffine25c6442019-10-11 13:50:28 +0100200 systemModules := sdkContext.systemModules()
201 if systemModules == "" {
202 ctx.PropertyErrorf("sdk_version",
203 `system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`)
204 } else if systemModules == "none" {
Colin Cross6d8d8c62019-10-28 15:10:03 -0700205 return sdkDep{
206 noStandardLibs: true,
207 }
Paul Duffine25c6442019-10-11 13:50:28 +0100208 }
209
Paul Duffin52d398a2019-06-11 12:31:14 +0100210 return sdkDep{
Colin Cross6d8d8c62019-10-28 15:10:03 -0700211 useModule: true,
Paul Duffin52d398a2019-06-11 12:31:14 +0100212 noStandardLibs: true,
Paul Duffine25c6442019-10-11 13:50:28 +0100213 systemModules: systemModules,
Colin Cross6cef4812019-10-17 14:23:50 -0700214 bootclasspath: []string{systemModules},
Paul Duffin52d398a2019-06-11 12:31:14 +0100215 }
Paul Duffin50c217c2019-06-12 13:25:22 +0100216 case "core_platform":
217 return sdkDep{
218 useDefaultLibs: true,
219 frameworkResModule: "framework-res",
220 noFrameworksLibs: true,
221 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800222 case "current":
Colin Cross3047fa22019-04-18 10:56:44 -0700223 return toModule("android_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800224 case "system_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700225 return toModule("android_system_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800226 case "test_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700227 return toModule("android_test_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800228 case "core_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700229 return toModule("core.current.stubs", "", nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800230 default:
231 return toPrebuilt(v)
232 }
233}
Colin Cross98fd5742019-01-09 23:04:25 -0800234
Colin Cross3047fa22019-04-18 10:56:44 -0700235func sdkPreSingletonFactory() android.Singleton {
236 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800237}
238
Colin Cross3047fa22019-04-18 10:56:44 -0700239type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800240
Colin Cross3047fa22019-04-18 10:56:44 -0700241func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800242 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
243 if err != nil {
244 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
245 }
246
247 var sdkVersions []int
248 for _, sdkJar := range sdkJars {
249 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
250 v, err := strconv.Atoi(dir)
251 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
252 continue
253 } else if err != nil {
254 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
255 }
256 sdkVersions = append(sdkVersions, v)
257 }
258
259 sort.Ints(sdkVersions)
260
Colin Cross3047fa22019-04-18 10:56:44 -0700261 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
262}
263
264func sdkSingletonFactory() android.Singleton {
265 return sdkSingleton{}
266}
267
268type sdkSingleton struct{}
269
270func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross10932872019-04-18 14:27:12 -0700271 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700272 return
273 }
274
Colin Cross10932872019-04-18 14:27:12 -0700275 createSdkFrameworkAidl(ctx)
276 createAPIFingerprint(ctx)
277}
Colin Cross3047fa22019-04-18 10:56:44 -0700278
Colin Cross10932872019-04-18 14:27:12 -0700279// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
280func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross3047fa22019-04-18 10:56:44 -0700281 stubsModules := []string{
282 "android_stubs_current",
283 "android_test_stubs_current",
284 "android_system_stubs_current",
285 }
286
287 stubsJars := make([]android.Paths, len(stubsModules))
288
289 ctx.VisitAllModules(func(module android.Module) {
290 // Collect dex jar paths for the modules listed above.
291 if j, ok := module.(Dependency); ok {
292 name := ctx.ModuleName(module)
293 if i := android.IndexList(name, stubsModules); i != -1 {
294 stubsJars[i] = j.HeaderJars()
295 }
296 }
297 })
298
299 var missingDeps []string
300
301 for i := range stubsJars {
302 if stubsJars[i] == nil {
303 if ctx.Config().AllowMissingDependencies() {
304 missingDeps = append(missingDeps, stubsModules[i])
305 } else {
306 ctx.Errorf("failed to find dex jar path for module %q",
307 stubsModules[i])
308 }
309 }
310 }
311
312 rule := android.NewRuleBuilder()
313 rule.MissingDeps(missingDeps)
314
315 var aidls android.Paths
316 for _, jars := range stubsJars {
317 for _, jar := range jars {
318 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
319
320 rule.Command().
321 Text("rm -f").Output(aidl)
322 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700323 BuiltTool(ctx, "sdkparcelables").
Colin Cross3047fa22019-04-18 10:56:44 -0700324 Input(jar).
325 Output(aidl)
326
327 aidls = append(aidls, aidl)
328 }
329 }
330
331 combinedAidl := sdkFrameworkAidlPath(ctx)
332 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
333
334 rule.Command().
335 Text("rm -f").Output(tempPath)
336 rule.Command().
337 Text("cat").
338 Inputs(aidls).
339 Text("| sort -u >").
340 Output(tempPath)
341
342 commitChangeForRestat(rule, tempPath, combinedAidl)
343
344 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
345}
346
347func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
348 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
349 return android.PathForOutput(ctx, "framework.aidl")
350 }).(android.OutputPath)
351}
352
Colin Cross10932872019-04-18 14:27:12 -0700353// Create api_fingerprint.txt
354func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Park71b519d2019-04-18 17:25:49 +0900355 out := ApiFingerprintPath(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700356
357 rule := android.NewRuleBuilder()
358
359 rule.Command().
360 Text("rm -f").Output(out)
361 cmd := rule.Command()
362
363 if ctx.Config().PlatformSdkCodename() == "REL" {
364 cmd.Text("echo REL >").Output(out)
365 } else if ctx.Config().IsPdkBuild() {
366 // TODO: get this from the PDK artifacts?
367 cmd.Text("echo PDK >").Output(out)
368 } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
369 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
370 if err != nil {
371 ctx.Errorf("error globbing API files: %s", err)
372 }
373
374 cmd.Text("cat").
375 Inputs(android.PathsForSource(ctx, in)).
Elliott Hughes34b49d12019-09-06 14:42:24 -0700376 Text("| md5sum | cut -d' ' -f1 >").
Colin Cross10932872019-04-18 14:27:12 -0700377 Output(out)
378 } else {
379 // Unbundled build
380 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
381 cmd.Text("echo").
382 Flag(ctx.Config().PlatformPreviewSdkVersion()).
383 Text(">").
384 Output(out)
385 }
386
387 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
388}
389
Jiyong Park71b519d2019-04-18 17:25:49 +0900390func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross10932872019-04-18 14:27:12 -0700391 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
392 return android.PathForOutput(ctx, "api_fingerprint.txt")
393 }).(android.OutputPath)
394}
395
396func sdkMakeVars(ctx android.MakeVarsContext) {
397 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700398 return
399 }
400
401 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Park71b519d2019-04-18 17:25:49 +0900402 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800403}