blob: c396d3ea03e9d5fe5fea53f82de875329c58bad0 [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")
Colin Crossdf8eebe2019-04-09 15:29:41 -0700140 zip := dir.Join(ctx, "boot.zip")
Colin Cross44df5812019-02-15 23:06:46 -0800141
Colin Crossc11e0c52019-05-08 15:18:22 -0700142 targets := dexpreoptTargets(ctx)
143
Dan Willemsen0f416782019-06-13 21:44:53 +0000144 imageConfig := bootImageConfig{
Colin Cross44df5812019-02-15 23:06:46 -0800145 name: "boot",
146 modules: nonUpdatableBootModules,
147 dexLocations: nonUpdatableBootLocations,
148 dexPaths: nonUpdatableBootDexPaths,
149 dir: dir,
150 symbolsDir: symbolsDir,
Dan Willemsen0f416782019-06-13 21:44:53 +0000151 images: make(map[android.ArchType]android.OutputPath),
152 imagesDeps: make(map[android.ArchType]android.Paths),
Colin Crossc11e0c52019-05-08 15:18:22 -0700153 targets: targets,
Colin Crossdf8eebe2019-04-09 15:29:41 -0700154 zip: zip,
Colin Cross44df5812019-02-15 23:06:46 -0800155 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000156
157 for _, target := range targets {
158 imageDir := dir.Join(ctx, "system/framework", target.Arch.ArchType.String())
159 imageConfig.images[target.Arch.ArchType] = imageDir.Join(ctx, "boot.art")
160
161 imagesDeps := make([]android.Path, 0, len(imageConfig.modules)*3)
162 for _, dep := range imageConfig.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex") {
163 imagesDeps = append(imagesDeps, dep)
164 }
165 imageConfig.imagesDeps[target.Arch.ArchType] = imagesDeps
166 }
167
168 return imageConfig
Colin Cross44df5812019-02-15 23:06:46 -0800169 }).(bootImageConfig)
170}
171
172var defaultBootImageConfigKey = android.NewOnceKey("defaultBootImageConfig")
173
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000174func apexBootImageConfig(ctx android.PathContext) bootImageConfig {
175 return ctx.Config().Once(apexBootImageConfigKey, func() interface{} {
176 global := dexpreoptGlobalConfig(ctx)
177
178 runtimeModules := global.RuntimeApexJars
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100179 nonFrameworkModules := concat(runtimeModules, global.ProductUpdatableBootModules)
180 frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules)
181 imageModules := concat(runtimeModules, frameworkModules)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000182
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100183 var bootLocations []string
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000184
185 for _, m := range runtimeModules {
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100186 bootLocations = append(bootLocations,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000187 filepath.Join("/apex/com.android.runtime/javalib", m+".jar"))
188 }
189
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100190 for _, m := range frameworkModules {
191 bootLocations = append(bootLocations,
192 filepath.Join("/system/framework", m+".jar"))
193 }
194
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000195 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
196 // the bootclasspath modules have been compiled. Set up known paths for them, the singleton rules will copy
197 // them there.
198 // TODO: use module dependencies instead
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100199 var bootDexPaths android.WritablePaths
200 for _, m := range imageModules {
201 bootDexPaths = append(bootDexPaths,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000202 android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_input", m+".jar"))
203 }
204
205 dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars")
206 symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_unstripped")
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000207
Colin Crossc11e0c52019-05-08 15:18:22 -0700208 targets := dexpreoptTargets(ctx)
209
Dan Willemsen0f416782019-06-13 21:44:53 +0000210 imageConfig := bootImageConfig{
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000211 name: "apex",
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100212 modules: imageModules,
213 dexLocations: bootLocations,
214 dexPaths: bootDexPaths,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000215 dir: dir,
216 symbolsDir: symbolsDir,
Colin Crossc11e0c52019-05-08 15:18:22 -0700217 targets: targets,
Dan Willemsen0f416782019-06-13 21:44:53 +0000218 images: make(map[android.ArchType]android.OutputPath),
219 imagesDeps: make(map[android.ArchType]android.Paths),
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000220 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000221
222 for _, target := range targets {
223 imageDir := dir.Join(ctx, "system/framework", target.Arch.ArchType.String())
224 imageConfig.images[target.Arch.ArchType] = imageDir.Join(ctx, "apex.art")
225
226 imagesDeps := make([]android.Path, 0, len(imageConfig.modules)*3)
227 for _, dep := range imageConfig.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex") {
228 imagesDeps = append(imagesDeps, dep)
229 }
230 imageConfig.imagesDeps[target.Arch.ArchType] = imagesDeps
231 }
232
233 return imageConfig
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000234 }).(bootImageConfig)
235}
236
237var apexBootImageConfigKey = android.NewOnceKey("apexBootImageConfig")
238
Colin Cross44df5812019-02-15 23:06:46 -0800239func defaultBootclasspath(ctx android.PathContext) []string {
240 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
241 global := dexpreoptGlobalConfig(ctx)
242 image := defaultBootImageConfig(ctx)
243 bootclasspath := append(copyOf(image.dexLocations), global.ProductUpdatableBootLocations...)
244 return bootclasspath
245 })
246}
247
248var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
249
250var copyOf = android.CopyOf
251
252func init() {
253 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
254}
255
256func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
257 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Nicolas Geoffray07b40072019-02-22 16:57:42 +0000258 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocations, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800259 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800260
261 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800262}