blob: 8c699b83a7ee32b8bbf19e6f556ece0f8885a3f4 [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{
Mathieu Chartier6adeee12019-06-26 10:01:36 -070052 DisablePreopt: true,
53 DisableGenerateProfile: true,
Colin Cross2d00f0d2019-05-09 21:50:00 -070054 }, nil}
Colin Cross44df5812019-02-15 23:06:46 -080055 })
Colin Cross2d00f0d2019-05-09 21:50:00 -070056 }).(globalConfigAndRaw)
Colin Cross44df5812019-02-15 23:06:46 -080057}
58
59// setDexpreoptTestGlobalConfig sets a GlobalConfig that future calls to dexpreoptGlobalConfig will return. It must
60// be called before the first call to dexpreoptGlobalConfig for the config.
61func setDexpreoptTestGlobalConfig(config android.Config, globalConfig dexpreopt.GlobalConfig) {
Colin Cross2d00f0d2019-05-09 21:50:00 -070062 config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
Colin Cross44df5812019-02-15 23:06:46 -080063}
64
65var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
66var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
67
68// systemServerClasspath returns the on-device locations of the modules in the system server classpath. It is computed
69// once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same
70// ctx.Config().
71func systemServerClasspath(ctx android.PathContext) []string {
72 return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string {
73 global := dexpreoptGlobalConfig(ctx)
74
75 var systemServerClasspathLocations []string
76 for _, m := range global.SystemServerJars {
77 systemServerClasspathLocations = append(systemServerClasspathLocations,
78 filepath.Join("/system/framework", m+".jar"))
79 }
80 return systemServerClasspathLocations
81 })
82}
83
84var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
85
Colin Crossc11e0c52019-05-08 15:18:22 -070086// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
87// supported through native bridge.
88func dexpreoptTargets(ctx android.PathContext) []android.Target {
89 var targets []android.Target
90 for i, target := range ctx.Config().Targets[android.Android] {
91 if ctx.Config().SecondArchIsTranslated() && i > 0 {
92 break
93 }
94
95 if target.NativeBridge == android.NativeBridgeDisabled {
96 targets = append(targets, target)
97 }
98 }
99
100 return targets
101}
102
Colin Cross44df5812019-02-15 23:06:46 -0800103// defaultBootImageConfig returns the bootImageConfig that will be used to dexpreopt modules. It is computed once the
104// first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same
105// ctx.Config().
106func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
107 return ctx.Config().Once(defaultBootImageConfigKey, func() interface{} {
108 global := dexpreoptGlobalConfig(ctx)
109
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100110 artModules := global.ArtApexJars
111 nonFrameworkModules := concat(artModules, global.ProductUpdatableBootModules)
Colin Cross44df5812019-02-15 23:06:46 -0800112 frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules)
113
114 var nonUpdatableBootModules []string
115 var nonUpdatableBootLocations []string
116
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100117 for _, m := range artModules {
Colin Cross44df5812019-02-15 23:06:46 -0800118 nonUpdatableBootModules = append(nonUpdatableBootModules, m)
119 nonUpdatableBootLocations = append(nonUpdatableBootLocations,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100120 filepath.Join("/apex/com.android.art/javalib", m+".jar"))
Colin Cross44df5812019-02-15 23:06:46 -0800121 }
122
123 for _, m := range frameworkModules {
124 nonUpdatableBootModules = append(nonUpdatableBootModules, m)
125 nonUpdatableBootLocations = append(nonUpdatableBootLocations,
126 filepath.Join("/system/framework", m+".jar"))
127 }
128
129 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
130 // the bootclasspath modules have been compiled. Set up known paths for them, the singleton rules will copy
131 // them there.
132 // TODO: use module dependencies instead
133 var nonUpdatableBootDexPaths android.WritablePaths
134 for _, m := range nonUpdatableBootModules {
135 nonUpdatableBootDexPaths = append(nonUpdatableBootDexPaths,
136 android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_input", m+".jar"))
137 }
138
139 dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars")
140 symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_bootjars_unstripped")
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
Dan Willemsen0f416782019-06-13 21:44:53 +0000145 imageConfig := bootImageConfig{
Colin Cross44df5812019-02-15 23:06:46 -0800146 name: "boot",
147 modules: nonUpdatableBootModules,
148 dexLocations: nonUpdatableBootLocations,
149 dexPaths: nonUpdatableBootDexPaths,
150 dir: dir,
151 symbolsDir: symbolsDir,
Dan Willemsen0f416782019-06-13 21:44:53 +0000152 images: make(map[android.ArchType]android.OutputPath),
153 imagesDeps: make(map[android.ArchType]android.Paths),
Colin Crossc11e0c52019-05-08 15:18:22 -0700154 targets: targets,
Colin Crossdf8eebe2019-04-09 15:29:41 -0700155 zip: zip,
Colin Cross44df5812019-02-15 23:06:46 -0800156 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000157
158 for _, target := range targets {
159 imageDir := dir.Join(ctx, "system/framework", target.Arch.ArchType.String())
160 imageConfig.images[target.Arch.ArchType] = imageDir.Join(ctx, "boot.art")
161
162 imagesDeps := make([]android.Path, 0, len(imageConfig.modules)*3)
163 for _, dep := range imageConfig.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex") {
164 imagesDeps = append(imagesDeps, dep)
165 }
166 imageConfig.imagesDeps[target.Arch.ArchType] = imagesDeps
167 }
168
169 return imageConfig
Colin Cross44df5812019-02-15 23:06:46 -0800170 }).(bootImageConfig)
171}
172
173var defaultBootImageConfigKey = android.NewOnceKey("defaultBootImageConfig")
174
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000175func apexBootImageConfig(ctx android.PathContext) bootImageConfig {
176 return ctx.Config().Once(apexBootImageConfigKey, func() interface{} {
177 global := dexpreoptGlobalConfig(ctx)
178
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100179 artModules := global.ArtApexJars
180 nonFrameworkModules := concat(artModules, global.ProductUpdatableBootModules)
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100181 frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules)
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100182 imageModules := concat(artModules, frameworkModules)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000183
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100184 var bootLocations []string
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000185
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100186 for _, m := range artModules {
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100187 bootLocations = append(bootLocations,
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100188 filepath.Join("/apex/com.android.art/javalib", m+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000189 }
190
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100191 for _, m := range frameworkModules {
192 bootLocations = append(bootLocations,
193 filepath.Join("/system/framework", m+".jar"))
194 }
195
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000196 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
197 // the bootclasspath modules have been compiled. Set up known paths for them, the singleton rules will copy
198 // them there.
199 // TODO: use module dependencies instead
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100200 var bootDexPaths android.WritablePaths
201 for _, m := range imageModules {
202 bootDexPaths = append(bootDexPaths,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000203 android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_input", m+".jar"))
204 }
205
206 dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars")
207 symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_apexjars_unstripped")
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000208
Colin Crossc11e0c52019-05-08 15:18:22 -0700209 targets := dexpreoptTargets(ctx)
210
Dan Willemsen0f416782019-06-13 21:44:53 +0000211 imageConfig := bootImageConfig{
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000212 name: "apex",
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100213 modules: imageModules,
214 dexLocations: bootLocations,
215 dexPaths: bootDexPaths,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000216 dir: dir,
217 symbolsDir: symbolsDir,
Colin Crossc11e0c52019-05-08 15:18:22 -0700218 targets: targets,
Dan Willemsen0f416782019-06-13 21:44:53 +0000219 images: make(map[android.ArchType]android.OutputPath),
220 imagesDeps: make(map[android.ArchType]android.Paths),
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000221 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000222
223 for _, target := range targets {
224 imageDir := dir.Join(ctx, "system/framework", target.Arch.ArchType.String())
225 imageConfig.images[target.Arch.ArchType] = imageDir.Join(ctx, "apex.art")
226
227 imagesDeps := make([]android.Path, 0, len(imageConfig.modules)*3)
228 for _, dep := range imageConfig.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex") {
229 imagesDeps = append(imagesDeps, dep)
230 }
231 imageConfig.imagesDeps[target.Arch.ArchType] = imagesDeps
232 }
233
234 return imageConfig
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000235 }).(bootImageConfig)
236}
237
238var apexBootImageConfigKey = android.NewOnceKey("apexBootImageConfig")
239
Colin Cross44df5812019-02-15 23:06:46 -0800240func defaultBootclasspath(ctx android.PathContext) []string {
241 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
242 global := dexpreoptGlobalConfig(ctx)
243 image := defaultBootImageConfig(ctx)
244 bootclasspath := append(copyOf(image.dexLocations), global.ProductUpdatableBootLocations...)
245 return bootclasspath
246 })
247}
248
249var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
250
251var copyOf = android.CopyOf
252
253func init() {
254 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
255}
256
257func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
258 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Nicolas Geoffray07b40072019-02-22 16:57:42 +0000259 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocations, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800260 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800261
262 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800263}