blob: 91e0dfbdd3f1f263816a560575e317df63dd027a [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 frameworkModules := android.RemoveListFromList(global.BootJars,
139 concat(artModules, getJarsFromApexJarPairs(global.UpdatableBootJars)))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000140
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000141 artSubdir := "apex/com.android.art/javalib"
142 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000143
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000144 var artLocations, frameworkLocations []string
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100145 for _, m := range artModules {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000146 artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar"))
147 }
148 for _, m := range frameworkModules {
149 frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000150 }
151
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000152 // ART config for the primary boot image in the ART apex.
153 // It includes the Core Libraries.
154 artCfg := bootImageConfig{
155 extension: false,
156 name: artBootImageName,
157 stem: "boot",
158 installSubdir: artSubdir,
159 modules: artModules,
160 dexLocations: artLocations,
161 dexLocationsDeps: artLocations,
162 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000163
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000164 // Framework config for the boot image extension.
Ulyana Trafimovich3fae7662019-12-11 10:31:32 +0000165 // It includes both the Core libraries and framework.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000166 frameworkCfg := bootImageConfig{
Ulyana Trafimovich3fae7662019-12-11 10:31:32 +0000167 extension: false,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000168 name: frameworkBootImageName,
169 stem: "boot",
170 installSubdir: frameworkSubdir,
Ulyana Trafimovich3fae7662019-12-11 10:31:32 +0000171 modules: concat(artModules, frameworkModules),
172 dexLocations: concat(artLocations, frameworkLocations),
173 dexLocationsDeps: concat(artLocations, frameworkLocations),
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000174 }
175
176 // Apex config for the boot image used in the JIT-zygote experiment.
177 // It includes both the Core libraries and framework.
178 apexCfg := bootImageConfig{
179 extension: false,
180 name: apexBootImageName,
181 stem: "apex",
182 installSubdir: frameworkSubdir,
183 modules: concat(artModules, frameworkModules),
184 dexLocations: concat(artLocations, frameworkLocations),
185 dexLocationsDeps: concat(artLocations, frameworkLocations),
186 }
187
188 configs := map[string]*bootImageConfig{
189 artBootImageName: &artCfg,
190 frameworkBootImageName: &frameworkCfg,
191 apexBootImageName: &apexCfg,
192 }
193
194 // common to all configs
195 for _, c := range configs {
196 c.targets = targets
197
198 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
199 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
200
201 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
202 imageName := c.firstModuleNameOrStem() + ".art"
203
204 c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()}
205
206 // The path to bootclasspath dex files needs to be known at module
207 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
208 // Set up known paths for them, the singleton rules will copy them there.
209 // TODO(b/143682396): use module dependencies instead
210 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
211 for _, m := range c.modules {
212 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar"))
213 }
214 c.dexPathsDeps = c.dexPaths
215
216 c.images = make(map[android.ArchType]android.OutputPath)
217 c.imagesDeps = make(map[android.ArchType]android.OutputPaths)
218
219 for _, target := range targets {
220 arch := target.Arch.ArchType
221 imageDir := c.dir.Join(ctx, c.installSubdir, arch.String())
222 c.images[arch] = imageDir.Join(ctx, imageName)
223 c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex")
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000224 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800225
226 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100227 }
228
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000229 return configs
230 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000231}
232
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000233func artBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000234 return *genBootImageConfigs(ctx)[artBootImageName]
235}
236
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000237func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000238 return *genBootImageConfigs(ctx)[frameworkBootImageName]
239}
240
241func apexBootImageConfig(ctx android.PathContext) bootImageConfig {
242 return *genBootImageConfigs(ctx)[apexBootImageName]
Ulya Trafimovich18263382019-10-23 15:56:32 +0100243}
244
Colin Cross44df5812019-02-15 23:06:46 -0800245func defaultBootclasspath(ctx android.PathContext) []string {
246 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
247 global := dexpreoptGlobalConfig(ctx)
248 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000249
Roshan Piusccc26ef2019-11-27 09:37:46 -0800250 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
251 for i, p := range global.UpdatableBootJars {
252 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
253 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000254
255 bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800256 return bootclasspath
257 })
258}
259
260var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
261
262var copyOf = android.CopyOf
263
264func init() {
265 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
266}
267
268func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
269 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000270 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800271 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800272
273 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800274}