blob: 8f732cf56bed2c1071fb69bbcfc4157682925fae [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"
Jiakai Zhangb8796202023-03-06 19:16:48 +000047 mainlineBootImageName = "mainline"
48 bootImageStem = "boot"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000049)
Ulyana Trafimovichde534412019-11-08 10:51:01 +000050
Jiakai Zhang6decef92022-01-12 17:56:19 +000051func genBootImageConfigRaw(ctx android.PathContext) map[string]*bootImageConfig {
52 return ctx.Config().Once(bootImageConfigRawKey, func() interface{} {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000053 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray72892f12019-02-22 15:34:40 +000054
Paul Duffin7d584e92020-10-23 18:26:03 +010055 artModules := global.ArtApexJars
Paul Duffin7d584e92020-10-23 18:26:03 +010056 frameworkModules := global.BootJars.RemoveList(artModules)
Jiakai Zhangb8796202023-03-06 19:16:48 +000057 mainlineBcpModules := global.ApexBootJars
58 frameworkSubdir := "system/framework"
Nicolas Geoffray72892f12019-02-22 15:34:40 +000059
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000060 // ART config for the primary boot image in the ART apex.
61 // It includes the Core Libraries.
62 artCfg := bootImageConfig{
Jiakai Zhang49b1eb62021-11-26 18:09:27 +000063 name: artBootImageName,
Jiakai Zhangb8796202023-03-06 19:16:48 +000064 stem: bootImageStem,
Jiakai Zhang49b1eb62021-11-26 18:09:27 +000065 installDirOnHost: "apex/art_boot_images/javalib",
Jiakai Zhangb8796202023-03-06 19:16:48 +000066 installDirOnDevice: frameworkSubdir,
Jiakai Zhang49b1eb62021-11-26 18:09:27 +000067 profileInstallPathInApex: "etc/boot-image.prof",
68 modules: artModules,
Nicolas Geoffrayb9a46fb2022-03-14 15:31:47 +000069 preloadedClassesFile: "art/build/boot/preloaded-classes",
Jiakai Zhang8e9ea8b2023-02-23 17:50:46 +000070 compilerFilter: "speed-profile",
Jiakai Zhangb8796202023-03-06 19:16:48 +000071 singleImage: false,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000072 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +000073
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000074 // Framework config for the boot image extension.
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +000075 // It includes framework libraries and depends on the ART config.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000076 frameworkCfg := bootImageConfig{
Nicolas Geoffrayb9a46fb2022-03-14 15:31:47 +000077 extends: &artCfg,
78 name: frameworkBootImageName,
Jiakai Zhangb8796202023-03-06 19:16:48 +000079 stem: bootImageStem,
Nicolas Geoffrayb9a46fb2022-03-14 15:31:47 +000080 installDirOnHost: frameworkSubdir,
81 installDirOnDevice: frameworkSubdir,
82 modules: frameworkModules,
83 preloadedClassesFile: "frameworks/base/config/preloaded-classes",
Jiakai Zhang8e9ea8b2023-02-23 17:50:46 +000084 compilerFilter: "speed-profile",
Jiakai Zhangb8796202023-03-06 19:16:48 +000085 singleImage: false,
86 }
87
88 mainlineCfg := bootImageConfig{
89 extends: &frameworkCfg,
90 name: mainlineBootImageName,
91 stem: bootImageStem,
92 installDirOnHost: frameworkSubdir,
93 installDirOnDevice: frameworkSubdir,
94 modules: mainlineBcpModules,
95 compilerFilter: "verify",
96 singleImage: true,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000097 }
98
Jiakai Zhang6decef92022-01-12 17:56:19 +000099 return map[string]*bootImageConfig{
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000100 artBootImageName: &artCfg,
101 frameworkBootImageName: &frameworkCfg,
Jiakai Zhangb8796202023-03-06 19:16:48 +0000102 mainlineBootImageName: &mainlineCfg,
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000103 }
Jiakai Zhang6decef92022-01-12 17:56:19 +0000104 }).(map[string]*bootImageConfig)
105}
106
107// Construct the global boot image configs.
108func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig {
109 return ctx.Config().Once(bootImageConfigKey, func() interface{} {
110 targets := dexpreoptTargets(ctx)
Jeongik Cha4753b392023-04-19 23:25:41 +0900111 archType := ctx.Config().Targets[android.Android][0].Arch.ArchType
112 deviceDir := android.PathForOutput(ctx, toDexpreoptDirName(archType))
Jiakai Zhang6decef92022-01-12 17:56:19 +0000113
114 configs := genBootImageConfigRaw(ctx)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000115
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000116 for _, c := range configs {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000117 c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars")
118 c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped")
119
120 // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension
Ulya Trafimovich8640ab92020-05-11 18:06:15 +0100121 imageName := c.firstModuleNameOrStem(ctx) + ".art"
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000122
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000123 // The path to bootclasspath dex files needs to be known at module
124 // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled.
125 // Set up known paths for them, the singleton rules will copy them there.
126 // TODO(b/143682396): use module dependencies instead
127 inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input")
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100128 c.dexPaths = c.modules.BuildPaths(ctx, inputDir)
Paul Duffin5f148ca2021-06-02 17:24:22 +0100129 c.dexPathsByModule = c.modules.BuildPathsByModule(ctx, inputDir)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000130 c.dexPathsDeps = c.dexPaths
131
David Srbeckyc177ebe2020-02-18 20:43:06 +0000132 // Create target-specific variants.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000133 for _, target := range targets {
134 arch := target.Arch.ArchType
Jeongik Chaa5969092021-05-07 18:53:21 +0900135 imageDir := c.dir.Join(ctx, target.Os.String(), c.installDirOnHost, arch.String())
David Srbeckyc177ebe2020-02-18 20:43:06 +0000136 variant := &bootImageVariant{
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900137 bootImageConfig: c,
138 target: target,
139 imagePathOnHost: imageDir.Join(ctx, imageName),
140 imagePathOnDevice: filepath.Join("/", c.installDirOnDevice, arch.String(), imageName),
141 imagesDeps: c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex"),
142 dexLocations: c.modules.DevicePaths(ctx.Config(), target.Os),
David Srbeckyab994982020-03-30 17:24:13 +0100143 }
144 variant.dexLocationsDeps = variant.dexLocations
David Srbeckyc177ebe2020-02-18 20:43:06 +0000145 c.variants = append(c.variants, variant)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000146 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800147
148 c.zip = c.dir.Join(ctx, c.name+".zip")
Nicolas Geoffrayfeef2ef2019-04-30 09:43:22 +0100149 }
150
Jiakai Zhang8fe3a412023-02-23 17:37:16 +0000151 visited := make(map[string]bool)
152 for _, c := range configs {
153 calculateDepsRecursive(c, targets, visited)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000154 }
Ulyana Trafimovich5a4ccd12019-12-18 17:32:33 +0000155
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000156 return configs
157 }).(map[string]*bootImageConfig)
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000158}
159
Jiakai Zhang8fe3a412023-02-23 17:37:16 +0000160// calculateDepsRecursive calculates the dependencies of the given boot image config and all its
161// ancestors, if they are not visited.
162// The boot images are supposed to form a tree, where the root is the primary boot image. We do not
163// expect loops (e.g., A extends B, B extends C, C extends A), and we let them crash soong with a
164// stack overflow.
165// Note that a boot image config only has a pointer to the parent, not to children. Therefore, we
166// first go up through the parent chain, and then go back down to visit every code along the path.
167// `visited` is a map where a key is a boot image name and the value indicates whether the boot
168// image config is visited. The boot image names are guaranteed to be unique because they come from
169// `genBootImageConfigRaw` above, which also returns a map and would fail in the first place if the
170// names were not unique.
171func calculateDepsRecursive(c *bootImageConfig, targets []android.Target, visited map[string]bool) {
172 if c.extends == nil || visited[c.name] {
173 return
174 }
175 if c.extends.extends != nil {
176 calculateDepsRecursive(c.extends, targets, visited)
177 }
178 visited[c.name] = true
179 c.dexPathsDeps = android.Concat(c.extends.dexPathsDeps, c.dexPathsDeps)
180 for i := range targets {
181 c.variants[i].baseImages = android.Concat(c.extends.variants[i].baseImages, android.OutputPaths{c.extends.variants[i].imagePathOnHost})
182 c.variants[i].baseImagesDeps = android.Concat(c.extends.variants[i].baseImagesDeps, c.extends.variants[i].imagesDeps.Paths())
183 c.variants[i].dexLocationsDeps = android.Concat(c.extends.variants[i].dexLocationsDeps, c.variants[i].dexLocationsDeps)
184 }
185}
186
David Srbeckyc177ebe2020-02-18 20:43:06 +0000187func artBootImageConfig(ctx android.PathContext) *bootImageConfig {
188 return genBootImageConfigs(ctx)[artBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000189}
190
David Srbeckyc177ebe2020-02-18 20:43:06 +0000191func defaultBootImageConfig(ctx android.PathContext) *bootImageConfig {
192 return genBootImageConfigs(ctx)[frameworkBootImageName]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000193}
194
Jiakai Zhangb8796202023-03-06 19:16:48 +0000195func mainlineBootImageConfig(ctx android.PathContext) *bootImageConfig {
196 return genBootImageConfigs(ctx)[mainlineBootImageName]
197}
198
satayevd604b212021-07-21 14:23:52 +0100199// Apex boot config allows to access build/install paths of apex boot jars without going
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000200// through the usual trouble of registering dependencies on those modules and extracting build paths
201// from those dependencies.
satayevd604b212021-07-21 14:23:52 +0100202type apexBootConfig struct {
203 // A list of apex boot jars.
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000204 modules android.ConfiguredJarList
205
satayevd604b212021-07-21 14:23:52 +0100206 // A list of predefined build paths to apex boot jars. They are configured very early,
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000207 // before the modules for these jars are processed and the actual paths are generated, and
208 // later on a singleton adds commands to copy actual jars to the predefined paths.
209 dexPaths android.WritablePaths
210
Paul Duffin5f148ca2021-06-02 17:24:22 +0100211 // Map from module name (without prebuilt_ prefix) to the predefined build path.
212 dexPathsByModule map[string]android.WritablePath
213
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000214 // A list of dex locations (a.k.a. on-device paths) to the boot jars.
215 dexLocations []string
216}
217
satayevd604b212021-07-21 14:23:52 +0100218var updatableBootConfigKey = android.NewOnceKey("apexBootConfig")
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000219
satayevd604b212021-07-21 14:23:52 +0100220// Returns apex boot config.
221func GetApexBootConfig(ctx android.PathContext) apexBootConfig {
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000222 return ctx.Config().Once(updatableBootConfigKey, func() interface{} {
satayevd604b212021-07-21 14:23:52 +0100223 apexBootJars := dexpreopt.GetGlobalConfig(ctx).ApexBootJars
Jeongik Cha4753b392023-04-19 23:25:41 +0900224 archType := ctx.Config().Targets[android.Android][0].Arch.ArchType
225 dir := android.PathForOutput(ctx, toDexpreoptDirName(archType), "apex_bootjars")
satayevd604b212021-07-21 14:23:52 +0100226 dexPaths := apexBootJars.BuildPaths(ctx, dir)
227 dexPathsByModuleName := apexBootJars.BuildPathsByModule(ctx, dir)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000228
satayevd604b212021-07-21 14:23:52 +0100229 dexLocations := apexBootJars.DevicePaths(ctx.Config(), android.Android)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000230
satayevd604b212021-07-21 14:23:52 +0100231 return apexBootConfig{apexBootJars, dexPaths, dexPathsByModuleName, dexLocations}
232 }).(apexBootConfig)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000233}
234
235// Returns a list of paths and a list of locations for the boot jars used in dexpreopt (to be
236// passed in -Xbootclasspath and -Xbootclasspath-locations arguments for dex2oat).
237func bcpForDexpreopt(ctx android.PathContext, withUpdatable bool) (android.WritablePaths, []string) {
238 // Non-updatable boot jars (they are used both in the boot image and in dexpreopt).
239 bootImage := defaultBootImageConfig(ctx)
240 dexPaths := bootImage.dexPathsDeps
241 // The dex locations for all Android variants are identical.
242 dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps
243
244 if withUpdatable {
satayevd604b212021-07-21 14:23:52 +0100245 // Apex boot jars (they are used only in dexpreopt, but not in the boot image).
246 apexBootConfig := GetApexBootConfig(ctx)
247 dexPaths = append(dexPaths, apexBootConfig.dexPaths...)
248 dexLocations = append(dexLocations, apexBootConfig.dexLocations...)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000249 }
250
251 return dexPaths, dexLocations
252}
253
Colin Cross44df5812019-02-15 23:06:46 -0800254var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath")
255
256var copyOf = android.CopyOf
257
258func init() {
259 android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars)
260}
261
262func dexpreoptConfigMakevars(ctx android.MakeVarsContext) {
Ulya Trafimovich249386a2020-07-01 14:31:13 +0100263 ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules.CopyOfApexJarPairs(), ":"))
Colin Cross44df5812019-02-15 23:06:46 -0800264}
Jeongik Cha4753b392023-04-19 23:25:41 +0900265
266func toDexpreoptDirName(arch android.ArchType) string {
267 return "dexpreopt_" + arch.String()
268}