blob: a3b264ed80087edfa1d1cdcde896ce38711cbeeb [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
Colin Cross800fe132019-02-11 14:21:24 -0800178 return false
179}
180
Colin Cross44df5812019-02-15 23:06:46 -0800181type dexpreoptBootJars struct {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000182 defaultBootImage *bootImageConfig
183 otherImages []*bootImageConfig
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.
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000189func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000190 if skipDexpreoptBootJars(ctx) {
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000191 return nil
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000192 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000193 // Include dexpreopt files for the primary boot image.
194 files := map[android.ArchType]android.OutputPaths{}
195 for _, variant := range artBootImageConfig(ctx).variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000196 // We also generate boot images for host (for testing), but we don't need those in the apex.
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000197 if variant.target.Os == android.Android {
198 files[variant.target.Arch.ArchType] = variant.imagesDeps
David Srbecky7f8dac12020-02-13 16:00:45 +0000199 }
David Srbeckyc177ebe2020-02-18 20:43:06 +0000200 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000201 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000202}
203
Colin Cross800fe132019-02-11 14:21:24 -0800204// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800205func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800206 if skipDexpreoptBootJars(ctx) {
207 return
208 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000209 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
210 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
211 return
212 }
Colin Cross800fe132019-02-11 14:21:24 -0800213
Colin Cross2d00f0d2019-05-09 21:50:00 -0700214 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
215 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
216
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000217 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800218
219 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
220 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
221 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
222 // on ASAN settings.
223 if len(ctx.Config().SanitizeDevice()) == 1 &&
224 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800225 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800226 return
227 }
228
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000229 // Always create the default boot image first, to get a unique profile rule for all images.
230 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000231 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
232 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800233
234 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800235}
236
David Srbeckyc177ebe2020-02-18 20:43:06 +0000237// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
238func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Colin Cross44df5812019-02-15 23:06:46 -0800239 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800240 ctx.VisitAllModules(func(module android.Module) {
241 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800242 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800243 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800244 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800245 bootDexJars[i] = j.DexJar()
246 }
247 }
248 })
249
250 var missingDeps []string
251 // Ensure all modules were converted to paths
252 for i := range bootDexJars {
253 if bootDexJars[i] == nil {
254 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800255 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800256 bootDexJars[i] = android.PathForOutput(ctx, "missing")
257 } else {
258 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800259 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800260 }
261 }
262 }
263
264 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
265 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
266 // already been set up can find them.
267 for i := range bootDexJars {
268 ctx.Build(pctx, android.BuildParams{
269 Rule: android.Cp,
270 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800271 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800272 })
273 }
274
Colin Cross44df5812019-02-15 23:06:46 -0800275 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100276 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800277
Colin Crossdf8eebe2019-04-09 15:29:41 -0700278 var allFiles android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000279 for _, variant := range image.variants {
280 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000281 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800282 }
Colin Cross44df5812019-02-15 23:06:46 -0800283
Colin Crossdf8eebe2019-04-09 15:29:41 -0700284 if image.zip != nil {
285 rule := android.NewRuleBuilder()
286 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700287 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700288 FlagWithOutput("-o ", image.zip).
289 FlagWithArg("-C ", image.dir.String()).
290 FlagWithInputList("-f ", allFiles, " -f ")
291
292 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
293 }
294
Colin Cross44df5812019-02-15 23:06:46 -0800295 return image
Colin Cross800fe132019-02-11 14:21:24 -0800296}
297
David Srbeckyc177ebe2020-02-18 20:43:06 +0000298func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
299 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800300
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000301 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000302 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800303
David Srbeckyc177ebe2020-02-18 20:43:06 +0000304 arch := image.target.Arch.ArchType
David Srbecky7f8dac12020-02-13 16:00:45 +0000305 os := image.target.Os.String() // We need to distinguish host-x86 and device-x86.
306 symbolsDir := image.symbolsDir.Join(ctx, os, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000307 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
David Srbecky7f8dac12020-02-13 16:00:45 +0000308 outputDir := image.dir.Join(ctx, os, image.installSubdir, arch.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000309 outputPath := outputDir.Join(ctx, image.stem+".oat")
310 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
311 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800312
313 rule := android.NewRuleBuilder()
314 rule.MissingDeps(missingDeps)
315
316 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
317 rule.Command().Text("rm").Flag("-f").
318 Flag(symbolsDir.Join(ctx, "*.art").String()).
319 Flag(symbolsDir.Join(ctx, "*.oat").String()).
320 Flag(symbolsDir.Join(ctx, "*.invocation").String())
321 rule.Command().Text("rm").Flag("-f").
322 Flag(outputDir.Join(ctx, "*.art").String()).
323 Flag(outputDir.Join(ctx, "*.oat").String()).
324 Flag(outputDir.Join(ctx, "*.invocation").String())
325
326 cmd := rule.Command()
327
328 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
329 if extraFlags == "" {
330 // Use ANDROID_LOG_TAGS to suppress most logging by default...
331 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
332 } else {
333 // ...unless the boot image is generated specifically for testing, then allow all logging.
334 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
335 }
336
337 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
338
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000339 cmd.Tool(globalSoong.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800340 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800341 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800342 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
343 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800344
Colin Cross69f59a32019-02-15 10:39:37 -0800345 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800346 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800347 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800348 }
349
Colin Cross44df5812019-02-15 23:06:46 -0800350 if global.DirtyImageObjects.Valid() {
351 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800352 }
353
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000354 if image.extension {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000355 artImage := image.primaryImages
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000356 cmd.
357 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
358 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
359 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
360 } else {
361 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
362 }
363
Colin Cross800fe132019-02-11 14:21:24 -0800364 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800365 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
366 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800367 Flag("--generate-debug-info").
368 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700369 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000370 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800371 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000372 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800373 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000374 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800375 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800376 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800377 FlagWithArg("--no-inline-from=", "core-oj.jar").
Ulya Trafimovichc0c98d52020-03-09 12:46:06 +0000378 Flag("--force-determinism").
Colin Cross800fe132019-02-11 14:21:24 -0800379 Flag("--abort-on-hard-verifier-error")
380
David Srbecky7f8dac12020-02-13 16:00:45 +0000381 // Use the default variant/features for host builds.
382 // The map below contains only device CPU info (which might be x86 on some devices).
383 if image.target.Os == android.Android {
384 cmd.FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch])
385 cmd.FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch])
386 }
387
Colin Cross44df5812019-02-15 23:06:46 -0800388 if global.BootFlags != "" {
389 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800390 }
391
392 if extraFlags != "" {
393 cmd.Flag(extraFlags)
394 }
395
Colin Cross0b9f31f2019-02-28 11:00:01 -0800396 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800397
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000398 installDir := filepath.Join("/", image.installSubdir, arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800399
Colin Cross800fe132019-02-11 14:21:24 -0800400 var vdexInstalls android.RuleBuilderInstalls
401 var unstrippedInstalls android.RuleBuilderInstalls
402
Colin Crossdf8eebe2019-04-09 15:29:41 -0700403 var zipFiles android.WritablePaths
404
Dan Willemsen0f416782019-06-13 21:44:53 +0000405 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
406 cmd.ImplicitOutput(artOrOat)
407 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800408
Dan Willemsen0f416782019-06-13 21:44:53 +0000409 // Install the .oat and .art files
410 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
411 }
Colin Cross800fe132019-02-11 14:21:24 -0800412
Dan Willemsen0f416782019-06-13 21:44:53 +0000413 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
414 cmd.ImplicitOutput(vdex)
415 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800416
David Srbecky7f8dac12020-02-13 16:00:45 +0000417 // Note that the vdex files are identical between architectures.
418 // Make rules will create symlinks to share them between architectures.
Colin Cross800fe132019-02-11 14:21:24 -0800419 vdexInstalls = append(vdexInstalls,
David Srbecky7f8dac12020-02-13 16:00:45 +0000420 android.RuleBuilderInstall{vdex, filepath.Join(installDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000421 }
422
423 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
424 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800425
426 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
427 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800428 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800429 }
430
David Srbecky7f8dac12020-02-13 16:00:45 +0000431 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+image.target.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800432
433 // save output and installed files for makevars
David Srbeckyc177ebe2020-02-18 20:43:06 +0000434 image.installs = rule.Installs()
435 image.vdexInstalls = vdexInstalls
436 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700437
438 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800439}
440
441const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
442It is likely that the boot classpath is inconsistent.
443Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
444
David Srbeckyc177ebe2020-02-18 20:43:06 +0000445func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000446 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000447 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000448
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700449 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000450 return nil
451 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000452 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000453 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800454
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000455 rule := android.NewRuleBuilder()
456 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800457
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000458 var bootImageProfile android.Path
459 if len(global.BootImageProfiles) > 1 {
460 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
461 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
462 bootImageProfile = combinedBootImageProfile
463 } else if len(global.BootImageProfiles) == 1 {
464 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000465 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
466 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000467 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000468 // No profile (not even a default one, which is the case on some branches
469 // like master-art-host that don't have frameworks/base).
470 // Return nil and continue without profile.
471 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000472 }
Colin Cross800fe132019-02-11 14:21:24 -0800473
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000474 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800475
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000476 rule.Command().
477 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000478 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000479 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000480 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
481 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000482 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800483
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000484 rule.Install(profile, "/system/etc/boot-image.prof")
485
486 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
487
488 image.profileInstalls = rule.Installs()
489
490 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000491 })
492 if profile == nil {
493 return nil // wrap nil into a typed pointer with value nil
494 }
495 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800496}
497
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000498var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
499
David Srbeckyc177ebe2020-02-18 20:43:06 +0000500func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000501 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000502 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100503
504 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
505 return nil
506 }
507 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100508 rule := android.NewRuleBuilder()
509 rule.MissingDeps(missingDeps)
510
511 // Some branches like master-art-host don't have frameworks/base, so manually
512 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
513 // and if they do they'll get a missing deps error.
514 defaultProfile := "frameworks/base/config/boot-profile.txt"
515 path := android.ExistentPathForSource(ctx, defaultProfile)
516 var bootFrameworkProfile android.Path
517 if path.Valid() {
518 bootFrameworkProfile = path.Path()
519 } else {
520 missingDeps = append(missingDeps, defaultProfile)
521 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
522 }
523
524 profile := image.dir.Join(ctx, "boot.bprof")
525
526 rule.Command().
527 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000528 Tool(globalSoong.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100529 Flag("--generate-boot-profile").
530 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000531 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
532 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100533 FlagWithOutput("--reference-profile-file=", profile)
534
535 rule.Install(profile, "/system/etc/boot-image.bprof")
536 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
537 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
538
539 return profile
540 }).(android.WritablePath)
541}
542
543var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
544
David Srbeckyc177ebe2020-02-18 20:43:06 +0000545func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800546 var allPhonies android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000547 for _, image := range image.variants {
548 arch := image.target.Arch.ArchType
David Srbecky46672322020-03-16 13:27:55 +0000549 suffix := arch.String()
550 // Host and target might both use x86 arch. We need to ensure the names are unique.
551 if image.target.Os.Class == android.Host {
552 suffix = "host-" + suffix
553 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800554 // Create a rule to call oatdump.
David Srbecky7f8dac12020-02-13 16:00:45 +0000555 output := android.PathForOutput(ctx, "boot."+suffix+".oatdump.txt")
Colin Crossc9a4c362019-02-26 21:13:48 -0800556 rule := android.NewRuleBuilder()
557 rule.Command().
558 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700559 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000560 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
561 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbeckyc177ebe2020-02-18 20:43:06 +0000562 FlagWithArg("--image=", strings.Join(image.imageLocations, ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800563 FlagWithOutput("--output=", output).
564 FlagWithArg("--instruction-set=", arch.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000565 rule.Build(pctx, ctx, "dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800566
567 // Create a phony rule that depends on the output file and prints the path.
David Srbecky7f8dac12020-02-13 16:00:45 +0000568 phony := android.PathForPhony(ctx, "dump-oat-boot-"+suffix)
Colin Crossc9a4c362019-02-26 21:13:48 -0800569 rule = android.NewRuleBuilder()
570 rule.Command().
571 Implicit(output).
572 ImplicitOutput(phony).
573 Text("echo").FlagWithArg("Output in ", output.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000574 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800575
David Srbecky46672322020-03-16 13:27:55 +0000576 // TODO: We need to make imageLocations per-variant to make oatdump work on host.
577 if image.target.Os == android.Android {
578 allPhonies = append(allPhonies, phony)
579 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800580 }
581
582 phony := android.PathForPhony(ctx, "dump-oat-boot")
583 ctx.Build(pctx, android.BuildParams{
584 Rule: android.Phony,
585 Output: phony,
586 Inputs: allPhonies,
587 Description: "dump-oat-boot",
588 })
589
590}
591
Colin Cross2d00f0d2019-05-09 21:50:00 -0700592func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000593 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700594
595 ctx.Build(pctx, android.BuildParams{
596 Rule: android.WriteFile,
597 Output: path,
598 Args: map[string]string{
599 "content": string(data),
600 },
601 })
602}
603
Colin Cross44df5812019-02-15 23:06:46 -0800604// Export paths for default boot image to Make
605func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700606 if d.dexpreoptConfigForMake != nil {
607 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000608 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700609 }
610
Colin Cross44df5812019-02-15 23:06:46 -0800611 image := d.defaultBootImage
612 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800613 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000614 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
615 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000616
617 var imageNames []string
618 for _, current := range append(d.otherImages, image) {
619 imageNames = append(imageNames, current.name)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000620 for _, current := range current.variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000621 suffix := ""
622 if current.target.Os.Class == android.Host {
623 suffix = "_host"
624 }
625 sfx := current.name + suffix + "_" + current.target.Arch.ArchType.String()
David Srbeckyc177ebe2020-02-18 20:43:06 +0000626 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, current.vdexInstalls.String())
627 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, current.images.String())
628 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(current.imagesDeps.Strings(), " "))
629 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, current.installs.String())
630 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, current.unstrippedInstalls.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000631 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800632
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000633 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(current.imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800634 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000635 }
636 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800637 }
Colin Cross800fe132019-02-11 14:21:24 -0800638}