blob: d50bcaece5372e53c6de47aa1f4b76cb4fe4ac6e [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)
32 android.RegisterMakeVarsProvider(pctx, sdkFrameworkAidlMakeVars)
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 Cross98fd5742019-01-09 23:04:25 -080037
Colin Crossfb6d7812019-01-09 22:17:55 -080038type sdkContext interface {
39 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
40 sdkVersion() string
41 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
42 minSdkVersion() string
43 // targetSdkVersion returns the target_sdk_version property of the current module, or sdkVersion() if it is not set.
44 targetSdkVersion() string
45}
46
47func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
48 switch v {
49 case "", "current", "system_current", "test_current", "core_current":
50 return ctx.Config().DefaultAppTargetSdk()
51 default:
52 return v
53 }
54}
55
56// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
57// it returns android.FutureApiLevel (10000).
58func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
59 switch v {
60 case "", "current", "test_current", "system_current", "core_current":
61 return ctx.Config().DefaultAppTargetSdkInt(), nil
62 default:
63 n := android.GetNumericSdkVersion(v)
64 if i, err := strconv.Atoi(n); err != nil {
65 return -1, fmt.Errorf("invalid sdk version %q", n)
66 } else {
67 return i, nil
68 }
69 }
70}
71
72func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
73 n, err := sdkVersionToNumber(ctx, v)
74 if err != nil {
75 return "", err
76 }
77 return strconv.Itoa(n), nil
78}
79
80func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
81 v := sdkContext.sdkVersion()
Colin Cross98fd5742019-01-09 23:04:25 -080082 // For PDK builds, use the latest SDK version instead of "current"
83 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -070084 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -080085 latestSdkVersion := 0
86 if len(sdkVersions) > 0 {
87 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
88 }
89 v = strconv.Itoa(latestSdkVersion)
90 }
91
Colin Crossff0daf42019-04-02 16:10:56 -070092 numericSdkVersion, err := sdkVersionToNumber(ctx, v)
Colin Crossfb6d7812019-01-09 22:17:55 -080093 if err != nil {
94 ctx.PropertyErrorf("sdk_version", "%s", err)
95 return sdkDep{}
96 }
97
Colin Crossfb6d7812019-01-09 22:17:55 -080098 toPrebuilt := func(sdk string) sdkDep {
99 var api, v string
100 if strings.Contains(sdk, "_") {
101 t := strings.Split(sdk, "_")
102 api = t[0]
103 v = t[1]
104 } else {
105 api = "public"
106 v = sdk
107 }
108 dir := filepath.Join("prebuilts", "sdk", v, api)
109 jar := filepath.Join(dir, "android.jar")
110 // There's no aidl for other SDKs yet.
111 // TODO(77525052): Add aidl files for other SDKs too.
112 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
113 aidl := filepath.Join(public_dir, "framework.aidl")
114 jarPath := android.ExistentPathForSource(ctx, jar)
115 aidlPath := android.ExistentPathForSource(ctx, aidl)
116 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
117
118 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
119 return sdkDep{
120 invalidVersion: true,
121 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
122 }
123 }
124
125 if !jarPath.Valid() {
126 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
127 return sdkDep{}
128 }
129
130 if !aidlPath.Valid() {
131 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
132 return sdkDep{}
133 }
134
135 return sdkDep{
136 useFiles: true,
137 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Cross3047fa22019-04-18 10:56:44 -0700138 aidl: android.OptionalPathForPath(aidlPath.Path()),
Colin Crossfb6d7812019-01-09 22:17:55 -0800139 }
140 }
141
Colin Cross3047fa22019-04-18 10:56:44 -0700142 toModule := func(m, r string, aidl android.Path) sdkDep {
Colin Crossfb6d7812019-01-09 22:17:55 -0800143 ret := sdkDep{
144 useModule: true,
145 modules: []string{m, config.DefaultLambdaStubsLibrary},
146 systemModules: m + "_system_modules",
147 frameworkResModule: r,
Colin Cross3047fa22019-04-18 10:56:44 -0700148 aidl: android.OptionalPathForPath(aidl),
Colin Crossfb6d7812019-01-09 22:17:55 -0800149 }
Colin Cross3047fa22019-04-18 10:56:44 -0700150
Colin Crossfb6d7812019-01-09 22:17:55 -0800151 if m == "core.current.stubs" {
152 ret.systemModules = "core-system-modules"
153 } else if m == "core.platform.api.stubs" {
154 ret.systemModules = "core-platform-api-stubs-system-modules"
155 }
156 return ret
157 }
158
Colin Cross98fd5742019-01-09 23:04:25 -0800159 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
160 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
Colin Crossff0daf42019-04-02 16:10:56 -0700161 if strings.HasPrefix(v, "system_") && numericSdkVersion != android.FutureApiLevel {
Colin Cross98fd5742019-01-09 23:04:25 -0800162 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
163 if ctx.DeviceSpecific() || ctx.SocSpecific() {
164 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
165 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
166 }
167 }
Colin Crossff0daf42019-04-02 16:10:56 -0700168 if len(allowed_versions) > 0 && !android.InList(strconv.Itoa(numericSdkVersion), allowed_versions) {
Colin Cross98fd5742019-01-09 23:04:25 -0800169 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
170 v, allowed_versions)
171 }
172 }
173
Colin Crossfb6d7812019-01-09 22:17:55 -0800174 if ctx.Config().UnbundledBuildPrebuiltSdks() && v != "" {
175 return toPrebuilt(v)
176 }
177
178 switch v {
179 case "":
180 return sdkDep{
181 useDefaultLibs: true,
182 frameworkResModule: "framework-res",
183 }
184 case "current":
Colin Cross3047fa22019-04-18 10:56:44 -0700185 return toModule("android_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800186 case "system_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700187 return toModule("android_system_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800188 case "test_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700189 return toModule("android_test_stubs_current", "framework-res", sdkFrameworkAidlPath(ctx))
Colin Crossfb6d7812019-01-09 22:17:55 -0800190 case "core_current":
Colin Cross3047fa22019-04-18 10:56:44 -0700191 return toModule("core.current.stubs", "", nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800192 default:
193 return toPrebuilt(v)
194 }
195}
Colin Cross98fd5742019-01-09 23:04:25 -0800196
Colin Cross3047fa22019-04-18 10:56:44 -0700197func sdkPreSingletonFactory() android.Singleton {
198 return sdkPreSingleton{}
Colin Cross98fd5742019-01-09 23:04:25 -0800199}
200
Colin Cross3047fa22019-04-18 10:56:44 -0700201type sdkPreSingleton struct{}
Colin Cross98fd5742019-01-09 23:04:25 -0800202
Colin Cross3047fa22019-04-18 10:56:44 -0700203func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross98fd5742019-01-09 23:04:25 -0800204 sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil)
205 if err != nil {
206 ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error())
207 }
208
209 var sdkVersions []int
210 for _, sdkJar := range sdkJars {
211 dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar)))
212 v, err := strconv.Atoi(dir)
213 if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax {
214 continue
215 } else if err != nil {
216 ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error())
217 }
218 sdkVersions = append(sdkVersions, v)
219 }
220
221 sort.Ints(sdkVersions)
222
Colin Cross3047fa22019-04-18 10:56:44 -0700223 ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions })
224}
225
226func sdkSingletonFactory() android.Singleton {
227 return sdkSingleton{}
228}
229
230type sdkSingleton struct{}
231
232func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) {
233 if ctx.Config().UnbundledBuildPrebuiltSdks() || ctx.Config().IsPdkBuild() {
234 return
235 }
236
237 // Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules.
238
239 stubsModules := []string{
240 "android_stubs_current",
241 "android_test_stubs_current",
242 "android_system_stubs_current",
243 }
244
245 stubsJars := make([]android.Paths, len(stubsModules))
246
247 ctx.VisitAllModules(func(module android.Module) {
248 // Collect dex jar paths for the modules listed above.
249 if j, ok := module.(Dependency); ok {
250 name := ctx.ModuleName(module)
251 if i := android.IndexList(name, stubsModules); i != -1 {
252 stubsJars[i] = j.HeaderJars()
253 }
254 }
255 })
256
257 var missingDeps []string
258
259 for i := range stubsJars {
260 if stubsJars[i] == nil {
261 if ctx.Config().AllowMissingDependencies() {
262 missingDeps = append(missingDeps, stubsModules[i])
263 } else {
264 ctx.Errorf("failed to find dex jar path for module %q",
265 stubsModules[i])
266 }
267 }
268 }
269
270 rule := android.NewRuleBuilder()
271 rule.MissingDeps(missingDeps)
272
273 var aidls android.Paths
274 for _, jars := range stubsJars {
275 for _, jar := range jars {
276 aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl"))
277
278 rule.Command().
279 Text("rm -f").Output(aidl)
280 rule.Command().
281 Tool(ctx.Config().HostToolPath(ctx, "sdkparcelables")).
282 Input(jar).
283 Output(aidl)
284
285 aidls = append(aidls, aidl)
286 }
287 }
288
289 combinedAidl := sdkFrameworkAidlPath(ctx)
290 tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp")
291
292 rule.Command().
293 Text("rm -f").Output(tempPath)
294 rule.Command().
295 Text("cat").
296 Inputs(aidls).
297 Text("| sort -u >").
298 Output(tempPath)
299
300 commitChangeForRestat(rule, tempPath, combinedAidl)
301
302 rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl")
303}
304
305func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath {
306 return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} {
307 return android.PathForOutput(ctx, "framework.aidl")
308 }).(android.OutputPath)
309}
310
311func sdkFrameworkAidlMakeVars(ctx android.MakeVarsContext) {
312 if ctx.Config().UnbundledBuildPrebuiltSdks() || ctx.Config().IsPdkBuild() {
313 return
314 }
315
316 ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String())
Colin Cross98fd5742019-01-09 23:04:25 -0800317}