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