blob: d4b7e845a3086ed7c0654b167e2dd2f35fa852a4 [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"
19 "strings"
20
21 "android/soong/android"
22 "android/soong/dexpreopt"
23
Colin Cross800fe132019-02-11 14:21:24 -080024 "github.com/google/blueprint/proptools"
25)
26
27func init() {
28 android.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory)
29}
30
31// The image "location" is a symbolic path that with multiarchitecture
32// support doesn't really exist on the device. Typically it is
33// /system/framework/boot.art and should be the same for all supported
34// architectures on the device. The concrete architecture specific
35// content actually ends up in a "filename" that contains an
Elliott Hughesda3a0712020-03-06 16:55:28 -080036// architecture specific directory name such as arm, arm64, x86, x86_64.
Colin Cross800fe132019-02-11 14:21:24 -080037//
38// Here are some example values for an x86_64 / x86 configuration:
39//
40// bootImages["x86_64"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86_64/boot.art"
41// dexpreopt.PathToLocation(bootImages["x86_64"], "x86_64") = "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
42//
43// bootImages["x86"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86/boot.art"
44// dexpreopt.PathToLocation(bootImages["x86"])= "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
45//
46// The location is passed as an argument to the ART tools like dex2oat instead of the real path. The ART tools
47// will then reconstruct the real path, so the rules must have a dependency on the real path.
48
David Srbeckyc177ebe2020-02-18 20:43:06 +000049// Target-independent description of pre-compiled boot image.
Colin Cross44df5812019-02-15 23:06:46 -080050type bootImageConfig struct {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000051 // Whether this image is an extension.
52 extension bool
53
54 // Image name (used in directory names and ninja rule names).
55 name string
56
57 // Basename of the image: the resulting filenames are <stem>[-<jar>].{art,oat,vdex}.
58 stem string
59
60 // Output directory for the image files.
61 dir android.OutputPath
62
63 // Output directory for the image files with debug symbols.
64 symbolsDir android.OutputPath
65
66 // Subdirectory where the image files are installed.
67 installSubdir string
68
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000069 // The names of jars that constitute this image.
70 modules []string
71
72 // The "locations" of jars.
73 dexLocations []string // for this image
74 dexLocationsDeps []string // for the dependency images and in this image
75
76 // File paths to jars.
77 dexPaths android.WritablePaths // for this image
78 dexPathsDeps android.WritablePaths // for the dependency images and in this image
79
80 // The "locations" of the dependency images and in this image.
81 imageLocations []string
82
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000083 // File path to a zip archive with all image files (or nil, if not needed).
84 zip android.WritablePath
David Srbeckyc177ebe2020-02-18 20:43:06 +000085
86 // Rules which should be used in make to install the outputs.
87 profileInstalls android.RuleBuilderInstalls
88
89 // Target-dependent fields.
90 variants []*bootImageVariant
91}
92
93// Target-dependent description of pre-compiled boot image.
94type bootImageVariant struct {
95 *bootImageConfig
96
97 // Target for which the image is generated.
98 target android.Target
99
100 // Paths to image files.
101 images android.OutputPath // first image file
102 imagesDeps android.OutputPaths // all files
103
104 // Only for extensions, paths to the primary boot images.
105 primaryImages android.OutputPath
106
107 // Rules which should be used in make to install the outputs.
108 installs android.RuleBuilderInstalls
109 vdexInstalls android.RuleBuilderInstalls
110 unstrippedInstalls android.RuleBuilderInstalls
111}
112
113func (image bootImageConfig) getVariant(target android.Target) *bootImageVariant {
114 for _, variant := range image.variants {
115 if variant.target.Os == target.Os && variant.target.Arch.ArchType == target.Arch.ArchType {
116 return variant
117 }
118 }
119 return nil
Colin Cross800fe132019-02-11 14:21:24 -0800120}
121
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000122func (image bootImageConfig) moduleName(idx int) string {
123 // Dexpreopt on the boot class path produces multiple files. The first dex file
124 // is converted into 'name'.art (to match the legacy assumption that 'name'.art
Dan Willemsen0f416782019-06-13 21:44:53 +0000125 // exists), and the rest are converted to 'name'-<jar>.art.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000126 m := image.modules[idx]
127 name := image.stem
128 if idx != 0 || image.extension {
129 name += "-" + stemOf(m)
130 }
131 return name
132}
Dan Willemsen0f416782019-06-13 21:44:53 +0000133
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000134func (image bootImageConfig) firstModuleNameOrStem() string {
135 if len(image.modules) > 0 {
136 return image.moduleName(0)
137 } else {
138 return image.stem
139 }
140}
141
142func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths {
143 ret := make(android.OutputPaths, 0, len(image.modules)*len(exts))
144 for i := range image.modules {
145 name := image.moduleName(i)
Dan Willemsen0f416782019-06-13 21:44:53 +0000146 for _, ext := range exts {
147 ret = append(ret, dir.Join(ctx, name+ext))
148 }
149 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000150 return ret
151}
152
Colin Cross800fe132019-02-11 14:21:24 -0800153func concat(lists ...[]string) []string {
154 var size int
155 for _, l := range lists {
156 size += len(l)
157 }
158 ret := make([]string, 0, size)
159 for _, l := range lists {
160 ret = append(ret, l...)
161 }
162 return ret
163}
164
Colin Cross800fe132019-02-11 14:21:24 -0800165func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800166 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800167}
168
169func skipDexpreoptBootJars(ctx android.PathContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000170 if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000171 return true
172 }
173
Colin Cross800fe132019-02-11 14:21:24 -0800174 if ctx.Config().UnbundledBuild() {
175 return true
176 }
177
178 if len(ctx.Config().Targets[android.Android]) == 0 {
179 // Host-only build
180 return true
181 }
182
183 return false
184}
185
Colin Cross44df5812019-02-15 23:06:46 -0800186type dexpreoptBootJars struct {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000187 defaultBootImage *bootImageConfig
188 otherImages []*bootImageConfig
Colin Cross2d00f0d2019-05-09 21:50:00 -0700189
190 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800191}
Colin Cross800fe132019-02-11 14:21:24 -0800192
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000193// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000194func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000195 if skipDexpreoptBootJars(ctx) {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000196 return nil
197 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000198 // Include dexpreopt files for the primary boot image.
David Srbeckyc177ebe2020-02-18 20:43:06 +0000199 files := map[android.ArchType]android.OutputPaths{}
200 for _, variant := range artBootImageConfig(ctx).variants {
201 files[variant.target.Arch.ArchType] = variant.imagesDeps
202 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000203 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000204}
205
Colin Cross800fe132019-02-11 14:21:24 -0800206// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800207func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800208 if skipDexpreoptBootJars(ctx) {
209 return
210 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000211 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
212 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
213 return
214 }
Colin Cross800fe132019-02-11 14:21:24 -0800215
Colin Cross2d00f0d2019-05-09 21:50:00 -0700216 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
217 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
218
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000219 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800220
221 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
222 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
223 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
224 // on ASAN settings.
225 if len(ctx.Config().SanitizeDevice()) == 1 &&
226 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800227 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800228 return
229 }
230
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000231 // Always create the default boot image first, to get a unique profile rule for all images.
232 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000233 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
234 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800235
236 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800237}
238
David Srbeckyc177ebe2020-02-18 20:43:06 +0000239// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
240func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Colin Cross44df5812019-02-15 23:06:46 -0800241 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800242 ctx.VisitAllModules(func(module android.Module) {
243 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800244 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800245 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800246 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800247 bootDexJars[i] = j.DexJar()
248 }
249 }
250 })
251
252 var missingDeps []string
253 // Ensure all modules were converted to paths
254 for i := range bootDexJars {
255 if bootDexJars[i] == nil {
256 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800257 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800258 bootDexJars[i] = android.PathForOutput(ctx, "missing")
259 } else {
260 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800261 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800262 }
263 }
264 }
265
266 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
267 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
268 // already been set up can find them.
269 for i := range bootDexJars {
270 ctx.Build(pctx, android.BuildParams{
271 Rule: android.Cp,
272 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800273 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800274 })
275 }
276
Colin Cross44df5812019-02-15 23:06:46 -0800277 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100278 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800279
Colin Crossdf8eebe2019-04-09 15:29:41 -0700280 var allFiles android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000281 for _, variant := range image.variants {
282 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000283 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800284 }
Colin Cross44df5812019-02-15 23:06:46 -0800285
Colin Crossdf8eebe2019-04-09 15:29:41 -0700286 if image.zip != nil {
287 rule := android.NewRuleBuilder()
288 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700289 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700290 FlagWithOutput("-o ", image.zip).
291 FlagWithArg("-C ", image.dir.String()).
292 FlagWithInputList("-f ", allFiles, " -f ")
293
294 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
295 }
296
Colin Cross44df5812019-02-15 23:06:46 -0800297 return image
Colin Cross800fe132019-02-11 14:21:24 -0800298}
299
David Srbeckyc177ebe2020-02-18 20:43:06 +0000300func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
301 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800302
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000303 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000304 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800305
David Srbeckyc177ebe2020-02-18 20:43:06 +0000306 arch := image.target.Arch.ArchType
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000307 symbolsDir := image.symbolsDir.Join(ctx, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000308 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000309 outputDir := image.dir.Join(ctx, image.installSubdir, arch.String())
310 outputPath := outputDir.Join(ctx, image.stem+".oat")
311 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
312 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800313
314 rule := android.NewRuleBuilder()
315 rule.MissingDeps(missingDeps)
316
317 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
318 rule.Command().Text("rm").Flag("-f").
319 Flag(symbolsDir.Join(ctx, "*.art").String()).
320 Flag(symbolsDir.Join(ctx, "*.oat").String()).
321 Flag(symbolsDir.Join(ctx, "*.invocation").String())
322 rule.Command().Text("rm").Flag("-f").
323 Flag(outputDir.Join(ctx, "*.art").String()).
324 Flag(outputDir.Join(ctx, "*.oat").String()).
325 Flag(outputDir.Join(ctx, "*.invocation").String())
326
327 cmd := rule.Command()
328
329 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
330 if extraFlags == "" {
331 // Use ANDROID_LOG_TAGS to suppress most logging by default...
332 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
333 } else {
334 // ...unless the boot image is generated specifically for testing, then allow all logging.
335 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
336 }
337
338 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
339
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000340 cmd.Tool(globalSoong.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800341 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800342 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800343 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
344 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800345
Colin Cross69f59a32019-02-15 10:39:37 -0800346 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800347 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800348 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800349 }
350
Colin Cross44df5812019-02-15 23:06:46 -0800351 if global.DirtyImageObjects.Valid() {
352 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800353 }
354
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000355 if image.extension {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000356 artImage := image.primaryImages
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000357 cmd.
358 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
359 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
360 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
361 } else {
362 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
363 }
364
Colin Cross800fe132019-02-11 14:21:24 -0800365 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800366 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
367 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800368 Flag("--generate-debug-info").
369 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700370 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000371 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800372 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000373 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800374 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000375 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800376 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800377 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
378 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
379 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800380 FlagWithArg("--no-inline-from=", "core-oj.jar").
381 Flag("--abort-on-hard-verifier-error")
382
Colin Cross44df5812019-02-15 23:06:46 -0800383 if global.BootFlags != "" {
384 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800385 }
386
387 if extraFlags != "" {
388 cmd.Flag(extraFlags)
389 }
390
Colin Cross0b9f31f2019-02-28 11:00:01 -0800391 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800392
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000393 installDir := filepath.Join("/", image.installSubdir, arch.String())
394 vdexInstallDir := filepath.Join("/", image.installSubdir)
Colin Cross800fe132019-02-11 14:21:24 -0800395
Colin Cross800fe132019-02-11 14:21:24 -0800396 var vdexInstalls android.RuleBuilderInstalls
397 var unstrippedInstalls android.RuleBuilderInstalls
398
Colin Crossdf8eebe2019-04-09 15:29:41 -0700399 var zipFiles android.WritablePaths
400
Dan Willemsen0f416782019-06-13 21:44:53 +0000401 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
402 cmd.ImplicitOutput(artOrOat)
403 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800404
Dan Willemsen0f416782019-06-13 21:44:53 +0000405 // Install the .oat and .art files
406 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
407 }
Colin Cross800fe132019-02-11 14:21:24 -0800408
Dan Willemsen0f416782019-06-13 21:44:53 +0000409 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
410 cmd.ImplicitOutput(vdex)
411 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800412
413 // The vdex files are identical between architectures, install them to a shared location. The Make rules will
414 // only use the install rules for one architecture, and will create symlinks into the architecture-specific
415 // directories.
416 vdexInstalls = append(vdexInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800417 android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000418 }
419
420 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
421 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800422
423 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
424 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800425 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800426 }
427
Colin Cross44df5812019-02-15 23:06:46 -0800428 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800429
430 // save output and installed files for makevars
David Srbeckyc177ebe2020-02-18 20:43:06 +0000431 image.installs = rule.Installs()
432 image.vdexInstalls = vdexInstalls
433 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700434
435 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800436}
437
438const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
439It is likely that the boot classpath is inconsistent.
440Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
441
David Srbeckyc177ebe2020-02-18 20:43:06 +0000442func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000443 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000444 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000445
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700446 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000447 return nil
448 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000449 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000450 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800451
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000452 rule := android.NewRuleBuilder()
453 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800454
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000455 var bootImageProfile android.Path
456 if len(global.BootImageProfiles) > 1 {
457 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
458 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
459 bootImageProfile = combinedBootImageProfile
460 } else if len(global.BootImageProfiles) == 1 {
461 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000462 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
463 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000464 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000465 // No profile (not even a default one, which is the case on some branches
466 // like master-art-host that don't have frameworks/base).
467 // Return nil and continue without profile.
468 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000469 }
Colin Cross800fe132019-02-11 14:21:24 -0800470
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000471 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800472
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000473 rule.Command().
474 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000475 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000476 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000477 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
478 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000479 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800480
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000481 rule.Install(profile, "/system/etc/boot-image.prof")
482
483 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
484
485 image.profileInstalls = rule.Installs()
486
487 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000488 })
489 if profile == nil {
490 return nil // wrap nil into a typed pointer with value nil
491 }
492 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800493}
494
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000495var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
496
David Srbeckyc177ebe2020-02-18 20:43:06 +0000497func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000498 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000499 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100500
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 Stjernholm75a48d82020-01-10 20:32:59 +0000525 Tool(globalSoong.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
David Srbeckyc177ebe2020-02-18 20:43:06 +0000542func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800543 var allPhonies android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000544 for _, image := range image.variants {
545 arch := image.target.Arch.ArchType
Colin Crossc9a4c362019-02-26 21:13:48 -0800546 // Create a rule to call oatdump.
547 output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt")
548 rule := android.NewRuleBuilder()
549 rule.Command().
550 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700551 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000552 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
553 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbeckyc177ebe2020-02-18 20:43:06 +0000554 FlagWithArg("--image=", strings.Join(image.imageLocations, ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800555 FlagWithOutput("--output=", output).
556 FlagWithArg("--instruction-set=", arch.String())
557 rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
558
559 // Create a phony rule that depends on the output file and prints the path.
560 phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String())
561 rule = android.NewRuleBuilder()
562 rule.Command().
563 Implicit(output).
564 ImplicitOutput(phony).
565 Text("echo").FlagWithArg("Output in ", output.String())
566 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
567
568 allPhonies = append(allPhonies, phony)
569 }
570
571 phony := android.PathForPhony(ctx, "dump-oat-boot")
572 ctx.Build(pctx, android.BuildParams{
573 Rule: android.Phony,
574 Output: phony,
575 Inputs: allPhonies,
576 Description: "dump-oat-boot",
577 })
578
579}
580
Colin Cross2d00f0d2019-05-09 21:50:00 -0700581func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000582 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700583
584 ctx.Build(pctx, android.BuildParams{
585 Rule: android.WriteFile,
586 Output: path,
587 Args: map[string]string{
588 "content": string(data),
589 },
590 })
591}
592
Colin Cross44df5812019-02-15 23:06:46 -0800593// Export paths for default boot image to Make
594func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700595 if d.dexpreoptConfigForMake != nil {
596 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000597 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700598 }
599
Colin Cross44df5812019-02-15 23:06:46 -0800600 image := d.defaultBootImage
601 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800602 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000603 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
604 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000605
606 var imageNames []string
607 for _, current := range append(d.otherImages, image) {
608 imageNames = append(imageNames, current.name)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000609 for _, current := range current.variants {
610 sfx := current.name + "_" + current.target.Arch.ArchType.String()
611 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, current.vdexInstalls.String())
612 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, current.images.String())
613 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(current.imagesDeps.Strings(), " "))
614 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, current.installs.String())
615 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, current.unstrippedInstalls.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000616 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800617
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000618 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(current.imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800619 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000620 }
621 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800622 }
Colin Cross800fe132019-02-11 14:21:24 -0800623}