blob: b3af911fbfe44bac246f912e703c66026c8d9d31 [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 (
124 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
125 artBootImageName = "art"
126 frameworkBootImageName = "boot"
127 apexBootImageName = "apex"
128)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000129
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000130// Construct the global boot image configs.
131func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
132 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
133
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000134 global := dexpreoptGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000135 targets := dexpreoptTargets(ctx)
136 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000137
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100138 artModules := global.ArtApexJars
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000139 frameworkModules := android.RemoveListFromList(global.BootJars,
140 concat(artModules, getJarsFromApexJarPairs(global.UpdatableBootJars)))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000141
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000142 artSubdir := "apex/com.android.art/javalib"
143 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000144
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000145 var artLocations, frameworkLocations []string
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100146 for _, m := range artModules {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000147 artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar"))
148 }
149 for _, m := range frameworkModules {
150 frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000151 }
152
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000153 // ART config for the primary boot image in the ART apex.
154 // It includes the Core Libraries.
155 artCfg := bootImageConfig{
156 extension: false,
157 name: artBootImageName,
158 stem: "boot",
159 installSubdir: artSubdir,
160 modules: artModules,
161 dexLocations: artLocations,
162 dexLocationsDeps: artLocations,
163 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000164
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000165 // Framework config for the boot image extension.
Ulyana Trafimovich3fae7662019-12-11 10:31:32 +0000166 // It includes both the Core libraries and framework.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000167 frameworkCfg := bootImageConfig{
Ulyana Trafimovich3fae7662019-12-11 10:31:32 +0000168 extension: false,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000169 name: frameworkBootImageName,
170 stem: "boot",
171 installSubdir: frameworkSubdir,
Ulyana Trafimovich3fae7662019-12-11 10:31:32 +0000172 modules: concat(artModules, frameworkModules),
173 dexLocations: concat(artLocations, frameworkLocations),
174 dexLocationsDeps: concat(artLocations, frameworkLocations),
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000175 }
176
177 // Apex config for the boot image used in the JIT-zygote experiment.
178 // It includes both the Core libraries and framework.
179 apexCfg := bootImageConfig{
180 extension: false,
181 name: apexBootImageName,
182 stem: "apex",
183 installSubdir: frameworkSubdir,
184 modules: concat(artModules, frameworkModules),
185 dexLocations: concat(artLocations, frameworkLocations),
186 dexLocationsDeps: concat(artLocations, frameworkLocations),
187 }
188
189 configs := map[string]*bootImageConfig{
190 artBootImageName: &artCfg,
191 frameworkBootImageName: &frameworkCfg,
192 apexBootImageName: &apexCfg,
193 }
194
195 // common to all configs
196 for _, c := range configs {
197 c.targets = targets
198
199 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
200 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
201
202 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
203 imageName := c.firstModuleNameOrStem() + ".art"
204
205 c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()}
206
207 // The path to bootclasspath dex files needs to be known at module
208 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
209 // Set up known paths for them, the singleton rules will copy them there.
210 // TODO(b/143682396): use module dependencies instead
211 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
212 for _, m := range c.modules {
213 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar"))
214 }
215 c.dexPathsDeps = c.dexPaths
216
217 c.images = make(map[android.ArchType]android.OutputPath)
218 c.imagesDeps = make(map[android.ArchType]android.OutputPaths)
219
220 for _, target := range targets {
221 arch := target.Arch.ArchType
222 imageDir := c.dir.Join(ctx, c.installSubdir, arch.String())
223 c.images[arch] = imageDir.Join(ctx, imageName)
224 c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex")
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000225 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800226
227 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100228 }
229
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000230 return configs
231 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000232}
233
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000234func artBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000235 return *genBootImageConfigs(ctx)[artBootImageName]
236}
237
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000238func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000239 return *genBootImageConfigs(ctx)[frameworkBootImageName]
240}
241
242func apexBootImageConfig(ctx android.PathContext) bootImageConfig {
243 return *genBootImageConfigs(ctx)[apexBootImageName]
Ulya Trafimovich18263382019-10-23 15:56:32 +0100244}
245
Colin Cross44df5812019-02-15 23:06:46 -0800246func defaultBootclasspath(ctx android.PathContext) []string {
247 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
248 global := dexpreoptGlobalConfig(ctx)
249 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000250
Roshan Piusccc26ef2019-11-27 09:37:46 -0800251 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
252 for i, p := range global.UpdatableBootJars {
253 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
254 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000255
256 bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800257 return bootclasspath
258 })
259}
260
261var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
262
263var copyOf = android.CopyOf
264
265func init() {
266 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
267}
268
269func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
270 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000271 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800272 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800273
274 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800275}