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