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