blob: 87f6d5e33871fe21fb3203b4ba705214110b3248 [file] [log] [blame]
Colin Cross800fe132019-02-11 14:21:24 -08001// Copyright 2019 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
18 "path/filepath"
Colin Crossc9a4c362019-02-26 21:13:48 -080019 "sort"
Colin Cross800fe132019-02-11 14:21:24 -080020 "strings"
21
22 "android/soong/android"
23 "android/soong/dexpreopt"
24
Colin Cross800fe132019-02-11 14:21:24 -080025 "github.com/google/blueprint/proptools"
26)
27
28func init() {
29 android.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory)
30}
31
32// The image "location" is a symbolic path that with multiarchitecture
33// support doesn't really exist on the device. Typically it is
34// /system/framework/boot.art and should be the same for all supported
35// architectures on the device. The concrete architecture specific
36// content actually ends up in a "filename" that contains an
37// architecture specific directory name such as arm, arm64, mips,
38// mips64, x86, x86_64.
39//
40// Here are some example values for an x86_64 / x86 configuration:
41//
42// bootImages["x86_64"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86_64/boot.art"
43// dexpreopt.PathToLocation(bootImages["x86_64"], "x86_64") = "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
44//
45// bootImages["x86"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86/boot.art"
46// dexpreopt.PathToLocation(bootImages["x86"])= "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
47//
48// The location is passed as an argument to the ART tools like dex2oat instead of the real path. The ART tools
49// will then reconstruct the real path, so the rules must have a dependency on the real path.
50
Colin Cross44df5812019-02-15 23:06:46 -080051type bootImageConfig struct {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000052 // Whether this image is an extension.
53 extension bool
54
55 // Image name (used in directory names and ninja rule names).
56 name string
57
58 // Basename of the image: the resulting filenames are <stem>[-<jar>].{art,oat,vdex}.
59 stem string
60
61 // Output directory for the image files.
62 dir android.OutputPath
63
64 // Output directory for the image files with debug symbols.
65 symbolsDir android.OutputPath
66
67 // Subdirectory where the image files are installed.
68 installSubdir string
69
70 // Targets for which the image is generated.
71 targets []android.Target
72
73 // The names of jars that constitute this image.
74 modules []string
75
76 // The "locations" of jars.
77 dexLocations []string // for this image
78 dexLocationsDeps []string // for the dependency images and in this image
79
80 // File paths to jars.
81 dexPaths android.WritablePaths // for this image
82 dexPathsDeps android.WritablePaths // for the dependency images and in this image
83
84 // The "locations" of the dependency images and in this image.
85 imageLocations []string
86
87 // Paths to image files (grouped by target).
88 images map[android.ArchType]android.OutputPath // first image file
89 imagesDeps map[android.ArchType]android.OutputPaths // all files
90
Ulya Trafimovichb0a2d372020-01-28 14:42:41 +000091 // Only for extensions, paths to the primary boot images (grouped by target).
92 primaryImages map[android.ArchType]android.OutputPath
93
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000094 // File path to a zip archive with all image files (or nil, if not needed).
95 zip android.WritablePath
Colin Cross800fe132019-02-11 14:21:24 -080096}
97
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000098func (image bootImageConfig) moduleName(idx int) string {
99 // Dexpreopt on the boot class path produces multiple files. The first dex file
100 // is converted into 'name'.art (to match the legacy assumption that 'name'.art
Dan Willemsen0f416782019-06-13 21:44:53 +0000101 // exists), and the rest are converted to 'name'-<jar>.art.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000102 m := image.modules[idx]
103 name := image.stem
104 if idx != 0 || image.extension {
105 name += "-" + stemOf(m)
106 }
107 return name
108}
Dan Willemsen0f416782019-06-13 21:44:53 +0000109
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000110func (image bootImageConfig) firstModuleNameOrStem() string {
111 if len(image.modules) > 0 {
112 return image.moduleName(0)
113 } else {
114 return image.stem
115 }
116}
117
118func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths {
119 ret := make(android.OutputPaths, 0, len(image.modules)*len(exts))
120 for i := range image.modules {
121 name := image.moduleName(i)
Dan Willemsen0f416782019-06-13 21:44:53 +0000122 for _, ext := range exts {
123 ret = append(ret, dir.Join(ctx, name+ext))
124 }
125 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000126 return ret
127}
128
Colin Cross44df5812019-02-15 23:06:46 -0800129type bootImage struct {
130 bootImageConfig
Colin Cross800fe132019-02-11 14:21:24 -0800131
Colin Cross44df5812019-02-15 23:06:46 -0800132 installs map[android.ArchType]android.RuleBuilderInstalls
133 vdexInstalls map[android.ArchType]android.RuleBuilderInstalls
134 unstrippedInstalls map[android.ArchType]android.RuleBuilderInstalls
Colin Cross800fe132019-02-11 14:21:24 -0800135
Colin Cross44df5812019-02-15 23:06:46 -0800136 profileInstalls android.RuleBuilderInstalls
137}
Colin Cross800fe132019-02-11 14:21:24 -0800138
Colin Cross44df5812019-02-15 23:06:46 -0800139func newBootImage(ctx android.PathContext, config bootImageConfig) *bootImage {
140 image := &bootImage{
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000141 bootImageConfig: config,
Colin Cross800fe132019-02-11 14:21:24 -0800142
Colin Cross44df5812019-02-15 23:06:46 -0800143 installs: make(map[android.ArchType]android.RuleBuilderInstalls),
144 vdexInstalls: make(map[android.ArchType]android.RuleBuilderInstalls),
145 unstrippedInstalls: make(map[android.ArchType]android.RuleBuilderInstalls),
146 }
Colin Cross800fe132019-02-11 14:21:24 -0800147
Colin Cross44df5812019-02-15 23:06:46 -0800148 return image
Colin Cross800fe132019-02-11 14:21:24 -0800149}
150
151func concat(lists ...[]string) []string {
152 var size int
153 for _, l := range lists {
154 size += len(l)
155 }
156 ret := make([]string, 0, size)
157 for _, l := range lists {
158 ret = append(ret, l...)
159 }
160 return ret
161}
162
Colin Cross800fe132019-02-11 14:21:24 -0800163func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800164 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800165}
166
167func skipDexpreoptBootJars(ctx android.PathContext) bool {
Hans Boehm453bf092020-01-25 01:44:30 +0000168 if dexpreoptGlobalConfig(ctx).DisablePreopt {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000169 return true
170 }
171
Colin Cross800fe132019-02-11 14:21:24 -0800172 if ctx.Config().UnbundledBuild() {
173 return true
174 }
175
176 if len(ctx.Config().Targets[android.Android]) == 0 {
177 // Host-only build
178 return true
179 }
180
181 return false
182}
183
Colin Cross44df5812019-02-15 23:06:46 -0800184type dexpreoptBootJars struct {
185 defaultBootImage *bootImage
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000186 otherImages []*bootImage
Colin Cross2d00f0d2019-05-09 21:50:00 -0700187
188 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800189}
Colin Cross800fe132019-02-11 14:21:24 -0800190
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000191// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000192func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000193 if skipDexpreoptBootJars(ctx) {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000194 return nil
195 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000196
197 // Include dexpreopt files for the primary boot image.
198 files := artBootImageConfig(ctx).imagesDeps
199
200 // For JIT-zygote config, also include dexpreopt files for the primary JIT-zygote image.
Hans Boehm453bf092020-01-25 01:44:30 +0000201 if dexpreoptGlobalConfig(ctx).UseApexImage {
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000202 for arch, paths := range artJZBootImageConfig(ctx).imagesDeps {
203 files[arch] = append(files[arch], paths...)
204 }
205 }
206
207 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000208}
209
Colin Cross800fe132019-02-11 14:21:24 -0800210// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800211func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800212 if skipDexpreoptBootJars(ctx) {
213 return
214 }
215
Colin Cross2d00f0d2019-05-09 21:50:00 -0700216 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
217 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
218
Hans Boehm453bf092020-01-25 01:44:30 +0000219 global := dexpreoptGlobalConfig(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)))
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000235 if global.GenerateApexImage {
236 // Create boot images for the JIT-zygote experiment.
Ulya Trafimovich57547452019-12-09 15:40:17 +0000237 d.otherImages = append(d.otherImages, buildBootImage(ctx, artJZBootImageConfig(ctx)))
238 d.otherImages = append(d.otherImages, buildBootImage(ctx, frameworkJZBootImageConfig(ctx)))
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000239 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800240
241 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800242}
243
244// buildBootImage takes a bootImageConfig, creates rules to build it, and returns a *bootImage.
245func buildBootImage(ctx android.SingletonContext, config bootImageConfig) *bootImage {
Colin Cross44df5812019-02-15 23:06:46 -0800246 image := newBootImage(ctx, config)
247
248 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800249 ctx.VisitAllModules(func(module android.Module) {
250 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800251 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800252 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800253 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800254 bootDexJars[i] = j.DexJar()
255 }
256 }
257 })
258
259 var missingDeps []string
260 // Ensure all modules were converted to paths
261 for i := range bootDexJars {
262 if bootDexJars[i] == nil {
263 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800264 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800265 bootDexJars[i] = android.PathForOutput(ctx, "missing")
266 } else {
267 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800268 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800269 }
270 }
271 }
272
273 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
274 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
275 // already been set up can find them.
276 for i := range bootDexJars {
277 ctx.Build(pctx, android.BuildParams{
278 Rule: android.Cp,
279 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800280 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800281 })
282 }
283
Colin Cross44df5812019-02-15 23:06:46 -0800284 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100285 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800286
Colin Crossdf8eebe2019-04-09 15:29:41 -0700287 var allFiles android.Paths
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000288 for _, target := range image.targets {
289 files := buildBootImageRuleForArch(ctx, image, target.Arch.ArchType, profile, missingDeps)
290 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800291 }
Colin Cross44df5812019-02-15 23:06:46 -0800292
Colin Crossdf8eebe2019-04-09 15:29:41 -0700293 if image.zip != nil {
294 rule := android.NewRuleBuilder()
295 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700296 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700297 FlagWithOutput("-o ", image.zip).
298 FlagWithArg("-C ", image.dir.String()).
299 FlagWithInputList("-f ", allFiles, " -f ")
300
301 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
302 }
303
Colin Cross44df5812019-02-15 23:06:46 -0800304 return image
Colin Cross800fe132019-02-11 14:21:24 -0800305}
306
Colin Cross44df5812019-02-15 23:06:46 -0800307func buildBootImageRuleForArch(ctx android.SingletonContext, image *bootImage,
Colin Crossdf8eebe2019-04-09 15:29:41 -0700308 arch android.ArchType, profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800309
Hans Boehm453bf092020-01-25 01:44:30 +0000310 global := dexpreoptGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800311
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000312 symbolsDir := image.symbolsDir.Join(ctx, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000313 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000314 outputDir := image.dir.Join(ctx, image.installSubdir, arch.String())
315 outputPath := outputDir.Join(ctx, image.stem+".oat")
316 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
317 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800318
319 rule := android.NewRuleBuilder()
320 rule.MissingDeps(missingDeps)
321
322 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
323 rule.Command().Text("rm").Flag("-f").
324 Flag(symbolsDir.Join(ctx, "*.art").String()).
325 Flag(symbolsDir.Join(ctx, "*.oat").String()).
326 Flag(symbolsDir.Join(ctx, "*.invocation").String())
327 rule.Command().Text("rm").Flag("-f").
328 Flag(outputDir.Join(ctx, "*.art").String()).
329 Flag(outputDir.Join(ctx, "*.oat").String()).
330 Flag(outputDir.Join(ctx, "*.invocation").String())
331
332 cmd := rule.Command()
333
334 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
335 if extraFlags == "" {
336 // Use ANDROID_LOG_TAGS to suppress most logging by default...
337 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
338 } else {
339 // ...unless the boot image is generated specifically for testing, then allow all logging.
340 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
341 }
342
343 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
344
Hans Boehme4b53422020-01-25 01:44:30 +0000345 cmd.Tool(global.SoongConfig.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800346 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800347 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800348 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
349 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800350
Colin Cross69f59a32019-02-15 10:39:37 -0800351 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800352 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800353 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800354 }
355
Colin Cross44df5812019-02-15 23:06:46 -0800356 if global.DirtyImageObjects.Valid() {
357 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800358 }
359
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000360 if image.extension {
Ulya Trafimovichb0a2d372020-01-28 14:42:41 +0000361 artImage := image.primaryImages[arch]
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000362 cmd.
363 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
364 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
365 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
366 } else {
367 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
368 }
369
Colin Cross800fe132019-02-11 14:21:24 -0800370 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800371 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
372 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800373 Flag("--generate-debug-info").
374 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700375 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000376 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800377 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000378 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800379 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000380 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800381 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800382 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
383 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
384 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800385 FlagWithArg("--no-inline-from=", "core-oj.jar").
386 Flag("--abort-on-hard-verifier-error")
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())
399 vdexInstallDir := filepath.Join("/", image.installSubdir)
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
418 // The vdex files are identical between architectures, install them to a shared location. The Make rules will
419 // only use the install rules for one architecture, and will create symlinks into the architecture-specific
420 // directories.
421 vdexInstalls = append(vdexInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800422 android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000423 }
424
425 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
426 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800427
428 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
429 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800430 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800431 }
432
Colin Cross44df5812019-02-15 23:06:46 -0800433 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800434
435 // save output and installed files for makevars
Colin Cross44df5812019-02-15 23:06:46 -0800436 image.installs[arch] = rule.Installs()
437 image.vdexInstalls[arch] = vdexInstalls
438 image.unstrippedInstalls[arch] = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700439
440 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800441}
442
443const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
444It is likely that the boot classpath is inconsistent.
445Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
446
Colin Cross44df5812019-02-15 23:06:46 -0800447func bootImageProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
Hans Boehm453bf092020-01-25 01:44:30 +0000448 global := dexpreoptGlobalConfig(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"`).
Hans Boehme4b53422020-01-25 01:44:30 +0000479 Tool(global.SoongConfig.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
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100501func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
Hans Boehm453bf092020-01-25 01:44:30 +0000502 global := dexpreoptGlobalConfig(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"`).
Hans Boehme4b53422020-01-25 01:44:30 +0000528 Tool(global.SoongConfig.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
Colin Crossc9a4c362019-02-26 21:13:48 -0800545func dumpOatRules(ctx android.SingletonContext, image *bootImage) {
546 var archs []android.ArchType
547 for arch := range image.images {
548 archs = append(archs, arch)
549 }
550 sort.Slice(archs, func(i, j int) bool { return archs[i].String() < archs[j].String() })
551
552 var allPhonies android.Paths
553 for _, arch := range archs {
554 // Create a rule to call oatdump.
555 output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt")
556 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, ":").
Ulya Trafimovich163664a2019-12-06 13:42:21 +0000562 FlagWithArg("--image=", strings.Join(image.imageLocations, ":")).Implicits(image.imagesDeps[arch].Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800563 FlagWithOutput("--output=", output).
564 FlagWithArg("--instruction-set=", arch.String())
565 rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
566
567 // Create a phony rule that depends on the output file and prints the path.
568 phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String())
569 rule = android.NewRuleBuilder()
570 rule.Command().
571 Implicit(output).
572 ImplicitOutput(phony).
573 Text("echo").FlagWithArg("Output in ", output.String())
574 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
575
576 allPhonies = append(allPhonies, phony)
577 }
578
579 phony := android.PathForPhony(ctx, "dump-oat-boot")
580 ctx.Build(pctx, android.BuildParams{
581 Rule: android.Phony,
582 Output: phony,
583 Inputs: allPhonies,
584 Description: "dump-oat-boot",
585 })
586
587}
588
Colin Cross2d00f0d2019-05-09 21:50:00 -0700589func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Hans Boehm453bf092020-01-25 01:44:30 +0000590 data := dexpreoptGlobalConfigRaw(ctx).data
Colin Cross2d00f0d2019-05-09 21:50:00 -0700591
592 ctx.Build(pctx, android.BuildParams{
593 Rule: android.WriteFile,
594 Output: path,
595 Args: map[string]string{
596 "content": string(data),
597 },
598 })
599}
600
Colin Cross44df5812019-02-15 23:06:46 -0800601// Export paths for default boot image to Make
602func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700603 if d.dexpreoptConfigForMake != nil {
604 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000605 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700606 }
607
Colin Cross44df5812019-02-15 23:06:46 -0800608 image := d.defaultBootImage
609 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800610 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000611 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
612 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000613
614 var imageNames []string
615 for _, current := range append(d.otherImages, image) {
616 imageNames = append(imageNames, current.name)
Colin Cross91268c62019-04-11 14:07:04 -0700617 var arches []android.ArchType
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000618 for arch, _ := range current.images {
Colin Cross91268c62019-04-11 14:07:04 -0700619 arches = append(arches, arch)
620 }
621
622 sort.Slice(arches, func(i, j int) bool { return arches[i].String() < arches[j].String() })
623
624 for _, arch := range arches {
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000625 sfx := current.name + "_" + arch.String()
626 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, current.vdexInstalls[arch].String())
627 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, current.images[arch].String())
628 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(current.imagesDeps[arch].Strings(), " "))
629 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, current.installs[arch].String())
630 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, current.unstrippedInstalls[arch].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}