blob: d903f456a651e25f72b5f8e201a946f06e4d1305 [file] [log] [blame]
Colin Cross44df5812019-02-15 23:06:46 -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 (
Colin Cross44df5812019-02-15 23:06:46 -080018 "path/filepath"
19 "strings"
Colin Cross2d00f0d2019-05-09 21:50:00 -070020
21 "android/soong/android"
22 "android/soong/dexpreopt"
Colin Cross44df5812019-02-15 23:06:46 -080023)
24
25// dexpreoptGlobalConfig returns the global dexpreopt.config. It is loaded once the first time it is called for any
26// ctx.Config(), and returns the same data for all future calls with the same ctx.Config(). A value can be inserted
27// for tests using setDexpreoptTestGlobalConfig.
28func dexpreoptGlobalConfig(ctx android.PathContext) dexpreopt.GlobalConfig {
Colin Cross2d00f0d2019-05-09 21:50:00 -070029 return dexpreoptGlobalConfigRaw(ctx).global
30}
31
32type globalConfigAndRaw struct {
33 global dexpreopt.GlobalConfig
34 data []byte
35}
36
37func dexpreoptGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
Colin Cross44df5812019-02-15 23:06:46 -080038 return ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} {
39 if f := ctx.Config().DexpreoptGlobalConfig(); f != "" {
40 ctx.AddNinjaFileDeps(f)
Colin Cross2d00f0d2019-05-09 21:50:00 -070041 globalConfig, data, err := dexpreopt.LoadGlobalConfig(ctx, f)
Colin Cross44df5812019-02-15 23:06:46 -080042 if err != nil {
43 panic(err)
44 }
Colin Cross2d00f0d2019-05-09 21:50:00 -070045 return globalConfigAndRaw{globalConfig, data}
Colin Cross44df5812019-02-15 23:06:46 -080046 }
47
48 // No global config filename set, see if there is a test config set
49 return ctx.Config().Once(dexpreoptTestGlobalConfigKey, func() interface{} {
50 // Nope, return a config with preopting disabled
Colin Cross2d00f0d2019-05-09 21:50:00 -070051 return globalConfigAndRaw{dexpreopt.GlobalConfig{
Colin Cross44df5812019-02-15 23:06:46 -080052 DisablePreopt: true,
Colin Cross2d00f0d2019-05-09 21:50:00 -070053 }, nil}
Colin Cross44df5812019-02-15 23:06:46 -080054 })
Colin Cross2d00f0d2019-05-09 21:50:00 -070055 }).(globalConfigAndRaw)
Colin Cross44df5812019-02-15 23:06:46 -080056}
57
58// setDexpreoptTestGlobalConfig sets a GlobalConfig that future calls to dexpreoptGlobalConfig will return. It must
59// be called before the first call to dexpreoptGlobalConfig for the config.
60func setDexpreoptTestGlobalConfig(config android.Config, globalConfig dexpreopt.GlobalConfig) {
Colin Cross2d00f0d2019-05-09 21:50:00 -070061 config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
Colin Cross44df5812019-02-15 23:06:46 -080062}
63
64var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
65var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
66
67// systemServerClasspath returns the on-device locations of the modules in the system server classpath. It is computed
68// once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same
69// ctx.Config().
70func systemServerClasspath(ctx android.PathContext) []string {
71 return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string {
72 global := dexpreoptGlobalConfig(ctx)
73
74 var systemServerClasspathLocations []string
75 for _, m := range global.SystemServerJars {
76 systemServerClasspathLocations = append(systemServerClasspathLocations,
77 filepath.Join("/system/framework", m+".jar"))
78 }
79 return systemServerClasspathLocations
80 })
81}
82
83var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
84
Colin Crossc11e0c52019-05-08 15:18:22 -070085// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
86// supported through native bridge.
87func dexpreoptTargets(ctx android.PathContext) []android.Target {
88 var targets []android.Target
89 for i, target := range ctx.Config().Targets[android.Android] {
90 if ctx.Config().SecondArchIsTranslated() && i > 0 {
91 break
92 }
93
94 if target.NativeBridge == android.NativeBridgeDisabled {
95 targets = append(targets, target)
96 }
97 }
98
99 return targets
100}
101
Colin Cross44df5812019-02-15 23:06:46 -0800102// defaultBootImageConfig returns the bootImageConfig that will be used to dexpreopt modules. It is computed once the
103// first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same
104// ctx.Config().
105func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
106 return ctx.Config().Once(defaultBootImageConfigKey, func() interface{} {
107 global := dexpreoptGlobalConfig(ctx)
108
109 runtimeModules := global.RuntimeApexJars
110 nonFrameworkModules := concat(runtimeModules, global.ProductUpdatableBootModules)
111 frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules)
112
113 var nonUpdatableBootModules []string
114 var nonUpdatableBootLocations []string
115
116 for _, m := range runtimeModules {
117 nonUpdatableBootModules = append(nonUpdatableBootModules, m)
118 nonUpdatableBootLocations = append(nonUpdatableBootLocations,
119 filepath.Join("/apex/com.android.runtime/javalib", m+".jar"))
120 }
121
122 for _, m := range frameworkModules {
123 nonUpdatableBootModules = append(nonUpdatableBootModules, m)
124 nonUpdatableBootLocations = append(nonUpdatableBootLocations,
125 filepath.Join("/system/framework", m+".jar"))
126 }
127
128 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
129 // the bootclasspath modules have been compiled. Set up known paths for them, the singleton rules will copy
130 // them there.
131 // TODO: use module dependencies instead
132 var nonUpdatableBootDexPaths android.WritablePaths
133 for _, m := range nonUpdatableBootModules {
134 nonUpdatableBootDexPaths = append(nonUpdatableBootDexPaths,
135 android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_input", m+".jar"))
136 }
137
138 dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars")
139 symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_unstripped")
140 images := make(map[android.ArchType]android.OutputPath)
Colin Crossdf8eebe2019-04-09 15:29:41 -0700141 zip := dir.Join(ctx, "boot.zip")
Colin Cross44df5812019-02-15 23:06:46 -0800142
Colin Crossc11e0c52019-05-08 15:18:22 -0700143 targets := dexpreoptTargets(ctx)
144
145 for _, target := range targets {
Colin Cross44df5812019-02-15 23:06:46 -0800146 images[target.Arch.ArchType] = dir.Join(ctx,
Colin Cross2cdd5df2019-02-25 10:25:24 -0800147 "system/framework", target.Arch.ArchType.String()).Join(ctx, "boot.art")
Colin Cross44df5812019-02-15 23:06:46 -0800148 }
149
150 return bootImageConfig{
151 name: "boot",
152 modules: nonUpdatableBootModules,
153 dexLocations: nonUpdatableBootLocations,
154 dexPaths: nonUpdatableBootDexPaths,
155 dir: dir,
156 symbolsDir: symbolsDir,
157 images: images,
Colin Crossc11e0c52019-05-08 15:18:22 -0700158 targets: targets,
Colin Crossdf8eebe2019-04-09 15:29:41 -0700159 zip: zip,
Colin Cross44df5812019-02-15 23:06:46 -0800160 }
161 }).(bootImageConfig)
162}
163
164var defaultBootImageConfigKey = android.NewOnceKey("defaultBootImageConfig")
165
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000166func apexBootImageConfig(ctx android.PathContext) bootImageConfig {
167 return ctx.Config().Once(apexBootImageConfigKey, func() interface{} {
168 global := dexpreoptGlobalConfig(ctx)
169
170 runtimeModules := global.RuntimeApexJars
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100171 nonFrameworkModules := concat(runtimeModules, global.ProductUpdatableBootModules)
172 frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules)
173 imageModules := concat(runtimeModules, frameworkModules)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000174
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100175 var bootLocations []string
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000176
177 for _, m := range runtimeModules {
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100178 bootLocations = append(bootLocations,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000179 filepath.Join("/apex/com.android.runtime/javalib", m+".jar"))
180 }
181
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100182 for _, m := range frameworkModules {
183 bootLocations = append(bootLocations,
184 filepath.Join("/system/framework", m+".jar"))
185 }
186
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000187 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
188 // the bootclasspath modules have been compiled. Set up known paths for them, the singleton rules will copy
189 // them there.
190 // TODO: use module dependencies instead
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100191 var bootDexPaths android.WritablePaths
192 for _, m := range imageModules {
193 bootDexPaths = append(bootDexPaths,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000194 android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_input", m+".jar"))
195 }
196
197 dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars")
198 symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_unstripped")
199 images := make(map[android.ArchType]android.OutputPath)
200
Colin Crossc11e0c52019-05-08 15:18:22 -0700201 targets := dexpreoptTargets(ctx)
202
203 for _, target := range targets {
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000204 images[target.Arch.ArchType] = dir.Join(ctx,
205 "system/framework", target.Arch.ArchType.String(), "apex.art")
206 }
207
208 return bootImageConfig{
209 name: "apex",
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100210 modules: imageModules,
211 dexLocations: bootLocations,
212 dexPaths: bootDexPaths,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000213 dir: dir,
214 symbolsDir: symbolsDir,
Colin Crossc11e0c52019-05-08 15:18:22 -0700215 targets: targets,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000216 images: images,
217 }
218 }).(bootImageConfig)
219}
220
221var apexBootImageConfigKey = android.NewOnceKey("apexBootImageConfig")
222
Colin Cross44df5812019-02-15 23:06:46 -0800223func defaultBootclasspath(ctx android.PathContext) []string {
224 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
225 global := dexpreoptGlobalConfig(ctx)
226 image := defaultBootImageConfig(ctx)
227 bootclasspath := append(copyOf(image.dexLocations), global.ProductUpdatableBootLocations...)
228 return bootclasspath
229 })
230}
231
232var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
233
234var copyOf = android.CopyOf
235
236func init() {
237 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
238}
239
240func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
241 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Nicolas Geoffray07b40072019-02-22 16:57:42 +0000242 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocations, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800243 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800244
245 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800246}