blob: dab04e7188cb3f83fe6e581662fc0e13330c7027 [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 Cross5c877912019-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 Cross9bdfaf02019-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 Cross9bdfaf02019-04-18 10:56:44 -070031 android.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
32 android.RegisterSingletonType("sdk", sdkSingletonFactory)
Colin Cross5c877912019-04-18 14:27:12 -070033 android.RegisterMakeVarsProvider(pctx, sdkMakeVars)
Colin Cross98fd5742019-01-09 23:04:25 -080034}
35
Colin Cross9bdfaf02019-04-18 10:56:44 -070036var sdkVersionsKey = android.NewOnceKey("sdkVersionsKey")
37var sdkFrameworkAidlPathKey = android.NewOnceKey("sdkFrameworkAidlPathKey")
Colin Cross5c877912019-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 {
41 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
42 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
49func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
50 switch v {
51 case "", "current", "system_current", "test_current", "core_current":
52 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).
60func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
61 switch v {
62 case "", "current", "test_current", "system_current", "core_current":
63 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
74func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
75 n, err := sdkVersionToNumber(ctx, v)
76 if err != nil {
77 return "", err
78 }
79 return strconv.Itoa(n), nil
80}
81
82func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
83 v := sdkContext.sdkVersion()
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 Cross9bdfaf02019-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 Cross9bdfaf02019-04-18 10:56:44 -0700140 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800141 }
142 }
143
Colin Cross9bdfaf02019-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 Cross9bdfaf02019-04-18 10:56:44 -0700150 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800151 }
Colin Cross9bdfaf02019-04-18 10:56:44 -0700152
Colin Crossfb6d7812019-01-09 22:17:55 -0800153 if m == "core.current.stubs" {
154 ret.systemModules = "core-system-modules"
155 } else if m == "core.platform.api.stubs" {
156 ret.systemModules = "core-platform-api-stubs-system-modules"
157 }
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
Colin Cross5c877912019-04-18 14:27:12 -0700176 if ctx.Config().UnbundledBuildUsePrebuiltSdks() && v != "" {
Colin Crossfb6d7812019-01-09 22:17:55 -0800177 return toPrebuilt(v)
178 }
179
180 switch v {
181 case "":
182 return sdkDep{
183 useDefaultLibs: true,
184 frameworkResModule: "framework-res",
Rashed Abdel-Tawab47d35aa2018-08-09 14:08:53 -0700185 lineageResModule: "org.lineageos.platform-res",
Colin Crossfb6d7812019-01-09 22:17:55 -0800186 }
187 case "current":
Colin Cross9bdfaf02019-04-18 10:56:44 -0700188 return toModule("android_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800189 case "system_current":
Colin Cross9bdfaf02019-04-18 10:56:44 -0700190 return toModule("android_system_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800191 case "test_current":
Colin Cross9bdfaf02019-04-18 10:56:44 -0700192 return toModule("android_test_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800193 case "core_current":
Colin Cross9bdfaf02019-04-18 10:56:44 -0700194 return toModule("core.current.stubs", "", nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800195 default:
196 return toPrebuilt(v)
197 }
198}
Colin Cross98fd5742019-01-09 23:04:25 -0800199
Colin Cross9bdfaf02019-04-18 10:56:44 -0700200func sdkPreSingletonFactory() android.Singleton {
201 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800202}
203
Colin Cross9bdfaf02019-04-18 10:56:44 -0700204type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800205
Colin Cross9bdfaf02019-04-18 10:56:44 -0700206func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800207 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
208 if err != nil {
209 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
210 }
211
212 var sdkVersions []int
213 for _, sdkJar := range sdkJars {
214 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
215 v, err := strconv.Atoi(dir)
216 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
217 continue
218 } else if err != nil {
219 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
220 }
221 sdkVersions = append(sdkVersions, v)
222 }
223
224 sort.Ints(sdkVersions)
225
Colin Cross9bdfaf02019-04-18 10:56:44 -0700226 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
227}
228
229func sdkSingletonFactory() android.Singleton {
230 return sdkSingleton{}
231}
232
233type sdkSingleton struct{}
234
235func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross5c877912019-04-18 14:27:12 -0700236 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross9bdfaf02019-04-18 10:56:44 -0700237 return
238 }
239
Colin Cross5c877912019-04-18 14:27:12 -0700240 createSdkFrameworkAidl(ctx)
241 createAPIFingerprint(ctx)
242}
Colin Cross9bdfaf02019-04-18 10:56:44 -0700243
Colin Cross5c877912019-04-18 14:27:12 -0700244// Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
245func createSdkFrameworkAidl(ctx android.SingletonContext) {
Colin Cross9bdfaf02019-04-18 10:56:44 -0700246 stubsModules := []string{
247 "android_stubs_current",
248 "android_test_stubs_current",
249 "android_system_stubs_current",
250 }
251
252 stubsJars := make([]android.Paths, len(stubsModules))
253
254 ctx.VisitAllModules(func(module android.Module) {
255 // Collect dex jar paths for the modules listed above.
256 if j, ok := module.(Dependency); ok {
257 name := ctx.ModuleName(module)
258 if i := android.IndexList(name, stubsModules); i != -1 {
259 stubsJars[i] = j.HeaderJars()
260 }
261 }
262 })
263
264 var missingDeps []string
265
266 for i := range stubsJars {
267 if stubsJars[i] == nil {
268 if ctx.Config().AllowMissingDependencies() {
269 missingDeps = append(missingDeps, stubsModules[i])
270 } else {
271 ctx.Errorf("failed to find dex jar path for module %q",
272 stubsModules[i])
273 }
274 }
275 }
276
277 rule := android.NewRuleBuilder()
278 rule.MissingDeps(missingDeps)
279
280 var aidls android.Paths
281 for _, jars := range stubsJars {
282 for _, jar := range jars {
283 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
284
285 rule.Command().
286 Text("rm -f").Output(aidl)
287 rule.Command().
288 Tool(ctx.Config().HostToolPath(ctx, "sdkparcelables")).
289 Input(jar).
290 Output(aidl)
291
292 aidls = append(aidls, aidl)
293 }
294 }
295
296 combinedAidl := sdkFrameworkAidlPath(ctx)
297 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
298
299 rule.Command().
300 Text("rm -f").Output(tempPath)
301 rule.Command().
302 Text("cat").
303 Inputs(aidls).
304 Text("| sort -u >").
305 Output(tempPath)
306
307 commitChangeForRestat(rule, tempPath, combinedAidl)
308
309 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
310}
311
312func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
313 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
314 return android.PathForOutput(ctx, "framework.aidl")
315 }).(android.OutputPath)
316}
317
Colin Cross5c877912019-04-18 14:27:12 -0700318// Create api_fingerprint.txt
319func createAPIFingerprint(ctx android.SingletonContext) {
Jiyong Parkd37a8822019-04-18 17:25:49 +0900320 out := ApiFingerprintPath(ctx)
Colin Cross5c877912019-04-18 14:27:12 -0700321
322 rule := android.NewRuleBuilder()
323
324 rule.Command().
325 Text("rm -f").Output(out)
326 cmd := rule.Command()
327
328 if ctx.Config().PlatformSdkCodename() == "REL" {
329 cmd.Text("echo REL >").Output(out)
330 } else if ctx.Config().IsPdkBuild() {
331 // TODO: get this from the PDK artifacts?
332 cmd.Text("echo PDK >").Output(out)
333 } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() {
334 in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil)
335 if err != nil {
336 ctx.Errorf("error globbing API files: %s", err)
337 }
338
339 cmd.Text("cat").
340 Inputs(android.PathsForSource(ctx, in)).
341 Text("|")
342
343 if runtime.GOOS == "darwin" {
344 cmd.Text("md5")
345 } else {
346 cmd.Text("md5sum")
347 }
348
349 cmd.Text("| cut -d' ' -f1 >").
350 Output(out)
351 } else {
352 // Unbundled build
353 // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one
354 cmd.Text("echo").
355 Flag(ctx.Config().PlatformPreviewSdkVersion()).
356 Text(">").
357 Output(out)
358 }
359
360 rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt")
361}
362
Jiyong Parkd37a8822019-04-18 17:25:49 +0900363func ApiFingerprintPath(ctx android.PathContext) android.OutputPath {
Colin Cross5c877912019-04-18 14:27:12 -0700364 return ctx.Config().Once(apiFingerprintPathKey, func() interface{} {
365 return android.PathForOutput(ctx, "api_fingerprint.txt")
366 }).(android.OutputPath)
367}
368
369func sdkMakeVars(ctx android.MakeVarsContext) {
370 if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() {
Colin Cross9bdfaf02019-04-18 10:56:44 -0700371 return
372 }
373
374 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Jiyong Parkd37a8822019-04-18 17:25:49 +0900375 ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800376}