blob: 2aa797a02b52fd5a79ca6c23e129763b5c8331a7 [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
Colin Cross0ea8ba82019-06-06 14:33:29 -070051func sdkVersionOrDefault(ctx android.BaseModuleContext, v string) string {
Colin Crossfb6d7812019-01-09 22:17:55 -080052 switch v {
Paul Duffin50c217c2019-06-12 13:25:22 +010053 case "", "none", "current", "test_current", "system_current", "core_current", "core_platform":
Pete Gillin230e2412020-01-21 13:41:17 +000054 return ctx.Config().DefaultAppTargetSdk()
Colin Crossfb6d7812019-01-09 22:17:55 -080055 default:
Pete Gillin230e2412020-01-21 13:41:17 +000056 return v
Colin Crossfb6d7812019-01-09 22:17:55 -080057 }
58}
59
60// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
61// it returns android.FutureApiLevel (10000).
Colin Cross1184b642019-12-30 18:43:07 -080062func sdkVersionToNumber(ctx android.EarlyModuleContext, v string) (int, error) {
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":
Colin Crossfb6d7812019-01-09 22:17:55 -080065 return ctx.Config().DefaultAppTargetSdkInt(), nil
66 default:
67 n := android.GetNumericSdkVersion(v)
68 if i, err := strconv.Atoi(n); err != nil {
69 return -1, fmt.Errorf("invalid sdk version %q", n)
70 } else {
71 return i, nil
72 }
73 }
74}
75
Colin Cross1184b642019-12-30 18:43:07 -080076func sdkVersionToNumberAsString(ctx android.EarlyModuleContext, v string) (string, error) {
Colin Crossfb6d7812019-01-09 22:17:55 -080077 n, err := sdkVersionToNumber(ctx, v)
78 if err != nil {
79 return "", err
80 }
81 return strconv.Itoa(n), nil
82}
83
Colin Cross1184b642019-12-30 18:43:07 -080084func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext sdkContext) sdkDep {
Colin Crossfb6d7812019-01-09 22:17:55 -080085 v := sdkContext.sdkVersion()
Paul Duffin5c2f9632019-06-12 14:21:31 +010086
Colin Cross98fd5742019-01-09 23:04:25 -080087 // For PDK builds, use the latest SDK version instead of "current"
88 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -070089 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -080090 latestSdkVersion := 0
91 if len(sdkVersions) > 0 {
92 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
93 }
94 v = strconv.Itoa(latestSdkVersion)
95 }
96
Colin Crossff0daf42019-04-02 16:10:56 -070097 numericSdkVersion, err := sdkVersionToNumber(ctx, v)
Colin Crossfb6d7812019-01-09 22:17:55 -080098 if err != nil {
99 ctx.PropertyErrorf("sdk_version", "%s", err)
100 return sdkDep{}
101 }
102
Colin Crossfb6d7812019-01-09 22:17:55 -0800103 toPrebuilt := func(sdk string) sdkDep {
104 var api, v string
105 if strings.Contains(sdk, "_") {
106 t := strings.Split(sdk, "_")
107 api = t[0]
108 v = t[1]
109 } else {
110 api = "public"
111 v = sdk
112 }
113 dir := filepath.Join("prebuilts", "sdk", v, api)
114 jar := filepath.Join(dir, "android.jar")
115 // There's no aidl for other SDKs yet.
116 // TODO(77525052): Add aidl files for other SDKs too.
117 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
118 aidl := filepath.Join(public_dir, "framework.aidl")
119 jarPath := android.ExistentPathForSource(ctx, jar)
120 aidlPath := android.ExistentPathForSource(ctx, aidl)
121 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
122
123 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
124 return sdkDep{
125 invalidVersion: true,
Colin Cross6cef4812019-10-17 14:23:50 -0700126 bootclasspath: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Crossfb6d7812019-01-09 22:17:55 -0800127 }
128 }
129
130 if !jarPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800131 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdk, jar)
Colin Crossfb6d7812019-01-09 22:17:55 -0800132 return sdkDep{}
133 }
134
135 if !aidlPath.Valid() {
Colin Cross07c88562020-01-07 09:34:44 -0800136 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdk, aidl)
Colin Crossfb6d7812019-01-09 22:17:55 -0800137 return sdkDep{}
138 }
139
140 return sdkDep{
141 useFiles: true,
142 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Cross3047fa22019-04-18 10:56:44 -0700143 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800144 }
145 }
146
Colin Cross3047fa22019-04-18 10:56:44 -0700147 toModule := func(m, r string, aidl android.Path) sdkDep {
Colin Cross6cef4812019-10-17 14:23:50 -0700148 return sdkDep{
Colin Crossfb6d7812019-01-09 22:17:55 -0800149 useModule: true,
Colin Cross6cef4812019-10-17 14:23:50 -0700150 bootclasspath: []string{m, config.DefaultLambdaStubsLibrary},
151 systemModules: "core-current-stubs-system-modules",
152 java9Classpath: []string{m},
Colin Crossfb6d7812019-01-09 22:17:55 -0800153 frameworkResModule: r,
Colin Cross3047fa22019-04-18 10:56:44 -0700154 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800155 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800156 }
157
Colin Cross98fd5742019-01-09 23:04:25 -0800158 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
159 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
Colin Crossff0daf42019-04-02 16:10:56 -0700160 if strings.HasPrefix(v, "system_") && numericSdkVersion != android.FutureApiLevel {
Colin Cross98fd5742019-01-09 23:04:25 -0800161 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
162 if ctx.DeviceSpecific() || ctx.SocSpecific() {
163 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
164 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
165 }
166 }
Colin Crossff0daf42019-04-02 16:10:56 -0700167 if len(allowed_versions) > 0 && !android.InList(strconv.Itoa(numericSdkVersion), allowed_versions) {
Colin Cross98fd5742019-01-09 23:04:25 -0800168 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
169 v, allowed_versions)
170 }
171 }
172
Paul Duffin50c217c2019-06-12 13:25:22 +0100173 if ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
174 v != "" && v != "none" && v != "core_platform" {
Colin Crossfb6d7812019-01-09 22:17:55 -0800175 return toPrebuilt(v)
176 }
177
178 switch v {
179 case "":
180 return sdkDep{
181 useDefaultLibs: true,
182 frameworkResModule: "framework-res",
183 }
Paul Duffin52d398a2019-06-11 12:31:14 +0100184 case "none":
Paul Duffine25c6442019-10-11 13:50:28 +0100185 systemModules := sdkContext.systemModules()
186 if systemModules == "" {
187 ctx.PropertyErrorf("sdk_version",
188 `system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`)
189 } else if systemModules == "none" {
Colin Cross6d8d8c62019-10-28 15:10:03 -0700190 return sdkDep{
191 noStandardLibs: true,
192 }
Paul Duffine25c6442019-10-11 13:50:28 +0100193 }
194
Paul Duffin52d398a2019-06-11 12:31:14 +0100195 return sdkDep{
Colin Cross6d8d8c62019-10-28 15:10:03 -0700196 useModule: true,
Paul Duffin52d398a2019-06-11 12:31:14 +0100197 noStandardLibs: true,
Paul Duffine25c6442019-10-11 13:50:28 +0100198 systemModules: systemModules,
Colin Cross6cef4812019-10-17 14:23:50 -0700199 bootclasspath: []string{systemModules},
Paul Duffin52d398a2019-06-11 12:31:14 +0100200 }
Paul Duffin50c217c2019-06-12 13:25:22 +0100201 case "core_platform":
202 return sdkDep{
203 useDefaultLibs: true,
204 frameworkResModule: "framework-res",
205 noFrameworksLibs: true,
206 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800207 case "current":
Colin Cross3047fa22019-04-18 10:56:44 -0700208 return toModule("android_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800209 case "system_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700210 return toModule("android_system_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800211 case "test_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700212 return toModule("android_test_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800213 case "core_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700214 return toModule("core.current.stubs", "", nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800215 default:
216 return toPrebuilt(v)
217 }
218}
Colin Cross98fd5742019-01-09 23:04:25 -0800219
Colin Cross3047fa22019-04-18 10:56:44 -0700220func sdkPreSingletonFactory() android.Singleton {
221 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800222}
223
Colin Cross3047fa22019-04-18 10:56:44 -0700224type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800225
Colin Cross3047fa22019-04-18 10:56:44 -0700226func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800227 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
228 if err != nil {
229 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
230 }
231
232 var sdkVersions []int
233 for _, sdkJar := range sdkJars {
234 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
235 v, err := strconv.Atoi(dir)
236 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
237 continue
238 } else if err != nil {
239 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
240 }
241 sdkVersions = append(sdkVersions, v)
242 }
243
244 sort.Ints(sdkVersions)
245
Colin Cross3047fa22019-04-18 10:56:44 -0700246 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
247}
248
249func sdkSingletonFactory() android.Singleton {
250 return sdkSingleton{}
251}
252
253type sdkSingleton struct{}
254
255func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross10932872019-04-18 14:27:12 -0700256 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700257 return
258 }
259
Colin Cross10932872019-04-18 14:27:12 -0700260 createSdkFrameworkAidl(ctx)
261 createAPIFingerprint(ctx)
262}
Colin Cross3047fa22019-04-18 10:56:44 -0700263
Colin Cross10932872019-04-18 14:27:12 -0700264// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
265func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross3047fa22019-04-18 10:56:44 -0700266 stubsModules := []string{
267 "android_stubs_current",
268 "android_test_stubs_current",
269 "android_system_stubs_current",
270 }
271
272 stubsJars := make([]android.Paths, len(stubsModules))
273
274 ctx.VisitAllModules(func(module android.Module) {
275 // Collect dex jar paths for the modules listed above.
276 if j, ok := module.(Dependency); ok {
277 name := ctx.ModuleName(module)
278 if i := android.IndexList(name, stubsModules); i != -1 {
279 stubsJars[i] = j.HeaderJars()
280 }
281 }
282 })
283
284 var missingDeps []string
285
286 for i := range stubsJars {
287 if stubsJars[i] == nil {
288 if ctx.Config().AllowMissingDependencies() {
289 missingDeps = append(missingDeps, stubsModules[i])
290 } else {
291 ctx.Errorf("failed to find dex jar path for module %q",
292 stubsModules[i])
293 }
294 }
295 }
296
297 rule := android.NewRuleBuilder()
298 rule.MissingDeps(missingDeps)
299
300 var aidls android.Paths
301 for _, jars := range stubsJars {
302 for _, jar := range jars {
303 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
304
305 rule.Command().
306 Text("rm -f").Output(aidl)
307 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700308 BuiltTool(ctx, "sdkparcelables").
Colin Cross3047fa22019-04-18 10:56:44 -0700309 Input(jar).
310 Output(aidl)
311
312 aidls = append(aidls, aidl)
313 }
314 }
315
316 combinedAidl := sdkFrameworkAidlPath(ctx)
317 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
318
319 rule.Command().
320 Text("rm -f").Output(tempPath)
321 rule.Command().
322 Text("cat").
323 Inputs(aidls).
324 Text("| sort -u >").
325 Output(tempPath)
326
327 commitChangeForRestat(rule, tempPath, combinedAidl)
328
329 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
330}
331
332func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
333 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
334 return android.PathForOutput(ctx, "framework.aidl")
335 }).(android.OutputPath)
336}
337
Colin Cross10932872019-04-18 14:27:12 -0700338// Create api_fingerprint.txt
339func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Park71b519d2019-04-18 17:25:49 +0900340 out := ApiFingerprintPath(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700341
342 rule := android.NewRuleBuilder()
343
344 rule.Command().
345 Text("rm -f").Output(out)
346 cmd := rule.Command()
347
348 if ctx.Config().PlatformSdkCodename() == "REL" {
349 cmd.Text("echo REL >").Output(out)
350 } else if ctx.Config().IsPdkBuild() {
351 // TODO: get this from the PDK artifacts?
352 cmd.Text("echo PDK >").Output(out)
353 } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
354 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
355 if err != nil {
356 ctx.Errorf("error globbing API files: %s", err)
357 }
358
359 cmd.Text("cat").
360 Inputs(android.PathsForSource(ctx, in)).
Elliott Hughes34b49d12019-09-06 14:42:24 -0700361 Text("| md5sum | cut -d' ' -f1 >").
Colin Cross10932872019-04-18 14:27:12 -0700362 Output(out)
363 } else {
364 // Unbundled build
365 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
366 cmd.Text("echo").
367 Flag(ctx.Config().PlatformPreviewSdkVersion()).
368 Text(">").
369 Output(out)
370 }
371
372 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
373}
374
Jiyong Park71b519d2019-04-18 17:25:49 +0900375func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross10932872019-04-18 14:27:12 -0700376 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
377 return android.PathForOutput(ctx, "api_fingerprint.txt")
378 }).(android.OutputPath)
379}
380
381func sdkMakeVars(ctx android.MakeVarsContext) {
382 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700383 return
384 }
385
386 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Park71b519d2019-04-18 17:25:49 +0900387 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800388}