blob: d7adb4051e65c753d3c8f33b49784454d20e2d0e [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
179 if len(ctx.Config().Targets[android.Android]) == 0 {
180 // Host-only build
181 return true
182 }
183
184 return false
185}
186
Colin Cross44df5812019-02-15 23:06:46 -0800187type dexpreoptBootJars struct {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000188 defaultBootImage *bootImageConfig
189 otherImages []*bootImageConfig
Colin Cross2d00f0d2019-05-09 21:50:00 -0700190
191 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800192}
Colin Cross800fe132019-02-11 14:21:24 -0800193
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000194// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000195func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000196 if skipDexpreoptBootJars(ctx) {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000197 return nil
198 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000199 // Include dexpreopt files for the primary boot image.
David Srbeckyc177ebe2020-02-18 20:43:06 +0000200 files := map[android.ArchType]android.OutputPaths{}
201 for _, variant := range artBootImageConfig(ctx).variants {
202 files[variant.target.Arch.ArchType] = variant.imagesDeps
203 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000204 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000205}
206
Colin Cross800fe132019-02-11 14:21:24 -0800207// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800208func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800209 if skipDexpreoptBootJars(ctx) {
210 return
211 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000212 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
213 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
214 return
215 }
Colin Cross800fe132019-02-11 14:21:24 -0800216
Colin Cross2d00f0d2019-05-09 21:50:00 -0700217 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
218 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
219
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000220 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800221
222 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
223 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
224 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
225 // on ASAN settings.
226 if len(ctx.Config().SanitizeDevice()) == 1 &&
227 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800228 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800229 return
230 }
231
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000232 // Always create the default boot image first, to get a unique profile rule for all images.
233 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000234 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
235 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800236
237 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800238}
239
David Srbeckyc177ebe2020-02-18 20:43:06 +0000240// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
241func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Colin Cross44df5812019-02-15 23:06:46 -0800242 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800243 ctx.VisitAllModules(func(module android.Module) {
244 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800245 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800246 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800247 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800248 bootDexJars[i] = j.DexJar()
249 }
250 }
251 })
252
253 var missingDeps []string
254 // Ensure all modules were converted to paths
255 for i := range bootDexJars {
256 if bootDexJars[i] == nil {
257 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800258 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800259 bootDexJars[i] = android.PathForOutput(ctx, "missing")
260 } else {
261 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800262 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800263 }
264 }
265 }
266
267 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
268 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
269 // already been set up can find them.
270 for i := range bootDexJars {
271 ctx.Build(pctx, android.BuildParams{
272 Rule: android.Cp,
273 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800274 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800275 })
276 }
277
Colin Cross44df5812019-02-15 23:06:46 -0800278 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100279 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800280
Colin Crossdf8eebe2019-04-09 15:29:41 -0700281 var allFiles android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000282 for _, variant := range image.variants {
283 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000284 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800285 }
Colin Cross44df5812019-02-15 23:06:46 -0800286
Colin Crossdf8eebe2019-04-09 15:29:41 -0700287 if image.zip != nil {
288 rule := android.NewRuleBuilder()
289 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700290 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700291 FlagWithOutput("-o ", image.zip).
292 FlagWithArg("-C ", image.dir.String()).
293 FlagWithInputList("-f ", allFiles, " -f ")
294
295 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
296 }
297
Colin Cross44df5812019-02-15 23:06:46 -0800298 return image
Colin Cross800fe132019-02-11 14:21:24 -0800299}
300
David Srbeckyc177ebe2020-02-18 20:43:06 +0000301func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
302 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800303
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000304 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000305 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800306
David Srbeckyc177ebe2020-02-18 20:43:06 +0000307 arch := image.target.Arch.ArchType
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000308 symbolsDir := image.symbolsDir.Join(ctx, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000309 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000310 outputDir := image.dir.Join(ctx, image.installSubdir, arch.String())
311 outputPath := outputDir.Join(ctx, image.stem+".oat")
312 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
313 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800314
315 rule := android.NewRuleBuilder()
316 rule.MissingDeps(missingDeps)
317
318 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
319 rule.Command().Text("rm").Flag("-f").
320 Flag(symbolsDir.Join(ctx, "*.art").String()).
321 Flag(symbolsDir.Join(ctx, "*.oat").String()).
322 Flag(symbolsDir.Join(ctx, "*.invocation").String())
323 rule.Command().Text("rm").Flag("-f").
324 Flag(outputDir.Join(ctx, "*.art").String()).
325 Flag(outputDir.Join(ctx, "*.oat").String()).
326 Flag(outputDir.Join(ctx, "*.invocation").String())
327
328 cmd := rule.Command()
329
330 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
331 if extraFlags == "" {
332 // Use ANDROID_LOG_TAGS to suppress most logging by default...
333 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
334 } else {
335 // ...unless the boot image is generated specifically for testing, then allow all logging.
336 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
337 }
338
339 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
340
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000341 cmd.Tool(globalSoong.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800342 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800343 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800344 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
345 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800346
Colin Cross69f59a32019-02-15 10:39:37 -0800347 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800348 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800349 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800350 }
351
Colin Cross44df5812019-02-15 23:06:46 -0800352 if global.DirtyImageObjects.Valid() {
353 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800354 }
355
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000356 if image.extension {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000357 artImage := image.primaryImages
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000358 cmd.
359 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
360 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
361 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
362 } else {
363 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
364 }
365
Colin Cross800fe132019-02-11 14:21:24 -0800366 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800367 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
368 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800369 Flag("--generate-debug-info").
370 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700371 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000372 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800373 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000374 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800375 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000376 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800377 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800378 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
379 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
380 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800381 FlagWithArg("--no-inline-from=", "core-oj.jar").
382 Flag("--abort-on-hard-verifier-error")
383
Colin Cross44df5812019-02-15 23:06:46 -0800384 if global.BootFlags != "" {
385 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800386 }
387
388 if extraFlags != "" {
389 cmd.Flag(extraFlags)
390 }
391
Colin Cross0b9f31f2019-02-28 11:00:01 -0800392 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800393
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000394 installDir := filepath.Join("/", image.installSubdir, arch.String())
395 vdexInstallDir := filepath.Join("/", image.installSubdir)
Colin Cross800fe132019-02-11 14:21:24 -0800396
Colin Cross800fe132019-02-11 14:21:24 -0800397 var vdexInstalls android.RuleBuilderInstalls
398 var unstrippedInstalls android.RuleBuilderInstalls
399
Colin Crossdf8eebe2019-04-09 15:29:41 -0700400 var zipFiles android.WritablePaths
401
Dan Willemsen0f416782019-06-13 21:44:53 +0000402 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
403 cmd.ImplicitOutput(artOrOat)
404 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800405
Dan Willemsen0f416782019-06-13 21:44:53 +0000406 // Install the .oat and .art files
407 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
408 }
Colin Cross800fe132019-02-11 14:21:24 -0800409
Dan Willemsen0f416782019-06-13 21:44:53 +0000410 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
411 cmd.ImplicitOutput(vdex)
412 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800413
414 // The vdex files are identical between architectures, install them to a shared location. The Make rules will
415 // only use the install rules for one architecture, and will create symlinks into the architecture-specific
416 // directories.
417 vdexInstalls = append(vdexInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800418 android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000419 }
420
421 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
422 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800423
424 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
425 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800426 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800427 }
428
Colin Cross44df5812019-02-15 23:06:46 -0800429 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800430
431 // save output and installed files for makevars
David Srbeckyc177ebe2020-02-18 20:43:06 +0000432 image.installs = rule.Installs()
433 image.vdexInstalls = vdexInstalls
434 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700435
436 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800437}
438
439const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
440It is likely that the boot classpath is inconsistent.
441Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
442
David Srbeckyc177ebe2020-02-18 20:43:06 +0000443func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000444 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000445 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000446
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700447 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000448 return nil
449 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000450 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000451 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800452
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000453 rule := android.NewRuleBuilder()
454 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800455
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000456 var bootImageProfile android.Path
457 if len(global.BootImageProfiles) > 1 {
458 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
459 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
460 bootImageProfile = combinedBootImageProfile
461 } else if len(global.BootImageProfiles) == 1 {
462 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000463 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
464 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000465 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000466 // No profile (not even a default one, which is the case on some branches
467 // like master-art-host that don't have frameworks/base).
468 // Return nil and continue without profile.
469 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000470 }
Colin Cross800fe132019-02-11 14:21:24 -0800471
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000472 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800473
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000474 rule.Command().
475 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000476 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000477 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000478 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
479 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000480 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800481
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000482 rule.Install(profile, "/system/etc/boot-image.prof")
483
484 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
485
486 image.profileInstalls = rule.Installs()
487
488 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000489 })
490 if profile == nil {
491 return nil // wrap nil into a typed pointer with value nil
492 }
493 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800494}
495
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000496var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
497
David Srbeckyc177ebe2020-02-18 20:43:06 +0000498func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000499 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000500 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100501
502 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
503 return nil
504 }
505 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100506 rule := android.NewRuleBuilder()
507 rule.MissingDeps(missingDeps)
508
509 // Some branches like master-art-host don't have frameworks/base, so manually
510 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
511 // and if they do they'll get a missing deps error.
512 defaultProfile := "frameworks/base/config/boot-profile.txt"
513 path := android.ExistentPathForSource(ctx, defaultProfile)
514 var bootFrameworkProfile android.Path
515 if path.Valid() {
516 bootFrameworkProfile = path.Path()
517 } else {
518 missingDeps = append(missingDeps, defaultProfile)
519 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
520 }
521
522 profile := image.dir.Join(ctx, "boot.bprof")
523
524 rule.Command().
525 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000526 Tool(globalSoong.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100527 Flag("--generate-boot-profile").
528 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000529 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
530 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100531 FlagWithOutput("--reference-profile-file=", profile)
532
533 rule.Install(profile, "/system/etc/boot-image.bprof")
534 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
535 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
536
537 return profile
538 }).(android.WritablePath)
539}
540
541var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
542
David Srbeckyc177ebe2020-02-18 20:43:06 +0000543func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800544 var allPhonies android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000545 for _, image := range image.variants {
546 arch := image.target.Arch.ArchType
Colin Crossc9a4c362019-02-26 21:13:48 -0800547 // Create a rule to call oatdump.
548 output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt")
549 rule := android.NewRuleBuilder()
550 rule.Command().
551 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700552 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000553 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
554 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbeckyc177ebe2020-02-18 20:43:06 +0000555 FlagWithArg("--image=", strings.Join(image.imageLocations, ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800556 FlagWithOutput("--output=", output).
557 FlagWithArg("--instruction-set=", arch.String())
558 rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
559
560 // Create a phony rule that depends on the output file and prints the path.
561 phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String())
562 rule = android.NewRuleBuilder()
563 rule.Command().
564 Implicit(output).
565 ImplicitOutput(phony).
566 Text("echo").FlagWithArg("Output in ", output.String())
567 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
568
569 allPhonies = append(allPhonies, phony)
570 }
571
572 phony := android.PathForPhony(ctx, "dump-oat-boot")
573 ctx.Build(pctx, android.BuildParams{
574 Rule: android.Phony,
575 Output: phony,
576 Inputs: allPhonies,
577 Description: "dump-oat-boot",
578 })
579
580}
581
Colin Cross2d00f0d2019-05-09 21:50:00 -0700582func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000583 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700584
585 ctx.Build(pctx, android.BuildParams{
586 Rule: android.WriteFile,
587 Output: path,
588 Args: map[string]string{
589 "content": string(data),
590 },
591 })
592}
593
Colin Cross44df5812019-02-15 23:06:46 -0800594// Export paths for default boot image to Make
595func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700596 if d.dexpreoptConfigForMake != nil {
597 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000598 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700599 }
600
Colin Cross44df5812019-02-15 23:06:46 -0800601 image := d.defaultBootImage
602 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800603 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000604 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
605 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000606
607 var imageNames []string
608 for _, current := range append(d.otherImages, image) {
609 imageNames = append(imageNames, current.name)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000610 for _, current := range current.variants {
611 sfx := current.name + "_" + current.target.Arch.ArchType.String()
612 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, current.vdexInstalls.String())
613 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, current.images.String())
614 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(current.imagesDeps.Strings(), " "))
615 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, current.installs.String())
616 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, current.unstrippedInstalls.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000617 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800618
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000619 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(current.imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800620 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000621 }
622 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800623 }
Colin Cross800fe132019-02-11 14:21:24 -0800624}