blob: 3451774c0685edb760e4c95af69b20823faa0cad [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
42 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
43 minSdkVersion() string
44 // targetSdkVersion returns the target_sdk_version property of the current module, or sdkVersion() if it is not set.
45 targetSdkVersion() string
46}
47
Colin Cross0ea8ba82019-06-06 14:33:29 -070048func sdkVersionOrDefault(ctx android.BaseModuleContext, v string) string {
Colin Crossfb6d7812019-01-09 22:17:55 -080049 switch v {
Paul Duffin50c217c2019-06-12 13:25:22 +010050 case "", "none", "current", "test_current", "system_current", "core_current", "core_platform":
Colin Crossfb6d7812019-01-09 22:17:55 -080051 return ctx.Config().DefaultAppTargetSdk()
52 default:
53 return v
54 }
55}
56
57// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
58// it returns android.FutureApiLevel (10000).
Colin Cross0ea8ba82019-06-06 14:33:29 -070059func sdkVersionToNumber(ctx android.BaseModuleContext, v string) (int, error) {
Colin Crossfb6d7812019-01-09 22:17:55 -080060 switch v {
Paul Duffin50c217c2019-06-12 13:25:22 +010061 case "", "none", "current", "test_current", "system_current", "core_current", "core_platform":
Colin Crossfb6d7812019-01-09 22:17:55 -080062 return ctx.Config().DefaultAppTargetSdkInt(), nil
63 default:
64 n := android.GetNumericSdkVersion(v)
65 if i, err := strconv.Atoi(n); err != nil {
66 return -1, fmt.Errorf("invalid sdk version %q", n)
67 } else {
68 return i, nil
69 }
70 }
71}
72
Colin Cross0ea8ba82019-06-06 14:33:29 -070073func sdkVersionToNumberAsString(ctx android.BaseModuleContext, v string) (string, error) {
Colin Crossfb6d7812019-01-09 22:17:55 -080074 n, err := sdkVersionToNumber(ctx, v)
75 if err != nil {
76 return "", err
77 }
78 return strconv.Itoa(n), nil
79}
80
Colin Cross0ea8ba82019-06-06 14:33:29 -070081func decodeSdkDep(ctx android.BaseModuleContext, sdkContext sdkContext) sdkDep {
Colin Crossfb6d7812019-01-09 22:17:55 -080082 v := sdkContext.sdkVersion()
Paul Duffin5c2f9632019-06-12 14:21:31 +010083
Colin Cross98fd5742019-01-09 23:04:25 -080084 // For PDK builds, use the latest SDK version instead of "current"
85 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -070086 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -080087 latestSdkVersion := 0
88 if len(sdkVersions) > 0 {
89 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
90 }
91 v = strconv.Itoa(latestSdkVersion)
92 }
93
Colin Crossff0daf42019-04-02 16:10:56 -070094 numericSdkVersion, err := sdkVersionToNumber(ctx, v)
Colin Crossfb6d7812019-01-09 22:17:55 -080095 if err != nil {
96 ctx.PropertyErrorf("sdk_version", "%s", err)
97 return sdkDep{}
98 }
99
Colin Crossfb6d7812019-01-09 22:17:55 -0800100 toPrebuilt := func(sdk string) sdkDep {
101 var api, v string
102 if strings.Contains(sdk, "_") {
103 t := strings.Split(sdk, "_")
104 api = t[0]
105 v = t[1]
106 } else {
107 api = "public"
108 v = sdk
109 }
110 dir := filepath.Join("prebuilts", "sdk", v, api)
111 jar := filepath.Join(dir, "android.jar")
112 // There's no aidl for other SDKs yet.
113 // TODO(77525052): Add aidl files for other SDKs too.
114 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
115 aidl := filepath.Join(public_dir, "framework.aidl")
116 jarPath := android.ExistentPathForSource(ctx, jar)
117 aidlPath := android.ExistentPathForSource(ctx, aidl)
118 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
119
120 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
121 return sdkDep{
122 invalidVersion: true,
123 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
124 }
125 }
126
127 if !jarPath.Valid() {
128 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
129 return sdkDep{}
130 }
131
132 if !aidlPath.Valid() {
133 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
134 return sdkDep{}
135 }
136
137 return sdkDep{
138 useFiles: true,
139 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Cross3047fa22019-04-18 10:56:44 -0700140 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800141 }
142 }
143
Colin Cross3047fa22019-04-18 10:56:44 -0700144 toModule := func(m, r string, aidl android.Path) sdkDep {
Colin Crossfb6d7812019-01-09 22:17:55 -0800145 ret := sdkDep{
146 useModule: true,
147 modules: []string{m, config.DefaultLambdaStubsLibrary},
148 systemModules: m + "_system_modules",
149 frameworkResModule: r,
Colin Cross3047fa22019-04-18 10:56:44 -0700150 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800151 }
Colin Cross3047fa22019-04-18 10:56:44 -0700152
Colin Crossfb6d7812019-01-09 22:17:55 -0800153 if m == "core.current.stubs" {
Neil Fullerba88c412018-10-21 22:57:26 +0100154 ret.systemModules = "core-current-stubs-system-modules"
Paul Duffin7aae6e72019-06-12 10:49:53 +0100155 // core_current does not include framework classes.
156 ret.noFrameworksLibs = true
Colin Crossfb6d7812019-01-09 22:17:55 -0800157 }
158 return ret
159 }
160
Colin Cross98fd5742019-01-09 23:04:25 -0800161 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
162 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
Colin Crossff0daf42019-04-02 16:10:56 -0700163 if strings.HasPrefix(v, "system_") && numericSdkVersion != android.FutureApiLevel {
Colin Cross98fd5742019-01-09 23:04:25 -0800164 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
165 if ctx.DeviceSpecific() || ctx.SocSpecific() {
166 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
167 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
168 }
169 }
Colin Crossff0daf42019-04-02 16:10:56 -0700170 if len(allowed_versions) > 0 && !android.InList(strconv.Itoa(numericSdkVersion), allowed_versions) {
Colin Cross98fd5742019-01-09 23:04:25 -0800171 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
172 v, allowed_versions)
173 }
174 }
175
Paul Duffin50c217c2019-06-12 13:25:22 +0100176 if ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
177 v != "" && v != "none" && v != "core_platform" {
Colin Crossfb6d7812019-01-09 22:17:55 -0800178 return toPrebuilt(v)
179 }
180
181 switch v {
182 case "":
183 return sdkDep{
184 useDefaultLibs: true,
185 frameworkResModule: "framework-res",
186 }
Paul Duffin52d398a2019-06-11 12:31:14 +0100187 case "none":
188 return sdkDep{
189 noStandardLibs: true,
190 }
Paul Duffin50c217c2019-06-12 13:25:22 +0100191 case "core_platform":
192 return sdkDep{
193 useDefaultLibs: true,
194 frameworkResModule: "framework-res",
195 noFrameworksLibs: true,
196 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800197 case "current":
Colin Cross3047fa22019-04-18 10:56:44 -0700198 return toModule("android_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800199 case "system_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700200 return toModule("android_system_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800201 case "test_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700202 return toModule("android_test_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800203 case "core_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700204 return toModule("core.current.stubs", "", nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800205 default:
206 return toPrebuilt(v)
207 }
208}
Colin Cross98fd5742019-01-09 23:04:25 -0800209
Colin Cross3047fa22019-04-18 10:56:44 -0700210func sdkPreSingletonFactory() android.Singleton {
211 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800212}
213
Colin Cross3047fa22019-04-18 10:56:44 -0700214type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800215
Colin Cross3047fa22019-04-18 10:56:44 -0700216func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800217 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
218 if err != nil {
219 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
220 }
221
222 var sdkVersions []int
223 for _, sdkJar := range sdkJars {
224 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
225 v, err := strconv.Atoi(dir)
226 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
227 continue
228 } else if err != nil {
229 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
230 }
231 sdkVersions = append(sdkVersions, v)
232 }
233
234 sort.Ints(sdkVersions)
235
Colin Cross3047fa22019-04-18 10:56:44 -0700236 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
237}
238
239func sdkSingletonFactory() android.Singleton {
240 return sdkSingleton{}
241}
242
243type sdkSingleton struct{}
244
245func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross10932872019-04-18 14:27:12 -0700246 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700247 return
248 }
249
Colin Cross10932872019-04-18 14:27:12 -0700250 createSdkFrameworkAidl(ctx)
251 createAPIFingerprint(ctx)
252}
Colin Cross3047fa22019-04-18 10:56:44 -0700253
Colin Cross10932872019-04-18 14:27:12 -0700254// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
255func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross3047fa22019-04-18 10:56:44 -0700256 stubsModules := []string{
257 "android_stubs_current",
258 "android_test_stubs_current",
259 "android_system_stubs_current",
260 }
261
262 stubsJars := make([]android.Paths, len(stubsModules))
263
264 ctx.VisitAllModules(func(module android.Module) {
265 // Collect dex jar paths for the modules listed above.
266 if j, ok := module.(Dependency); ok {
267 name := ctx.ModuleName(module)
268 if i := android.IndexList(name, stubsModules); i != -1 {
269 stubsJars[i] = j.HeaderJars()
270 }
271 }
272 })
273
274 var missingDeps []string
275
276 for i := range stubsJars {
277 if stubsJars[i] == nil {
278 if ctx.Config().AllowMissingDependencies() {
279 missingDeps = append(missingDeps, stubsModules[i])
280 } else {
281 ctx.Errorf("failed to find dex jar path for module %q",
282 stubsModules[i])
283 }
284 }
285 }
286
287 rule := android.NewRuleBuilder()
288 rule.MissingDeps(missingDeps)
289
290 var aidls android.Paths
291 for _, jars := range stubsJars {
292 for _, jar := range jars {
293 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
294
295 rule.Command().
296 Text("rm -f").Output(aidl)
297 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700298 BuiltTool(ctx, "sdkparcelables").
Colin Cross3047fa22019-04-18 10:56:44 -0700299 Input(jar).
300 Output(aidl)
301
302 aidls = append(aidls, aidl)
303 }
304 }
305
306 combinedAidl := sdkFrameworkAidlPath(ctx)
307 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
308
309 rule.Command().
310 Text("rm -f").Output(tempPath)
311 rule.Command().
312 Text("cat").
313 Inputs(aidls).
314 Text("| sort -u >").
315 Output(tempPath)
316
317 commitChangeForRestat(rule, tempPath, combinedAidl)
318
319 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
320}
321
322func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
323 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
324 return android.PathForOutput(ctx, "framework.aidl")
325 }).(android.OutputPath)
326}
327
Colin Cross10932872019-04-18 14:27:12 -0700328// Create api_fingerprint.txt
329func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Park71b519d2019-04-18 17:25:49 +0900330 out := ApiFingerprintPath(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700331
332 rule := android.NewRuleBuilder()
333
334 rule.Command().
335 Text("rm -f").Output(out)
336 cmd := rule.Command()
337
338 if ctx.Config().PlatformSdkCodename() == "REL" {
339 cmd.Text("echo REL >").Output(out)
340 } else if ctx.Config().IsPdkBuild() {
341 // TODO: get this from the PDK artifacts?
342 cmd.Text("echo PDK >").Output(out)
343 } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
344 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
345 if err != nil {
346 ctx.Errorf("error globbing API files: %s", err)
347 }
348
349 cmd.Text("cat").
350 Inputs(android.PathsForSource(ctx, in)).
Elliott Hughes34b49d12019-09-06 14:42:24 -0700351 Text("| md5sum | cut -d' ' -f1 >").
Colin Cross10932872019-04-18 14:27:12 -0700352 Output(out)
353 } else {
354 // Unbundled build
355 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
356 cmd.Text("echo").
357 Flag(ctx.Config().PlatformPreviewSdkVersion()).
358 Text(">").
359 Output(out)
360 }
361
362 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
363}
364
Jiyong Park71b519d2019-04-18 17:25:49 +0900365func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross10932872019-04-18 14:27:12 -0700366 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
367 return android.PathForOutput(ctx, "api_fingerprint.txt")
368 }).(android.OutputPath)
369}
370
371func sdkMakeVars(ctx android.MakeVarsContext) {
372 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700373 return
374 }
375
376 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Park71b519d2019-04-18 17:25:49 +0900377 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800378}