blob: 5e72cee7cc0838455a6ecc34cf09e8300e6a661f [file] [log] [blame]
Colin Cross800fe132019-02-11 14:21:24 -08001// 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
15package java
16
17import (
18 "path/filepath"
Colin Crossc9a4c362019-02-26 21:13:48 -080019 "sort"
Colin Cross800fe132019-02-11 14:21:24 -080020 "strings"
21
22 "android/soong/android"
23 "android/soong/dexpreopt"
24
Colin Cross800fe132019-02-11 14:21:24 -080025 "github.com/google/blueprint/proptools"
26)
27
28func init() {
29 android.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory)
30}
31
32// The image "location" is a symbolic path that with multiarchitecture
33// support doesn't really exist on the device. Typically it is
34// /system/framework/boot.art and should be the same for all supported
35// architectures on the device. The concrete architecture specific
36// content actually ends up in a "filename" that contains an
37// architecture specific directory name such as arm, arm64, mips,
38// mips64, x86, x86_64.
39//
40// Here are some example values for an x86_64 / x86 configuration:
41//
42// bootImages["x86_64"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86_64/boot.art"
43// dexpreopt.PathToLocation(bootImages["x86_64"], "x86_64") = "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
44//
45// bootImages["x86"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86/boot.art"
46// dexpreopt.PathToLocation(bootImages["x86"])= "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
47//
48// The location is passed as an argument to the ART tools like dex2oat instead of the real path. The ART tools
49// will then reconstruct the real path, so the rules must have a dependency on the real path.
50
Colin Cross44df5812019-02-15 23:06:46 -080051type bootImageConfig struct {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000052 // Whether this image is an extension.
53 extension bool
54
55 // Image name (used in directory names and ninja rule names).
56 name string
57
58 // Basename of the image: the resulting filenames are <stem>[-<jar>].{art,oat,vdex}.
59 stem string
60
61 // Output directory for the image files.
62 dir android.OutputPath
63
64 // Output directory for the image files with debug symbols.
65 symbolsDir android.OutputPath
66
67 // Subdirectory where the image files are installed.
68 installSubdir string
69
70 // Targets for which the image is generated.
71 targets []android.Target
72
73 // The names of jars that constitute this image.
74 modules []string
75
76 // The "locations" of jars.
77 dexLocations []string // for this image
78 dexLocationsDeps []string // for the dependency images and in this image
79
80 // File paths to jars.
81 dexPaths android.WritablePaths // for this image
82 dexPathsDeps android.WritablePaths // for the dependency images and in this image
83
84 // The "locations" of the dependency images and in this image.
85 imageLocations []string
86
87 // Paths to image files (grouped by target).
88 images map[android.ArchType]android.OutputPath // first image file
89 imagesDeps map[android.ArchType]android.OutputPaths // all files
90
91 // File path to a zip archive with all image files (or nil, if not needed).
92 zip android.WritablePath
Colin Cross800fe132019-02-11 14:21:24 -080093}
94
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000095func (image bootImageConfig) moduleName(idx int) string {
96 // Dexpreopt on the boot class path produces multiple files. The first dex file
97 // is converted into 'name'.art (to match the legacy assumption that 'name'.art
Dan Willemsen0f416782019-06-13 21:44:53 +000098 // exists), and the rest are converted to 'name'-<jar>.art.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000099 m := image.modules[idx]
100 name := image.stem
101 if idx != 0 || image.extension {
102 name += "-" + stemOf(m)
103 }
104 return name
105}
Dan Willemsen0f416782019-06-13 21:44:53 +0000106
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000107func (image bootImageConfig) firstModuleNameOrStem() string {
108 if len(image.modules) > 0 {
109 return image.moduleName(0)
110 } else {
111 return image.stem
112 }
113}
114
115func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths {
116 ret := make(android.OutputPaths, 0, len(image.modules)*len(exts))
117 for i := range image.modules {
118 name := image.moduleName(i)
Dan Willemsen0f416782019-06-13 21:44:53 +0000119 for _, ext := range exts {
120 ret = append(ret, dir.Join(ctx, name+ext))
121 }
122 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000123 return ret
124}
125
Colin Cross44df5812019-02-15 23:06:46 -0800126type bootImage struct {
127 bootImageConfig
Colin Cross800fe132019-02-11 14:21:24 -0800128
Colin Cross44df5812019-02-15 23:06:46 -0800129 installs map[android.ArchType]android.RuleBuilderInstalls
130 vdexInstalls map[android.ArchType]android.RuleBuilderInstalls
131 unstrippedInstalls map[android.ArchType]android.RuleBuilderInstalls
Colin Cross800fe132019-02-11 14:21:24 -0800132
Colin Cross44df5812019-02-15 23:06:46 -0800133 profileInstalls android.RuleBuilderInstalls
134}
Colin Cross800fe132019-02-11 14:21:24 -0800135
Colin Cross44df5812019-02-15 23:06:46 -0800136func newBootImage(ctx android.PathContext, config bootImageConfig) *bootImage {
137 image := &bootImage{
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000138 bootImageConfig: config,
Colin Cross800fe132019-02-11 14:21:24 -0800139
Colin Cross44df5812019-02-15 23:06:46 -0800140 installs: make(map[android.ArchType]android.RuleBuilderInstalls),
141 vdexInstalls: make(map[android.ArchType]android.RuleBuilderInstalls),
142 unstrippedInstalls: make(map[android.ArchType]android.RuleBuilderInstalls),
143 }
Colin Cross800fe132019-02-11 14:21:24 -0800144
Colin Cross44df5812019-02-15 23:06:46 -0800145 return image
Colin Cross800fe132019-02-11 14:21:24 -0800146}
147
148func concat(lists ...[]string) []string {
149 var size int
150 for _, l := range lists {
151 size += len(l)
152 }
153 ret := make([]string, 0, size)
154 for _, l := range lists {
155 ret = append(ret, l...)
156 }
157 return ret
158}
159
Colin Cross800fe132019-02-11 14:21:24 -0800160func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800161 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800162}
163
164func skipDexpreoptBootJars(ctx android.PathContext) bool {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000165 if dexpreoptGlobalConfig(ctx).DisablePreopt {
166 return true
167 }
168
Colin Cross800fe132019-02-11 14:21:24 -0800169 if ctx.Config().UnbundledBuild() {
170 return true
171 }
172
173 if len(ctx.Config().Targets[android.Android]) == 0 {
174 // Host-only build
175 return true
176 }
177
178 return false
179}
180
Colin Cross44df5812019-02-15 23:06:46 -0800181type dexpreoptBootJars struct {
182 defaultBootImage *bootImage
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000183 otherImages []*bootImage
Colin Cross2d00f0d2019-05-09 21:50:00 -0700184
185 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800186}
Colin Cross800fe132019-02-11 14:21:24 -0800187
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000188// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000189func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
190 if skipDexpreoptBootJars(ctx) {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000191 return nil
192 }
193 return artBootImageConfig(ctx).imagesDeps
194}
195
Colin Cross800fe132019-02-11 14:21:24 -0800196// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800197func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800198 if skipDexpreoptBootJars(ctx) {
199 return
200 }
201
Colin Cross2d00f0d2019-05-09 21:50:00 -0700202 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
203 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
204
Colin Cross44df5812019-02-15 23:06:46 -0800205 global := dexpreoptGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800206
207 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
208 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
209 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
210 // on ASAN settings.
211 if len(ctx.Config().SanitizeDevice()) == 1 &&
212 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800213 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800214 return
215 }
216
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000217 // Always create the default boot image first, to get a unique profile rule for all images.
Colin Cross44df5812019-02-15 23:06:46 -0800218 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000219 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
220 buildBootImage(ctx, artBootImageConfig(ctx))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000221 if global.GenerateApexImage {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000222 // Create boot images for the JIT-zygote experiment.
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000223 d.otherImages = append(d.otherImages, buildBootImage(ctx, apexBootImageConfig(ctx)))
224 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800225
226 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800227}
228
229// buildBootImage takes a bootImageConfig, creates rules to build it, and returns a *bootImage.
230func buildBootImage(ctx android.SingletonContext, config bootImageConfig) *bootImage {
Colin Cross44df5812019-02-15 23:06:46 -0800231 image := newBootImage(ctx, config)
232
233 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800234 ctx.VisitAllModules(func(module android.Module) {
235 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800236 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800237 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800238 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800239 bootDexJars[i] = j.DexJar()
240 }
241 }
242 })
243
244 var missingDeps []string
245 // Ensure all modules were converted to paths
246 for i := range bootDexJars {
247 if bootDexJars[i] == nil {
248 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800249 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800250 bootDexJars[i] = android.PathForOutput(ctx, "missing")
251 } else {
252 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800253 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800254 }
255 }
256 }
257
258 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
259 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
260 // already been set up can find them.
261 for i := range bootDexJars {
262 ctx.Build(pctx, android.BuildParams{
263 Rule: android.Cp,
264 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800265 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800266 })
267 }
268
Colin Cross44df5812019-02-15 23:06:46 -0800269 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100270 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800271
Colin Crossdf8eebe2019-04-09 15:29:41 -0700272 var allFiles android.Paths
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000273 for _, target := range image.targets {
274 files := buildBootImageRuleForArch(ctx, image, target.Arch.ArchType, profile, missingDeps)
275 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800276 }
Colin Cross44df5812019-02-15 23:06:46 -0800277
Colin Crossdf8eebe2019-04-09 15:29:41 -0700278 if image.zip != nil {
279 rule := android.NewRuleBuilder()
280 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700281 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700282 FlagWithOutput("-o ", image.zip).
283 FlagWithArg("-C ", image.dir.String()).
284 FlagWithInputList("-f ", allFiles, " -f ")
285
286 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
287 }
288
Colin Cross44df5812019-02-15 23:06:46 -0800289 return image
Colin Cross800fe132019-02-11 14:21:24 -0800290}
291
Colin Cross44df5812019-02-15 23:06:46 -0800292func buildBootImageRuleForArch(ctx android.SingletonContext, image *bootImage,
Colin Crossdf8eebe2019-04-09 15:29:41 -0700293 arch android.ArchType, profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800294
Colin Cross44df5812019-02-15 23:06:46 -0800295 global := dexpreoptGlobalConfig(ctx)
296
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000297 symbolsDir := image.symbolsDir.Join(ctx, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000298 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000299 outputDir := image.dir.Join(ctx, image.installSubdir, arch.String())
300 outputPath := outputDir.Join(ctx, image.stem+".oat")
301 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
302 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800303
304 rule := android.NewRuleBuilder()
305 rule.MissingDeps(missingDeps)
306
307 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
308 rule.Command().Text("rm").Flag("-f").
309 Flag(symbolsDir.Join(ctx, "*.art").String()).
310 Flag(symbolsDir.Join(ctx, "*.oat").String()).
311 Flag(symbolsDir.Join(ctx, "*.invocation").String())
312 rule.Command().Text("rm").Flag("-f").
313 Flag(outputDir.Join(ctx, "*.art").String()).
314 Flag(outputDir.Join(ctx, "*.oat").String()).
315 Flag(outputDir.Join(ctx, "*.invocation").String())
316
317 cmd := rule.Command()
318
319 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
320 if extraFlags == "" {
321 // Use ANDROID_LOG_TAGS to suppress most logging by default...
322 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
323 } else {
324 // ...unless the boot image is generated specifically for testing, then allow all logging.
325 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
326 }
327
328 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
329
Colin Cross44df5812019-02-15 23:06:46 -0800330 cmd.Tool(global.Tools.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800331 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800332 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800333 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
334 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800335
Colin Cross69f59a32019-02-15 10:39:37 -0800336 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800337 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800338 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800339 }
340
Colin Cross44df5812019-02-15 23:06:46 -0800341 if global.DirtyImageObjects.Valid() {
342 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800343 }
344
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000345 if image.extension {
346 artImage := artBootImageConfig(ctx).images[arch]
347 cmd.
348 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
349 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
350 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
351 } else {
352 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
353 }
354
Colin Cross800fe132019-02-11 14:21:24 -0800355 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800356 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
357 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800358 Flag("--generate-debug-info").
359 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700360 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000361 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800362 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000363 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800364 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000365 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800366 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800367 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
368 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
369 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800370 FlagWithArg("--no-inline-from=", "core-oj.jar").
371 Flag("--abort-on-hard-verifier-error")
372
Colin Cross44df5812019-02-15 23:06:46 -0800373 if global.BootFlags != "" {
374 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800375 }
376
377 if extraFlags != "" {
378 cmd.Flag(extraFlags)
379 }
380
Colin Cross0b9f31f2019-02-28 11:00:01 -0800381 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800382
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000383 installDir := filepath.Join("/", image.installSubdir, arch.String())
384 vdexInstallDir := filepath.Join("/", image.installSubdir)
Colin Cross800fe132019-02-11 14:21:24 -0800385
Colin Cross800fe132019-02-11 14:21:24 -0800386 var vdexInstalls android.RuleBuilderInstalls
387 var unstrippedInstalls android.RuleBuilderInstalls
388
Colin Crossdf8eebe2019-04-09 15:29:41 -0700389 var zipFiles android.WritablePaths
390
Dan Willemsen0f416782019-06-13 21:44:53 +0000391 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
392 cmd.ImplicitOutput(artOrOat)
393 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800394
Dan Willemsen0f416782019-06-13 21:44:53 +0000395 // Install the .oat and .art files
396 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
397 }
Colin Cross800fe132019-02-11 14:21:24 -0800398
Dan Willemsen0f416782019-06-13 21:44:53 +0000399 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
400 cmd.ImplicitOutput(vdex)
401 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800402
403 // The vdex files are identical between architectures, install them to a shared location. The Make rules will
404 // only use the install rules for one architecture, and will create symlinks into the architecture-specific
405 // directories.
406 vdexInstalls = append(vdexInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800407 android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000408 }
409
410 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
411 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800412
413 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
414 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800415 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800416 }
417
Colin Cross44df5812019-02-15 23:06:46 -0800418 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800419
420 // save output and installed files for makevars
Colin Cross44df5812019-02-15 23:06:46 -0800421 image.installs[arch] = rule.Installs()
422 image.vdexInstalls[arch] = vdexInstalls
423 image.unstrippedInstalls[arch] = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700424
425 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800426}
427
428const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
429It is likely that the boot classpath is inconsistent.
430Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
431
Colin Cross44df5812019-02-15 23:06:46 -0800432func bootImageProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000433 global := dexpreoptGlobalConfig(ctx)
434
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700435 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000436 return nil
437 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000438 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000439 tools := global.Tools
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000440 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800441
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000442 rule := android.NewRuleBuilder()
443 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800444
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000445 var bootImageProfile android.Path
446 if len(global.BootImageProfiles) > 1 {
447 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
448 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
449 bootImageProfile = combinedBootImageProfile
450 } else if len(global.BootImageProfiles) == 1 {
451 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000452 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
453 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000454 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000455 // No profile (not even a default one, which is the case on some branches
456 // like master-art-host that don't have frameworks/base).
457 // Return nil and continue without profile.
458 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000459 }
Colin Cross800fe132019-02-11 14:21:24 -0800460
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000461 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800462
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000463 rule.Command().
464 Text(`ANDROID_LOG_TAGS="*:e"`).
465 Tool(tools.Profman).
466 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000467 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
468 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000469 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800470
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000471 rule.Install(profile, "/system/etc/boot-image.prof")
472
473 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
474
475 image.profileInstalls = rule.Installs()
476
477 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000478 })
479 if profile == nil {
480 return nil // wrap nil into a typed pointer with value nil
481 }
482 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800483}
484
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000485var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
486
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100487func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
488 global := dexpreoptGlobalConfig(ctx)
489
490 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
491 return nil
492 }
493 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
494 tools := global.Tools
495
496 rule := android.NewRuleBuilder()
497 rule.MissingDeps(missingDeps)
498
499 // Some branches like master-art-host don't have frameworks/base, so manually
500 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
501 // and if they do they'll get a missing deps error.
502 defaultProfile := "frameworks/base/config/boot-profile.txt"
503 path := android.ExistentPathForSource(ctx, defaultProfile)
504 var bootFrameworkProfile android.Path
505 if path.Valid() {
506 bootFrameworkProfile = path.Path()
507 } else {
508 missingDeps = append(missingDeps, defaultProfile)
509 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
510 }
511
512 profile := image.dir.Join(ctx, "boot.bprof")
513
514 rule.Command().
515 Text(`ANDROID_LOG_TAGS="*:e"`).
516 Tool(tools.Profman).
517 Flag("--generate-boot-profile").
518 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000519 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
520 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100521 FlagWithOutput("--reference-profile-file=", profile)
522
523 rule.Install(profile, "/system/etc/boot-image.bprof")
524 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
525 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
526
527 return profile
528 }).(android.WritablePath)
529}
530
531var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
532
Colin Crossc9a4c362019-02-26 21:13:48 -0800533func dumpOatRules(ctx android.SingletonContext, image *bootImage) {
534 var archs []android.ArchType
535 for arch := range image.images {
536 archs = append(archs, arch)
537 }
538 sort.Slice(archs, func(i, j int) bool { return archs[i].String() < archs[j].String() })
539
540 var allPhonies android.Paths
541 for _, arch := range archs {
542 // Create a rule to call oatdump.
543 output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt")
544 rule := android.NewRuleBuilder()
545 rule.Command().
546 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700547 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000548 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
549 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
Colin Crossc9a4c362019-02-26 21:13:48 -0800550 FlagWithArg("--image=", dexpreopt.PathToLocation(image.images[arch], arch)).Implicit(image.images[arch]).
551 FlagWithOutput("--output=", output).
552 FlagWithArg("--instruction-set=", arch.String())
553 rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
554
555 // Create a phony rule that depends on the output file and prints the path.
556 phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String())
557 rule = android.NewRuleBuilder()
558 rule.Command().
559 Implicit(output).
560 ImplicitOutput(phony).
561 Text("echo").FlagWithArg("Output in ", output.String())
562 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
563
564 allPhonies = append(allPhonies, phony)
565 }
566
567 phony := android.PathForPhony(ctx, "dump-oat-boot")
568 ctx.Build(pctx, android.BuildParams{
569 Rule: android.Phony,
570 Output: phony,
571 Inputs: allPhonies,
572 Description: "dump-oat-boot",
573 })
574
575}
576
Colin Cross2d00f0d2019-05-09 21:50:00 -0700577func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
578 data := dexpreoptGlobalConfigRaw(ctx).data
579
580 ctx.Build(pctx, android.BuildParams{
581 Rule: android.WriteFile,
582 Output: path,
583 Args: map[string]string{
584 "content": string(data),
585 },
586 })
587}
588
Colin Cross44df5812019-02-15 23:06:46 -0800589// Export paths for default boot image to Make
590func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700591 if d.dexpreoptConfigForMake != nil {
592 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
593 }
594
Colin Cross44df5812019-02-15 23:06:46 -0800595 image := d.defaultBootImage
596 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800597 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000598 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
599 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
600 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS", strings.Join(image.imageLocations, ":"))
Colin Crossdf8eebe2019-04-09 15:29:41 -0700601 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+image.name, image.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000602
603 var imageNames []string
604 for _, current := range append(d.otherImages, image) {
605 imageNames = append(imageNames, current.name)
Colin Cross91268c62019-04-11 14:07:04 -0700606 var arches []android.ArchType
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000607 for arch, _ := range current.images {
Colin Cross91268c62019-04-11 14:07:04 -0700608 arches = append(arches, arch)
609 }
610
611 sort.Slice(arches, func(i, j int) bool { return arches[i].String() < arches[j].String() })
612
613 for _, arch := range arches {
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000614 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.vdexInstalls[arch].String())
615 ctx.Strict("DEXPREOPT_IMAGE_"+current.name+"_"+arch.String(), current.images[arch].String())
Dan Willemsen0f416782019-06-13 21:44:53 +0000616 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+current.name+"_"+arch.String(), strings.Join(current.imagesDeps[arch].Strings(), " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000617 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.installs[arch].String())
618 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.unstrippedInstalls[arch].String())
Colin Crossdf8eebe2019-04-09 15:29:41 -0700619 if current.zip != nil {
620 }
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000621 }
622 }
623 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800624 }
Colin Cross800fe132019-02-11 14:21:24 -0800625}