blob: 7d0bd8fb79168de14b4f46e7a8b23a0523431606 [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 {
Hans Boehme4b53422020-01-25 01:44:30 +000042 soongConfig := dexpreopt.CreateGlobalSoongConfig(ctx)
43 globalConfig, err := dexpreopt.LoadGlobalConfig(ctx, data, soongConfig)
Hans Boehm453bf092020-01-25 01:44:30 +000044 if err != nil {
45 panic(err)
46 }
47 return globalConfigAndRaw{globalConfig, data}
48 }
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
53 return globalConfigAndRaw{dexpreopt.GlobalConfig{
54 DisablePreopt: true,
55 DisableGenerateProfile: true,
56 }, nil}
57 })
58 }).(globalConfigAndRaw)
59}
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) {
64 config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfigAndRaw{globalConfig, nil} })
65}
66
67var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig")
68var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig")
69
Colin Cross44df5812019-02-15 23:06:46 -080070// 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().
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000073func systemServerClasspath(ctx android.MakeVarsContext) []string {
Colin Cross44df5812019-02-15 23:06:46 -080074 return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string {
Hans Boehm453bf092020-01-25 01:44:30 +000075 global := dexpreoptGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -080076 var systemServerClasspathLocations []string
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000077 for _, m := range *DexpreoptedSystemServerJars(ctx.Config()) {
Colin Cross44df5812019-02-15 23:06:46 -080078 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
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000114var (
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000115 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
116 artBootImageName = "art"
117 frameworkBootImageName = "boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000118)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000119
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000120// Construct the global boot image configs.
121func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
122 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
123
Hans Boehm453bf092020-01-25 01:44:30 +0000124 global := dexpreoptGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000125 targets := dexpreoptTargets(ctx)
126 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000127
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100128 artModules := global.ArtApexJars
Ulya Trafimovich44561882020-01-03 13:25:54 +0000129 // With EMMA_INSTRUMENT_FRAMEWORK=true the Core libraries depend on jacoco.
130 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
131 artModules = append(artModules, "jacocoagent")
132 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000133 frameworkModules := android.RemoveListFromList(global.BootJars,
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000134 concat(artModules, dexpreopt.GetJarsFromApexJarPairs(global.UpdatableBootJars)))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000135
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000136 artSubdir := "apex/com.android.art/javalib"
137 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000138
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000139 var artLocations, frameworkLocations []string
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100140 for _, m := range artModules {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000141 artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar"))
142 }
143 for _, m := range frameworkModules {
144 frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000145 }
146
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000147 // ART config for the primary boot image in the ART apex.
148 // It includes the Core Libraries.
149 artCfg := bootImageConfig{
150 extension: false,
151 name: artBootImageName,
152 stem: "boot",
153 installSubdir: artSubdir,
154 modules: artModules,
155 dexLocations: artLocations,
156 dexLocationsDeps: artLocations,
157 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000158
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000159 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000160 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000161 frameworkCfg := bootImageConfig{
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000162 extension: true,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000163 name: frameworkBootImageName,
164 stem: "boot",
165 installSubdir: frameworkSubdir,
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000166 modules: frameworkModules,
167 dexLocations: frameworkLocations,
168 dexLocationsDeps: append(artLocations, frameworkLocations...),
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000169 }
170
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000171 configs := map[string]*bootImageConfig{
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000172 artBootImageName: &artCfg,
173 frameworkBootImageName: &frameworkCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000174 }
175
176 // common to all configs
177 for _, c := range configs {
178 c.targets = targets
179
180 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
181 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
182
183 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
184 imageName := c.firstModuleNameOrStem() + ".art"
185
186 c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()}
187
188 // The path to bootclasspath dex files needs to be known at module
189 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
190 // Set up known paths for them, the singleton rules will copy them there.
191 // TODO(b/143682396): use module dependencies instead
192 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
193 for _, m := range c.modules {
194 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar"))
195 }
196 c.dexPathsDeps = c.dexPaths
197
198 c.images = make(map[android.ArchType]android.OutputPath)
199 c.imagesDeps = make(map[android.ArchType]android.OutputPaths)
200
201 for _, target := range targets {
202 arch := target.Arch.ArchType
203 imageDir := c.dir.Join(ctx, c.installSubdir, arch.String())
204 c.images[arch] = imageDir.Join(ctx, imageName)
205 c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex")
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000206 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800207
208 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100209 }
210
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000211 // specific to the framework config
212 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
Ulya Trafimovichb0a2d372020-01-28 14:42:41 +0000213 frameworkCfg.primaryImages = artCfg.images
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000214 frameworkCfg.imageLocations = append(artCfg.imageLocations, frameworkCfg.imageLocations...)
215
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000216 return configs
217 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000218}
219
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000220func artBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000221 return *genBootImageConfigs(ctx)[artBootImageName]
222}
223
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000224func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000225 return *genBootImageConfigs(ctx)[frameworkBootImageName]
226}
227
Colin Cross44df5812019-02-15 23:06:46 -0800228func defaultBootclasspath(ctx android.PathContext) []string {
229 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
Hans Boehm453bf092020-01-25 01:44:30 +0000230 global := dexpreoptGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800231 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000232
Roshan Piusccc26ef2019-11-27 09:37:46 -0800233 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
234 for i, p := range global.UpdatableBootJars {
235 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
236 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000237
238 bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800239 return bootclasspath
240 })
241}
242
243var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
244
245var copyOf = android.CopyOf
246
247func init() {
248 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
249}
250
251func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
252 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000253 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800254 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800255
256 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800257}