blob: 76c78cb2973cbbf2ea5553e6642ae03d8be579c8 [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 Crossc11e0c52019-05-08 15:18:22 -070025// dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures
26// supported through native bridge.
27func dexpreoptTargets(ctx android.PathContext) []android.Target {
28 var targets []android.Target
Colin Cross3b19f5d2019-09-17 14:45:31 -070029 for _, target := range ctx.Config().Targets[android.Android] {
Colin Crossc11e0c52019-05-08 15:18:22 -070030 if target.NativeBridge == android.NativeBridgeDisabled {
31 targets = append(targets, target)
32 }
33 }
David Srbecky7f8dac12020-02-13 16:00:45 +000034 // We may also need the images on host in order to run host-based tests.
Colin Cross0c66bc62021-07-20 09:47:41 -070035 for _, target := range ctx.Config().Targets[ctx.Config().BuildOS] {
David Srbecky7f8dac12020-02-13 16:00:45 +000036 targets = append(targets, target)
37 }
Colin Crossc11e0c52019-05-08 15:18:22 -070038
39 return targets
40}
41
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000042var (
Ulya Trafimovich4cdada22020-02-10 15:29:28 +000043 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
Jiakai Zhang6decef92022-01-12 17:56:19 +000044 bootImageConfigRawKey = android.NewOnceKey("bootImageConfigRaw")
Ulya Trafimovich4cdada22020-02-10 15:29:28 +000045 artBootImageName = "art"
46 frameworkBootImageName = "boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000047)
Ulyana Trafimovichde534412019-11-08 10:51:01 +000048
Jiakai Zhang6decef92022-01-12 17:56:19 +000049func genBootImageConfigRaw(ctx android.PathContext) map[string]*bootImageConfig {
50 return ctx.Config().Once(bootImageConfigRawKey, func() interface{} {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000051 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray72892f12019-02-22 15:34:40 +000052
Paul Duffin7d584e92020-10-23 18:26:03 +010053 artModules := global.ArtApexJars
Paul Duffin7d584e92020-10-23 18:26:03 +010054 frameworkModules := global.BootJars.RemoveList(artModules)
Nicolas Geoffray72892f12019-02-22 15:34:40 +000055
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000056 // ART config for the primary boot image in the ART apex.
57 // It includes the Core Libraries.
58 artCfg := bootImageConfig{
Jiakai Zhang49b1eb62021-11-26 18:09:27 +000059 name: artBootImageName,
Jiakai Zhangdb160c02023-03-02 12:56:13 +000060 stem: "boot",
Jiakai Zhang49b1eb62021-11-26 18:09:27 +000061 installDirOnHost: "apex/art_boot_images/javalib",
Jiakai Zhangdb160c02023-03-02 12:56:13 +000062 installDirOnDevice: "system/framework",
Jiakai Zhang49b1eb62021-11-26 18:09:27 +000063 profileInstallPathInApex: "etc/boot-image.prof",
64 modules: artModules,
Nicolas Geoffrayb9a46fb2022-03-14 15:31:47 +000065 preloadedClassesFile: "art/build/boot/preloaded-classes",
Jiakai Zhang8e9ea8b2023-02-23 17:50:46 +000066 compilerFilter: "speed-profile",
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000067 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +000068
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000069 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +000070 // It includes framework libraries and depends on the ART config.
Jiakai Zhangdb160c02023-03-02 12:56:13 +000071 frameworkSubdir := "system/framework"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000072 frameworkCfg := bootImageConfig{
Nicolas Geoffrayb9a46fb2022-03-14 15:31:47 +000073 extends: &artCfg,
74 name: frameworkBootImageName,
Jiakai Zhangdb160c02023-03-02 12:56:13 +000075 stem: "boot",
Nicolas Geoffrayb9a46fb2022-03-14 15:31:47 +000076 installDirOnHost: frameworkSubdir,
77 installDirOnDevice: frameworkSubdir,
78 modules: frameworkModules,
79 preloadedClassesFile: "frameworks/base/config/preloaded-classes",
Jiakai Zhang8e9ea8b2023-02-23 17:50:46 +000080 compilerFilter: "speed-profile",
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000081 }
82
Jiakai Zhang6decef92022-01-12 17:56:19 +000083 return map[string]*bootImageConfig{
Ulya Trafimovich4cdada22020-02-10 15:29:28 +000084 artBootImageName: &artCfg,
85 frameworkBootImageName: &frameworkCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000086 }
Jiakai Zhang6decef92022-01-12 17:56:19 +000087 }).(map[string]*bootImageConfig)
88}
89
90// Construct the global boot image configs.
91func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
92 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
93 targets := dexpreoptTargets(ctx)
94 deviceDir := android.PathForOutput(ctx, ctx.Config().DeviceName())
95
96 configs := genBootImageConfigRaw(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000097
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000098 for _, c := range configs {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000099 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
100 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
101
102 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
Ulya Trafimovich8640ab92020-05-11 18:06:15 +0100103 imageName := c.firstModuleNameOrStem(ctx) + ".art"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000104
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000105 // The path to bootclasspath dex files needs to be known at module
106 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
107 // Set up known paths for them, the singleton rules will copy them there.
108 // TODO(b/143682396): use module dependencies instead
109 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100110 c.dexPaths = c.modules.BuildPaths(ctx, inputDir)
Paul Duffin5f148ca2021-06-02 17:24:22 +0100111 c.dexPathsByModule = c.modules.BuildPathsByModule(ctx, inputDir)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000112 c.dexPathsDeps = c.dexPaths
113
David Srbeckyc177ebe2020-02-18 20:43:06 +0000114 // Create target-specific variants.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000115 for _, target := range targets {
116 arch := target.Arch.ArchType
Jeongik Chaa5969092021-05-07 18:53:21 +0900117 imageDir := c.dir.Join(ctx, target.Os.String(), c.installDirOnHost, arch.String())
David Srbeckyc177ebe2020-02-18 20:43:06 +0000118 variant := &bootImageVariant{
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900119 bootImageConfig: c,
120 target: target,
121 imagePathOnHost: imageDir.Join(ctx, imageName),
122 imagePathOnDevice: filepath.Join("/", c.installDirOnDevice, arch.String(), imageName),
123 imagesDeps: c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex"),
124 dexLocations: c.modules.DevicePaths(ctx.Config(), target.Os),
David Srbeckyab994982020-03-30 17:24:13 +0100125 }
126 variant.dexLocationsDeps = variant.dexLocations
David Srbeckyc177ebe2020-02-18 20:43:06 +0000127 c.variants = append(c.variants, variant)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000128 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800129
130 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100131 }
132
Jiakai Zhang8fe3a412023-02-23 17:37:16 +0000133 visited := make(map[string]bool)
134 for _, c := range configs {
135 calculateDepsRecursive(c, targets, visited)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000136 }
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000137
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000138 return configs
139 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000140}
141
Jiakai Zhang8fe3a412023-02-23 17:37:16 +0000142// calculateDepsRecursive calculates the dependencies of the given boot image config and all its
143// ancestors, if they are not visited.
144// The boot images are supposed to form a tree, where the root is the primary boot image. We do not
145// expect loops (e.g., A extends B, B extends C, C extends A), and we let them crash soong with a
146// stack overflow.
147// Note that a boot image config only has a pointer to the parent, not to children. Therefore, we
148// first go up through the parent chain, and then go back down to visit every code along the path.
149// `visited` is a map where a key is a boot image name and the value indicates whether the boot
150// image config is visited. The boot image names are guaranteed to be unique because they come from
151// `genBootImageConfigRaw` above, which also returns a map and would fail in the first place if the
152// names were not unique.
153func calculateDepsRecursive(c *bootImageConfig, targets []android.Target, visited map[string]bool) {
154 if c.extends == nil || visited[c.name] {
155 return
156 }
157 if c.extends.extends != nil {
158 calculateDepsRecursive(c.extends, targets, visited)
159 }
160 visited[c.name] = true
161 c.dexPathsDeps = android.Concat(c.extends.dexPathsDeps, c.dexPathsDeps)
162 for i := range targets {
163 c.variants[i].baseImages = android.Concat(c.extends.variants[i].baseImages, android.OutputPaths{c.extends.variants[i].imagePathOnHost})
164 c.variants[i].baseImagesDeps = android.Concat(c.extends.variants[i].baseImagesDeps, c.extends.variants[i].imagesDeps.Paths())
165 c.variants[i].dexLocationsDeps = android.Concat(c.extends.variants[i].dexLocationsDeps, c.variants[i].dexLocationsDeps)
166 }
167}
168
David Srbeckyc177ebe2020-02-18 20:43:06 +0000169func artBootImageConfig(ctx android.PathContext) *bootImageConfig {
170 return genBootImageConfigs(ctx)[artBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000171}
172
David Srbeckyc177ebe2020-02-18 20:43:06 +0000173func defaultBootImageConfig(ctx android.PathContext) *bootImageConfig {
174 return genBootImageConfigs(ctx)[frameworkBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000175}
176
satayevd604b212021-07-21 14:23:52 +0100177// Apex boot config allows to access build/install paths of apex boot jars without going
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000178// through the usual trouble of registering dependencies on those modules and extracting build paths
179// from those dependencies.
satayevd604b212021-07-21 14:23:52 +0100180type apexBootConfig struct {
181 // A list of apex boot jars.
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000182 modules android.ConfiguredJarList
183
satayevd604b212021-07-21 14:23:52 +0100184 // A list of predefined build paths to apex boot jars. They are configured very early,
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000185 // before the modules for these jars are processed and the actual paths are generated, and
186 // later on a singleton adds commands to copy actual jars to the predefined paths.
187 dexPaths android.WritablePaths
188
Paul Duffin5f148ca2021-06-02 17:24:22 +0100189 // Map from module name (without prebuilt_ prefix) to the predefined build path.
190 dexPathsByModule map[string]android.WritablePath
191
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000192 // A list of dex locations (a.k.a. on-device paths) to the boot jars.
193 dexLocations []string
194}
195
satayevd604b212021-07-21 14:23:52 +0100196var updatableBootConfigKey = android.NewOnceKey("apexBootConfig")
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000197
satayevd604b212021-07-21 14:23:52 +0100198// Returns apex boot config.
199func GetApexBootConfig(ctx android.PathContext) apexBootConfig {
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000200 return ctx.Config().Once(updatableBootConfigKey, func() interface{} {
satayevd604b212021-07-21 14:23:52 +0100201 apexBootJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000202
satayevd604b212021-07-21 14:23:52 +0100203 dir := android.PathForOutput(ctx, ctx.Config().DeviceName(), "apex_bootjars")
204 dexPaths := apexBootJars.BuildPaths(ctx, dir)
205 dexPathsByModuleName := apexBootJars.BuildPathsByModule(ctx, dir)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000206
satayevd604b212021-07-21 14:23:52 +0100207 dexLocations := apexBootJars.DevicePaths(ctx.Config(), android.Android)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000208
satayevd604b212021-07-21 14:23:52 +0100209 return apexBootConfig{apexBootJars, dexPaths, dexPathsByModuleName, dexLocations}
210 }).(apexBootConfig)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000211}
212
213// Returns a list of paths and a list of locations for the boot jars used in dexpreopt (to be
214// passed in -Xbootclasspath and -Xbootclasspath-locations arguments for dex2oat).
215func bcpForDexpreopt(ctx android.PathContext, withUpdatable bool) (android.WritablePaths, []string) {
216 // Non-updatable boot jars (they are used both in the boot image and in dexpreopt).
217 bootImage := defaultBootImageConfig(ctx)
218 dexPaths := bootImage.dexPathsDeps
219 // The dex locations for all Android variants are identical.
220 dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps
221
222 if withUpdatable {
satayevd604b212021-07-21 14:23:52 +0100223 // Apex boot jars (they are used only in dexpreopt, but not in the boot image).
224 apexBootConfig := GetApexBootConfig(ctx)
225 dexPaths = append(dexPaths, apexBootConfig.dexPaths...)
226 dexLocations = append(dexLocations, apexBootConfig.dexLocations...)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000227 }
228
229 return dexPaths, dexLocations
230}
231
Colin Cross44df5812019-02-15 23:06:46 -0800232var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
233
234var copyOf = android.CopyOf
235
236func init() {
237 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
238}
239
240func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100241 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules.CopyOfApexJarPairs(), ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800242}