blob: 57b6125f6db701cb5a0927828ff464711a617fe3 [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().
73func systemServerClasspath(ctx android.PathContext) []string {
74 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
77 var systemServerClasspathLocations []string
78 for _, m := range global.SystemServerJars {
79 systemServerClasspathLocations = append(systemServerClasspathLocations,
80 filepath.Join("/system/framework", m+".jar"))
81 }
Roshan Pius9b51a402019-11-21 12:36:53 -080082 for _, m := range global.UpdatableSystemServerJars {
Roshan Pius9b51a402019-11-21 12:36:53 -080083 systemServerClasspathLocations = append(systemServerClasspathLocations,
Roshan Piusccc26ef2019-11-27 09:37:46 -080084 dexpreopt.GetJarLocationFromApexJarPair(m))
Roshan Pius9b51a402019-11-21 12:36:53 -080085 }
Colin Cross44df5812019-02-15 23:06:46 -080086 return systemServerClasspathLocations
87 })
88}
89
90var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
91
Colin Crossc11e0c52019-05-08 15:18:22 -070092// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
93// supported through native bridge.
94func dexpreoptTargets(ctx android.PathContext) []android.Target {
95 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070096 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070097 if target.NativeBridge == android.NativeBridgeDisabled {
98 targets = append(targets, target)
99 }
100 }
101
102 return targets
103}
104
Jiyong Park0b238752019-10-29 11:23:10 +0900105func stemOf(moduleName string) string {
106 // b/139391334: the stem of framework-minus-apex is framework
107 // This is hard coded here until we find a good way to query the stem
108 // of a module before any other mutators are run
109 if moduleName == "framework-minus-apex" {
110 return "framework"
111 }
112 return moduleName
113}
114
Roshan Piusccc26ef2019-11-27 09:37:46 -0800115func getJarsFromApexJarPairs(apexJarPairs []string) []string {
116 modules := make([]string, len(apexJarPairs))
117 for i, p := range apexJarPairs {
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000118 _, jar := android.SplitApexJarPair(p)
Roshan Piusccc26ef2019-11-27 09:37:46 -0800119 modules[i] = jar
120 }
121 return modules
122}
123
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000124var (
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000125 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
126 artBootImageName = "art"
127 frameworkBootImageName = "boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000128)
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
Hans Boehm453bf092020-01-25 01:44:30 +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 Trafimovich44561882020-01-03 13:25:54 +0000139 // With EMMA_INSTRUMENT_FRAMEWORK=true the Core libraries depend on jacoco.
140 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
141 artModules = append(artModules, "jacocoagent")
142 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000143 frameworkModules := android.RemoveListFromList(global.BootJars,
144 concat(artModules, getJarsFromApexJarPairs(global.UpdatableBootJars)))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000145
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000146 artSubdir := "apex/com.android.art/javalib"
147 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000148
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000149 var artLocations, frameworkLocations []string
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100150 for _, m := range artModules {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000151 artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar"))
152 }
153 for _, m := range frameworkModules {
154 frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000155 }
156
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000157 // ART config for the primary boot image in the ART apex.
158 // It includes the Core Libraries.
159 artCfg := bootImageConfig{
160 extension: false,
161 name: artBootImageName,
162 stem: "boot",
163 installSubdir: artSubdir,
164 modules: artModules,
165 dexLocations: artLocations,
166 dexLocationsDeps: artLocations,
167 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000168
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000169 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000170 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000171 frameworkCfg := bootImageConfig{
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000172 extension: true,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000173 name: frameworkBootImageName,
174 stem: "boot",
175 installSubdir: frameworkSubdir,
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000176 modules: frameworkModules,
177 dexLocations: frameworkLocations,
178 dexLocationsDeps: append(artLocations, frameworkLocations...),
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000179 }
180
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000181 configs := map[string]*bootImageConfig{
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000182 artBootImageName: &artCfg,
183 frameworkBootImageName: &frameworkCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000184 }
185
186 // common to all configs
187 for _, c := range configs {
188 c.targets = targets
189
190 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
191 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
192
193 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
194 imageName := c.firstModuleNameOrStem() + ".art"
195
196 c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()}
197
198 // The path to bootclasspath dex files needs to be known at module
199 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
200 // Set up known paths for them, the singleton rules will copy them there.
201 // TODO(b/143682396): use module dependencies instead
202 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
203 for _, m := range c.modules {
204 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar"))
205 }
206 c.dexPathsDeps = c.dexPaths
207
208 c.images = make(map[android.ArchType]android.OutputPath)
209 c.imagesDeps = make(map[android.ArchType]android.OutputPaths)
210
211 for _, target := range targets {
212 arch := target.Arch.ArchType
213 imageDir := c.dir.Join(ctx, c.installSubdir, arch.String())
214 c.images[arch] = imageDir.Join(ctx, imageName)
215 c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex")
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000216 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800217
218 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100219 }
220
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000221 // specific to the framework config
222 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
Ulya Trafimovichb0a2d372020-01-28 14:42:41 +0000223 frameworkCfg.primaryImages = artCfg.images
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000224 frameworkCfg.imageLocations = append(artCfg.imageLocations, frameworkCfg.imageLocations...)
225
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000226 return configs
227 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000228}
229
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000230func artBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000231 return *genBootImageConfigs(ctx)[artBootImageName]
232}
233
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000234func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000235 return *genBootImageConfigs(ctx)[frameworkBootImageName]
236}
237
Colin Cross44df5812019-02-15 23:06:46 -0800238func defaultBootclasspath(ctx android.PathContext) []string {
239 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
Hans Boehm453bf092020-01-25 01:44:30 +0000240 global := dexpreoptGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800241 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000242
Roshan Piusccc26ef2019-11-27 09:37:46 -0800243 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
244 for i, p := range global.UpdatableBootJars {
245 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
246 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000247
248 bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800249 return bootclasspath
250 })
251}
252
253var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
254
255var copyOf = android.CopyOf
256
257func init() {
258 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
259}
260
261func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
262 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000263 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800264 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800265
266 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800267}