blob: fb5c325d3543bc8bd5e265f82b29871c1bb869c0 [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 (
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +010043 bootImageConfigKey = android.NewOnceKey("bootImageConfig")
44 bootImageConfigRawKey = android.NewOnceKey("bootImageConfigRaw")
45 frameworkBootImageName = "boot"
46 mainlineBootImageName = "mainline"
47 bootImageStem = "boot"
Spandan Dasda739a32023-12-13 00:06:32 +000048 ProfileInstallPathInApex = "etc/boot-image.prof"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000049)
Ulyana Trafimovichde534412019-11-08 10:51:01 +000050
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +010051// getImageNames returns an ordered list of image names. The order doesn't matter but needs to be
52// deterministic. The names listed here must match the map keys returned by genBootImageConfigs.
53func getImageNames() []string {
54 return []string{"art", "boot", "mainline"}
55}
56
Jiakai Zhang6decef92022-01-12 17:56:19 +000057func genBootImageConfigRaw(ctx android.PathContext) map[string]*bootImageConfig {
58 return ctx.Config().Once(bootImageConfigRawKey, func() interface{} {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000059 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray72892f12019-02-22 15:34:40 +000060
Jiakai Zhang556bdf82023-07-12 16:51:57 +010061 artBootImageName := "art" // Keep this local to avoid accidental references.
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +010062 frameworkModules := global.BootJars // This includes `global.ArtApexJars`.
Jiakai Zhangb8796202023-03-06 19:16:48 +000063 mainlineBcpModules := global.ApexBootJars
64 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +000065
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +010066 profileImports := []string{"com.android.art"}
67
68 // ART boot image for testing only. Do not rely on it to make any build-time decision.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000069 artCfg := bootImageConfig{
Colin Crossdcc61562025-02-01 14:10:29 -080070 name: artBootImageName,
71 enabledIfExists: "art-bootclasspath-fragment",
72 stem: bootImageStem,
73 installDir: "apex/art_boot_images/javalib",
74 modules: global.TestOnlyArtBootImageJars,
75 preloadedClassesFile: "art/build/boot/preloaded-classes",
76 compilerFilter: "speed-profile",
77 singleImage: false,
78 profileImports: profileImports,
79 profileProviderModule: "art-bootclasspath-fragment",
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000080 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +000081
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000082 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +000083 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000084 frameworkCfg := bootImageConfig{
Nicolas Geoffrayb9a46fb2022-03-14 15:31:47 +000085 name: frameworkBootImageName,
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +010086 enabledIfExists: "platform-bootclasspath",
Jiakai Zhangb8796202023-03-06 19:16:48 +000087 stem: bootImageStem,
Jiakai Zhang09d88df2023-05-10 17:04:53 +010088 installDir: frameworkSubdir,
Nicolas Geoffrayb9a46fb2022-03-14 15:31:47 +000089 modules: frameworkModules,
90 preloadedClassesFile: "frameworks/base/config/preloaded-classes",
Jiakai Zhang8e9ea8b2023-02-23 17:50:46 +000091 compilerFilter: "speed-profile",
Jiakai Zhangb8796202023-03-06 19:16:48 +000092 singleImage: false,
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +010093 profileImports: profileImports,
Jiakai Zhangb8796202023-03-06 19:16:48 +000094 }
95
96 mainlineCfg := bootImageConfig{
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +010097 extends: &frameworkCfg,
98 name: mainlineBootImageName,
99 enabledIfExists: "platform-bootclasspath",
100 stem: bootImageStem,
101 installDir: frameworkSubdir,
102 modules: mainlineBcpModules,
103 compilerFilter: "verify",
104 singleImage: true,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000105 }
106
Jiakai Zhang6decef92022-01-12 17:56:19 +0000107 return map[string]*bootImageConfig{
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000108 artBootImageName: &artCfg,
109 frameworkBootImageName: &frameworkCfg,
Jiakai Zhangb8796202023-03-06 19:16:48 +0000110 mainlineBootImageName: &mainlineCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000111 }
Jiakai Zhang6decef92022-01-12 17:56:19 +0000112 }).(map[string]*bootImageConfig)
113}
114
115// Construct the global boot image configs.
116func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
117 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
118 targets := dexpreoptTargets(ctx)
Jiakai Zhang7d292222024-01-18 17:27:42 +0000119 deviceDir := android.PathForOutput(ctx, dexpreopt.GetDexpreoptDirName(ctx))
Jiakai Zhang6decef92022-01-12 17:56:19 +0000120
121 configs := genBootImageConfigRaw(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000122
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000123 for _, c := range configs {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000124 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
125 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
126
127 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
Ulya Trafimovich8640ab92020-05-11 18:06:15 +0100128 imageName := c.firstModuleNameOrStem(ctx) + ".art"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000129
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000130 // The path to bootclasspath dex files needs to be known at module
131 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
132 // Set up known paths for them, the singleton rules will copy them there.
133 // TODO(b/143682396): use module dependencies instead
134 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100135 c.dexPaths = c.modules.BuildPaths(ctx, inputDir)
Paul Duffin5f148ca2021-06-02 17:24:22 +0100136 c.dexPathsByModule = c.modules.BuildPathsByModule(ctx, inputDir)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000137 c.dexPathsDeps = c.dexPaths
138
David Srbeckyc177ebe2020-02-18 20:43:06 +0000139 // Create target-specific variants.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000140 for _, target := range targets {
141 arch := target.Arch.ArchType
Jiakai Zhang09d88df2023-05-10 17:04:53 +0100142 imageDir := c.dir.Join(ctx, target.Os.String(), c.installDir, arch.String())
David Srbeckyc177ebe2020-02-18 20:43:06 +0000143 variant := &bootImageVariant{
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900144 bootImageConfig: c,
145 target: target,
146 imagePathOnHost: imageDir.Join(ctx, imageName),
Jiakai Zhang09d88df2023-05-10 17:04:53 +0100147 imagePathOnDevice: filepath.Join("/", c.installDir, arch.String(), imageName),
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900148 imagesDeps: c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex"),
149 dexLocations: c.modules.DevicePaths(ctx.Config(), target.Os),
David Srbeckyab994982020-03-30 17:24:13 +0100150 }
151 variant.dexLocationsDeps = variant.dexLocations
David Srbeckyc177ebe2020-02-18 20:43:06 +0000152 c.variants = append(c.variants, variant)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000153 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800154
155 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100156 }
157
Jiakai Zhang8fe3a412023-02-23 17:37:16 +0000158 visited := make(map[string]bool)
159 for _, c := range configs {
160 calculateDepsRecursive(c, targets, visited)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000161 }
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000162
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000163 return configs
164 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000165}
166
Jiakai Zhang8fe3a412023-02-23 17:37:16 +0000167// calculateDepsRecursive calculates the dependencies of the given boot image config and all its
168// ancestors, if they are not visited.
169// The boot images are supposed to form a tree, where the root is the primary boot image. We do not
170// expect loops (e.g., A extends B, B extends C, C extends A), and we let them crash soong with a
171// stack overflow.
172// Note that a boot image config only has a pointer to the parent, not to children. Therefore, we
173// first go up through the parent chain, and then go back down to visit every code along the path.
174// `visited` is a map where a key is a boot image name and the value indicates whether the boot
175// image config is visited. The boot image names are guaranteed to be unique because they come from
176// `genBootImageConfigRaw` above, which also returns a map and would fail in the first place if the
177// names were not unique.
178func calculateDepsRecursive(c *bootImageConfig, targets []android.Target, visited map[string]bool) {
179 if c.extends == nil || visited[c.name] {
180 return
181 }
182 if c.extends.extends != nil {
183 calculateDepsRecursive(c.extends, targets, visited)
184 }
185 visited[c.name] = true
186 c.dexPathsDeps = android.Concat(c.extends.dexPathsDeps, c.dexPathsDeps)
187 for i := range targets {
188 c.variants[i].baseImages = android.Concat(c.extends.variants[i].baseImages, android.OutputPaths{c.extends.variants[i].imagePathOnHost})
189 c.variants[i].baseImagesDeps = android.Concat(c.extends.variants[i].baseImagesDeps, c.extends.variants[i].imagesDeps.Paths())
190 c.variants[i].dexLocationsDeps = android.Concat(c.extends.variants[i].dexLocationsDeps, c.variants[i].dexLocationsDeps)
191 }
192}
193
David Srbeckyc177ebe2020-02-18 20:43:06 +0000194func defaultBootImageConfig(ctx android.PathContext) *bootImageConfig {
195 return genBootImageConfigs(ctx)[frameworkBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000196}
197
Jiakai Zhangb8796202023-03-06 19:16:48 +0000198func mainlineBootImageConfig(ctx android.PathContext) *bootImageConfig {
199 return genBootImageConfigs(ctx)[mainlineBootImageName]
200}
201
Jiakai Zhangcb13b5d2023-07-13 11:03:38 +0100202// isProfileProviderApex returns true if this apex provides a boot image profile.
203func isProfileProviderApex(ctx android.PathContext, apexName string) bool {
204 for _, config := range genBootImageConfigs(ctx) {
205 for _, profileImport := range config.profileImports {
206 if profileImport == apexName {
207 return true
208 }
209 }
210 }
211 return false
212}
213
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000214// Returns a list of paths and a list of locations for the boot jars used in dexpreopt (to be
215// passed in -Xbootclasspath and -Xbootclasspath-locations arguments for dex2oat).
216func bcpForDexpreopt(ctx android.PathContext, withUpdatable bool) (android.WritablePaths, []string) {
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000217 bootImage := defaultBootImageConfig(ctx)
Jiakai Zhangc6879f32023-11-06 16:31:19 +0000218 if withUpdatable {
219 bootImage = mainlineBootImageConfig(ctx)
220 }
221
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000222 dexPaths := bootImage.dexPathsDeps
223 // The dex locations for all Android variants are identical.
224 dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps
225
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000226 return dexPaths, dexLocations
227}
228
Colin Cross44df5812019-02-15 23:06:46 -0800229var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
230
Colin Cross44df5812019-02-15 23:06:46 -0800231func init() {
232 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
233}
234
235func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100236 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules.CopyOfApexJarPairs(), ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800237}