blob: 57a770e1c9ee93f93dd0d5ca8be9d9d3711aa30a [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 }
Roshan Pius9b51a402019-11-21 12:36:53 -080080 for _, m := range global.UpdatableSystemServerJars {
Roshan Pius9b51a402019-11-21 12:36:53 -080081 systemServerClasspathLocations = append(systemServerClasspathLocations,
Roshan Piusccc26ef2019-11-27 09:37:46 -080082 dexpreopt.GetJarLocationFromApexJarPair(m))
Roshan Pius9b51a402019-11-21 12:36:53 -080083 }
Colin Cross44df5812019-02-15 23:06:46 -080084 return systemServerClasspathLocations
85 })
86}
87
88var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
89
Colin Crossc11e0c52019-05-08 15:18:22 -070090// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
91// supported through native bridge.
92func dexpreoptTargets(ctx android.PathContext) []android.Target {
93 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070094 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070095 if target.NativeBridge == android.NativeBridgeDisabled {
96 targets = append(targets, target)
97 }
98 }
99
100 return targets
101}
102
Jiyong Park0b238752019-10-29 11:23:10 +0900103func stemOf(moduleName string) string {
104 // b/139391334: the stem of framework-minus-apex is framework
105 // This is hard coded here until we find a good way to query the stem
106 // of a module before any other mutators are run
107 if moduleName == "framework-minus-apex" {
108 return "framework"
109 }
110 return moduleName
111}
112
Roshan Piusccc26ef2019-11-27 09:37:46 -0800113func getJarsFromApexJarPairs(apexJarPairs []string) []string {
114 modules := make([]string, len(apexJarPairs))
115 for i, p := range apexJarPairs {
116 _, jar := dexpreopt.SplitApexJarPair(p)
117 modules[i] = jar
118 }
119 return modules
120}
121
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000122var (
123 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
124 artBootImageName = "art"
125 frameworkBootImageName = "boot"
126 apexBootImageName = "apex"
127)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000128
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000129// Construct the global boot image configs.
130func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
131 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
132
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000133 global := dexpreoptGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000134 targets := dexpreoptTargets(ctx)
135 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000136
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100137 artModules := global.ArtApexJars
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000138 // In coverage builds ART boot class path jars are instrumented and have additional dependencies.
139 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
140 artModules = append(artModules, "jacocoagent")
141 }
142 frameworkModules := android.RemoveListFromList(global.BootJars,
143 concat(artModules, getJarsFromApexJarPairs(global.UpdatableBootJars)))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000144
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000145 artSubdir := "apex/com.android.art/javalib"
146 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000147
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000148 var artLocations, frameworkLocations []string
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100149 for _, m := range artModules {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000150 artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar"))
151 }
152 for _, m := range frameworkModules {
153 frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000154 }
155
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000156 // ART config for the primary boot image in the ART apex.
157 // It includes the Core Libraries.
158 artCfg := bootImageConfig{
159 extension: false,
160 name: artBootImageName,
161 stem: "boot",
162 installSubdir: artSubdir,
163 modules: artModules,
164 dexLocations: artLocations,
165 dexLocationsDeps: artLocations,
166 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000167
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000168 // Framework config for the boot image extension.
169 // It includes framework libraries and depends on the ART config.
170 frameworkCfg := bootImageConfig{
171 extension: true,
172 name: frameworkBootImageName,
173 stem: "boot",
174 installSubdir: frameworkSubdir,
175 modules: frameworkModules,
176 dexLocations: frameworkLocations,
177 dexLocationsDeps: append(artLocations, frameworkLocations...),
178 }
179
180 // Apex config for the boot image used in the JIT-zygote experiment.
181 // It includes both the Core libraries and framework.
182 apexCfg := bootImageConfig{
183 extension: false,
184 name: apexBootImageName,
185 stem: "apex",
186 installSubdir: frameworkSubdir,
187 modules: concat(artModules, frameworkModules),
188 dexLocations: concat(artLocations, frameworkLocations),
189 dexLocationsDeps: concat(artLocations, frameworkLocations),
190 }
191
192 configs := map[string]*bootImageConfig{
193 artBootImageName: &artCfg,
194 frameworkBootImageName: &frameworkCfg,
195 apexBootImageName: &apexCfg,
196 }
197
198 // common to all configs
199 for _, c := range configs {
200 c.targets = targets
201
202 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
203 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
204
205 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
206 imageName := c.firstModuleNameOrStem() + ".art"
207
208 c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()}
209
210 // The path to bootclasspath dex files needs to be known at module
211 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
212 // Set up known paths for them, the singleton rules will copy them there.
213 // TODO(b/143682396): use module dependencies instead
214 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
215 for _, m := range c.modules {
216 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar"))
217 }
218 c.dexPathsDeps = c.dexPaths
219
220 c.images = make(map[android.ArchType]android.OutputPath)
221 c.imagesDeps = make(map[android.ArchType]android.OutputPaths)
222
223 for _, target := range targets {
224 arch := target.Arch.ArchType
225 imageDir := c.dir.Join(ctx, c.installSubdir, arch.String())
226 c.images[arch] = imageDir.Join(ctx, imageName)
227 c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex")
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000228 }
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100229 }
230
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000231 // specific to the framework config
232 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
233 frameworkCfg.imageLocations = append(artCfg.imageLocations, frameworkCfg.imageLocations...)
234 frameworkCfg.zip = frameworkCfg.dir.Join(ctx, frameworkCfg.stem+".zip")
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000235
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000236 return configs
237 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000238}
239
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000240func artBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000241 return *genBootImageConfigs(ctx)[artBootImageName]
242}
243
244func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
245 return *genBootImageConfigs(ctx)[frameworkBootImageName]
246}
247
248func apexBootImageConfig(ctx android.PathContext) bootImageConfig {
249 return *genBootImageConfigs(ctx)[apexBootImageName]
Ulya Trafimovich18263382019-10-23 15:56:32 +0100250}
251
Colin Cross44df5812019-02-15 23:06:46 -0800252func defaultBootclasspath(ctx android.PathContext) []string {
253 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
254 global := dexpreoptGlobalConfig(ctx)
255 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000256
Roshan Piusccc26ef2019-11-27 09:37:46 -0800257 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
258 for i, p := range global.UpdatableBootJars {
259 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
260 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000261
262 bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800263 return bootclasspath
264 })
265}
266
267var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
268
269var copyOf = android.CopyOf
270
271func init() {
272 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
273}
274
275func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
276 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000277 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800278 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800279
280 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800281}