| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -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 ( | 
|  | 18 | "path/filepath" | 
| Vladimir Marko | 205e6c2 | 2020-04-01 13:52:27 +0100 | [diff] [blame] | 19 | "sort" | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 20 | "strings" | 
|  | 21 |  | 
|  | 22 | "android/soong/android" | 
|  | 23 | "android/soong/dexpreopt" | 
|  | 24 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" | 
|  | 26 | ) | 
|  | 27 |  | 
|  | 28 | func init() { | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 29 | RegisterDexpreoptBootJarsComponents(android.InitRegistrationContext) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 30 | } | 
|  | 31 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 32 | // Target-independent description of pre-compiled boot image. | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 33 | type bootImageConfig struct { | 
| David Srbecky | 1aacc6c | 2020-03-26 11:10:45 +0000 | [diff] [blame] | 34 | // If this image is an extension, the image that it extends. | 
|  | 35 | extends *bootImageConfig | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 36 |  | 
|  | 37 | // Image name (used in directory names and ninja rule names). | 
|  | 38 | name string | 
|  | 39 |  | 
|  | 40 | // Basename of the image: the resulting filenames are <stem>[-<jar>].{art,oat,vdex}. | 
|  | 41 | stem string | 
|  | 42 |  | 
|  | 43 | // Output directory for the image files. | 
|  | 44 | dir android.OutputPath | 
|  | 45 |  | 
|  | 46 | // Output directory for the image files with debug symbols. | 
|  | 47 | symbolsDir android.OutputPath | 
|  | 48 |  | 
|  | 49 | // Subdirectory where the image files are installed. | 
|  | 50 | installSubdir string | 
|  | 51 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 52 | // The names of jars that constitute this image. | 
|  | 53 | modules []string | 
|  | 54 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 55 | // File paths to jars. | 
|  | 56 | dexPaths     android.WritablePaths // for this image | 
|  | 57 | dexPathsDeps android.WritablePaths // for the dependency images and in this image | 
|  | 58 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 59 | // File path to a zip archive with all image files (or nil, if not needed). | 
|  | 60 | zip android.WritablePath | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 61 |  | 
|  | 62 | // Rules which should be used in make to install the outputs. | 
|  | 63 | profileInstalls android.RuleBuilderInstalls | 
|  | 64 |  | 
|  | 65 | // Target-dependent fields. | 
|  | 66 | variants []*bootImageVariant | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | // Target-dependent description of pre-compiled boot image. | 
|  | 70 | type bootImageVariant struct { | 
|  | 71 | *bootImageConfig | 
|  | 72 |  | 
|  | 73 | // Target for which the image is generated. | 
|  | 74 | target android.Target | 
|  | 75 |  | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 76 | // The "locations" of jars. | 
|  | 77 | dexLocations     []string // for this image | 
|  | 78 | dexLocationsDeps []string // for the dependency images and in this image | 
|  | 79 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 80 | // Paths to image files. | 
|  | 81 | images     android.OutputPath  // first image file | 
|  | 82 | imagesDeps android.OutputPaths // all files | 
|  | 83 |  | 
|  | 84 | // Only for extensions, paths to the primary boot images. | 
|  | 85 | primaryImages android.OutputPath | 
|  | 86 |  | 
|  | 87 | // Rules which should be used in make to install the outputs. | 
|  | 88 | installs           android.RuleBuilderInstalls | 
|  | 89 | vdexInstalls       android.RuleBuilderInstalls | 
|  | 90 | unstrippedInstalls android.RuleBuilderInstalls | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | func (image bootImageConfig) getVariant(target android.Target) *bootImageVariant { | 
|  | 94 | for _, variant := range image.variants { | 
|  | 95 | if variant.target.Os == target.Os && variant.target.Arch.ArchType == target.Arch.ArchType { | 
|  | 96 | return variant | 
|  | 97 | } | 
|  | 98 | } | 
|  | 99 | return nil | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 102 | // Return any (the first) variant which is for the device (as opposed to for the host) | 
|  | 103 | func (image bootImageConfig) getAnyAndroidVariant() *bootImageVariant { | 
|  | 104 | for _, variant := range image.variants { | 
|  | 105 | if variant.target.Os == android.Android { | 
|  | 106 | return variant | 
|  | 107 | } | 
|  | 108 | } | 
|  | 109 | return nil | 
|  | 110 | } | 
|  | 111 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 112 | func (image bootImageConfig) moduleName(idx int) string { | 
|  | 113 | // Dexpreopt on the boot class path produces multiple files. The first dex file | 
|  | 114 | // is converted into 'name'.art (to match the legacy assumption that 'name'.art | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 115 | // exists), and the rest are converted to 'name'-<jar>.art. | 
| Ulya Trafimovich | 50c4a4b | 2020-04-21 15:36:33 +0100 | [diff] [blame] | 116 | _, m := android.SplitApexJarPair(image.modules[idx]) | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 117 | name := image.stem | 
| David Srbecky | 1aacc6c | 2020-03-26 11:10:45 +0000 | [diff] [blame] | 118 | if idx != 0 || image.extends != nil { | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 119 | name += "-" + stemOf(m) | 
|  | 120 | } | 
|  | 121 | return name | 
|  | 122 | } | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 123 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 124 | func (image bootImageConfig) firstModuleNameOrStem() string { | 
|  | 125 | if len(image.modules) > 0 { | 
|  | 126 | return image.moduleName(0) | 
|  | 127 | } else { | 
|  | 128 | return image.stem | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths { | 
|  | 133 | ret := make(android.OutputPaths, 0, len(image.modules)*len(exts)) | 
|  | 134 | for i := range image.modules { | 
|  | 135 | name := image.moduleName(i) | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 136 | for _, ext := range exts { | 
|  | 137 | ret = append(ret, dir.Join(ctx, name+ext)) | 
|  | 138 | } | 
|  | 139 | } | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 140 | return ret | 
|  | 141 | } | 
|  | 142 |  | 
| David Srbecky | 1aacc6c | 2020-03-26 11:10:45 +0000 | [diff] [blame] | 143 | // The image "location" is a symbolic path that, with multiarchitecture support, doesn't really | 
|  | 144 | // exist on the device. Typically it is /apex/com.android.art/javalib/boot.art and should be the | 
|  | 145 | // same for all supported architectures on the device. The concrete architecture specific files | 
|  | 146 | // actually end up in architecture-specific sub-directory such as arm, arm64, x86, or x86_64. | 
|  | 147 | // | 
|  | 148 | // For example a physical file | 
|  | 149 | // "/apex/com.android.art/javalib/x86/boot.art" has "image location" | 
|  | 150 | // "/apex/com.android.art/javalib/boot.art" (which is not an actual file). | 
|  | 151 | // | 
|  | 152 | // The location is passed as an argument to the ART tools like dex2oat instead of the real path. | 
|  | 153 | // ART tools will then reconstruct the architecture-specific real path. | 
|  | 154 | func (image *bootImageVariant) imageLocations() (imageLocations []string) { | 
|  | 155 | if image.extends != nil { | 
|  | 156 | imageLocations = image.extends.getVariant(image.target).imageLocations() | 
|  | 157 | } | 
|  | 158 | return append(imageLocations, dexpreopt.PathToLocation(image.images, image.target.Arch.ArchType)) | 
|  | 159 | } | 
|  | 160 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 161 | func concat(lists ...[]string) []string { | 
|  | 162 | var size int | 
|  | 163 | for _, l := range lists { | 
|  | 164 | size += len(l) | 
|  | 165 | } | 
|  | 166 | ret := make([]string, 0, size) | 
|  | 167 | for _, l := range lists { | 
|  | 168 | ret = append(ret, l...) | 
|  | 169 | } | 
|  | 170 | return ret | 
|  | 171 | } | 
|  | 172 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 173 | func dexpreoptBootJarsFactory() android.Singleton { | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 174 | return &dexpreoptBootJars{} | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 177 | func RegisterDexpreoptBootJarsComponents(ctx android.RegistrationContext) { | 
|  | 178 | ctx.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory) | 
|  | 179 | } | 
|  | 180 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 181 | func skipDexpreoptBootJars(ctx android.PathContext) bool { | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 182 | if dexpreopt.GetGlobalConfig(ctx).DisablePreopt { | 
| Ulya Trafimovich | acb33e0 | 2019-11-01 17:57:29 +0000 | [diff] [blame] | 183 | return true | 
|  | 184 | } | 
|  | 185 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 186 | if ctx.Config().UnbundledBuild() { | 
|  | 187 | return true | 
|  | 188 | } | 
|  | 189 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 190 | return false | 
|  | 191 | } | 
|  | 192 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 193 | type dexpreoptBootJars struct { | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 194 | defaultBootImage *bootImageConfig | 
|  | 195 | otherImages      []*bootImageConfig | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 196 |  | 
|  | 197 | dexpreoptConfigForMake android.WritablePath | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 198 | } | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 199 |  | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 200 | // Accessor function for the apex package. Returns nil if dexpreopt is disabled. | 
| Tim Joines | c1ef1bb | 2020-03-18 18:00:41 +0000 | [diff] [blame] | 201 | func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths { | 
| Ulya Trafimovich | 4456188 | 2020-01-03 13:25:54 +0000 | [diff] [blame] | 202 | if skipDexpreoptBootJars(ctx) { | 
| Tim Joines | c1ef1bb | 2020-03-18 18:00:41 +0000 | [diff] [blame] | 203 | return nil | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 204 | } | 
| Tim Joines | c1ef1bb | 2020-03-18 18:00:41 +0000 | [diff] [blame] | 205 | // Include dexpreopt files for the primary boot image. | 
|  | 206 | files := map[android.ArchType]android.OutputPaths{} | 
|  | 207 | for _, variant := range artBootImageConfig(ctx).variants { | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 208 | // We also generate boot images for host (for testing), but we don't need those in the apex. | 
| Tim Joines | c1ef1bb | 2020-03-18 18:00:41 +0000 | [diff] [blame] | 209 | if variant.target.Os == android.Android { | 
|  | 210 | files[variant.target.Arch.ArchType] = variant.imagesDeps | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 211 | } | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 212 | } | 
| Tim Joines | c1ef1bb | 2020-03-18 18:00:41 +0000 | [diff] [blame] | 213 | return files | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 214 | } | 
|  | 215 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 216 | // dexpreoptBoot singleton rules | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 217 | func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) { | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 218 | if skipDexpreoptBootJars(ctx) { | 
|  | 219 | return | 
|  | 220 | } | 
| Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 221 | if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil { | 
|  | 222 | // No module has enabled dexpreopting, so we assume there will be no boot image to make. | 
|  | 223 | return | 
|  | 224 | } | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 225 |  | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 226 | d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config") | 
|  | 227 | writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake) | 
|  | 228 |  | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 229 | global := dexpreopt.GetGlobalConfig(ctx) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 230 |  | 
|  | 231 | // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths | 
|  | 232 | // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds. | 
|  | 233 | // Note: this is technically incorrect. Compiled code contains stack checks which may depend | 
|  | 234 | //       on ASAN settings. | 
|  | 235 | if len(ctx.Config().SanitizeDevice()) == 1 && | 
|  | 236 | ctx.Config().SanitizeDevice()[0] == "address" && | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 237 | global.SanitizeLite { | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 238 | return | 
|  | 239 | } | 
|  | 240 |  | 
| Lingfeng Yang | 54191fa | 2019-12-19 16:40:09 +0000 | [diff] [blame] | 241 | // Always create the default boot image first, to get a unique profile rule for all images. | 
|  | 242 | d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx)) | 
| Ulya Trafimovich | 4456188 | 2020-01-03 13:25:54 +0000 | [diff] [blame] | 243 | // Create boot image for the ART apex (build artifacts are accessed via the global boot image config). | 
|  | 244 | d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx))) | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 245 |  | 
|  | 246 | dumpOatRules(ctx, d.defaultBootImage) | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 249 | // Inspect this module to see if it contains a bootclasspath dex jar. | 
|  | 250 | // Note that the same jar may occur in multiple modules. | 
|  | 251 | // This logic is tested in the apex package to avoid import cycle apex <-> java. | 
|  | 252 | func getBootImageJar(ctx android.SingletonContext, image *bootImageConfig, module android.Module) (int, android.Path) { | 
|  | 253 | // All apex Java libraries have non-installable platform variants, skip them. | 
|  | 254 | if module.IsSkipInstall() { | 
|  | 255 | return -1, nil | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | jar, hasJar := module.(interface{ DexJar() android.Path }) | 
|  | 259 | if !hasJar { | 
|  | 260 | return -1, nil | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | name := ctx.ModuleName(module) | 
| Ulya Trafimovich | 50c4a4b | 2020-04-21 15:36:33 +0100 | [diff] [blame] | 264 | index := android.IndexList(name, android.GetJarsFromApexJarPairs(image.modules)) | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 265 | if index == -1 { | 
|  | 266 | return -1, nil | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | // Check that this module satisfies constraints for a particular boot image. | 
|  | 270 | apex, isApexModule := module.(android.ApexModule) | 
| Ulya Trafimovich | 7c140d8 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 271 | fromUpdatableApex := isApexModule && apex.Updatable() | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 272 | if image.name == artBootImageName { | 
|  | 273 | if isApexModule && strings.HasPrefix(apex.ApexName(), "com.android.art.") { | 
| Ulya Trafimovich | 7c140d8 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 274 | // ok: found the jar in the ART apex | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 275 | } else if isApexModule && apex.IsForPlatform() && Bool(module.(*Library).deviceProperties.Hostdex) { | 
| Ulya Trafimovich | 7c140d8 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 276 | // exception (skip and continue): special "hostdex" platform variant | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 277 | return -1, nil | 
| Ulya Trafimovich | e0ce4ba | 2020-04-08 15:00:49 +0100 | [diff] [blame] | 278 | } else if name == "jacocoagent" && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") { | 
| Ulya Trafimovich | 7c140d8 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 279 | // exception (skip and continue): Jacoco platform variant for a coverage build | 
| Ulya Trafimovich | e0ce4ba | 2020-04-08 15:00:49 +0100 | [diff] [blame] | 280 | return -1, nil | 
| Ulya Trafimovich | 7c140d8 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 281 | } else if fromUpdatableApex { | 
|  | 282 | // error: this jar is part of an updatable apex other than ART | 
|  | 283 | ctx.Errorf("module '%s' from updatable apex '%s' is not allowed in the ART boot image", name, apex.ApexName()) | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 284 | } else { | 
| Ulya Trafimovich | 7c140d8 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 285 | // error: this jar is part of the platform or a non-updatable apex | 
|  | 286 | ctx.Errorf("module '%s' is not allowed in the ART boot image", name) | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 287 | } | 
|  | 288 | } else if image.name == frameworkBootImageName { | 
| Ulya Trafimovich | 7c140d8 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 289 | if !fromUpdatableApex { | 
|  | 290 | // ok: this jar is part of the platform or a non-updatable apex | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 291 | } else { | 
| Ulya Trafimovich | 7c140d8 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 292 | // error: this jar is part of an updatable apex | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 293 | ctx.Errorf("module '%s' from updatable apex '%s' is not allowed in the framework boot image", name, apex.ApexName()) | 
|  | 294 | } | 
|  | 295 | } else { | 
|  | 296 | panic("unknown boot image: " + image.name) | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | return index, jar.DexJar() | 
|  | 300 | } | 
|  | 301 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 302 | // buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image. | 
|  | 303 | func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig { | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 304 | // Collect dex jar paths for the boot image modules. | 
|  | 305 | // This logic is tested in the apex package to avoid import cycle apex <-> java. | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 306 | bootDexJars := make(android.Paths, len(image.modules)) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 307 | ctx.VisitAllModules(func(module android.Module) { | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 308 | if i, j := getBootImageJar(ctx, image, module); i != -1 { | 
|  | 309 | bootDexJars[i] = j | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 310 | } | 
|  | 311 | }) | 
|  | 312 |  | 
|  | 313 | var missingDeps []string | 
|  | 314 | // Ensure all modules were converted to paths | 
|  | 315 | for i := range bootDexJars { | 
|  | 316 | if bootDexJars[i] == nil { | 
| Ulya Trafimovich | 50c4a4b | 2020-04-21 15:36:33 +0100 | [diff] [blame] | 317 | _, m := android.SplitApexJarPair(image.modules[i]) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 318 | if ctx.Config().AllowMissingDependencies() { | 
| Ulya Trafimovich | 50c4a4b | 2020-04-21 15:36:33 +0100 | [diff] [blame] | 319 | missingDeps = append(missingDeps, m) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 320 | bootDexJars[i] = android.PathForOutput(ctx, "missing") | 
|  | 321 | } else { | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 322 | ctx.Errorf("failed to find a dex jar path for module '%s'"+ | 
| Ulya Trafimovich | 50c4a4b | 2020-04-21 15:36:33 +0100 | [diff] [blame] | 323 | ", note that some jars may be filtered out by module constraints", m) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 324 | } | 
|  | 325 | } | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before | 
|  | 329 | // the bootclasspath modules have been compiled.  Copy the dex jars there so the module rules that have | 
|  | 330 | // already been set up can find them. | 
|  | 331 | for i := range bootDexJars { | 
|  | 332 | ctx.Build(pctx, android.BuildParams{ | 
|  | 333 | Rule:   android.Cp, | 
|  | 334 | Input:  bootDexJars[i], | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 335 | Output: image.dexPaths[i], | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 336 | }) | 
|  | 337 | } | 
|  | 338 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 339 | profile := bootImageProfileRule(ctx, image, missingDeps) | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 340 | bootFrameworkProfileRule(ctx, image, missingDeps) | 
| Vladimir Marko | 205e6c2 | 2020-04-01 13:52:27 +0100 | [diff] [blame] | 341 | updatableBcpPackagesRule(ctx, image, missingDeps) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 342 |  | 
| Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 343 | var allFiles android.Paths | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 344 | for _, variant := range image.variants { | 
|  | 345 | files := buildBootImageVariant(ctx, variant, profile, missingDeps) | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 346 | allFiles = append(allFiles, files.Paths()...) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 347 | } | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 348 |  | 
| Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 349 | if image.zip != nil { | 
|  | 350 | rule := android.NewRuleBuilder() | 
|  | 351 | rule.Command(). | 
| Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 352 | BuiltTool(ctx, "soong_zip"). | 
| Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 353 | FlagWithOutput("-o ", image.zip). | 
|  | 354 | FlagWithArg("-C ", image.dir.String()). | 
|  | 355 | FlagWithInputList("-f ", allFiles, " -f ") | 
|  | 356 |  | 
|  | 357 | rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image") | 
|  | 358 | } | 
|  | 359 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 360 | return image | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 361 | } | 
|  | 362 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 363 | func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant, | 
|  | 364 | profile android.Path, missingDeps []string) android.WritablePaths { | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 365 |  | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 366 | globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx) | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 367 | global := dexpreopt.GetGlobalConfig(ctx) | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 368 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 369 | arch := image.target.Arch.ArchType | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 370 | os := image.target.Os.String() // We need to distinguish host-x86 and device-x86. | 
|  | 371 | symbolsDir := image.symbolsDir.Join(ctx, os, image.installSubdir, arch.String()) | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 372 | symbolsFile := symbolsDir.Join(ctx, image.stem+".oat") | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 373 | outputDir := image.dir.Join(ctx, os, image.installSubdir, arch.String()) | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 374 | outputPath := outputDir.Join(ctx, image.stem+".oat") | 
|  | 375 | oatLocation := dexpreopt.PathToLocation(outputPath, arch) | 
|  | 376 | imagePath := outputPath.ReplaceExtension(ctx, "art") | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 377 |  | 
|  | 378 | rule := android.NewRuleBuilder() | 
|  | 379 | rule.MissingDeps(missingDeps) | 
|  | 380 |  | 
|  | 381 | rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String()) | 
|  | 382 | rule.Command().Text("rm").Flag("-f"). | 
|  | 383 | Flag(symbolsDir.Join(ctx, "*.art").String()). | 
|  | 384 | Flag(symbolsDir.Join(ctx, "*.oat").String()). | 
|  | 385 | Flag(symbolsDir.Join(ctx, "*.invocation").String()) | 
|  | 386 | rule.Command().Text("rm").Flag("-f"). | 
|  | 387 | Flag(outputDir.Join(ctx, "*.art").String()). | 
|  | 388 | Flag(outputDir.Join(ctx, "*.oat").String()). | 
|  | 389 | Flag(outputDir.Join(ctx, "*.invocation").String()) | 
|  | 390 |  | 
|  | 391 | cmd := rule.Command() | 
|  | 392 |  | 
|  | 393 | extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS") | 
|  | 394 | if extraFlags == "" { | 
|  | 395 | // Use ANDROID_LOG_TAGS to suppress most logging by default... | 
|  | 396 | cmd.Text(`ANDROID_LOG_TAGS="*:e"`) | 
|  | 397 | } else { | 
|  | 398 | // ...unless the boot image is generated specifically for testing, then allow all logging. | 
|  | 399 | cmd.Text(`ANDROID_LOG_TAGS="*:v"`) | 
|  | 400 | } | 
|  | 401 |  | 
|  | 402 | invocationPath := outputPath.ReplaceExtension(ctx, "invocation") | 
|  | 403 |  | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 404 | cmd.Tool(globalSoong.Dex2oat). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 405 | Flag("--avoid-storing-invocation"). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 406 | FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath). | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 407 | Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms). | 
|  | 408 | Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 409 |  | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 410 | if profile != nil { | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 411 | cmd.FlagWithArg("--compiler-filter=", "speed-profile") | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 412 | cmd.FlagWithInput("--profile-file=", profile) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 413 | } | 
|  | 414 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 415 | if global.DirtyImageObjects.Valid() { | 
|  | 416 | cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path()) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 417 | } | 
|  | 418 |  | 
| David Srbecky | 1aacc6c | 2020-03-26 11:10:45 +0000 | [diff] [blame] | 419 | if image.extends != nil { | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 420 | artImage := image.primaryImages | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 421 | cmd. | 
|  | 422 | Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":"). | 
|  | 423 | Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":"). | 
|  | 424 | FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage) | 
|  | 425 | } else { | 
|  | 426 | cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress()) | 
|  | 427 | } | 
|  | 428 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 429 | cmd. | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 430 | FlagForEachInput("--dex-file=", image.dexPaths.Paths()). | 
|  | 431 | FlagForEachArg("--dex-location=", image.dexLocations). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 432 | Flag("--generate-debug-info"). | 
|  | 433 | Flag("--generate-build-id"). | 
| Mathieu Chartier | 54fd807 | 2019-07-26 13:50:04 -0700 | [diff] [blame] | 434 | Flag("--image-format=lz4hc"). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 435 | FlagWithArg("--oat-symbols=", symbolsFile.String()). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 436 | Flag("--strip"). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 437 | FlagWithArg("--oat-file=", outputPath.String()). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 438 | FlagWithArg("--oat-location=", oatLocation). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 439 | FlagWithArg("--image=", imagePath.String()). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 440 | FlagWithArg("--instruction-set=", arch.String()). | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 441 | FlagWithArg("--android-root=", global.EmptyDirectory). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 442 | FlagWithArg("--no-inline-from=", "core-oj.jar"). | 
| Ulya Trafimovich | c0c98d5 | 2020-03-09 12:46:06 +0000 | [diff] [blame] | 443 | Flag("--force-determinism"). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 444 | Flag("--abort-on-hard-verifier-error") | 
|  | 445 |  | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 446 | // Use the default variant/features for host builds. | 
|  | 447 | // The map below contains only device CPU info (which might be x86 on some devices). | 
|  | 448 | if image.target.Os == android.Android { | 
|  | 449 | cmd.FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]) | 
|  | 450 | cmd.FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]) | 
|  | 451 | } | 
|  | 452 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 453 | if global.BootFlags != "" { | 
|  | 454 | cmd.Flag(global.BootFlags) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 455 | } | 
|  | 456 |  | 
|  | 457 | if extraFlags != "" { | 
|  | 458 | cmd.Flag(extraFlags) | 
|  | 459 | } | 
|  | 460 |  | 
| Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 461 | cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage)) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 462 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 463 | installDir := filepath.Join("/", image.installSubdir, arch.String()) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 464 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 465 | var vdexInstalls android.RuleBuilderInstalls | 
|  | 466 | var unstrippedInstalls android.RuleBuilderInstalls | 
|  | 467 |  | 
| Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 468 | var zipFiles android.WritablePaths | 
|  | 469 |  | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 470 | for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") { | 
|  | 471 | cmd.ImplicitOutput(artOrOat) | 
|  | 472 | zipFiles = append(zipFiles, artOrOat) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 473 |  | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 474 | // Install the .oat and .art files | 
|  | 475 | rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base())) | 
|  | 476 | } | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 477 |  | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 478 | for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") { | 
|  | 479 | cmd.ImplicitOutput(vdex) | 
|  | 480 | zipFiles = append(zipFiles, vdex) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 481 |  | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 482 | // Note that the vdex files are identical between architectures. | 
|  | 483 | // Make rules will create symlinks to share them between architectures. | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 484 | vdexInstalls = append(vdexInstalls, | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 485 | android.RuleBuilderInstall{vdex, filepath.Join(installDir, vdex.Base())}) | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 486 | } | 
|  | 487 |  | 
|  | 488 | for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") { | 
|  | 489 | cmd.ImplicitOutput(unstrippedOat) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 490 |  | 
|  | 491 | // Install the unstripped oat files.  The Make rules will put these in $(TARGET_OUT_UNSTRIPPED) | 
|  | 492 | unstrippedInstalls = append(unstrippedInstalls, | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 493 | android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())}) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 494 | } | 
|  | 495 |  | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 496 | rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+image.target.String(), "dexpreopt "+image.name+" jars "+arch.String()) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 497 |  | 
|  | 498 | // save output and installed files for makevars | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 499 | image.installs = rule.Installs() | 
|  | 500 | image.vdexInstalls = vdexInstalls | 
|  | 501 | image.unstrippedInstalls = unstrippedInstalls | 
| Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 502 |  | 
|  | 503 | return zipFiles | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 504 | } | 
|  | 505 |  | 
|  | 506 | const failureMessage = `ERROR: Dex2oat failed to compile a boot image. | 
|  | 507 | It is likely that the boot classpath is inconsistent. | 
|  | 508 | Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.` | 
|  | 509 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 510 | func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath { | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 511 | globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx) | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 512 | global := dexpreopt.GetGlobalConfig(ctx) | 
| Nicolas Geoffray | 27c7cc6 | 2019-02-24 16:04:52 +0000 | [diff] [blame] | 513 |  | 
| Mathieu Chartier | 6adeee1 | 2019-06-26 10:01:36 -0700 | [diff] [blame] | 514 | if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() { | 
| Nicolas Geoffray | 27c7cc6 | 2019-02-24 16:04:52 +0000 | [diff] [blame] | 515 | return nil | 
|  | 516 | } | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 517 | profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} { | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 518 | defaultProfile := "frameworks/base/config/boot-image-profile.txt" | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 519 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 520 | rule := android.NewRuleBuilder() | 
|  | 521 | rule.MissingDeps(missingDeps) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 522 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 523 | var bootImageProfile android.Path | 
|  | 524 | if len(global.BootImageProfiles) > 1 { | 
|  | 525 | combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt") | 
|  | 526 | rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile) | 
|  | 527 | bootImageProfile = combinedBootImageProfile | 
|  | 528 | } else if len(global.BootImageProfiles) == 1 { | 
|  | 529 | bootImageProfile = global.BootImageProfiles[0] | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 530 | } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() { | 
|  | 531 | bootImageProfile = path.Path() | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 532 | } else { | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 533 | // No profile (not even a default one, which is the case on some branches | 
|  | 534 | // like master-art-host that don't have frameworks/base). | 
|  | 535 | // Return nil and continue without profile. | 
|  | 536 | return nil | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 537 | } | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 538 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 539 | profile := image.dir.Join(ctx, "boot.prof") | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 540 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 541 | rule.Command(). | 
|  | 542 | Text(`ANDROID_LOG_TAGS="*:e"`). | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 543 | Tool(globalSoong.Profman). | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 544 | FlagWithInput("--create-profile-from=", bootImageProfile). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 545 | FlagForEachInput("--apk=", image.dexPathsDeps.Paths()). | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 546 | FlagForEachArg("--dex-location=", image.getAnyAndroidVariant().dexLocationsDeps). | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 547 | FlagWithOutput("--reference-profile-file=", profile) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 548 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 549 | rule.Install(profile, "/system/etc/boot-image.prof") | 
|  | 550 |  | 
|  | 551 | rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars") | 
|  | 552 |  | 
|  | 553 | image.profileInstalls = rule.Installs() | 
|  | 554 |  | 
|  | 555 | return profile | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 556 | }) | 
|  | 557 | if profile == nil { | 
|  | 558 | return nil // wrap nil into a typed pointer with value nil | 
|  | 559 | } | 
|  | 560 | return profile.(android.WritablePath) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 561 | } | 
|  | 562 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 563 | var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule") | 
|  | 564 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 565 | func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath { | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 566 | globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx) | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 567 | global := dexpreopt.GetGlobalConfig(ctx) | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 568 |  | 
|  | 569 | if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() { | 
|  | 570 | return nil | 
|  | 571 | } | 
|  | 572 | return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} { | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 573 | rule := android.NewRuleBuilder() | 
|  | 574 | rule.MissingDeps(missingDeps) | 
|  | 575 |  | 
|  | 576 | // Some branches like master-art-host don't have frameworks/base, so manually | 
|  | 577 | // handle the case that the default is missing.  Those branches won't attempt to build the profile rule, | 
|  | 578 | // and if they do they'll get a missing deps error. | 
|  | 579 | defaultProfile := "frameworks/base/config/boot-profile.txt" | 
|  | 580 | path := android.ExistentPathForSource(ctx, defaultProfile) | 
|  | 581 | var bootFrameworkProfile android.Path | 
|  | 582 | if path.Valid() { | 
|  | 583 | bootFrameworkProfile = path.Path() | 
|  | 584 | } else { | 
|  | 585 | missingDeps = append(missingDeps, defaultProfile) | 
|  | 586 | bootFrameworkProfile = android.PathForOutput(ctx, "missing") | 
|  | 587 | } | 
|  | 588 |  | 
|  | 589 | profile := image.dir.Join(ctx, "boot.bprof") | 
|  | 590 |  | 
|  | 591 | rule.Command(). | 
|  | 592 | Text(`ANDROID_LOG_TAGS="*:e"`). | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 593 | Tool(globalSoong.Profman). | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 594 | Flag("--generate-boot-profile"). | 
|  | 595 | FlagWithInput("--create-profile-from=", bootFrameworkProfile). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 596 | FlagForEachInput("--apk=", image.dexPathsDeps.Paths()). | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 597 | FlagForEachArg("--dex-location=", image.getAnyAndroidVariant().dexLocationsDeps). | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 598 | FlagWithOutput("--reference-profile-file=", profile) | 
|  | 599 |  | 
|  | 600 | rule.Install(profile, "/system/etc/boot-image.bprof") | 
|  | 601 | rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars") | 
|  | 602 | image.profileInstalls = append(image.profileInstalls, rule.Installs()...) | 
|  | 603 |  | 
|  | 604 | return profile | 
|  | 605 | }).(android.WritablePath) | 
|  | 606 | } | 
|  | 607 |  | 
|  | 608 | var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule") | 
|  | 609 |  | 
| Vladimir Marko | 205e6c2 | 2020-04-01 13:52:27 +0100 | [diff] [blame] | 610 | func updatableBcpPackagesRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath { | 
|  | 611 | if ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() { | 
|  | 612 | return nil | 
|  | 613 | } | 
|  | 614 |  | 
|  | 615 | return ctx.Config().Once(updatableBcpPackagesRuleKey, func() interface{} { | 
|  | 616 | global := dexpreopt.GetGlobalConfig(ctx) | 
| Ulya Trafimovich | 50c4a4b | 2020-04-21 15:36:33 +0100 | [diff] [blame] | 617 | updatableModules := android.GetJarsFromApexJarPairs(global.UpdatableBootJars) | 
| Vladimir Marko | 205e6c2 | 2020-04-01 13:52:27 +0100 | [diff] [blame] | 618 |  | 
|  | 619 | // Collect `permitted_packages` for updatable boot jars. | 
|  | 620 | var updatablePackages []string | 
|  | 621 | ctx.VisitAllModules(func(module android.Module) { | 
|  | 622 | if j, ok := module.(*Library); ok { | 
|  | 623 | name := ctx.ModuleName(module) | 
|  | 624 | if i := android.IndexList(name, updatableModules); i != -1 { | 
|  | 625 | pp := j.properties.Permitted_packages | 
|  | 626 | if len(pp) > 0 { | 
|  | 627 | updatablePackages = append(updatablePackages, pp...) | 
|  | 628 | } else { | 
|  | 629 | ctx.Errorf("Missing permitted_packages for %s", name) | 
|  | 630 | } | 
|  | 631 | // Do not match the same library repeatedly. | 
|  | 632 | updatableModules = append(updatableModules[:i], updatableModules[i+1:]...) | 
|  | 633 | } | 
|  | 634 | } | 
|  | 635 | }) | 
|  | 636 |  | 
|  | 637 | // Sort updatable packages to ensure deterministic ordering. | 
|  | 638 | sort.Strings(updatablePackages) | 
|  | 639 |  | 
|  | 640 | updatableBcpPackagesName := "updatable-bcp-packages.txt" | 
|  | 641 | updatableBcpPackages := image.dir.Join(ctx, updatableBcpPackagesName) | 
|  | 642 |  | 
|  | 643 | ctx.Build(pctx, android.BuildParams{ | 
|  | 644 | Rule:   android.WriteFile, | 
|  | 645 | Output: updatableBcpPackages, | 
|  | 646 | Args: map[string]string{ | 
|  | 647 | // WriteFile automatically adds the last end-of-line. | 
|  | 648 | "content": strings.Join(updatablePackages, "\\n"), | 
|  | 649 | }, | 
|  | 650 | }) | 
|  | 651 |  | 
|  | 652 | rule := android.NewRuleBuilder() | 
|  | 653 | rule.MissingDeps(missingDeps) | 
|  | 654 | rule.Install(updatableBcpPackages, "/system/etc/"+updatableBcpPackagesName) | 
|  | 655 | // TODO: Rename `profileInstalls` to `extraInstalls`? | 
|  | 656 | // Maybe even move the field out of the bootImageConfig into some higher level type? | 
|  | 657 | image.profileInstalls = append(image.profileInstalls, rule.Installs()...) | 
|  | 658 |  | 
|  | 659 | return updatableBcpPackages | 
|  | 660 | }).(android.WritablePath) | 
|  | 661 | } | 
|  | 662 |  | 
|  | 663 | var updatableBcpPackagesRuleKey = android.NewOnceKey("updatableBcpPackagesRule") | 
|  | 664 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 665 | func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) { | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 666 | var allPhonies android.Paths | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 667 | for _, image := range image.variants { | 
|  | 668 | arch := image.target.Arch.ArchType | 
| David Srbecky | 4667232 | 2020-03-16 13:27:55 +0000 | [diff] [blame] | 669 | suffix := arch.String() | 
|  | 670 | // Host and target might both use x86 arch. We need to ensure the names are unique. | 
|  | 671 | if image.target.Os.Class == android.Host { | 
|  | 672 | suffix = "host-" + suffix | 
|  | 673 | } | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 674 | // Create a rule to call oatdump. | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 675 | output := android.PathForOutput(ctx, "boot."+suffix+".oatdump.txt") | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 676 | rule := android.NewRuleBuilder() | 
|  | 677 | rule.Command(). | 
|  | 678 | // TODO: for now, use the debug version for better error reporting | 
| Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 679 | BuiltTool(ctx, "oatdumpd"). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 680 | FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":"). | 
|  | 681 | FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":"). | 
| David Srbecky | 1aacc6c | 2020-03-26 11:10:45 +0000 | [diff] [blame] | 682 | FlagWithArg("--image=", strings.Join(image.imageLocations(), ":")).Implicits(image.imagesDeps.Paths()). | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 683 | FlagWithOutput("--output=", output). | 
|  | 684 | FlagWithArg("--instruction-set=", arch.String()) | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 685 | rule.Build(pctx, ctx, "dump-oat-boot-"+suffix, "dump oat boot "+arch.String()) | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 686 |  | 
|  | 687 | // Create a phony rule that depends on the output file and prints the path. | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 688 | phony := android.PathForPhony(ctx, "dump-oat-boot-"+suffix) | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 689 | rule = android.NewRuleBuilder() | 
|  | 690 | rule.Command(). | 
|  | 691 | Implicit(output). | 
|  | 692 | ImplicitOutput(phony). | 
|  | 693 | Text("echo").FlagWithArg("Output in ", output.String()) | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 694 | rule.Build(pctx, ctx, "phony-dump-oat-boot-"+suffix, "dump oat boot "+arch.String()) | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 695 |  | 
| David Srbecky | 1aacc6c | 2020-03-26 11:10:45 +0000 | [diff] [blame] | 696 | allPhonies = append(allPhonies, phony) | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 697 | } | 
|  | 698 |  | 
|  | 699 | phony := android.PathForPhony(ctx, "dump-oat-boot") | 
|  | 700 | ctx.Build(pctx, android.BuildParams{ | 
|  | 701 | Rule:        android.Phony, | 
|  | 702 | Output:      phony, | 
|  | 703 | Inputs:      allPhonies, | 
|  | 704 | Description: "dump-oat-boot", | 
|  | 705 | }) | 
|  | 706 |  | 
|  | 707 | } | 
|  | 708 |  | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 709 | func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) { | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 710 | data := dexpreopt.GetGlobalConfigRawData(ctx) | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 711 |  | 
|  | 712 | ctx.Build(pctx, android.BuildParams{ | 
|  | 713 | Rule:   android.WriteFile, | 
|  | 714 | Output: path, | 
|  | 715 | Args: map[string]string{ | 
|  | 716 | "content": string(data), | 
|  | 717 | }, | 
|  | 718 | }) | 
|  | 719 | } | 
|  | 720 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 721 | // Export paths for default boot image to Make | 
|  | 722 | func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) { | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 723 | if d.dexpreoptConfigForMake != nil { | 
|  | 724 | ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String()) | 
| Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 725 | ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String()) | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 726 | } | 
|  | 727 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 728 | image := d.defaultBootImage | 
|  | 729 | if image != nil { | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 730 | ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String()) | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 731 | ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " ")) | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 732 | ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.getAnyAndroidVariant().dexLocationsDeps, " ")) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 733 |  | 
|  | 734 | var imageNames []string | 
|  | 735 | for _, current := range append(d.otherImages, image) { | 
|  | 736 | imageNames = append(imageNames, current.name) | 
| David Srbecky | 1aacc6c | 2020-03-26 11:10:45 +0000 | [diff] [blame] | 737 | for _, variant := range current.variants { | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 738 | suffix := "" | 
| David Srbecky | 1aacc6c | 2020-03-26 11:10:45 +0000 | [diff] [blame] | 739 | if variant.target.Os.Class == android.Host { | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 740 | suffix = "_host" | 
|  | 741 | } | 
| David Srbecky | 1aacc6c | 2020-03-26 11:10:45 +0000 | [diff] [blame] | 742 | sfx := variant.name + suffix + "_" + variant.target.Arch.ArchType.String() | 
|  | 743 | ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, variant.vdexInstalls.String()) | 
|  | 744 | ctx.Strict("DEXPREOPT_IMAGE_"+sfx, variant.images.String()) | 
|  | 745 | ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(variant.imagesDeps.Strings(), " ")) | 
|  | 746 | ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, variant.installs.String()) | 
|  | 747 | ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, variant.unstrippedInstalls.String()) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 748 | } | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 749 | imageLocations := current.getAnyAndroidVariant().imageLocations() | 
| David Srbecky | 1aacc6c | 2020-03-26 11:10:45 +0000 | [diff] [blame] | 750 | ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(imageLocations, ":")) | 
| Colin Cross | 31bf00d | 2019-12-04 13:16:01 -0800 | [diff] [blame] | 751 | ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String()) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 752 | } | 
|  | 753 | ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " ")) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 754 | } | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 755 | } |