blob: d8584d2b053ce480b82b6b7defab8ed3b2d63b05 [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
Colin Cross0ea8ba82019-06-06 14:33:29 -070050func sdkVersionOrDefault(ctx android.BaseModuleContext, v string) string {
Colin Crossfb6d7812019-01-09 22:17:55 -080051 switch v {
Paul Duffin50c217c2019-06-12 13:25:22 +010052 case "", "none", "current", "test_current", "system_current", "core_current", "core_platform":
Colin Crossfb6d7812019-01-09 22:17:55 -080053 return ctx.Config().DefaultAppTargetSdk()
54 default:
55 return v
56 }
57}
58
59// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
60// it returns android.FutureApiLevel (10000).
Colin Cross0ea8ba82019-06-06 14:33:29 -070061func sdkVersionToNumber(ctx android.BaseModuleContext, v string) (int, error) {
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":
Colin Crossfb6d7812019-01-09 22:17:55 -080064 return ctx.Config().DefaultAppTargetSdkInt(), nil
65 default:
66 n := android.GetNumericSdkVersion(v)
67 if i, err := strconv.Atoi(n); err != nil {
68 return -1, fmt.Errorf("invalid sdk version %q", n)
69 } else {
70 return i, nil
71 }
72 }
73}
74
Colin Cross0ea8ba82019-06-06 14:33:29 -070075func sdkVersionToNumberAsString(ctx android.BaseModuleContext, v string) (string, error) {
Colin Crossfb6d7812019-01-09 22:17:55 -080076 n, err := sdkVersionToNumber(ctx, v)
77 if err != nil {
78 return "", err
79 }
80 return strconv.Itoa(n), nil
81}
82
Colin Cross0ea8ba82019-06-06 14:33:29 -070083func decodeSdkDep(ctx android.BaseModuleContext, sdkContext sdkContext) sdkDep {
Colin Crossfb6d7812019-01-09 22:17:55 -080084 v := sdkContext.sdkVersion()
Paul Duffin5c2f9632019-06-12 14:21:31 +010085
Colin Cross98fd5742019-01-09 23:04:25 -080086 // For PDK builds, use the latest SDK version instead of "current"
87 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -070088 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -080089 latestSdkVersion := 0
90 if len(sdkVersions) > 0 {
91 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
92 }
93 v = strconv.Itoa(latestSdkVersion)
94 }
95
Colin Crossff0daf42019-04-02 16:10:56 -070096 numericSdkVersion, err := sdkVersionToNumber(ctx, v)
Colin Crossfb6d7812019-01-09 22:17:55 -080097 if err != nil {
98 ctx.PropertyErrorf("sdk_version", "%s", err)
99 return sdkDep{}
100 }
101
Colin Crossfb6d7812019-01-09 22:17:55 -0800102 toPrebuilt := func(sdk string) sdkDep {
103 var api, v string
104 if strings.Contains(sdk, "_") {
105 t := strings.Split(sdk, "_")
106 api = t[0]
107 v = t[1]
108 } else {
109 api = "public"
110 v = sdk
111 }
112 dir := filepath.Join("prebuilts", "sdk", v, api)
113 jar := filepath.Join(dir, "android.jar")
114 // There's no aidl for other SDKs yet.
115 // TODO(77525052): Add aidl files for other SDKs too.
116 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
117 aidl := filepath.Join(public_dir, "framework.aidl")
118 jarPath := android.ExistentPathForSource(ctx, jar)
119 aidlPath := android.ExistentPathForSource(ctx, aidl)
120 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
121
122 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
123 return sdkDep{
124 invalidVersion: true,
125 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
126 }
127 }
128
129 if !jarPath.Valid() {
130 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
131 return sdkDep{}
132 }
133
134 if !aidlPath.Valid() {
135 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
136 return sdkDep{}
137 }
138
139 return sdkDep{
140 useFiles: true,
141 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Cross3047fa22019-04-18 10:56:44 -0700142 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800143 }
144 }
145
Colin Cross3047fa22019-04-18 10:56:44 -0700146 toModule := func(m, r string, aidl android.Path) sdkDep {
Colin Crossfb6d7812019-01-09 22:17:55 -0800147 ret := sdkDep{
148 useModule: true,
149 modules: []string{m, config.DefaultLambdaStubsLibrary},
150 systemModules: m + "_system_modules",
151 frameworkResModule: r,
Colin Cross3047fa22019-04-18 10:56:44 -0700152 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800153 }
Colin Cross3047fa22019-04-18 10:56:44 -0700154
Colin Crossfb6d7812019-01-09 22:17:55 -0800155 if m == "core.current.stubs" {
Neil Fullerba88c412018-10-21 22:57:26 +0100156 ret.systemModules = "core-current-stubs-system-modules"
Paul Duffin7aae6e72019-06-12 10:49:53 +0100157 // core_current does not include framework classes.
158 ret.noFrameworksLibs = true
Colin Crossfb6d7812019-01-09 22:17:55 -0800159 }
160 return ret
161 }
162
Colin Cross98fd5742019-01-09 23:04:25 -0800163 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
164 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
Colin Crossff0daf42019-04-02 16:10:56 -0700165 if strings.HasPrefix(v, "system_") && numericSdkVersion != android.FutureApiLevel {
Colin Cross98fd5742019-01-09 23:04:25 -0800166 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
167 if ctx.DeviceSpecific() || ctx.SocSpecific() {
168 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
169 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
170 }
171 }
Colin Crossff0daf42019-04-02 16:10:56 -0700172 if len(allowed_versions) > 0 && !android.InList(strconv.Itoa(numericSdkVersion), allowed_versions) {
Colin Cross98fd5742019-01-09 23:04:25 -0800173 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
174 v, allowed_versions)
175 }
176 }
177
Paul Duffin50c217c2019-06-12 13:25:22 +0100178 if ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
179 v != "" && v != "none" && v != "core_platform" {
Colin Crossfb6d7812019-01-09 22:17:55 -0800180 return toPrebuilt(v)
181 }
182
183 switch v {
184 case "":
185 return sdkDep{
186 useDefaultLibs: true,
187 frameworkResModule: "framework-res",
188 }
Paul Duffin52d398a2019-06-11 12:31:14 +0100189 case "none":
Paul Duffine25c6442019-10-11 13:50:28 +0100190 systemModules := sdkContext.systemModules()
191 if systemModules == "" {
192 ctx.PropertyErrorf("sdk_version",
193 `system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`)
194 } else if systemModules == "none" {
Colin Cross6d8d8c62019-10-28 15:10:03 -0700195 return sdkDep{
196 noStandardLibs: true,
197 }
Paul Duffine25c6442019-10-11 13:50:28 +0100198 }
199
Paul Duffin52d398a2019-06-11 12:31:14 +0100200 return sdkDep{
Colin Cross6d8d8c62019-10-28 15:10:03 -0700201 useModule: true,
Paul Duffin52d398a2019-06-11 12:31:14 +0100202 noStandardLibs: true,
Paul Duffine25c6442019-10-11 13:50:28 +0100203 systemModules: systemModules,
Colin Cross6d8d8c62019-10-28 15:10:03 -0700204 modules: []string{systemModules},
Paul Duffin52d398a2019-06-11 12:31:14 +0100205 }
Paul Duffin50c217c2019-06-12 13:25:22 +0100206 case "core_platform":
207 return sdkDep{
208 useDefaultLibs: true,
209 frameworkResModule: "framework-res",
210 noFrameworksLibs: true,
211 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800212 case "current":
Colin Cross3047fa22019-04-18 10:56:44 -0700213 return toModule("android_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800214 case "system_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700215 return toModule("android_system_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800216 case "test_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700217 return toModule("android_test_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800218 case "core_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700219 return toModule("core.current.stubs", "", nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800220 default:
221 return toPrebuilt(v)
222 }
223}
Colin Cross98fd5742019-01-09 23:04:25 -0800224
Colin Cross3047fa22019-04-18 10:56:44 -0700225func sdkPreSingletonFactory() android.Singleton {
226 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800227}
228
Colin Cross3047fa22019-04-18 10:56:44 -0700229type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800230
Colin Cross3047fa22019-04-18 10:56:44 -0700231func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800232 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
233 if err != nil {
234 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
235 }
236
237 var sdkVersions []int
238 for _, sdkJar := range sdkJars {
239 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
240 v, err := strconv.Atoi(dir)
241 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
242 continue
243 } else if err != nil {
244 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
245 }
246 sdkVersions = append(sdkVersions, v)
247 }
248
249 sort.Ints(sdkVersions)
250
Colin Cross3047fa22019-04-18 10:56:44 -0700251 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
252}
253
254func sdkSingletonFactory() android.Singleton {
255 return sdkSingleton{}
256}
257
258type sdkSingleton struct{}
259
260func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross10932872019-04-18 14:27:12 -0700261 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700262 return
263 }
264
Colin Cross10932872019-04-18 14:27:12 -0700265 createSdkFrameworkAidl(ctx)
266 createAPIFingerprint(ctx)
267}
Colin Cross3047fa22019-04-18 10:56:44 -0700268
Colin Cross10932872019-04-18 14:27:12 -0700269// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
270func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross3047fa22019-04-18 10:56:44 -0700271 stubsModules := []string{
272 "android_stubs_current",
273 "android_test_stubs_current",
274 "android_system_stubs_current",
275 }
276
277 stubsJars := make([]android.Paths, len(stubsModules))
278
279 ctx.VisitAllModules(func(module android.Module) {
280 // Collect dex jar paths for the modules listed above.
281 if j, ok := module.(Dependency); ok {
282 name := ctx.ModuleName(module)
283 if i := android.IndexList(name, stubsModules); i != -1 {
284 stubsJars[i] = j.HeaderJars()
285 }
286 }
287 })
288
289 var missingDeps []string
290
291 for i := range stubsJars {
292 if stubsJars[i] == nil {
293 if ctx.Config().AllowMissingDependencies() {
294 missingDeps = append(missingDeps, stubsModules[i])
295 } else {
296 ctx.Errorf("failed to find dex jar path for module %q",
297 stubsModules[i])
298 }
299 }
300 }
301
302 rule := android.NewRuleBuilder()
303 rule.MissingDeps(missingDeps)
304
305 var aidls android.Paths
306 for _, jars := range stubsJars {
307 for _, jar := range jars {
308 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
309
310 rule.Command().
311 Text("rm -f").Output(aidl)
312 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700313 BuiltTool(ctx, "sdkparcelables").
Colin Cross3047fa22019-04-18 10:56:44 -0700314 Input(jar).
315 Output(aidl)
316
317 aidls = append(aidls, aidl)
318 }
319 }
320
321 combinedAidl := sdkFrameworkAidlPath(ctx)
322 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
323
324 rule.Command().
325 Text("rm -f").Output(tempPath)
326 rule.Command().
327 Text("cat").
328 Inputs(aidls).
329 Text("| sort -u >").
330 Output(tempPath)
331
332 commitChangeForRestat(rule, tempPath, combinedAidl)
333
334 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
335}
336
337func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
338 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
339 return android.PathForOutput(ctx, "framework.aidl")
340 }).(android.OutputPath)
341}
342
Colin Cross10932872019-04-18 14:27:12 -0700343// Create api_fingerprint.txt
344func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Park71b519d2019-04-18 17:25:49 +0900345 out := ApiFingerprintPath(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700346
347 rule := android.NewRuleBuilder()
348
349 rule.Command().
350 Text("rm -f").Output(out)
351 cmd := rule.Command()
352
353 if ctx.Config().PlatformSdkCodename() == "REL" {
354 cmd.Text("echo REL >").Output(out)
355 } else if ctx.Config().IsPdkBuild() {
356 // TODO: get this from the PDK artifacts?
357 cmd.Text("echo PDK >").Output(out)
358 } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
359 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
360 if err != nil {
361 ctx.Errorf("error globbing API files: %s", err)
362 }
363
364 cmd.Text("cat").
365 Inputs(android.PathsForSource(ctx, in)).
Elliott Hughes34b49d12019-09-06 14:42:24 -0700366 Text("| md5sum | cut -d' ' -f1 >").
Colin Cross10932872019-04-18 14:27:12 -0700367 Output(out)
368 } else {
369 // Unbundled build
370 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
371 cmd.Text("echo").
372 Flag(ctx.Config().PlatformPreviewSdkVersion()).
373 Text(">").
374 Output(out)
375 }
376
377 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
378}
379
Jiyong Park71b519d2019-04-18 17:25:49 +0900380func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross10932872019-04-18 14:27:12 -0700381 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
382 return android.PathForOutput(ctx, "api_fingerprint.txt")
383 }).(android.OutputPath)
384}
385
386func sdkMakeVars(ctx android.MakeVarsContext) {
387 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700388 return
389 }
390
391 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Park71b519d2019-04-18 17:25:49 +0900392 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800393}