blob: 3287ea6c157c5753f996c5ee80c0272bb644504a [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 != "" {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000040 soongConfig := dexpreopt.CreateGlobalSoongConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -080041 ctx.AddNinjaFileDeps(f)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000042 globalConfig, data, err := dexpreopt.LoadGlobalConfig(ctx, f, soongConfig)
Colin Cross44df5812019-02-15 23:06:46 -080043 if err != nil {
44 panic(err)
45 }
Colin Cross2d00f0d2019-05-09 21:50:00 -070046 return globalConfigAndRaw{globalConfig, data}
Colin Cross44df5812019-02-15 23:06:46 -080047 }
48
49 // No global config filename set, see if there is a test config set
50 return ctx.Config().Once(dexpreoptTestGlobalConfigKey, func() interface{} {
51 // Nope, return a config with preopting disabled
Colin Cross2d00f0d2019-05-09 21:50:00 -070052 return globalConfigAndRaw{dexpreopt.GlobalConfig{
Mathieu Chartier6adeee12019-06-26 10:01:36 -070053 DisablePreopt: true,
54 DisableGenerateProfile: true,
Colin Cross2d00f0d2019-05-09 21:50:00 -070055 }, nil}
Colin Cross44df5812019-02-15 23:06:46 -080056 })
Colin Cross2d00f0d2019-05-09 21:50:00 -070057 }).(globalConfigAndRaw)
Colin Cross44df5812019-02-15 23:06:46 -080058}
59
60// setDexpreoptTestGlobalConfig sets a GlobalConfig that future calls to dexpreoptGlobalConfig will return. It must
61// be called before the first call to dexpreoptGlobalConfig for the config.
62func setDexpreoptTestGlobalConfig(config android.Config, globalConfig dexpreopt.GlobalConfig) {
Colin Cross2d00f0d2019-05-09 21:50:00 -070063 config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
Colin Cross44df5812019-02-15 23:06:46 -080064}
65
66var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
67var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
68
69// systemServerClasspath returns the on-device locations of the modules in the system server classpath. It is computed
70// once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same
71// ctx.Config().
72func systemServerClasspath(ctx android.PathContext) []string {
73 return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string {
74 global := dexpreoptGlobalConfig(ctx)
75
76 var systemServerClasspathLocations []string
77 for _, m := range global.SystemServerJars {
78 systemServerClasspathLocations = append(systemServerClasspathLocations,
79 filepath.Join("/system/framework", m+".jar"))
80 }
Roshan Pius9b51a402019-11-21 12:36:53 -080081 for _, m := range global.UpdatableSystemServerJars {
Roshan Pius9b51a402019-11-21 12:36:53 -080082 systemServerClasspathLocations = append(systemServerClasspathLocations,
Roshan Piusccc26ef2019-11-27 09:37:46 -080083 dexpreopt.GetJarLocationFromApexJarPair(m))
Roshan Pius9b51a402019-11-21 12:36:53 -080084 }
Colin Cross44df5812019-02-15 23:06:46 -080085 return systemServerClasspathLocations
86 })
87}
88
89var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
90
Colin Crossc11e0c52019-05-08 15:18:22 -070091// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
92// supported through native bridge.
93func dexpreoptTargets(ctx android.PathContext) []android.Target {
94 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070095 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070096 if target.NativeBridge == android.NativeBridgeDisabled {
97 targets = append(targets, target)
98 }
99 }
100
101 return targets
102}
103
Jiyong Park0b238752019-10-29 11:23:10 +0900104func stemOf(moduleName string) string {
105 // b/139391334: the stem of framework-minus-apex is framework
106 // This is hard coded here until we find a good way to query the stem
107 // of a module before any other mutators are run
108 if moduleName == "framework-minus-apex" {
109 return "framework"
110 }
111 return moduleName
112}
113
Roshan Piusccc26ef2019-11-27 09:37:46 -0800114func getJarsFromApexJarPairs(apexJarPairs []string) []string {
115 modules := make([]string, len(apexJarPairs))
116 for i, p := range apexJarPairs {
117 _, jar := dexpreopt.SplitApexJarPair(p)
118 modules[i] = jar
119 }
120 return modules
121}
122
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000123var (
Ulya Trafimovich57547452019-12-09 15:40:17 +0000124 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
125 artBootImageName = "art"
126 frameworkBootImageName = "boot"
127 artJZBootImageName = "jitzygote-art"
128 frameworkJZBootImageName = "jitzygote-boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000129)
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 Trafimovich5a4ccd12019-12-18 17:32:33 +0000171 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000172 frameworkCfg := bootImageConfig{
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000173 extension: true,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000174 name: frameworkBootImageName,
175 stem: "boot",
176 installSubdir: frameworkSubdir,
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000177 modules: frameworkModules,
178 dexLocations: frameworkLocations,
179 dexLocationsDeps: append(artLocations, frameworkLocations...),
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000180 }
181
Ulya Trafimovich57547452019-12-09 15:40:17 +0000182 // ART config for JIT-zygote boot image.
183 artJZCfg := bootImageConfig{
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000184 extension: false,
Ulya Trafimovich57547452019-12-09 15:40:17 +0000185 name: artJZBootImageName,
186 stem: "apex",
187 installSubdir: artSubdir,
188 modules: artModules,
189 dexLocations: artLocations,
190 dexLocationsDeps: artLocations,
191 }
192
193 // Framework config for JIT-zygote boot image extension.
194 frameworkJZCfg := bootImageConfig{
195 extension: true,
196 name: frameworkJZBootImageName,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000197 stem: "apex",
198 installSubdir: frameworkSubdir,
Ulya Trafimovich57547452019-12-09 15:40:17 +0000199 modules: frameworkModules,
200 dexLocations: frameworkLocations,
201 dexLocationsDeps: append(artLocations, frameworkLocations...),
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000202 }
203
204 configs := map[string]*bootImageConfig{
Ulya Trafimovich57547452019-12-09 15:40:17 +0000205 artBootImageName: &artCfg,
206 frameworkBootImageName: &frameworkCfg,
207 artJZBootImageName: &artJZCfg,
208 frameworkJZBootImageName: &frameworkJZCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000209 }
210
211 // common to all configs
212 for _, c := range configs {
213 c.targets = targets
214
215 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
216 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
217
218 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
219 imageName := c.firstModuleNameOrStem() + ".art"
220
221 c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()}
222
223 // The path to bootclasspath dex files needs to be known at module
224 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
225 // Set up known paths for them, the singleton rules will copy them there.
226 // TODO(b/143682396): use module dependencies instead
227 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
228 for _, m := range c.modules {
229 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar"))
230 }
231 c.dexPathsDeps = c.dexPaths
232
233 c.images = make(map[android.ArchType]android.OutputPath)
234 c.imagesDeps = make(map[android.ArchType]android.OutputPaths)
235
236 for _, target := range targets {
237 arch := target.Arch.ArchType
238 imageDir := c.dir.Join(ctx, c.installSubdir, arch.String())
239 c.images[arch] = imageDir.Join(ctx, imageName)
240 c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex")
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000241 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800242
243 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100244 }
245
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000246 // specific to the framework config
247 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
248 frameworkCfg.imageLocations = append(artCfg.imageLocations, frameworkCfg.imageLocations...)
249
Ulya Trafimovich57547452019-12-09 15:40:17 +0000250 // specific to the jitzygote-framework config
251 frameworkJZCfg.dexPathsDeps = append(artJZCfg.dexPathsDeps, frameworkJZCfg.dexPathsDeps...)
252 frameworkJZCfg.imageLocations = append(artJZCfg.imageLocations, frameworkJZCfg.imageLocations...)
253
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000254 return configs
255 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000256}
257
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000258func artBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000259 return *genBootImageConfigs(ctx)[artBootImageName]
260}
261
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000262func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000263 return *genBootImageConfigs(ctx)[frameworkBootImageName]
264}
265
Ulya Trafimovich57547452019-12-09 15:40:17 +0000266func artJZBootImageConfig(ctx android.PathContext) bootImageConfig {
267 return *genBootImageConfigs(ctx)[artJZBootImageName]
268}
269
270func frameworkJZBootImageConfig(ctx android.PathContext) bootImageConfig {
271 return *genBootImageConfigs(ctx)[frameworkJZBootImageName]
Ulya Trafimovich18263382019-10-23 15:56:32 +0100272}
273
Colin Cross44df5812019-02-15 23:06:46 -0800274func defaultBootclasspath(ctx android.PathContext) []string {
275 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
276 global := dexpreoptGlobalConfig(ctx)
277 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000278
Roshan Piusccc26ef2019-11-27 09:37:46 -0800279 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
280 for i, p := range global.UpdatableBootJars {
281 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
282 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000283
284 bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800285 return bootclasspath
286 })
287}
288
289var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
290
291var copyOf = android.CopyOf
292
293func init() {
294 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
295}
296
297func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
298 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000299 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800300 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800301
302 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800303}