blob: 0aa9005f69e41f0aac6ab8813757b4ff13abdd42 [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
Hans Boehm453bf092020-01-25 01:44:30 +000025// 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 {
29 return dexpreoptGlobalConfigRaw(ctx).global
30}
31
32type globalConfigAndRaw struct {
33 global dexpreopt.GlobalConfig
34 data []byte
35}
36
37func dexpreoptGlobalConfigRaw(ctx android.PathContext) globalConfigAndRaw {
38 return ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} {
39 if data, err := ctx.Config().DexpreoptGlobalConfig(ctx); err != nil {
40 panic(err)
41 } else if data != nil {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000042 globalConfig, err := dexpreopt.LoadGlobalConfig(ctx, data)
Hans Boehm453bf092020-01-25 01:44:30 +000043 if err != nil {
44 panic(err)
45 }
46 return globalConfigAndRaw{globalConfig, data}
47 }
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
52 return globalConfigAndRaw{dexpreopt.GlobalConfig{
53 DisablePreopt: true,
54 DisableGenerateProfile: true,
55 }, nil}
56 })
57 }).(globalConfigAndRaw)
58}
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) {
63 config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
64}
65
66var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
67var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
68
Colin Cross44df5812019-02-15 23:06:46 -080069// 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().
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000072func systemServerClasspath(ctx android.MakeVarsContext) []string {
Colin Cross44df5812019-02-15 23:06:46 -080073 return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string {
Hans Boehm453bf092020-01-25 01:44:30 +000074 global := dexpreoptGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -080075 var systemServerClasspathLocations []string
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000076 for _, m := range *DexpreoptedSystemServerJars(ctx.Config()) {
Colin Cross44df5812019-02-15 23:06:46 -080077 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
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000113var (
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000114 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
115 artBootImageName = "art"
116 frameworkBootImageName = "boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000117)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000118
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000119// Construct the global boot image configs.
120func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
121 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
122
Hans Boehm453bf092020-01-25 01:44:30 +0000123 global := dexpreoptGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000124 targets := dexpreoptTargets(ctx)
125 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000126
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100127 artModules := global.ArtApexJars
Ulya Trafimovich44561882020-01-03 13:25:54 +0000128 // With EMMA_INSTRUMENT_FRAMEWORK=true the Core libraries depend on jacoco.
129 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
130 artModules = append(artModules, "jacocoagent")
131 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000132 frameworkModules := android.RemoveListFromList(global.BootJars,
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000133 concat(artModules, dexpreopt.GetJarsFromApexJarPairs(global.UpdatableBootJars)))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000134
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000135 artSubdir := "apex/com.android.art/javalib"
136 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000137
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000138 var artLocations, frameworkLocations []string
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100139 for _, m := range artModules {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000140 artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar"))
141 }
142 for _, m := range frameworkModules {
143 frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000144 }
145
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000146 // ART config for the primary boot image in the ART apex.
147 // It includes the Core Libraries.
148 artCfg := bootImageConfig{
149 extension: false,
150 name: artBootImageName,
151 stem: "boot",
152 installSubdir: artSubdir,
153 modules: artModules,
154 dexLocations: artLocations,
155 dexLocationsDeps: artLocations,
156 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000157
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000158 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000159 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000160 frameworkCfg := bootImageConfig{
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000161 extension: true,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000162 name: frameworkBootImageName,
163 stem: "boot",
164 installSubdir: frameworkSubdir,
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000165 modules: frameworkModules,
166 dexLocations: frameworkLocations,
167 dexLocationsDeps: append(artLocations, frameworkLocations...),
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000168 }
169
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000170 configs := map[string]*bootImageConfig{
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000171 artBootImageName: &artCfg,
172 frameworkBootImageName: &frameworkCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000173 }
174
175 // common to all configs
176 for _, c := range configs {
177 c.targets = targets
178
179 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
180 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
181
182 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
183 imageName := c.firstModuleNameOrStem() + ".art"
184
185 c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()}
186
187 // The path to bootclasspath dex files needs to be known at module
188 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
189 // Set up known paths for them, the singleton rules will copy them there.
190 // TODO(b/143682396): use module dependencies instead
191 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
192 for _, m := range c.modules {
193 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar"))
194 }
195 c.dexPathsDeps = c.dexPaths
196
197 c.images = make(map[android.ArchType]android.OutputPath)
198 c.imagesDeps = make(map[android.ArchType]android.OutputPaths)
199
200 for _, target := range targets {
201 arch := target.Arch.ArchType
202 imageDir := c.dir.Join(ctx, c.installSubdir, arch.String())
203 c.images[arch] = imageDir.Join(ctx, imageName)
204 c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex")
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000205 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800206
207 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100208 }
209
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000210 // specific to the framework config
211 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
Ulya Trafimovichb0a2d372020-01-28 14:42:41 +0000212 frameworkCfg.primaryImages = artCfg.images
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000213 frameworkCfg.imageLocations = append(artCfg.imageLocations, frameworkCfg.imageLocations...)
214
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000215 return configs
216 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000217}
218
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000219func artBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000220 return *genBootImageConfigs(ctx)[artBootImageName]
221}
222
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000223func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000224 return *genBootImageConfigs(ctx)[frameworkBootImageName]
225}
226
Colin Cross44df5812019-02-15 23:06:46 -0800227func defaultBootclasspath(ctx android.PathContext) []string {
228 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
Hans Boehm453bf092020-01-25 01:44:30 +0000229 global := dexpreoptGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800230 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000231
Roshan Piusccc26ef2019-11-27 09:37:46 -0800232 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
233 for i, p := range global.UpdatableBootJars {
234 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
235 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000236
237 bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800238 return bootclasspath
239 })
240}
241
242var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
243
244var copyOf = android.CopyOf
245
246func init() {
247 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
248}
249
250func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
251 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000252 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800253 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800254
255 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800256}