Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 18 | "path/filepath" |
| 19 | "strings" |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/dexpreopt" |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 25 | // dexpreoptTargets returns the list of targets that are relevant to dexpreopting, which excludes architectures |
| 26 | // supported through native bridge. |
| 27 | func dexpreoptTargets(ctx android.PathContext) []android.Target { |
| 28 | var targets []android.Target |
Colin Cross | 3b19f5d | 2019-09-17 14:45:31 -0700 | [diff] [blame] | 29 | for _, target := range ctx.Config().Targets[android.Android] { |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 30 | if target.NativeBridge == android.NativeBridgeDisabled { |
| 31 | targets = append(targets, target) |
| 32 | } |
| 33 | } |
David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 34 | // We may also need the images on host in order to run host-based tests. |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 35 | for _, target := range ctx.Config().Targets[ctx.Config().BuildOS] { |
David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 36 | targets = append(targets, target) |
| 37 | } |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 38 | |
| 39 | return targets |
| 40 | } |
| 41 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 42 | var ( |
Jiakai Zhang | cb13b5d | 2023-07-13 11:03:38 +0100 | [diff] [blame] | 43 | bootImageConfigKey = android.NewOnceKey("bootImageConfig") |
| 44 | bootImageConfigRawKey = android.NewOnceKey("bootImageConfigRaw") |
| 45 | frameworkBootImageName = "boot" |
| 46 | mainlineBootImageName = "mainline" |
| 47 | bootImageStem = "boot" |
| 48 | profileInstallPathInApex = "etc/boot-image.prof" |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 49 | ) |
Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 50 | |
Jiakai Zhang | cb13b5d | 2023-07-13 11:03:38 +0100 | [diff] [blame] | 51 | // 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. |
| 53 | func getImageNames() []string { |
| 54 | return []string{"art", "boot", "mainline"} |
| 55 | } |
| 56 | |
Jiakai Zhang | 6decef9 | 2022-01-12 17:56:19 +0000 | [diff] [blame] | 57 | func genBootImageConfigRaw(ctx android.PathContext) map[string]*bootImageConfig { |
| 58 | return ctx.Config().Once(bootImageConfigRawKey, func() interface{} { |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 59 | global := dexpreopt.GetGlobalConfig(ctx) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 60 | |
Jiakai Zhang | 556bdf8 | 2023-07-12 16:51:57 +0100 | [diff] [blame] | 61 | artBootImageName := "art" // Keep this local to avoid accidental references. |
Jiakai Zhang | cb13b5d | 2023-07-13 11:03:38 +0100 | [diff] [blame] | 62 | frameworkModules := global.BootJars // This includes `global.ArtApexJars`. |
Jiakai Zhang | b879620 | 2023-03-06 19:16:48 +0000 | [diff] [blame] | 63 | mainlineBcpModules := global.ApexBootJars |
| 64 | frameworkSubdir := "system/framework" |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 65 | |
Jiakai Zhang | cb13b5d | 2023-07-13 11:03:38 +0100 | [diff] [blame] | 66 | 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 Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 69 | artCfg := bootImageConfig{ |
Jiakai Zhang | cb13b5d | 2023-07-13 11:03:38 +0100 | [diff] [blame] | 70 | name: artBootImageName, |
| 71 | enabledIfExists: "art-bootclasspath-fragment", |
| 72 | stem: bootImageStem, |
| 73 | installDir: "apex/art_boot_images/javalib", |
Jiakai Zhang | 556bdf8 | 2023-07-12 16:51:57 +0100 | [diff] [blame] | 74 | modules: global.TestOnlyArtBootImageJars, |
Jiakai Zhang | cb13b5d | 2023-07-13 11:03:38 +0100 | [diff] [blame] | 75 | preloadedClassesFile: "art/build/boot/preloaded-classes", |
| 76 | compilerFilter: "speed-profile", |
| 77 | singleImage: false, |
| 78 | profileImports: profileImports, |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 79 | } |
Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 80 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 81 | // Framework config for the boot image extension. |
Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 82 | // It includes framework libraries and depends on the ART config. |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 83 | frameworkCfg := bootImageConfig{ |
Nicolas Geoffray | b9a46fb | 2022-03-14 15:31:47 +0000 | [diff] [blame] | 84 | name: frameworkBootImageName, |
Jiakai Zhang | cb13b5d | 2023-07-13 11:03:38 +0100 | [diff] [blame] | 85 | enabledIfExists: "platform-bootclasspath", |
Jiakai Zhang | b879620 | 2023-03-06 19:16:48 +0000 | [diff] [blame] | 86 | stem: bootImageStem, |
Jiakai Zhang | 09d88df | 2023-05-10 17:04:53 +0100 | [diff] [blame] | 87 | installDir: frameworkSubdir, |
Nicolas Geoffray | b9a46fb | 2022-03-14 15:31:47 +0000 | [diff] [blame] | 88 | modules: frameworkModules, |
| 89 | preloadedClassesFile: "frameworks/base/config/preloaded-classes", |
Jiakai Zhang | 8e9ea8b | 2023-02-23 17:50:46 +0000 | [diff] [blame] | 90 | compilerFilter: "speed-profile", |
Jiakai Zhang | b879620 | 2023-03-06 19:16:48 +0000 | [diff] [blame] | 91 | singleImage: false, |
Jiakai Zhang | cb13b5d | 2023-07-13 11:03:38 +0100 | [diff] [blame] | 92 | profileImports: profileImports, |
Jiakai Zhang | b879620 | 2023-03-06 19:16:48 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | mainlineCfg := bootImageConfig{ |
Jiakai Zhang | cb13b5d | 2023-07-13 11:03:38 +0100 | [diff] [blame] | 96 | extends: &frameworkCfg, |
| 97 | name: mainlineBootImageName, |
| 98 | enabledIfExists: "platform-bootclasspath", |
| 99 | stem: bootImageStem, |
| 100 | installDir: frameworkSubdir, |
| 101 | modules: mainlineBcpModules, |
| 102 | compilerFilter: "verify", |
| 103 | singleImage: true, |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Jiakai Zhang | 6decef9 | 2022-01-12 17:56:19 +0000 | [diff] [blame] | 106 | return map[string]*bootImageConfig{ |
Ulya Trafimovich | 4cdada2 | 2020-02-10 15:29:28 +0000 | [diff] [blame] | 107 | artBootImageName: &artCfg, |
| 108 | frameworkBootImageName: &frameworkCfg, |
Jiakai Zhang | b879620 | 2023-03-06 19:16:48 +0000 | [diff] [blame] | 109 | mainlineBootImageName: &mainlineCfg, |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 110 | } |
Jiakai Zhang | 6decef9 | 2022-01-12 17:56:19 +0000 | [diff] [blame] | 111 | }).(map[string]*bootImageConfig) |
| 112 | } |
| 113 | |
| 114 | // Construct the global boot image configs. |
| 115 | func genBootImageConfigs(ctx android.PathContext) map[string]*bootImageConfig { |
| 116 | return ctx.Config().Once(bootImageConfigKey, func() interface{} { |
| 117 | targets := dexpreoptTargets(ctx) |
Jiakai Zhang | b1639db | 2023-07-11 15:03:13 +0100 | [diff] [blame] | 118 | deviceDir := android.PathForOutput(ctx, getDexpreoptDirName(ctx)) |
Jiakai Zhang | 6decef9 | 2022-01-12 17:56:19 +0000 | [diff] [blame] | 119 | |
| 120 | configs := genBootImageConfigRaw(ctx) |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 121 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 122 | for _, c := range configs { |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 123 | c.dir = deviceDir.Join(ctx, "dex_"+c.name+"jars") |
| 124 | c.symbolsDir = deviceDir.Join(ctx, "dex_"+c.name+"jars_unstripped") |
| 125 | |
| 126 | // expands to <stem>.art for primary image and <stem>-<1st module>.art for extension |
Ulya Trafimovich | 8640ab9 | 2020-05-11 18:06:15 +0100 | [diff] [blame] | 127 | imageName := c.firstModuleNameOrStem(ctx) + ".art" |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 128 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 129 | // The path to bootclasspath dex files needs to be known at module |
| 130 | // GenerateAndroidBuildAction time, before the bootclasspath modules have been compiled. |
| 131 | // Set up known paths for them, the singleton rules will copy them there. |
| 132 | // TODO(b/143682396): use module dependencies instead |
| 133 | inputDir := deviceDir.Join(ctx, "dex_"+c.name+"jars_input") |
Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 134 | c.dexPaths = c.modules.BuildPaths(ctx, inputDir) |
Paul Duffin | 5f148ca | 2021-06-02 17:24:22 +0100 | [diff] [blame] | 135 | c.dexPathsByModule = c.modules.BuildPathsByModule(ctx, inputDir) |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 136 | c.dexPathsDeps = c.dexPaths |
| 137 | |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 138 | // Create target-specific variants. |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 139 | for _, target := range targets { |
| 140 | arch := target.Arch.ArchType |
Jiakai Zhang | 09d88df | 2023-05-10 17:04:53 +0100 | [diff] [blame] | 141 | imageDir := c.dir.Join(ctx, target.Os.String(), c.installDir, arch.String()) |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 142 | variant := &bootImageVariant{ |
Jeongik Cha | 4dda75e | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 143 | bootImageConfig: c, |
| 144 | target: target, |
| 145 | imagePathOnHost: imageDir.Join(ctx, imageName), |
Jiakai Zhang | 09d88df | 2023-05-10 17:04:53 +0100 | [diff] [blame] | 146 | imagePathOnDevice: filepath.Join("/", c.installDir, arch.String(), imageName), |
Jeongik Cha | 4dda75e | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 147 | imagesDeps: c.moduleFiles(ctx, imageDir, ".art", ".oat", ".vdex"), |
| 148 | dexLocations: c.modules.DevicePaths(ctx.Config(), target.Os), |
David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 149 | } |
| 150 | variant.dexLocationsDeps = variant.dexLocations |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 151 | c.variants = append(c.variants, variant) |
Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 152 | } |
Colin Cross | 31bf00d | 2019-12-04 13:16:01 -0800 | [diff] [blame] | 153 | |
| 154 | c.zip = c.dir.Join(ctx, c.name+".zip") |
Nicolas Geoffray | feef2ef | 2019-04-30 09:43:22 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Jiakai Zhang | 8fe3a41 | 2023-02-23 17:37:16 +0000 | [diff] [blame] | 157 | visited := make(map[string]bool) |
| 158 | for _, c := range configs { |
| 159 | calculateDepsRecursive(c, targets, visited) |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 160 | } |
Ulyana Trafimovich | 5a4ccd1 | 2019-12-18 17:32:33 +0000 | [diff] [blame] | 161 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 162 | return configs |
| 163 | }).(map[string]*bootImageConfig) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Jiakai Zhang | 8fe3a41 | 2023-02-23 17:37:16 +0000 | [diff] [blame] | 166 | // calculateDepsRecursive calculates the dependencies of the given boot image config and all its |
| 167 | // ancestors, if they are not visited. |
| 168 | // The boot images are supposed to form a tree, where the root is the primary boot image. We do not |
| 169 | // expect loops (e.g., A extends B, B extends C, C extends A), and we let them crash soong with a |
| 170 | // stack overflow. |
| 171 | // Note that a boot image config only has a pointer to the parent, not to children. Therefore, we |
| 172 | // first go up through the parent chain, and then go back down to visit every code along the path. |
| 173 | // `visited` is a map where a key is a boot image name and the value indicates whether the boot |
| 174 | // image config is visited. The boot image names are guaranteed to be unique because they come from |
| 175 | // `genBootImageConfigRaw` above, which also returns a map and would fail in the first place if the |
| 176 | // names were not unique. |
| 177 | func calculateDepsRecursive(c *bootImageConfig, targets []android.Target, visited map[string]bool) { |
| 178 | if c.extends == nil || visited[c.name] { |
| 179 | return |
| 180 | } |
| 181 | if c.extends.extends != nil { |
| 182 | calculateDepsRecursive(c.extends, targets, visited) |
| 183 | } |
| 184 | visited[c.name] = true |
| 185 | c.dexPathsDeps = android.Concat(c.extends.dexPathsDeps, c.dexPathsDeps) |
| 186 | for i := range targets { |
| 187 | c.variants[i].baseImages = android.Concat(c.extends.variants[i].baseImages, android.OutputPaths{c.extends.variants[i].imagePathOnHost}) |
| 188 | c.variants[i].baseImagesDeps = android.Concat(c.extends.variants[i].baseImagesDeps, c.extends.variants[i].imagesDeps.Paths()) |
| 189 | c.variants[i].dexLocationsDeps = android.Concat(c.extends.variants[i].dexLocationsDeps, c.variants[i].dexLocationsDeps) |
| 190 | } |
| 191 | } |
| 192 | |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 193 | func defaultBootImageConfig(ctx android.PathContext) *bootImageConfig { |
| 194 | return genBootImageConfigs(ctx)[frameworkBootImageName] |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Jiakai Zhang | b879620 | 2023-03-06 19:16:48 +0000 | [diff] [blame] | 197 | func mainlineBootImageConfig(ctx android.PathContext) *bootImageConfig { |
| 198 | return genBootImageConfigs(ctx)[mainlineBootImageName] |
| 199 | } |
| 200 | |
Jiakai Zhang | cb13b5d | 2023-07-13 11:03:38 +0100 | [diff] [blame] | 201 | // isProfileProviderApex returns true if this apex provides a boot image profile. |
| 202 | func isProfileProviderApex(ctx android.PathContext, apexName string) bool { |
| 203 | for _, config := range genBootImageConfigs(ctx) { |
| 204 | for _, profileImport := range config.profileImports { |
| 205 | if profileImport == apexName { |
| 206 | return true |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | return false |
| 211 | } |
| 212 | |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 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). |
| 215 | func bcpForDexpreopt(ctx android.PathContext, withUpdatable bool) (android.WritablePaths, []string) { |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 216 | bootImage := defaultBootImageConfig(ctx) |
Jiakai Zhang | c6879f3 | 2023-11-06 16:31:19 +0000 | [diff] [blame] | 217 | if withUpdatable { |
| 218 | bootImage = mainlineBootImageConfig(ctx) |
| 219 | } |
| 220 | |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 221 | dexPaths := bootImage.dexPathsDeps |
| 222 | // The dex locations for all Android variants are identical. |
| 223 | dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps |
| 224 | |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 225 | return dexPaths, dexLocations |
| 226 | } |
| 227 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 228 | var defaultBootclasspathKey = android.NewOnceKey("defaultBootclasspath") |
| 229 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 230 | func init() { |
| 231 | android.RegisterMakeVarsProvider(pctx, dexpreoptConfigMakevars) |
| 232 | } |
| 233 | |
| 234 | func dexpreoptConfigMakevars(ctx android.MakeVarsContext) { |
Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 235 | ctx.Strict("DEXPREOPT_BOOT_JARS_MODULES", strings.Join(defaultBootImageConfig(ctx).modules.CopyOfApexJarPairs(), ":")) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 236 | } |
Jeongik Cha | 4753b39 | 2023-04-19 23:25:41 +0900 | [diff] [blame] | 237 | |
Jiakai Zhang | b1639db | 2023-07-11 15:03:13 +0100 | [diff] [blame] | 238 | func getDexpreoptDirName(ctx android.PathContext) string { |
| 239 | prefix := "dexpreopt_" |
| 240 | targets := ctx.Config().Targets[android.Android] |
| 241 | if len(targets) > 0 { |
Cole Faust | 06ea531 | 2023-10-18 17:38:40 -0700 | [diff] [blame] | 242 | return prefix + targets[0].Arch.ArchType.String() |
Jiakai Zhang | b1639db | 2023-07-11 15:03:13 +0100 | [diff] [blame] | 243 | } |
Cole Faust | 06ea531 | 2023-10-18 17:38:40 -0700 | [diff] [blame] | 244 | return prefix + "unknown_target" |
Jeongik Cha | 4753b39 | 2023-04-19 23:25:41 +0900 | [diff] [blame] | 245 | } |