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" |
Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [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 | |
| 25 | "github.com/google/blueprint/pathtools" |
| 26 | "github.com/google/blueprint/proptools" |
| 27 | ) |
| 28 | |
| 29 | func init() { |
| 30 | android.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory) |
| 31 | } |
| 32 | |
| 33 | // The image "location" is a symbolic path that with multiarchitecture |
| 34 | // support doesn't really exist on the device. Typically it is |
| 35 | // /system/framework/boot.art and should be the same for all supported |
| 36 | // architectures on the device. The concrete architecture specific |
| 37 | // content actually ends up in a "filename" that contains an |
| 38 | // architecture specific directory name such as arm, arm64, mips, |
| 39 | // mips64, x86, x86_64. |
| 40 | // |
| 41 | // Here are some example values for an x86_64 / x86 configuration: |
| 42 | // |
| 43 | // bootImages["x86_64"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86_64/boot.art" |
| 44 | // dexpreopt.PathToLocation(bootImages["x86_64"], "x86_64") = "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art" |
| 45 | // |
| 46 | // bootImages["x86"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86/boot.art" |
| 47 | // dexpreopt.PathToLocation(bootImages["x86"])= "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art" |
| 48 | // |
| 49 | // The location is passed as an argument to the ART tools like dex2oat instead of the real path. The ART tools |
| 50 | // will then reconstruct the real path, so the rules must have a dependency on the real path. |
| 51 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 52 | type bootImageConfig struct { |
| 53 | name string |
| 54 | modules []string |
| 55 | dexLocations []string |
| 56 | dexPaths android.WritablePaths |
| 57 | dir android.OutputPath |
| 58 | symbolsDir android.OutputPath |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 59 | targets []android.Target |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 60 | images map[android.ArchType]android.OutputPath |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 61 | imagesDeps map[android.ArchType]android.Paths |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 62 | zip android.WritablePath |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 65 | func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) []android.OutputPath { |
| 66 | ret := make([]android.OutputPath, 0, len(image.modules)*len(exts)) |
| 67 | |
| 68 | // dex preopt on the bootclasspath produces multiple files. The first dex file |
| 69 | // is converted into to 'name'.art (to match the legacy assumption that 'name'.art |
| 70 | // exists), and the rest are converted to 'name'-<jar>.art. |
| 71 | // In addition, each .art file has an associated .oat and .vdex file, and an |
| 72 | // unstripped .oat file |
| 73 | for i, m := range image.modules { |
| 74 | name := image.name |
| 75 | if i != 0 { |
| 76 | name += "-" + m |
| 77 | } |
| 78 | |
| 79 | for _, ext := range exts { |
| 80 | ret = append(ret, dir.Join(ctx, name+ext)) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return ret |
| 85 | } |
| 86 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 87 | type bootImage struct { |
| 88 | bootImageConfig |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 89 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 90 | installs map[android.ArchType]android.RuleBuilderInstalls |
| 91 | vdexInstalls map[android.ArchType]android.RuleBuilderInstalls |
| 92 | unstrippedInstalls map[android.ArchType]android.RuleBuilderInstalls |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 93 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 94 | profileInstalls android.RuleBuilderInstalls |
| 95 | } |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 96 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 97 | func newBootImage(ctx android.PathContext, config bootImageConfig) *bootImage { |
| 98 | image := &bootImage{ |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 99 | bootImageConfig: config, |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 100 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 101 | installs: make(map[android.ArchType]android.RuleBuilderInstalls), |
| 102 | vdexInstalls: make(map[android.ArchType]android.RuleBuilderInstalls), |
| 103 | unstrippedInstalls: make(map[android.ArchType]android.RuleBuilderInstalls), |
| 104 | } |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 105 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 106 | return image |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | func concat(lists ...[]string) []string { |
| 110 | var size int |
| 111 | for _, l := range lists { |
| 112 | size += len(l) |
| 113 | } |
| 114 | ret := make([]string, 0, size) |
| 115 | for _, l := range lists { |
| 116 | ret = append(ret, l...) |
| 117 | } |
| 118 | return ret |
| 119 | } |
| 120 | |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 121 | func dexpreoptBootJarsFactory() android.Singleton { |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 122 | return &dexpreoptBootJars{} |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | func skipDexpreoptBootJars(ctx android.PathContext) bool { |
| 126 | if ctx.Config().UnbundledBuild() { |
| 127 | return true |
| 128 | } |
| 129 | |
| 130 | if len(ctx.Config().Targets[android.Android]) == 0 { |
| 131 | // Host-only build |
| 132 | return true |
| 133 | } |
| 134 | |
| 135 | return false |
| 136 | } |
| 137 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 138 | type dexpreoptBootJars struct { |
| 139 | defaultBootImage *bootImage |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 140 | otherImages []*bootImage |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 141 | |
| 142 | dexpreoptConfigForMake android.WritablePath |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 143 | } |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 144 | |
| 145 | // dexpreoptBoot singleton rules |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 146 | func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) { |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 147 | if skipDexpreoptBootJars(ctx) { |
| 148 | return |
| 149 | } |
| 150 | |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 151 | d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config") |
| 152 | writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake) |
| 153 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 154 | global := dexpreoptGlobalConfig(ctx) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 155 | |
| 156 | // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths |
| 157 | // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds. |
| 158 | // Note: this is technically incorrect. Compiled code contains stack checks which may depend |
| 159 | // on ASAN settings. |
| 160 | if len(ctx.Config().SanitizeDevice()) == 1 && |
| 161 | ctx.Config().SanitizeDevice()[0] == "address" && |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 162 | global.SanitizeLite { |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 163 | return |
| 164 | } |
| 165 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 166 | // Always create the default boot image first, to get a unique profile rule for all images. |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 167 | d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx)) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 168 | if global.GenerateApexImage { |
| 169 | d.otherImages = append(d.otherImages, buildBootImage(ctx, apexBootImageConfig(ctx))) |
| 170 | } |
Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 171 | |
| 172 | dumpOatRules(ctx, d.defaultBootImage) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // buildBootImage takes a bootImageConfig, creates rules to build it, and returns a *bootImage. |
| 176 | func buildBootImage(ctx android.SingletonContext, config bootImageConfig) *bootImage { |
| 177 | global := dexpreoptGlobalConfig(ctx) |
| 178 | |
| 179 | image := newBootImage(ctx, config) |
| 180 | |
| 181 | bootDexJars := make(android.Paths, len(image.modules)) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 182 | |
| 183 | ctx.VisitAllModules(func(module android.Module) { |
| 184 | // Collect dex jar paths for the modules listed above. |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 185 | if j, ok := module.(interface{ DexJar() android.Path }); ok { |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 186 | name := ctx.ModuleName(module) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 187 | if i := android.IndexList(name, image.modules); i != -1 { |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 188 | bootDexJars[i] = j.DexJar() |
| 189 | } |
| 190 | } |
| 191 | }) |
| 192 | |
| 193 | var missingDeps []string |
| 194 | // Ensure all modules were converted to paths |
| 195 | for i := range bootDexJars { |
| 196 | if bootDexJars[i] == nil { |
| 197 | if ctx.Config().AllowMissingDependencies() { |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 198 | missingDeps = append(missingDeps, image.modules[i]) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 199 | bootDexJars[i] = android.PathForOutput(ctx, "missing") |
| 200 | } else { |
| 201 | ctx.Errorf("failed to find dex jar path for module %q", |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 202 | image.modules[i]) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before |
| 208 | // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have |
| 209 | // already been set up can find them. |
| 210 | for i := range bootDexJars { |
| 211 | ctx.Build(pctx, android.BuildParams{ |
| 212 | Rule: android.Cp, |
| 213 | Input: bootDexJars[i], |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 214 | Output: image.dexPaths[i], |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 215 | }) |
| 216 | } |
| 217 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 218 | profile := bootImageProfileRule(ctx, image, missingDeps) |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 219 | bootFrameworkProfileRule(ctx, image, missingDeps) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 220 | |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 221 | var allFiles android.Paths |
| 222 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 223 | if !global.DisablePreopt { |
Colin Cross | c11e0c5 | 2019-05-08 15:18:22 -0700 | [diff] [blame] | 224 | for _, target := range image.targets { |
| 225 | files := buildBootImageRuleForArch(ctx, image, target.Arch.ArchType, profile, missingDeps) |
| 226 | allFiles = append(allFiles, files.Paths()...) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 227 | } |
| 228 | } |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 229 | |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 230 | if image.zip != nil { |
| 231 | rule := android.NewRuleBuilder() |
| 232 | rule.Command(). |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 233 | BuiltTool(ctx, "soong_zip"). |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 234 | FlagWithOutput("-o ", image.zip). |
| 235 | FlagWithArg("-C ", image.dir.String()). |
| 236 | FlagWithInputList("-f ", allFiles, " -f ") |
| 237 | |
| 238 | rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image") |
| 239 | } |
| 240 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 241 | return image |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 244 | func buildBootImageRuleForArch(ctx android.SingletonContext, image *bootImage, |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 245 | arch android.ArchType, profile android.Path, missingDeps []string) android.WritablePaths { |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 246 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 247 | global := dexpreoptGlobalConfig(ctx) |
| 248 | |
| 249 | symbolsDir := image.symbolsDir.Join(ctx, "system/framework", arch.String()) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 250 | symbolsFile := symbolsDir.Join(ctx, image.name+".oat") |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 251 | outputDir := image.dir.Join(ctx, "system/framework", arch.String()) |
| 252 | outputPath := image.images[arch] |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 253 | oatLocation := pathtools.ReplaceExtension(dexpreopt.PathToLocation(outputPath, arch), "oat") |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 254 | |
| 255 | rule := android.NewRuleBuilder() |
| 256 | rule.MissingDeps(missingDeps) |
| 257 | |
| 258 | rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String()) |
| 259 | rule.Command().Text("rm").Flag("-f"). |
| 260 | Flag(symbolsDir.Join(ctx, "*.art").String()). |
| 261 | Flag(symbolsDir.Join(ctx, "*.oat").String()). |
| 262 | Flag(symbolsDir.Join(ctx, "*.invocation").String()) |
| 263 | rule.Command().Text("rm").Flag("-f"). |
| 264 | Flag(outputDir.Join(ctx, "*.art").String()). |
| 265 | Flag(outputDir.Join(ctx, "*.oat").String()). |
| 266 | Flag(outputDir.Join(ctx, "*.invocation").String()) |
| 267 | |
| 268 | cmd := rule.Command() |
| 269 | |
| 270 | extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS") |
| 271 | if extraFlags == "" { |
| 272 | // Use ANDROID_LOG_TAGS to suppress most logging by default... |
| 273 | cmd.Text(`ANDROID_LOG_TAGS="*:e"`) |
| 274 | } else { |
| 275 | // ...unless the boot image is generated specifically for testing, then allow all logging. |
| 276 | cmd.Text(`ANDROID_LOG_TAGS="*:v"`) |
| 277 | } |
| 278 | |
| 279 | invocationPath := outputPath.ReplaceExtension(ctx, "invocation") |
| 280 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 281 | cmd.Tool(global.Tools.Dex2oat). |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 282 | Flag("--avoid-storing-invocation"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 283 | FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath). |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 284 | Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms). |
| 285 | Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 286 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 287 | if profile != nil { |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 288 | cmd.FlagWithArg("--compiler-filter=", "speed-profile") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 289 | cmd.FlagWithInput("--profile-file=", profile) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 292 | if global.DirtyImageObjects.Valid() { |
| 293 | cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path()) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | cmd. |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 297 | FlagForEachInput("--dex-file=", image.dexPaths.Paths()). |
| 298 | FlagForEachArg("--dex-location=", image.dexLocations). |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 299 | Flag("--generate-debug-info"). |
| 300 | Flag("--generate-build-id"). |
Mathieu Chartier | 54fd807 | 2019-07-26 13:50:04 -0700 | [diff] [blame] | 301 | Flag("--image-format=lz4hc"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 302 | FlagWithOutput("--oat-symbols=", symbolsFile). |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 303 | Flag("--strip"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 304 | FlagWithOutput("--oat-file=", outputPath.ReplaceExtension(ctx, "oat")). |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 305 | FlagWithArg("--oat-location=", oatLocation). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 306 | FlagWithOutput("--image=", outputPath). |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 307 | FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress()). |
| 308 | FlagWithArg("--instruction-set=", arch.String()). |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 309 | FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]). |
| 310 | FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]). |
| 311 | FlagWithArg("--android-root=", global.EmptyDirectory). |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 312 | FlagWithArg("--no-inline-from=", "core-oj.jar"). |
| 313 | Flag("--abort-on-hard-verifier-error") |
| 314 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 315 | if global.BootFlags != "" { |
| 316 | cmd.Flag(global.BootFlags) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | if extraFlags != "" { |
| 320 | cmd.Flag(extraFlags) |
| 321 | } |
| 322 | |
Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 323 | cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage)) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 324 | |
| 325 | installDir := filepath.Join("/system/framework", arch.String()) |
| 326 | vdexInstallDir := filepath.Join("/system/framework") |
| 327 | |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 328 | var vdexInstalls android.RuleBuilderInstalls |
| 329 | var unstrippedInstalls android.RuleBuilderInstalls |
| 330 | |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 331 | var zipFiles android.WritablePaths |
| 332 | |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 333 | for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") { |
| 334 | cmd.ImplicitOutput(artOrOat) |
| 335 | zipFiles = append(zipFiles, artOrOat) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 336 | |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 337 | // Install the .oat and .art files |
| 338 | rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base())) |
| 339 | } |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 340 | |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 341 | for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") { |
| 342 | cmd.ImplicitOutput(vdex) |
| 343 | zipFiles = append(zipFiles, vdex) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 344 | |
| 345 | // The vdex files are identical between architectures, install them to a shared location. The Make rules will |
| 346 | // only use the install rules for one architecture, and will create symlinks into the architecture-specific |
| 347 | // directories. |
| 348 | vdexInstalls = append(vdexInstalls, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 349 | android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())}) |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") { |
| 353 | cmd.ImplicitOutput(unstrippedOat) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 354 | |
| 355 | // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED) |
| 356 | unstrippedInstalls = append(unstrippedInstalls, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 357 | android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())}) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 360 | rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String()) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 361 | |
| 362 | // save output and installed files for makevars |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 363 | image.installs[arch] = rule.Installs() |
| 364 | image.vdexInstalls[arch] = vdexInstalls |
| 365 | image.unstrippedInstalls[arch] = unstrippedInstalls |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 366 | |
| 367 | return zipFiles |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | const failureMessage = `ERROR: Dex2oat failed to compile a boot image. |
| 371 | It is likely that the boot classpath is inconsistent. |
| 372 | Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.` |
| 373 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 374 | func bootImageProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath { |
Nicolas Geoffray | 27c7cc6 | 2019-02-24 16:04:52 +0000 | [diff] [blame] | 375 | global := dexpreoptGlobalConfig(ctx) |
| 376 | |
Mathieu Chartier | 6adeee1 | 2019-06-26 10:01:36 -0700 | [diff] [blame] | 377 | if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() { |
Nicolas Geoffray | 27c7cc6 | 2019-02-24 16:04:52 +0000 | [diff] [blame] | 378 | return nil |
| 379 | } |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 380 | return ctx.Config().Once(bootImageProfileRuleKey, func() interface{} { |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 381 | tools := global.Tools |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 382 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 383 | rule := android.NewRuleBuilder() |
| 384 | rule.MissingDeps(missingDeps) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 385 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 386 | var bootImageProfile android.Path |
| 387 | if len(global.BootImageProfiles) > 1 { |
| 388 | combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt") |
| 389 | rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile) |
| 390 | bootImageProfile = combinedBootImageProfile |
| 391 | } else if len(global.BootImageProfiles) == 1 { |
| 392 | bootImageProfile = global.BootImageProfiles[0] |
| 393 | } else { |
| 394 | // If not set, use the default. Some branches like master-art-host don't have frameworks/base, so manually |
| 395 | // handle the case that the default is missing. Those branches won't attempt to build the profile rule, |
| 396 | // and if they do they'll get a missing deps error. |
| 397 | defaultProfile := "frameworks/base/config/boot-image-profile.txt" |
| 398 | path := android.ExistentPathForSource(ctx, defaultProfile) |
| 399 | if path.Valid() { |
| 400 | bootImageProfile = path.Path() |
| 401 | } else { |
| 402 | missingDeps = append(missingDeps, defaultProfile) |
| 403 | bootImageProfile = android.PathForOutput(ctx, "missing") |
| 404 | } |
| 405 | } |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 406 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 407 | profile := image.dir.Join(ctx, "boot.prof") |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 408 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 409 | rule.Command(). |
| 410 | Text(`ANDROID_LOG_TAGS="*:e"`). |
| 411 | Tool(tools.Profman). |
| 412 | FlagWithInput("--create-profile-from=", bootImageProfile). |
| 413 | FlagForEachInput("--apk=", image.dexPaths.Paths()). |
| 414 | FlagForEachArg("--dex-location=", image.dexLocations). |
| 415 | FlagWithOutput("--reference-profile-file=", profile) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 416 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 417 | rule.Install(profile, "/system/etc/boot-image.prof") |
| 418 | |
| 419 | rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars") |
| 420 | |
| 421 | image.profileInstalls = rule.Installs() |
| 422 | |
| 423 | return profile |
| 424 | }).(android.WritablePath) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 427 | var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule") |
| 428 | |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 429 | func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath { |
| 430 | global := dexpreoptGlobalConfig(ctx) |
| 431 | |
| 432 | if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() { |
| 433 | return nil |
| 434 | } |
| 435 | return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} { |
| 436 | tools := global.Tools |
| 437 | |
| 438 | rule := android.NewRuleBuilder() |
| 439 | rule.MissingDeps(missingDeps) |
| 440 | |
| 441 | // Some branches like master-art-host don't have frameworks/base, so manually |
| 442 | // handle the case that the default is missing. Those branches won't attempt to build the profile rule, |
| 443 | // and if they do they'll get a missing deps error. |
| 444 | defaultProfile := "frameworks/base/config/boot-profile.txt" |
| 445 | path := android.ExistentPathForSource(ctx, defaultProfile) |
| 446 | var bootFrameworkProfile android.Path |
| 447 | if path.Valid() { |
| 448 | bootFrameworkProfile = path.Path() |
| 449 | } else { |
| 450 | missingDeps = append(missingDeps, defaultProfile) |
| 451 | bootFrameworkProfile = android.PathForOutput(ctx, "missing") |
| 452 | } |
| 453 | |
| 454 | profile := image.dir.Join(ctx, "boot.bprof") |
| 455 | |
| 456 | rule.Command(). |
| 457 | Text(`ANDROID_LOG_TAGS="*:e"`). |
| 458 | Tool(tools.Profman). |
| 459 | Flag("--generate-boot-profile"). |
| 460 | FlagWithInput("--create-profile-from=", bootFrameworkProfile). |
| 461 | FlagForEachInput("--apk=", image.dexPaths.Paths()). |
| 462 | FlagForEachArg("--dex-location=", image.dexLocations). |
| 463 | FlagWithOutput("--reference-profile-file=", profile) |
| 464 | |
| 465 | rule.Install(profile, "/system/etc/boot-image.bprof") |
| 466 | rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars") |
| 467 | image.profileInstalls = append(image.profileInstalls, rule.Installs()...) |
| 468 | |
| 469 | return profile |
| 470 | }).(android.WritablePath) |
| 471 | } |
| 472 | |
| 473 | var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule") |
| 474 | |
Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 475 | func dumpOatRules(ctx android.SingletonContext, image *bootImage) { |
| 476 | var archs []android.ArchType |
| 477 | for arch := range image.images { |
| 478 | archs = append(archs, arch) |
| 479 | } |
| 480 | sort.Slice(archs, func(i, j int) bool { return archs[i].String() < archs[j].String() }) |
| 481 | |
| 482 | var allPhonies android.Paths |
| 483 | for _, arch := range archs { |
| 484 | // Create a rule to call oatdump. |
| 485 | output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt") |
| 486 | rule := android.NewRuleBuilder() |
| 487 | rule.Command(). |
| 488 | // TODO: for now, use the debug version for better error reporting |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 489 | BuiltTool(ctx, "oatdumpd"). |
Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 490 | FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPaths.Paths(), ":"). |
| 491 | FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocations, ":"). |
| 492 | FlagWithArg("--image=", dexpreopt.PathToLocation(image.images[arch], arch)).Implicit(image.images[arch]). |
| 493 | FlagWithOutput("--output=", output). |
| 494 | FlagWithArg("--instruction-set=", arch.String()) |
| 495 | rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String()) |
| 496 | |
| 497 | // Create a phony rule that depends on the output file and prints the path. |
| 498 | phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String()) |
| 499 | rule = android.NewRuleBuilder() |
| 500 | rule.Command(). |
| 501 | Implicit(output). |
| 502 | ImplicitOutput(phony). |
| 503 | Text("echo").FlagWithArg("Output in ", output.String()) |
| 504 | rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String()) |
| 505 | |
| 506 | allPhonies = append(allPhonies, phony) |
| 507 | } |
| 508 | |
| 509 | phony := android.PathForPhony(ctx, "dump-oat-boot") |
| 510 | ctx.Build(pctx, android.BuildParams{ |
| 511 | Rule: android.Phony, |
| 512 | Output: phony, |
| 513 | Inputs: allPhonies, |
| 514 | Description: "dump-oat-boot", |
| 515 | }) |
| 516 | |
| 517 | } |
| 518 | |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 519 | func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) { |
| 520 | data := dexpreoptGlobalConfigRaw(ctx).data |
| 521 | |
| 522 | ctx.Build(pctx, android.BuildParams{ |
| 523 | Rule: android.WriteFile, |
| 524 | Output: path, |
| 525 | Args: map[string]string{ |
| 526 | "content": string(data), |
| 527 | }, |
| 528 | }) |
| 529 | } |
| 530 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 531 | // Export paths for default boot image to Make |
| 532 | func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) { |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 533 | if d.dexpreoptConfigForMake != nil { |
| 534 | ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String()) |
| 535 | } |
| 536 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 537 | image := d.defaultBootImage |
| 538 | if image != nil { |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 539 | ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String()) |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 540 | ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPaths.Strings(), " ")) |
| 541 | ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocations, " ")) |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 542 | ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+image.name, image.zip.String()) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 543 | |
| 544 | var imageNames []string |
| 545 | for _, current := range append(d.otherImages, image) { |
| 546 | imageNames = append(imageNames, current.name) |
Colin Cross | 91268c6 | 2019-04-11 14:07:04 -0700 | [diff] [blame] | 547 | var arches []android.ArchType |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 548 | for arch, _ := range current.images { |
Colin Cross | 91268c6 | 2019-04-11 14:07:04 -0700 | [diff] [blame] | 549 | arches = append(arches, arch) |
| 550 | } |
| 551 | |
| 552 | sort.Slice(arches, func(i, j int) bool { return arches[i].String() < arches[j].String() }) |
| 553 | |
| 554 | for _, arch := range arches { |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 555 | ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.vdexInstalls[arch].String()) |
| 556 | ctx.Strict("DEXPREOPT_IMAGE_"+current.name+"_"+arch.String(), current.images[arch].String()) |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 557 | ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+current.name+"_"+arch.String(), strings.Join(current.imagesDeps[arch].Strings(), " ")) |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 558 | ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.installs[arch].String()) |
| 559 | ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.unstrippedInstalls[arch].String()) |
Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 560 | if current.zip != nil { |
| 561 | } |
Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " ")) |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 565 | } |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 566 | } |