blob: f3191e741646a80800ea4da0d103ef4669d23646 [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{} {
Colin Cross988414c2020-01-11 01:11:46 +000039 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
40 panic(err)
41 } else if data != nil {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000042 soongConfig := dexpreopt.CreateGlobalSoongConfig(ctx)
Colin Cross988414c2020-01-11 01:11:46 +000043 globalConfig, err := dexpreopt.LoadGlobalConfig(ctx, data, soongConfig)
Colin Cross44df5812019-02-15 23:06:46 -080044 if err != nil {
45 panic(err)
46 }
Colin Cross2d00f0d2019-05-09 21:50:00 -070047 return globalConfigAndRaw{globalConfig, data}
Colin Cross44df5812019-02-15 23:06:46 -080048 }
49
50 // No global config filename set, see if there is a test config set
51 return ctx.Config().Once(dexpreoptTestGlobalConfigKey, func() interface{} {
52 // Nope, return a config with preopting disabled
Colin Cross2d00f0d2019-05-09 21:50:00 -070053 return globalConfigAndRaw{dexpreopt.GlobalConfig{
Mathieu Chartier6adeee12019-06-26 10:01:36 -070054 DisablePreopt: true,
55 DisableGenerateProfile: true,
Colin Cross2d00f0d2019-05-09 21:50:00 -070056 }, nil}
Colin Cross44df5812019-02-15 23:06:46 -080057 })
Colin Cross2d00f0d2019-05-09 21:50:00 -070058 }).(globalConfigAndRaw)
Colin Cross44df5812019-02-15 23:06:46 -080059}
60
61// setDexpreoptTestGlobalConfig sets a GlobalConfig that future calls to dexpreoptGlobalConfig will return. It must
62// be called before the first call to dexpreoptGlobalConfig for the config.
63func setDexpreoptTestGlobalConfig(config android.Config, globalConfig dexpreopt.GlobalConfig) {
Colin Cross2d00f0d2019-05-09 21:50:00 -070064 config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
Colin Cross44df5812019-02-15 23:06:46 -080065}
66
67var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
68var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
69
70// systemServerClasspath returns the on-device locations of the modules in the system server classpath. It is computed
71// once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same
72// ctx.Config().
73func systemServerClasspath(ctx android.PathContext) []string {
74 return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string {
75 global := dexpreoptGlobalConfig(ctx)
76
77 var systemServerClasspathLocations []string
78 for _, m := range global.SystemServerJars {
79 systemServerClasspathLocations = append(systemServerClasspathLocations,
80 filepath.Join("/system/framework", m+".jar"))
81 }
Roshan Pius9b51a402019-11-21 12:36:53 -080082 for _, m := range global.UpdatableSystemServerJars {
Roshan Pius9b51a402019-11-21 12:36:53 -080083 systemServerClasspathLocations = append(systemServerClasspathLocations,
Roshan Piusccc26ef2019-11-27 09:37:46 -080084 dexpreopt.GetJarLocationFromApexJarPair(m))
Roshan Pius9b51a402019-11-21 12:36:53 -080085 }
Colin Cross44df5812019-02-15 23:06:46 -080086 return systemServerClasspathLocations
87 })
88}
89
90var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
91
Colin Crossc11e0c52019-05-08 15:18:22 -070092// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
93// supported through native bridge.
94func dexpreoptTargets(ctx android.PathContext) []android.Target {
95 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070096 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070097 if target.NativeBridge == android.NativeBridgeDisabled {
98 targets = append(targets, target)
99 }
100 }
101
102 return targets
103}
104
Jiyong Park0b238752019-10-29 11:23:10 +0900105func stemOf(moduleName string) string {
106 // b/139391334: the stem of framework-minus-apex is framework
107 // This is hard coded here until we find a good way to query the stem
108 // of a module before any other mutators are run
109 if moduleName == "framework-minus-apex" {
110 return "framework"
111 }
112 return moduleName
113}
114
Roshan Piusccc26ef2019-11-27 09:37:46 -0800115func getJarsFromApexJarPairs(apexJarPairs []string) []string {
116 modules := make([]string, len(apexJarPairs))
117 for i, p := range apexJarPairs {
118 _, jar := dexpreopt.SplitApexJarPair(p)
119 modules[i] = jar
120 }
121 return modules
122}
123
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000124var (
125 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
126 artBootImageName = "art"
127 frameworkBootImageName = "boot"
128 apexBootImageName = "apex"
129)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000130
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000131// Construct the global boot image configs.
132func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
133 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
134
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000135 global := dexpreoptGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000136 targets := dexpreoptTargets(ctx)
137 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000138
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100139 artModules := global.ArtApexJars
Ulya Trafimovich44561882020-01-03 13:25:54 +0000140 // With EMMA_INSTRUMENT_FRAMEWORK=true the Core libraries depend on jacoco.
141 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
142 artModules = append(artModules, "jacocoagent")
143 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000144 frameworkModules := android.RemoveListFromList(global.BootJars,
145 concat(artModules, getJarsFromApexJarPairs(global.UpdatableBootJars)))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000146
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000147 artSubdir := "apex/com.android.art/javalib"
148 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000149
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000150 var artLocations, frameworkLocations []string
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100151 for _, m := range artModules {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000152 artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar"))
153 }
154 for _, m := range frameworkModules {
155 frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000156 }
157
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000158 // ART config for the primary boot image in the ART apex.
159 // It includes the Core Libraries.
160 artCfg := bootImageConfig{
161 extension: false,
162 name: artBootImageName,
163 stem: "boot",
164 installSubdir: artSubdir,
165 modules: artModules,
166 dexLocations: artLocations,
167 dexLocationsDeps: artLocations,
168 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000169
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000170 // Framework config for the boot image extension.
Ulyana Trafimovich3fae7662019-12-11 10:31:32 +0000171 // It includes both the Core libraries and framework.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000172 frameworkCfg := bootImageConfig{
Ulyana Trafimovich3fae7662019-12-11 10:31:32 +0000173 extension: false,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000174 name: frameworkBootImageName,
175 stem: "boot",
176 installSubdir: frameworkSubdir,
Ulyana Trafimovich3fae7662019-12-11 10:31:32 +0000177 modules: concat(artModules, frameworkModules),
178 dexLocations: concat(artLocations, frameworkLocations),
179 dexLocationsDeps: concat(artLocations, frameworkLocations),
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000180 }
181
182 // Apex config for the boot image used in the JIT-zygote experiment.
183 // It includes both the Core libraries and framework.
184 apexCfg := bootImageConfig{
185 extension: false,
186 name: apexBootImageName,
187 stem: "apex",
188 installSubdir: frameworkSubdir,
189 modules: concat(artModules, frameworkModules),
190 dexLocations: concat(artLocations, frameworkLocations),
191 dexLocationsDeps: concat(artLocations, frameworkLocations),
192 }
193
194 configs := map[string]*bootImageConfig{
195 artBootImageName: &artCfg,
196 frameworkBootImageName: &frameworkCfg,
197 apexBootImageName: &apexCfg,
198 }
199
200 // common to all configs
201 for _, c := range configs {
202 c.targets = targets
203
204 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
205 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
206
207 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
208 imageName := c.firstModuleNameOrStem() + ".art"
209
210 c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()}
211
212 // The path to bootclasspath dex files needs to be known at module
213 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
214 // Set up known paths for them, the singleton rules will copy them there.
215 // TODO(b/143682396): use module dependencies instead
216 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
217 for _, m := range c.modules {
218 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar"))
219 }
220 c.dexPathsDeps = c.dexPaths
221
222 c.images = make(map[android.ArchType]android.OutputPath)
223 c.imagesDeps = make(map[android.ArchType]android.OutputPaths)
224
225 for _, target := range targets {
226 arch := target.Arch.ArchType
227 imageDir := c.dir.Join(ctx, c.installSubdir, arch.String())
228 c.images[arch] = imageDir.Join(ctx, imageName)
229 c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex")
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000230 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800231
232 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100233 }
234
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000235 return configs
236 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000237}
238
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000239func artBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000240 return *genBootImageConfigs(ctx)[artBootImageName]
241}
242
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000243func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000244 return *genBootImageConfigs(ctx)[frameworkBootImageName]
245}
246
247func apexBootImageConfig(ctx android.PathContext) bootImageConfig {
248 return *genBootImageConfigs(ctx)[apexBootImageName]
Ulya Trafimovich18263382019-10-23 15:56:32 +0100249}
250
Colin Cross44df5812019-02-15 23:06:46 -0800251func defaultBootclasspath(ctx android.PathContext) []string {
252 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
253 global := dexpreoptGlobalConfig(ctx)
254 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000255
Roshan Piusccc26ef2019-11-27 09:37:46 -0800256 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
257 for i, p := range global.UpdatableBootJars {
258 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
259 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000260
261 bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800262 return bootclasspath
263 })
264}
265
266var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
267
268var copyOf = android.CopyOf
269
270func init() {
271 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
272}
273
274func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
275 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000276 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800277 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800278
279 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800280}