blob: a1a9a764c99391e650d3276ac996c100be000247 [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
Colin Cross44df5812019-02-15 23:06:46 -080025// systemServerClasspath returns the on-device locations of the modules in the system server classpath. It is computed
26// once the first time it is called for any ctx.Config(), and returns the same slice for all future calls with the same
27// ctx.Config().
28func systemServerClasspath(ctx android.PathContext) []string {
29 return ctx.Config().OnceStringSlice(systemServerClasspathKey, func() []string {
Martin Stjernholmdae8a802020-01-20 18:12:23 +000030 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -080031
32 var systemServerClasspathLocations []string
33 for _, m := range global.SystemServerJars {
34 systemServerClasspathLocations = append(systemServerClasspathLocations,
35 filepath.Join("/system/framework", m+".jar"))
36 }
Roshan Pius9b51a402019-11-21 12:36:53 -080037 for _, m := range global.UpdatableSystemServerJars {
Roshan Pius9b51a402019-11-21 12:36:53 -080038 systemServerClasspathLocations = append(systemServerClasspathLocations,
Roshan Piusccc26ef2019-11-27 09:37:46 -080039 dexpreopt.GetJarLocationFromApexJarPair(m))
Roshan Pius9b51a402019-11-21 12:36:53 -080040 }
Colin Cross44df5812019-02-15 23:06:46 -080041 return systemServerClasspathLocations
42 })
43}
44
45var systemServerClasspathKey = android.NewOnceKey("systemServerClasspath")
46
Colin Crossc11e0c52019-05-08 15:18:22 -070047// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
48// supported through native bridge.
49func dexpreoptTargets(ctx android.PathContext) []android.Target {
50 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070051 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070052 if target.NativeBridge == android.NativeBridgeDisabled {
53 targets = append(targets, target)
54 }
55 }
56
57 return targets
58}
59
Jiyong Park0b238752019-10-29 11:23:10 +090060func stemOf(moduleName string) string {
61 // b/139391334: the stem of framework-minus-apex is framework
62 // This is hard coded here until we find a good way to query the stem
63 // of a module before any other mutators are run
64 if moduleName == "framework-minus-apex" {
65 return "framework"
66 }
67 return moduleName
68}
69
Roshan Piusccc26ef2019-11-27 09:37:46 -080070func getJarsFromApexJarPairs(apexJarPairs []string) []string {
71 modules := make([]string, len(apexJarPairs))
72 for i, p := range apexJarPairs {
73 _, jar := dexpreopt.SplitApexJarPair(p)
74 modules[i] = jar
75 }
76 return modules
77}
78
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000079var (
Ulya Trafimovich57547452019-12-09 15:40:17 +000080 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
81 artBootImageName = "art"
82 frameworkBootImageName = "boot"
83 artJZBootImageName = "jitzygote-art"
84 frameworkJZBootImageName = "jitzygote-boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000085)
Ulyana Trafimovichde534412019-11-08 10:51:01 +000086
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000087// Construct the global boot image configs.
88func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
89 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
90
Martin Stjernholmdae8a802020-01-20 18:12:23 +000091 global := dexpreopt.GetGlobalConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000092 targets := dexpreoptTargets(ctx)
93 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
Nicolas Geoffray72892f12019-02-22 15:34:40 +000094
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +010095 artModules := global.ArtApexJars
Ulya Trafimovich44561882020-01-03 13:25:54 +000096 // With EMMA_INSTRUMENT_FRAMEWORK=true the Core libraries depend on jacoco.
97 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
98 artModules = append(artModules, "jacocoagent")
99 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000100 frameworkModules := android.RemoveListFromList(global.BootJars,
101 concat(artModules, getJarsFromApexJarPairs(global.UpdatableBootJars)))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000102
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000103 artSubdir := "apex/com.android.art/javalib"
104 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000105
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000106 var artLocations, frameworkLocations []string
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100107 for _, m := range artModules {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000108 artLocations = append(artLocations, filepath.Join("/"+artSubdir, stemOf(m)+".jar"))
109 }
110 for _, m := range frameworkModules {
111 frameworkLocations = append(frameworkLocations, filepath.Join("/"+frameworkSubdir, stemOf(m)+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000112 }
113
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000114 // ART config for the primary boot image in the ART apex.
115 // It includes the Core Libraries.
116 artCfg := bootImageConfig{
117 extension: false,
118 name: artBootImageName,
119 stem: "boot",
120 installSubdir: artSubdir,
121 modules: artModules,
122 dexLocations: artLocations,
123 dexLocationsDeps: artLocations,
124 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000125
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000126 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000127 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000128 frameworkCfg := bootImageConfig{
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000129 extension: true,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000130 name: frameworkBootImageName,
131 stem: "boot",
132 installSubdir: frameworkSubdir,
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000133 modules: frameworkModules,
134 dexLocations: frameworkLocations,
135 dexLocationsDeps: append(artLocations, frameworkLocations...),
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000136 }
137
Ulya Trafimovich57547452019-12-09 15:40:17 +0000138 // ART config for JIT-zygote boot image.
139 artJZCfg := bootImageConfig{
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000140 extension: false,
Ulya Trafimovich57547452019-12-09 15:40:17 +0000141 name: artJZBootImageName,
142 stem: "apex",
143 installSubdir: artSubdir,
144 modules: artModules,
145 dexLocations: artLocations,
146 dexLocationsDeps: artLocations,
147 }
148
149 // Framework config for JIT-zygote boot image extension.
150 frameworkJZCfg := bootImageConfig{
151 extension: true,
152 name: frameworkJZBootImageName,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000153 stem: "apex",
154 installSubdir: frameworkSubdir,
Ulya Trafimovich57547452019-12-09 15:40:17 +0000155 modules: frameworkModules,
156 dexLocations: frameworkLocations,
157 dexLocationsDeps: append(artLocations, frameworkLocations...),
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000158 }
159
160 configs := map[string]*bootImageConfig{
Ulya Trafimovich57547452019-12-09 15:40:17 +0000161 artBootImageName: &artCfg,
162 frameworkBootImageName: &frameworkCfg,
163 artJZBootImageName: &artJZCfg,
164 frameworkJZBootImageName: &frameworkJZCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000165 }
166
167 // common to all configs
168 for _, c := range configs {
169 c.targets = targets
170
171 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
172 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
173
174 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
175 imageName := c.firstModuleNameOrStem() + ".art"
176
177 c.imageLocations = []string{c.dir.Join(ctx, c.installSubdir, imageName).String()}
178
179 // The path to bootclasspath dex files needs to be known at module
180 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
181 // Set up known paths for them, the singleton rules will copy them there.
182 // TODO(b/143682396): use module dependencies instead
183 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
184 for _, m := range c.modules {
185 c.dexPaths = append(c.dexPaths, inputDir.Join(ctx, stemOf(m)+".jar"))
186 }
187 c.dexPathsDeps = c.dexPaths
188
189 c.images = make(map[android.ArchType]android.OutputPath)
190 c.imagesDeps = make(map[android.ArchType]android.OutputPaths)
191
192 for _, target := range targets {
193 arch := target.Arch.ArchType
194 imageDir := c.dir.Join(ctx, c.installSubdir, arch.String())
195 c.images[arch] = imageDir.Join(ctx, imageName)
196 c.imagesDeps[arch] = c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex")
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000197 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800198
199 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100200 }
201
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000202 // specific to the framework config
203 frameworkCfg.dexPathsDeps = append(artCfg.dexPathsDeps, frameworkCfg.dexPathsDeps...)
204 frameworkCfg.imageLocations = append(artCfg.imageLocations, frameworkCfg.imageLocations...)
205
Ulya Trafimovich57547452019-12-09 15:40:17 +0000206 // specific to the jitzygote-framework config
207 frameworkJZCfg.dexPathsDeps = append(artJZCfg.dexPathsDeps, frameworkJZCfg.dexPathsDeps...)
208 frameworkJZCfg.imageLocations = append(artJZCfg.imageLocations, frameworkJZCfg.imageLocations...)
209
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000210 return configs
211 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000212}
213
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000214func artBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000215 return *genBootImageConfigs(ctx)[artBootImageName]
216}
217
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000218func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000219 return *genBootImageConfigs(ctx)[frameworkBootImageName]
220}
221
Ulya Trafimovich57547452019-12-09 15:40:17 +0000222func artJZBootImageConfig(ctx android.PathContext) bootImageConfig {
223 return *genBootImageConfigs(ctx)[artJZBootImageName]
224}
225
226func frameworkJZBootImageConfig(ctx android.PathContext) bootImageConfig {
227 return *genBootImageConfigs(ctx)[frameworkJZBootImageName]
Ulya Trafimovich18263382019-10-23 15:56:32 +0100228}
229
Colin Cross44df5812019-02-15 23:06:46 -0800230func defaultBootclasspath(ctx android.PathContext) []string {
231 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
Martin Stjernholmdae8a802020-01-20 18:12:23 +0000232 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800233 image := defaultBootImageConfig(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000234
Roshan Piusccc26ef2019-11-27 09:37:46 -0800235 updatableBootclasspath := make([]string, len(global.UpdatableBootJars))
236 for i, p := range global.UpdatableBootJars {
237 updatableBootclasspath[i] = dexpreopt.GetJarLocationFromApexJarPair(p)
238 }
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000239
240 bootclasspath := append(copyOf(image.dexLocationsDeps), updatableBootclasspath...)
Colin Cross44df5812019-02-15 23:06:46 -0800241 return bootclasspath
242 })
243}
244
245var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
246
247var copyOf = android.CopyOf
248
249func init() {
250 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
251}
252
253func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
254 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000255 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocationsDeps, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800256 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800257
258 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800259}