blob: c6aa7fe0d3e1a13c41ba00e2a6ca9bcb44140fb6 [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 {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000190 if skipDexpreoptBootJars(ctx) {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000191 return nil
192 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000193
194 // Include dexpreopt files for the primary boot image.
195 files := artBootImageConfig(ctx).imagesDeps
196
197 // For JIT-zygote config, also include dexpreopt files for the primary JIT-zygote image.
198 if dexpreoptGlobalConfig(ctx).UseApexImage {
199 for arch, paths := range artJZBootImageConfig(ctx).imagesDeps {
200 files[arch] = append(files[arch], paths...)
201 }
202 }
203
204 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000205}
206
Colin Cross800fe132019-02-11 14:21:24 -0800207// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800208func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800209 if skipDexpreoptBootJars(ctx) {
210 return
211 }
212
Colin Cross2d00f0d2019-05-09 21:50:00 -0700213 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
214 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
215
Colin Cross44df5812019-02-15 23:06:46 -0800216 global := dexpreoptGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800217
218 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
219 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
220 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
221 // on ASAN settings.
222 if len(ctx.Config().SanitizeDevice()) == 1 &&
223 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800224 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800225 return
226 }
227
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000228 // Always create the default boot image first, to get a unique profile rule for all images.
229 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000230 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
231 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000232 if global.GenerateApexImage {
233 // Create boot images for the JIT-zygote experiment.
Ulya Trafimovich57547452019-12-09 15:40:17 +0000234 d.otherImages = append(d.otherImages, buildBootImage(ctx, artJZBootImageConfig(ctx)))
235 d.otherImages = append(d.otherImages, buildBootImage(ctx, frameworkJZBootImageConfig(ctx)))
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000236 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800237
238 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800239}
240
241// buildBootImage takes a bootImageConfig, creates rules to build it, and returns a *bootImage.
242func buildBootImage(ctx android.SingletonContext, config bootImageConfig) *bootImage {
Colin Cross44df5812019-02-15 23:06:46 -0800243 image := newBootImage(ctx, config)
244
245 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800246 ctx.VisitAllModules(func(module android.Module) {
247 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800248 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800249 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800250 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800251 bootDexJars[i] = j.DexJar()
252 }
253 }
254 })
255
256 var missingDeps []string
257 // Ensure all modules were converted to paths
258 for i := range bootDexJars {
259 if bootDexJars[i] == nil {
260 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800261 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800262 bootDexJars[i] = android.PathForOutput(ctx, "missing")
263 } else {
264 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800265 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800266 }
267 }
268 }
269
270 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
271 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
272 // already been set up can find them.
273 for i := range bootDexJars {
274 ctx.Build(pctx, android.BuildParams{
275 Rule: android.Cp,
276 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800277 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800278 })
279 }
280
Colin Cross44df5812019-02-15 23:06:46 -0800281 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100282 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800283
Colin Crossdf8eebe2019-04-09 15:29:41 -0700284 var allFiles android.Paths
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000285 for _, target := range image.targets {
286 files := buildBootImageRuleForArch(ctx, image, target.Arch.ArchType, profile, missingDeps)
287 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800288 }
Colin Cross44df5812019-02-15 23:06:46 -0800289
Colin Crossdf8eebe2019-04-09 15:29:41 -0700290 if image.zip != nil {
291 rule := android.NewRuleBuilder()
292 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700293 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700294 FlagWithOutput("-o ", image.zip).
295 FlagWithArg("-C ", image.dir.String()).
296 FlagWithInputList("-f ", allFiles, " -f ")
297
298 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
299 }
300
Colin Cross44df5812019-02-15 23:06:46 -0800301 return image
Colin Cross800fe132019-02-11 14:21:24 -0800302}
303
Colin Cross44df5812019-02-15 23:06:46 -0800304func buildBootImageRuleForArch(ctx android.SingletonContext, image *bootImage,
Colin Crossdf8eebe2019-04-09 15:29:41 -0700305 arch android.ArchType, profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800306
Colin Cross44df5812019-02-15 23:06:46 -0800307 global := dexpreoptGlobalConfig(ctx)
308
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000309 symbolsDir := image.symbolsDir.Join(ctx, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000310 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000311 outputDir := image.dir.Join(ctx, image.installSubdir, arch.String())
312 outputPath := outputDir.Join(ctx, image.stem+".oat")
313 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
314 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800315
316 rule := android.NewRuleBuilder()
317 rule.MissingDeps(missingDeps)
318
319 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
320 rule.Command().Text("rm").Flag("-f").
321 Flag(symbolsDir.Join(ctx, "*.art").String()).
322 Flag(symbolsDir.Join(ctx, "*.oat").String()).
323 Flag(symbolsDir.Join(ctx, "*.invocation").String())
324 rule.Command().Text("rm").Flag("-f").
325 Flag(outputDir.Join(ctx, "*.art").String()).
326 Flag(outputDir.Join(ctx, "*.oat").String()).
327 Flag(outputDir.Join(ctx, "*.invocation").String())
328
329 cmd := rule.Command()
330
331 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
332 if extraFlags == "" {
333 // Use ANDROID_LOG_TAGS to suppress most logging by default...
334 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
335 } else {
336 // ...unless the boot image is generated specifically for testing, then allow all logging.
337 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
338 }
339
340 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
341
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000342 cmd.Tool(global.SoongConfig.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800343 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800344 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800345 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
346 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800347
Colin Cross69f59a32019-02-15 10:39:37 -0800348 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800349 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800350 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800351 }
352
Colin Cross44df5812019-02-15 23:06:46 -0800353 if global.DirtyImageObjects.Valid() {
354 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800355 }
356
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000357 if image.extension {
358 artImage := artBootImageConfig(ctx).images[arch]
359 cmd.
360 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
361 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
362 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
363 } else {
364 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
365 }
366
Colin Cross800fe132019-02-11 14:21:24 -0800367 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800368 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
369 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800370 Flag("--generate-debug-info").
371 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700372 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000373 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800374 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000375 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800376 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000377 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800378 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800379 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
380 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
381 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800382 FlagWithArg("--no-inline-from=", "core-oj.jar").
383 Flag("--abort-on-hard-verifier-error")
384
Colin Cross44df5812019-02-15 23:06:46 -0800385 if global.BootFlags != "" {
386 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800387 }
388
389 if extraFlags != "" {
390 cmd.Flag(extraFlags)
391 }
392
Colin Cross0b9f31f2019-02-28 11:00:01 -0800393 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800394
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000395 installDir := filepath.Join("/", image.installSubdir, arch.String())
396 vdexInstallDir := filepath.Join("/", image.installSubdir)
Colin Cross800fe132019-02-11 14:21:24 -0800397
Colin Cross800fe132019-02-11 14:21:24 -0800398 var vdexInstalls android.RuleBuilderInstalls
399 var unstrippedInstalls android.RuleBuilderInstalls
400
Colin Crossdf8eebe2019-04-09 15:29:41 -0700401 var zipFiles android.WritablePaths
402
Dan Willemsen0f416782019-06-13 21:44:53 +0000403 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
404 cmd.ImplicitOutput(artOrOat)
405 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800406
Dan Willemsen0f416782019-06-13 21:44:53 +0000407 // Install the .oat and .art files
408 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
409 }
Colin Cross800fe132019-02-11 14:21:24 -0800410
Dan Willemsen0f416782019-06-13 21:44:53 +0000411 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
412 cmd.ImplicitOutput(vdex)
413 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800414
415 // The vdex files are identical between architectures, install them to a shared location. The Make rules will
416 // only use the install rules for one architecture, and will create symlinks into the architecture-specific
417 // directories.
418 vdexInstalls = append(vdexInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800419 android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000420 }
421
422 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
423 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800424
425 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
426 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800427 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800428 }
429
Colin Cross44df5812019-02-15 23:06:46 -0800430 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800431
432 // save output and installed files for makevars
Colin Cross44df5812019-02-15 23:06:46 -0800433 image.installs[arch] = rule.Installs()
434 image.vdexInstalls[arch] = vdexInstalls
435 image.unstrippedInstalls[arch] = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700436
437 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800438}
439
440const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
441It is likely that the boot classpath is inconsistent.
442Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
443
Colin Cross44df5812019-02-15 23:06:46 -0800444func bootImageProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000445 global := dexpreoptGlobalConfig(ctx)
446
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700447 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000448 return nil
449 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000450 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000451 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800452
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000453 rule := android.NewRuleBuilder()
454 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800455
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000456 var bootImageProfile android.Path
457 if len(global.BootImageProfiles) > 1 {
458 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
459 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
460 bootImageProfile = combinedBootImageProfile
461 } else if len(global.BootImageProfiles) == 1 {
462 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000463 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
464 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000465 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000466 // No profile (not even a default one, which is the case on some branches
467 // like master-art-host that don't have frameworks/base).
468 // Return nil and continue without profile.
469 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000470 }
Colin Cross800fe132019-02-11 14:21:24 -0800471
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000472 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800473
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000474 rule.Command().
475 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000476 Tool(global.SoongConfig.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000477 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000478 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
479 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000480 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800481
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000482 rule.Install(profile, "/system/etc/boot-image.prof")
483
484 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
485
486 image.profileInstalls = rule.Installs()
487
488 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000489 })
490 if profile == nil {
491 return nil // wrap nil into a typed pointer with value nil
492 }
493 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800494}
495
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000496var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
497
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100498func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
499 global := dexpreoptGlobalConfig(ctx)
500
501 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
502 return nil
503 }
504 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100505 rule := android.NewRuleBuilder()
506 rule.MissingDeps(missingDeps)
507
508 // Some branches like master-art-host don't have frameworks/base, so manually
509 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
510 // and if they do they'll get a missing deps error.
511 defaultProfile := "frameworks/base/config/boot-profile.txt"
512 path := android.ExistentPathForSource(ctx, defaultProfile)
513 var bootFrameworkProfile android.Path
514 if path.Valid() {
515 bootFrameworkProfile = path.Path()
516 } else {
517 missingDeps = append(missingDeps, defaultProfile)
518 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
519 }
520
521 profile := image.dir.Join(ctx, "boot.bprof")
522
523 rule.Command().
524 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000525 Tool(global.SoongConfig.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100526 Flag("--generate-boot-profile").
527 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000528 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
529 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100530 FlagWithOutput("--reference-profile-file=", profile)
531
532 rule.Install(profile, "/system/etc/boot-image.bprof")
533 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
534 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
535
536 return profile
537 }).(android.WritablePath)
538}
539
540var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
541
Colin Crossc9a4c362019-02-26 21:13:48 -0800542func dumpOatRules(ctx android.SingletonContext, image *bootImage) {
543 var archs []android.ArchType
544 for arch := range image.images {
545 archs = append(archs, arch)
546 }
547 sort.Slice(archs, func(i, j int) bool { return archs[i].String() < archs[j].String() })
548
549 var allPhonies android.Paths
550 for _, arch := range archs {
551 // Create a rule to call oatdump.
552 output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt")
553 rule := android.NewRuleBuilder()
554 rule.Command().
555 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700556 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000557 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
558 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
Ulya Trafimovich163664a2019-12-06 13:42:21 +0000559 FlagWithArg("--image=", strings.Join(image.imageLocations, ":")).Implicits(image.imagesDeps[arch].Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800560 FlagWithOutput("--output=", output).
561 FlagWithArg("--instruction-set=", arch.String())
562 rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
563
564 // Create a phony rule that depends on the output file and prints the path.
565 phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String())
566 rule = android.NewRuleBuilder()
567 rule.Command().
568 Implicit(output).
569 ImplicitOutput(phony).
570 Text("echo").FlagWithArg("Output in ", output.String())
571 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
572
573 allPhonies = append(allPhonies, phony)
574 }
575
576 phony := android.PathForPhony(ctx, "dump-oat-boot")
577 ctx.Build(pctx, android.BuildParams{
578 Rule: android.Phony,
579 Output: phony,
580 Inputs: allPhonies,
581 Description: "dump-oat-boot",
582 })
583
584}
585
Colin Cross2d00f0d2019-05-09 21:50:00 -0700586func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
587 data := dexpreoptGlobalConfigRaw(ctx).data
588
589 ctx.Build(pctx, android.BuildParams{
590 Rule: android.WriteFile,
591 Output: path,
592 Args: map[string]string{
593 "content": string(data),
594 },
595 })
596}
597
Colin Cross44df5812019-02-15 23:06:46 -0800598// Export paths for default boot image to Make
599func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700600 if d.dexpreoptConfigForMake != nil {
601 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000602 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700603 }
604
Colin Cross44df5812019-02-15 23:06:46 -0800605 image := d.defaultBootImage
606 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800607 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000608 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
609 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000610
611 var imageNames []string
612 for _, current := range append(d.otherImages, image) {
613 imageNames = append(imageNames, current.name)
Colin Cross91268c62019-04-11 14:07:04 -0700614 var arches []android.ArchType
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000615 for arch, _ := range current.images {
Colin Cross91268c62019-04-11 14:07:04 -0700616 arches = append(arches, arch)
617 }
618
619 sort.Slice(arches, func(i, j int) bool { return arches[i].String() < arches[j].String() })
620
621 for _, arch := range arches {
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000622 sfx := current.name + "_" + arch.String()
623 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, current.vdexInstalls[arch].String())
624 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, current.images[arch].String())
625 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(current.imagesDeps[arch].Strings(), " "))
626 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, current.installs[arch].String())
627 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, current.unstrippedInstalls[arch].String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000628 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800629
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000630 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(current.imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800631 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000632 }
633 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800634 }
Colin Cross800fe132019-02-11 14:21:24 -0800635}