blob: 4747c645c2069c30f59cc7142b9007029dd81970 [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 {
Ulyana Trafimovichf2cb7e92019-11-27 12:26:49 +000081 apex, jar := dexpreopt.SplitApexJarPair(m)
Roshan Pius9b51a402019-11-21 12:36:53 -080082 systemServerClasspathLocations = append(systemServerClasspathLocations,
Ulyana Trafimovichf2cb7e92019-11-27 12:26:49 +000083 filepath.Join("/apex", apex, "javalib", jar+".jar"))
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
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000114// Construct a variant of the global config for dexpreopted bootclasspath jars. The variants differ
115// in the list of input jars (libcore, framework, or both), in the naming scheme for the dexpreopt
116// files (ART recognizes "apex" names as special), and whether to include a zip archive.
117//
118// 'name' is a string unique for each profile (used in directory names and ninja rule names)
119// 'stem' is the basename of the image: the resulting filenames are <stem>[-<jar>].{art,oat,vdex}.
120func getBootImageConfig(ctx android.PathContext, key android.OnceKey, name string, stem string,
121 needZip bool, artApexJarsOnly bool) bootImageConfig {
122
Ulya Trafimovich18263382019-10-23 15:56:32 +0100123 return ctx.Config().Once(key, func() interface{} {
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000124 global := dexpreoptGlobalConfig(ctx)
125
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100126 artModules := global.ArtApexJars
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000127 imageModules := artModules
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000128
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100129 var bootLocations []string
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000130
Martin Stjernholmcc4b0ad2019-07-05 22:38:25 +0100131 for _, m := range artModules {
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100132 bootLocations = append(bootLocations,
Jiyong Park0b238752019-10-29 11:23:10 +0900133 filepath.Join("/apex/com.android.art/javalib", stemOf(m)+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000134 }
135
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000136 if !artApexJarsOnly {
137 nonFrameworkModules := concat(artModules, global.ProductUpdatableBootModules)
138 frameworkModules := android.RemoveListFromList(global.BootJars, nonFrameworkModules)
139 imageModules = concat(imageModules, frameworkModules)
140
141 for _, m := range frameworkModules {
142 bootLocations = append(bootLocations,
143 filepath.Join("/system/framework", stemOf(m)+".jar"))
144 }
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100145 }
146
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000147 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
148 // the bootclasspath modules have been compiled. Set up known paths for them, the singleton rules will copy
149 // them there.
Jiyong Park0b238752019-10-29 11:23:10 +0900150 // TODO(b/143682396): use module dependencies instead
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100151 var bootDexPaths android.WritablePaths
152 for _, m := range imageModules {
153 bootDexPaths = append(bootDexPaths,
Nicolas Geoffray24babe32019-10-30 11:26:52 +0000154 android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_"+name+"jars_input", m+".jar"))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000155 }
156
Nicolas Geoffray24babe32019-10-30 11:26:52 +0000157 dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_"+name+"jars")
158 symbolsDir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "dex_"+name+"jars_unstripped")
Ulya Trafimovich18263382019-10-23 15:56:32 +0100159
160 var zip android.WritablePath
161 if needZip {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000162 zip = dir.Join(ctx, stem+".zip")
Ulya Trafimovich18263382019-10-23 15:56:32 +0100163 }
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000164
Colin Crossc11e0c52019-05-08 15:18:22 -0700165 targets := dexpreoptTargets(ctx)
166
Dan Willemsen0f416782019-06-13 21:44:53 +0000167 imageConfig := bootImageConfig{
Ulya Trafimovich18263382019-10-23 15:56:32 +0100168 name: name,
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000169 stem: stem,
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100170 modules: imageModules,
171 dexLocations: bootLocations,
172 dexPaths: bootDexPaths,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000173 dir: dir,
174 symbolsDir: symbolsDir,
Colin Crossc11e0c52019-05-08 15:18:22 -0700175 targets: targets,
Dan Willemsen0f416782019-06-13 21:44:53 +0000176 images: make(map[android.ArchType]android.OutputPath),
177 imagesDeps: make(map[android.ArchType]android.Paths),
Ulya Trafimovich18263382019-10-23 15:56:32 +0100178 zip: zip,
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000179 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000180
181 for _, target := range targets {
182 imageDir := dir.Join(ctx, "system/framework", target.Arch.ArchType.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000183 imageConfig.images[target.Arch.ArchType] = imageDir.Join(ctx, stem+".art")
Dan Willemsen0f416782019-06-13 21:44:53 +0000184
185 imagesDeps := make([]android.Path, 0, len(imageConfig.modules)*3)
186 for _, dep := range imageConfig.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex") {
187 imagesDeps = append(imagesDeps, dep)
188 }
189 imageConfig.imagesDeps[target.Arch.ArchType] = imagesDeps
190 }
191
192 return imageConfig
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000193 }).(bootImageConfig)
194}
195
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000196// Default config is the one that goes in the system image. It includes both libcore and framework.
Ulya Trafimovich18263382019-10-23 15:56:32 +0100197var defaultBootImageConfigKey = android.NewOnceKey("defaultBootImageConfig")
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000198
Ulyana Trafimovichbf0e4762019-11-07 16:12:13 +0000199func defaultBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000200 return getBootImageConfig(ctx, defaultBootImageConfigKey, "boot", "boot", true, false)
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000201}
202
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000203// Apex config is used for the JIT-zygote experiment. It includes both libcore and framework, but AOT-compiles only libcore.
204var apexBootImageConfigKey = android.NewOnceKey("apexBootImageConfig")
205
Ulyana Trafimovichbf0e4762019-11-07 16:12:13 +0000206func apexBootImageConfig(ctx android.PathContext) bootImageConfig {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000207 return getBootImageConfig(ctx, apexBootImageConfigKey, "apex", "apex", false, false)
208}
209
210// ART config is the one used for the ART apex. It includes only libcore.
211var artBootImageConfigKey = android.NewOnceKey("artBootImageConfig")
212
213func artBootImageConfig(ctx android.PathContext) bootImageConfig {
214 return getBootImageConfig(ctx, artBootImageConfigKey, "art", "boot", false, true)
Ulya Trafimovich18263382019-10-23 15:56:32 +0100215}
216
Colin Cross44df5812019-02-15 23:06:46 -0800217func defaultBootclasspath(ctx android.PathContext) []string {
218 return ctx.Config().OnceStringSlice(defaultBootclasspathKey, func() []string {
219 global := dexpreoptGlobalConfig(ctx)
220 image := defaultBootImageConfig(ctx)
221 bootclasspath := append(copyOf(image.dexLocations), global.ProductUpdatableBootLocations...)
222 return bootclasspath
223 })
224}
225
226var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
227
228var copyOf = android.CopyOf
229
230func init() {
231 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
232}
233
234func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
235 ctx.Strict("PRODUCT_BOOTCLASSPATH", strings.Join(defaultBootclasspath(ctx), ":"))
Nicolas Geoffray07b40072019-02-22 16:57:42 +0000236 ctx.Strict("PRODUCT_DEX2OAT_BOOTCLASSPATH", strings.Join(defaultBootImageConfig(ctx).dexLocations, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800237 ctx.Strict("PRODUCT_SYSTEM_SERVER_CLASSPATH", strings.Join(systemServerClasspath(ctx), ":"))
Colin Cross9be41522019-02-20 10:40:13 -0800238
239 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules, ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800240}