blob: 8e621c7135873a8f5ff04e443d0d8a9743b1c1a0 [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
36// architecture specific directory name such as arm, arm64, mips,
37// mips64, x86, x86_64.
38//
39// Here are some example values for an x86_64 / x86 configuration:
40//
41// bootImages["x86_64"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86_64/boot.art"
42// dexpreopt.PathToLocation(bootImages["x86_64"], "x86_64") = "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
43//
44// bootImages["x86"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86/boot.art"
45// dexpreopt.PathToLocation(bootImages["x86"])= "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
46//
47// The location is passed as an argument to the ART tools like dex2oat instead of the real path. The ART tools
48// will then reconstruct the real path, so the rules must have a dependency on the real path.
49
David Srbeckyc177ebe2020-02-18 20:43:06 +000050// Target-independent description of pre-compiled boot image.
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
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000070 // The names of jars that constitute this image.
71 modules []string
72
73 // The "locations" of jars.
74 dexLocations []string // for this image
75 dexLocationsDeps []string // for the dependency images and in this image
76
77 // File paths to jars.
78 dexPaths android.WritablePaths // for this image
79 dexPathsDeps android.WritablePaths // for the dependency images and in this image
80
81 // The "locations" of the dependency images and in this image.
82 imageLocations []string
83
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000084 // File path to a zip archive with all image files (or nil, if not needed).
85 zip android.WritablePath
David Srbeckyc177ebe2020-02-18 20:43:06 +000086
87 // Rules which should be used in make to install the outputs.
88 profileInstalls android.RuleBuilderInstalls
89
90 // Target-dependent fields.
91 variants []*bootImageVariant
92}
93
94// Target-dependent description of pre-compiled boot image.
95type bootImageVariant struct {
96 *bootImageConfig
97
98 // Target for which the image is generated.
99 target android.Target
100
101 // Paths to image files.
102 images android.OutputPath // first image file
103 imagesDeps android.OutputPaths // all files
104
105 // Only for extensions, paths to the primary boot images.
106 primaryImages android.OutputPath
107
108 // Rules which should be used in make to install the outputs.
109 installs android.RuleBuilderInstalls
110 vdexInstalls android.RuleBuilderInstalls
111 unstrippedInstalls android.RuleBuilderInstalls
112}
113
114func (image bootImageConfig) getVariant(target android.Target) *bootImageVariant {
115 for _, variant := range image.variants {
116 if variant.target.Os == target.Os && variant.target.Arch.ArchType == target.Arch.ArchType {
117 return variant
118 }
119 }
120 return nil
Colin Cross800fe132019-02-11 14:21:24 -0800121}
122
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000123func (image bootImageConfig) moduleName(idx int) string {
124 // Dexpreopt on the boot class path produces multiple files. The first dex file
125 // is converted into 'name'.art (to match the legacy assumption that 'name'.art
Dan Willemsen0f416782019-06-13 21:44:53 +0000126 // exists), and the rest are converted to 'name'-<jar>.art.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000127 m := image.modules[idx]
128 name := image.stem
129 if idx != 0 || image.extension {
130 name += "-" + stemOf(m)
131 }
132 return name
133}
Dan Willemsen0f416782019-06-13 21:44:53 +0000134
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000135func (image bootImageConfig) firstModuleNameOrStem() string {
136 if len(image.modules) > 0 {
137 return image.moduleName(0)
138 } else {
139 return image.stem
140 }
141}
142
143func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths {
144 ret := make(android.OutputPaths, 0, len(image.modules)*len(exts))
145 for i := range image.modules {
146 name := image.moduleName(i)
Dan Willemsen0f416782019-06-13 21:44:53 +0000147 for _, ext := range exts {
148 ret = append(ret, dir.Join(ctx, name+ext))
149 }
150 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000151 return ret
152}
153
Colin Cross800fe132019-02-11 14:21:24 -0800154func concat(lists ...[]string) []string {
155 var size int
156 for _, l := range lists {
157 size += len(l)
158 }
159 ret := make([]string, 0, size)
160 for _, l := range lists {
161 ret = append(ret, l...)
162 }
163 return ret
164}
165
Colin Cross800fe132019-02-11 14:21:24 -0800166func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800167 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800168}
169
170func skipDexpreoptBootJars(ctx android.PathContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000171 if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000172 return true
173 }
174
Colin Cross800fe132019-02-11 14:21:24 -0800175 if ctx.Config().UnbundledBuild() {
176 return true
177 }
178
Colin Cross800fe132019-02-11 14:21:24 -0800179 return false
180}
181
Colin Cross44df5812019-02-15 23:06:46 -0800182type dexpreoptBootJars struct {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000183 defaultBootImage *bootImageConfig
184 otherImages []*bootImageConfig
Colin Cross2d00f0d2019-05-09 21:50:00 -0700185
186 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800187}
Colin Cross800fe132019-02-11 14:21:24 -0800188
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000189// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000190func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000191 if skipDexpreoptBootJars(ctx) {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000192 return nil
193 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000194 // Include dexpreopt files for the primary boot image.
David Srbeckyc177ebe2020-02-18 20:43:06 +0000195 files := map[android.ArchType]android.OutputPaths{}
196 for _, variant := range artBootImageConfig(ctx).variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000197 // We also generate boot images for host (for testing), but we don't need those in the apex.
198 if variant.target.Os == android.Android {
199 files[variant.target.Arch.ArchType] = variant.imagesDeps
200 }
David Srbeckyc177ebe2020-02-18 20:43:06 +0000201 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000202 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000203}
204
Colin Cross800fe132019-02-11 14:21:24 -0800205// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800206func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800207 if skipDexpreoptBootJars(ctx) {
208 return
209 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000210 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
211 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
212 return
213 }
Colin Cross800fe132019-02-11 14:21:24 -0800214
Colin Cross2d00f0d2019-05-09 21:50:00 -0700215 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
216 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
217
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000218 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800219
220 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
221 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
222 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
223 // on ASAN settings.
224 if len(ctx.Config().SanitizeDevice()) == 1 &&
225 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800226 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800227 return
228 }
229
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000230 // Always create the default boot image first, to get a unique profile rule for all images.
231 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000232 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
233 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800234
235 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800236}
237
David Srbeckyc177ebe2020-02-18 20:43:06 +0000238// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
239func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Colin Cross44df5812019-02-15 23:06:46 -0800240 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800241 ctx.VisitAllModules(func(module android.Module) {
242 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800243 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800244 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800245 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800246 bootDexJars[i] = j.DexJar()
247 }
248 }
249 })
250
251 var missingDeps []string
252 // Ensure all modules were converted to paths
253 for i := range bootDexJars {
254 if bootDexJars[i] == nil {
255 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800256 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800257 bootDexJars[i] = android.PathForOutput(ctx, "missing")
258 } else {
259 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800260 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800261 }
262 }
263 }
264
265 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
266 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
267 // already been set up can find them.
268 for i := range bootDexJars {
269 ctx.Build(pctx, android.BuildParams{
270 Rule: android.Cp,
271 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800272 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800273 })
274 }
275
Colin Cross44df5812019-02-15 23:06:46 -0800276 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100277 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800278
Colin Crossdf8eebe2019-04-09 15:29:41 -0700279 var allFiles android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000280 for _, variant := range image.variants {
281 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000282 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800283 }
Colin Cross44df5812019-02-15 23:06:46 -0800284
Colin Crossdf8eebe2019-04-09 15:29:41 -0700285 if image.zip != nil {
286 rule := android.NewRuleBuilder()
287 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700288 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700289 FlagWithOutput("-o ", image.zip).
290 FlagWithArg("-C ", image.dir.String()).
291 FlagWithInputList("-f ", allFiles, " -f ")
292
293 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
294 }
295
Colin Cross44df5812019-02-15 23:06:46 -0800296 return image
Colin Cross800fe132019-02-11 14:21:24 -0800297}
298
David Srbeckyc177ebe2020-02-18 20:43:06 +0000299func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
300 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800301
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000302 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000303 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800304
David Srbeckyc177ebe2020-02-18 20:43:06 +0000305 arch := image.target.Arch.ArchType
David Srbecky7f8dac12020-02-13 16:00:45 +0000306 os := image.target.Os.String() // We need to distinguish host-x86 and device-x86.
307 symbolsDir := image.symbolsDir.Join(ctx, os, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000308 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
David Srbecky7f8dac12020-02-13 16:00:45 +0000309 outputDir := image.dir.Join(ctx, os, image.installSubdir, arch.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000310 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("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800378 FlagWithArg("--no-inline-from=", "core-oj.jar").
Ulya Trafimovichc0c98d52020-03-09 12:46:06 +0000379 Flag("--force-determinism").
Colin Cross800fe132019-02-11 14:21:24 -0800380 Flag("--abort-on-hard-verifier-error")
381
David Srbecky7f8dac12020-02-13 16:00:45 +0000382 // Use the default variant/features for host builds.
383 // The map below contains only device CPU info (which might be x86 on some devices).
384 if image.target.Os == android.Android {
385 cmd.FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch])
386 cmd.FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch])
387 }
388
Colin Cross44df5812019-02-15 23:06:46 -0800389 if global.BootFlags != "" {
390 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800391 }
392
393 if extraFlags != "" {
394 cmd.Flag(extraFlags)
395 }
396
Colin Cross0b9f31f2019-02-28 11:00:01 -0800397 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800398
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000399 installDir := filepath.Join("/", image.installSubdir, arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800400
Colin Cross800fe132019-02-11 14:21:24 -0800401 var vdexInstalls android.RuleBuilderInstalls
402 var unstrippedInstalls android.RuleBuilderInstalls
403
Colin Crossdf8eebe2019-04-09 15:29:41 -0700404 var zipFiles android.WritablePaths
405
Dan Willemsen0f416782019-06-13 21:44:53 +0000406 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
407 cmd.ImplicitOutput(artOrOat)
408 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800409
Dan Willemsen0f416782019-06-13 21:44:53 +0000410 // Install the .oat and .art files
411 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
412 }
Colin Cross800fe132019-02-11 14:21:24 -0800413
Dan Willemsen0f416782019-06-13 21:44:53 +0000414 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
415 cmd.ImplicitOutput(vdex)
416 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800417
David Srbecky7f8dac12020-02-13 16:00:45 +0000418 // Note that the vdex files are identical between architectures.
419 // Make rules will create symlinks to share them between architectures.
Colin Cross800fe132019-02-11 14:21:24 -0800420 vdexInstalls = append(vdexInstalls,
David Srbecky7f8dac12020-02-13 16:00:45 +0000421 android.RuleBuilderInstall{vdex, filepath.Join(installDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000422 }
423
424 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
425 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800426
427 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
428 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800429 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800430 }
431
David Srbecky7f8dac12020-02-13 16:00:45 +0000432 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+image.target.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800433
434 // save output and installed files for makevars
David Srbeckyc177ebe2020-02-18 20:43:06 +0000435 image.installs = rule.Installs()
436 image.vdexInstalls = vdexInstalls
437 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700438
439 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800440}
441
442const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
443It is likely that the boot classpath is inconsistent.
444Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
445
David Srbeckyc177ebe2020-02-18 20:43:06 +0000446func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000447 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000448 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000449
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700450 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000451 return nil
452 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000453 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000454 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800455
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000456 rule := android.NewRuleBuilder()
457 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800458
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000459 var bootImageProfile android.Path
460 if len(global.BootImageProfiles) > 1 {
461 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
462 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
463 bootImageProfile = combinedBootImageProfile
464 } else if len(global.BootImageProfiles) == 1 {
465 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000466 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
467 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000468 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000469 // No profile (not even a default one, which is the case on some branches
470 // like master-art-host that don't have frameworks/base).
471 // Return nil and continue without profile.
472 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000473 }
Colin Cross800fe132019-02-11 14:21:24 -0800474
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000475 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800476
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000477 rule.Command().
478 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000479 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000480 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000481 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
482 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000483 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800484
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000485 rule.Install(profile, "/system/etc/boot-image.prof")
486
487 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
488
489 image.profileInstalls = rule.Installs()
490
491 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000492 })
493 if profile == nil {
494 return nil // wrap nil into a typed pointer with value nil
495 }
496 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800497}
498
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000499var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
500
David Srbeckyc177ebe2020-02-18 20:43:06 +0000501func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000502 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000503 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100504
505 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
506 return nil
507 }
508 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100509 rule := android.NewRuleBuilder()
510 rule.MissingDeps(missingDeps)
511
512 // Some branches like master-art-host don't have frameworks/base, so manually
513 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
514 // and if they do they'll get a missing deps error.
515 defaultProfile := "frameworks/base/config/boot-profile.txt"
516 path := android.ExistentPathForSource(ctx, defaultProfile)
517 var bootFrameworkProfile android.Path
518 if path.Valid() {
519 bootFrameworkProfile = path.Path()
520 } else {
521 missingDeps = append(missingDeps, defaultProfile)
522 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
523 }
524
525 profile := image.dir.Join(ctx, "boot.bprof")
526
527 rule.Command().
528 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000529 Tool(globalSoong.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100530 Flag("--generate-boot-profile").
531 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000532 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
533 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100534 FlagWithOutput("--reference-profile-file=", profile)
535
536 rule.Install(profile, "/system/etc/boot-image.bprof")
537 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
538 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
539
540 return profile
541 }).(android.WritablePath)
542}
543
544var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
545
David Srbeckyc177ebe2020-02-18 20:43:06 +0000546func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800547 var allPhonies android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000548 for _, image := range image.variants {
549 arch := image.target.Arch.ArchType
David Srbecky46672322020-03-16 13:27:55 +0000550 suffix := arch.String()
551 // Host and target might both use x86 arch. We need to ensure the names are unique.
552 if image.target.Os.Class == android.Host {
553 suffix = "host-" + suffix
554 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800555 // Create a rule to call oatdump.
David Srbecky7f8dac12020-02-13 16:00:45 +0000556 output := android.PathForOutput(ctx, "boot."+suffix+".oatdump.txt")
Colin Crossc9a4c362019-02-26 21:13:48 -0800557 rule := android.NewRuleBuilder()
558 rule.Command().
559 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700560 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000561 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
562 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbeckyc177ebe2020-02-18 20:43:06 +0000563 FlagWithArg("--image=", strings.Join(image.imageLocations, ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800564 FlagWithOutput("--output=", output).
565 FlagWithArg("--instruction-set=", arch.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000566 rule.Build(pctx, ctx, "dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800567
568 // Create a phony rule that depends on the output file and prints the path.
David Srbecky7f8dac12020-02-13 16:00:45 +0000569 phony := android.PathForPhony(ctx, "dump-oat-boot-"+suffix)
Colin Crossc9a4c362019-02-26 21:13:48 -0800570 rule = android.NewRuleBuilder()
571 rule.Command().
572 Implicit(output).
573 ImplicitOutput(phony).
574 Text("echo").FlagWithArg("Output in ", output.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000575 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800576
David Srbecky46672322020-03-16 13:27:55 +0000577 // TODO: We need to make imageLocations per-variant to make oatdump work on host.
578 if image.target.Os == android.Android {
579 allPhonies = append(allPhonies, phony)
580 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800581 }
582
583 phony := android.PathForPhony(ctx, "dump-oat-boot")
584 ctx.Build(pctx, android.BuildParams{
585 Rule: android.Phony,
586 Output: phony,
587 Inputs: allPhonies,
588 Description: "dump-oat-boot",
589 })
590
591}
592
Colin Cross2d00f0d2019-05-09 21:50:00 -0700593func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000594 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700595
596 ctx.Build(pctx, android.BuildParams{
597 Rule: android.WriteFile,
598 Output: path,
599 Args: map[string]string{
600 "content": string(data),
601 },
602 })
603}
604
Colin Cross44df5812019-02-15 23:06:46 -0800605// Export paths for default boot image to Make
606func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700607 if d.dexpreoptConfigForMake != nil {
608 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000609 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700610 }
611
Colin Cross44df5812019-02-15 23:06:46 -0800612 image := d.defaultBootImage
613 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800614 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000615 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
616 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000617
618 var imageNames []string
619 for _, current := range append(d.otherImages, image) {
620 imageNames = append(imageNames, current.name)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000621 for _, current := range current.variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000622 suffix := ""
623 if current.target.Os.Class == android.Host {
624 suffix = "_host"
625 }
626 sfx := current.name + suffix + "_" + current.target.Arch.ArchType.String()
David Srbeckyc177ebe2020-02-18 20:43:06 +0000627 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, current.vdexInstalls.String())
628 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, current.images.String())
629 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(current.imagesDeps.Strings(), " "))
630 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, current.installs.String())
631 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, current.unstrippedInstalls.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000632 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800633
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000634 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(current.imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800635 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000636 }
637 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800638 }
Colin Cross800fe132019-02-11 14:21:24 -0800639}