blob: 7b79a49f2f87fa78c00b71bed5f1d26d49d67416 [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 Cross10932872019-04-18 14:27:12 -070022 "runtime"
Colin Cross98fd5742019-01-09 23:04:25 -080023 "sort"
Colin Crossfb6d7812019-01-09 22:17:55 -080024 "strconv"
25 "strings"
Colin Cross3047fa22019-04-18 10:56:44 -070026
27 "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
43 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
44 minSdkVersion() string
45 // targetSdkVersion returns the target_sdk_version property of the current module, or sdkVersion() if it is not set.
46 targetSdkVersion() string
47}
48
Colin Cross0ea8ba82019-06-06 14:33:29 -070049func sdkVersionOrDefault(ctx android.BaseModuleContext, v string) string {
Colin Crossfb6d7812019-01-09 22:17:55 -080050 switch v {
Paul Duffin50c217c2019-06-12 13:25:22 +010051 case "", "none", "current", "test_current", "system_current", "core_current", "core_platform":
Colin Crossfb6d7812019-01-09 22:17:55 -080052 return ctx.Config().DefaultAppTargetSdk()
53 default:
54 return v
55 }
56}
57
58// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
59// it returns android.FutureApiLevel (10000).
Colin Cross0ea8ba82019-06-06 14:33:29 -070060func sdkVersionToNumber(ctx android.BaseModuleContext, v string) (int, error) {
Colin Crossfb6d7812019-01-09 22:17:55 -080061 switch v {
Paul Duffin50c217c2019-06-12 13:25:22 +010062 case "", "none", "current", "test_current", "system_current", "core_current", "core_platform":
Colin Crossfb6d7812019-01-09 22:17:55 -080063 return ctx.Config().DefaultAppTargetSdkInt(), nil
64 default:
65 n := android.GetNumericSdkVersion(v)
66 if i, err := strconv.Atoi(n); err != nil {
67 return -1, fmt.Errorf("invalid sdk version %q", n)
68 } else {
69 return i, nil
70 }
71 }
72}
73
Colin Cross0ea8ba82019-06-06 14:33:29 -070074func sdkVersionToNumberAsString(ctx android.BaseModuleContext, v string) (string, error) {
Colin Crossfb6d7812019-01-09 22:17:55 -080075 n, err := sdkVersionToNumber(ctx, v)
76 if err != nil {
77 return "", err
78 }
79 return strconv.Itoa(n), nil
80}
81
Colin Cross0ea8ba82019-06-06 14:33:29 -070082func decodeSdkDep(ctx android.BaseModuleContext, sdkContext sdkContext) sdkDep {
Colin Crossfb6d7812019-01-09 22:17:55 -080083 v := sdkContext.sdkVersion()
Paul Duffin5c2f9632019-06-12 14:21:31 +010084
Colin Cross98fd5742019-01-09 23:04:25 -080085 // For PDK builds, use the latest SDK version instead of "current"
86 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -070087 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -080088 latestSdkVersion := 0
89 if len(sdkVersions) > 0 {
90 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
91 }
92 v = strconv.Itoa(latestSdkVersion)
93 }
94
Colin Crossff0daf42019-04-02 16:10:56 -070095 numericSdkVersion, err := sdkVersionToNumber(ctx, v)
Colin Crossfb6d7812019-01-09 22:17:55 -080096 if err != nil {
97 ctx.PropertyErrorf("sdk_version", "%s", err)
98 return sdkDep{}
99 }
100
Colin Crossfb6d7812019-01-09 22:17:55 -0800101 toPrebuilt := func(sdk string) sdkDep {
102 var api, v string
103 if strings.Contains(sdk, "_") {
104 t := strings.Split(sdk, "_")
105 api = t[0]
106 v = t[1]
107 } else {
108 api = "public"
109 v = sdk
110 }
111 dir := filepath.Join("prebuilts", "sdk", v, api)
112 jar := filepath.Join(dir, "android.jar")
113 // There's no aidl for other SDKs yet.
114 // TODO(77525052): Add aidl files for other SDKs too.
115 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
116 aidl := filepath.Join(public_dir, "framework.aidl")
117 jarPath := android.ExistentPathForSource(ctx, jar)
118 aidlPath := android.ExistentPathForSource(ctx, aidl)
119 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
120
121 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
122 return sdkDep{
123 invalidVersion: true,
124 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
125 }
126 }
127
128 if !jarPath.Valid() {
129 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
130 return sdkDep{}
131 }
132
133 if !aidlPath.Valid() {
134 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
135 return sdkDep{}
136 }
137
138 return sdkDep{
139 useFiles: true,
140 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Cross3047fa22019-04-18 10:56:44 -0700141 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800142 }
143 }
144
Colin Cross3047fa22019-04-18 10:56:44 -0700145 toModule := func(m, r string, aidl android.Path) sdkDep {
Colin Crossfb6d7812019-01-09 22:17:55 -0800146 ret := sdkDep{
147 useModule: true,
148 modules: []string{m, config.DefaultLambdaStubsLibrary},
149 systemModules: m + "_system_modules",
150 frameworkResModule: r,
Colin Cross3047fa22019-04-18 10:56:44 -0700151 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800152 }
Colin Cross3047fa22019-04-18 10:56:44 -0700153
Colin Crossfb6d7812019-01-09 22:17:55 -0800154 if m == "core.current.stubs" {
Neil Fullerba88c412018-10-21 22:57:26 +0100155 ret.systemModules = "core-current-stubs-system-modules"
Paul Duffin7aae6e72019-06-12 10:49:53 +0100156 // core_current does not include framework classes.
157 ret.noFrameworksLibs = true
Colin Crossfb6d7812019-01-09 22:17:55 -0800158 }
159 return ret
160 }
161
Colin Cross98fd5742019-01-09 23:04:25 -0800162 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
163 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
Colin Crossff0daf42019-04-02 16:10:56 -0700164 if strings.HasPrefix(v, "system_") && numericSdkVersion != android.FutureApiLevel {
Colin Cross98fd5742019-01-09 23:04:25 -0800165 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
166 if ctx.DeviceSpecific() || ctx.SocSpecific() {
167 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
168 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
169 }
170 }
Colin Crossff0daf42019-04-02 16:10:56 -0700171 if len(allowed_versions) > 0 && !android.InList(strconv.Itoa(numericSdkVersion), allowed_versions) {
Colin Cross98fd5742019-01-09 23:04:25 -0800172 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
173 v, allowed_versions)
174 }
175 }
176
Paul Duffin50c217c2019-06-12 13:25:22 +0100177 if ctx.Config().UnbundledBuildUsePrebuiltSdks() &&
178 v != "" && v != "none" && v != "core_platform" {
Colin Crossfb6d7812019-01-09 22:17:55 -0800179 return toPrebuilt(v)
180 }
181
182 switch v {
183 case "":
184 return sdkDep{
185 useDefaultLibs: true,
186 frameworkResModule: "framework-res",
187 }
Paul Duffin52d398a2019-06-11 12:31:14 +0100188 case "none":
189 return sdkDep{
190 noStandardLibs: true,
191 }
Paul Duffin50c217c2019-06-12 13:25:22 +0100192 case "core_platform":
193 return sdkDep{
194 useDefaultLibs: true,
195 frameworkResModule: "framework-res",
196 noFrameworksLibs: true,
197 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800198 case "current":
Colin Cross3047fa22019-04-18 10:56:44 -0700199 return toModule("android_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800200 case "system_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700201 return toModule("android_system_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800202 case "test_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700203 return toModule("android_test_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800204 case "core_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700205 return toModule("core.current.stubs", "", nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800206 default:
207 return toPrebuilt(v)
208 }
209}
Colin Cross98fd5742019-01-09 23:04:25 -0800210
Colin Cross3047fa22019-04-18 10:56:44 -0700211func sdkPreSingletonFactory() android.Singleton {
212 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800213}
214
Colin Cross3047fa22019-04-18 10:56:44 -0700215type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800216
Colin Cross3047fa22019-04-18 10:56:44 -0700217func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800218 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
219 if err != nil {
220 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
221 }
222
223 var sdkVersions []int
224 for _, sdkJar := range sdkJars {
225 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
226 v, err := strconv.Atoi(dir)
227 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
228 continue
229 } else if err != nil {
230 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
231 }
232 sdkVersions = append(sdkVersions, v)
233 }
234
235 sort.Ints(sdkVersions)
236
Colin Cross3047fa22019-04-18 10:56:44 -0700237 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
238}
239
240func sdkSingletonFactory() android.Singleton {
241 return sdkSingleton{}
242}
243
244type sdkSingleton struct{}
245
246func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross10932872019-04-18 14:27:12 -0700247 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700248 return
249 }
250
Colin Cross10932872019-04-18 14:27:12 -0700251 createSdkFrameworkAidl(ctx)
252 createAPIFingerprint(ctx)
253}
Colin Cross3047fa22019-04-18 10:56:44 -0700254
Colin Cross10932872019-04-18 14:27:12 -0700255// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
256func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross3047fa22019-04-18 10:56:44 -0700257 stubsModules := []string{
258 "android_stubs_current",
259 "android_test_stubs_current",
260 "android_system_stubs_current",
261 }
262
263 stubsJars := make([]android.Paths, len(stubsModules))
264
265 ctx.VisitAllModules(func(module android.Module) {
266 // Collect dex jar paths for the modules listed above.
267 if j, ok := module.(Dependency); ok {
268 name := ctx.ModuleName(module)
269 if i := android.IndexList(name, stubsModules); i != -1 {
270 stubsJars[i] = j.HeaderJars()
271 }
272 }
273 })
274
275 var missingDeps []string
276
277 for i := range stubsJars {
278 if stubsJars[i] == nil {
279 if ctx.Config().AllowMissingDependencies() {
280 missingDeps = append(missingDeps, stubsModules[i])
281 } else {
282 ctx.Errorf("failed to find dex jar path for module %q",
283 stubsModules[i])
284 }
285 }
286 }
287
288 rule := android.NewRuleBuilder()
289 rule.MissingDeps(missingDeps)
290
291 var aidls android.Paths
292 for _, jars := range stubsJars {
293 for _, jar := range jars {
294 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
295
296 rule.Command().
297 Text("rm -f").Output(aidl)
298 rule.Command().
299 Tool(ctx.Config().HostToolPath(ctx, "sdkparcelables")).
300 Input(jar).
301 Output(aidl)
302
303 aidls = append(aidls, aidl)
304 }
305 }
306
307 combinedAidl := sdkFrameworkAidlPath(ctx)
308 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
309
310 rule.Command().
311 Text("rm -f").Output(tempPath)
312 rule.Command().
313 Text("cat").
314 Inputs(aidls).
315 Text("| sort -u >").
316 Output(tempPath)
317
318 commitChangeForRestat(rule, tempPath, combinedAidl)
319
320 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
321}
322
323func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
324 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
325 return android.PathForOutput(ctx, "framework.aidl")
326 }).(android.OutputPath)
327}
328
Colin Cross10932872019-04-18 14:27:12 -0700329// Create api_fingerprint.txt
330func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Park71b519d2019-04-18 17:25:49 +0900331 out := ApiFingerprintPath(ctx)
Colin Cross10932872019-04-18 14:27:12 -0700332
333 rule := android.NewRuleBuilder()
334
335 rule.Command().
336 Text("rm -f").Output(out)
337 cmd := rule.Command()
338
339 if ctx.Config().PlatformSdkCodename() == "REL" {
340 cmd.Text("echo REL >").Output(out)
341 } else if ctx.Config().IsPdkBuild() {
342 // TODO: get this from the PDK artifacts?
343 cmd.Text("echo PDK >").Output(out)
344 } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
345 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
346 if err != nil {
347 ctx.Errorf("error globbing API files: %s", err)
348 }
349
350 cmd.Text("cat").
351 Inputs(android.PathsForSource(ctx, in)).
352 Text("|")
353
354 if runtime.GOOS == "darwin" {
355 cmd.Text("md5")
356 } else {
357 cmd.Text("md5sum")
358 }
359
360 cmd.Text("| cut -d' ' -f1 >").
361 Output(out)
362 } else {
363 // Unbundled build
364 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
365 cmd.Text("echo").
366 Flag(ctx.Config().PlatformPreviewSdkVersion()).
367 Text(">").
368 Output(out)
369 }
370
371 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
372}
373
Jiyong Park71b519d2019-04-18 17:25:49 +0900374func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross10932872019-04-18 14:27:12 -0700375 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
376 return android.PathForOutput(ctx, "api_fingerprint.txt")
377 }).(android.OutputPath)
378}
379
380func sdkMakeVars(ctx android.MakeVarsContext) {
381 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross3047fa22019-04-18 10:56:44 -0700382 return
383 }
384
385 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Park71b519d2019-04-18 17:25:49 +0900386 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800387}