blob: cc2c0962a5761e236f4cf7ea3ffbf5abf0f57a23 [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() {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000028 RegisterDexpreoptBootJarsComponents(android.InitRegistrationContext)
Colin Cross800fe132019-02-11 14:21:24 -080029}
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
Ulya Trafimovichb28cc372020-01-13 15:18:16 +0000169func RegisterDexpreoptBootJarsComponents(ctx android.RegistrationContext) {
170 ctx.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory)
171}
172
Colin Cross800fe132019-02-11 14:21:24 -0800173func skipDexpreoptBootJars(ctx android.PathContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000174 if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000175 return true
176 }
177
Colin Cross800fe132019-02-11 14:21:24 -0800178 if ctx.Config().UnbundledBuild() {
179 return true
180 }
181
Colin Cross800fe132019-02-11 14:21:24 -0800182 return false
183}
184
Colin Cross44df5812019-02-15 23:06:46 -0800185type dexpreoptBootJars struct {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000186 defaultBootImage *bootImageConfig
187 otherImages []*bootImageConfig
Colin Cross2d00f0d2019-05-09 21:50:00 -0700188
189 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800190}
Colin Cross800fe132019-02-11 14:21:24 -0800191
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000192// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000193func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000194 if skipDexpreoptBootJars(ctx) {
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000195 return nil
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000196 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000197 // Include dexpreopt files for the primary boot image.
198 files := map[android.ArchType]android.OutputPaths{}
199 for _, variant := range artBootImageConfig(ctx).variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000200 // 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 +0000201 if variant.target.Os == android.Android {
202 files[variant.target.Arch.ArchType] = variant.imagesDeps
David Srbecky7f8dac12020-02-13 16:00:45 +0000203 }
David Srbeckyc177ebe2020-02-18 20:43:06 +0000204 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000205 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000206}
207
Colin Cross800fe132019-02-11 14:21:24 -0800208// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800209func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800210 if skipDexpreoptBootJars(ctx) {
211 return
212 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000213 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
214 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
215 return
216 }
Colin Cross800fe132019-02-11 14:21:24 -0800217
Colin Cross2d00f0d2019-05-09 21:50:00 -0700218 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
219 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
220
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000221 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800222
223 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
224 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
225 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
226 // on ASAN settings.
227 if len(ctx.Config().SanitizeDevice()) == 1 &&
228 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800229 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800230 return
231 }
232
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000233 // Always create the default boot image first, to get a unique profile rule for all images.
234 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000235 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
236 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800237
238 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800239}
240
Ulya Trafimovichb28cc372020-01-13 15:18:16 +0000241// Inspect this module to see if it contains a bootclasspath dex jar.
242// Note that the same jar may occur in multiple modules.
243// This logic is tested in the apex package to avoid import cycle apex <-> java.
244func getBootImageJar(ctx android.SingletonContext, image *bootImageConfig, module android.Module) (int, android.Path) {
245 // All apex Java libraries have non-installable platform variants, skip them.
246 if module.IsSkipInstall() {
247 return -1, nil
248 }
249
250 jar, hasJar := module.(interface{ DexJar() android.Path })
251 if !hasJar {
252 return -1, nil
253 }
254
255 name := ctx.ModuleName(module)
256 index := android.IndexList(name, image.modules)
257 if index == -1 {
258 return -1, nil
259 }
260
261 // Check that this module satisfies constraints for a particular boot image.
262 apex, isApexModule := module.(android.ApexModule)
263 if image.name == artBootImageName {
264 if isApexModule && strings.HasPrefix(apex.ApexName(), "com.android.art.") {
265 // ok, found the jar in the ART apex
266 } else if isApexModule && !apex.IsForPlatform() {
267 // this jar is part of an updatable apex other than ART, fail immediately
268 ctx.Errorf("module '%s' from updatable apex '%s' is not allowed in the ART boot image", name, apex.ApexName())
269 } else if isApexModule && apex.IsForPlatform() && Bool(module.(*Library).deviceProperties.Hostdex) {
270 // this is a special "hostdex" variant, skip it and resume search
271 return -1, nil
272 } else {
273 // this (installable) jar is part of the platform, fail immediately
274 ctx.Errorf("module '%s' is part of the platform and not allowed in the ART boot image", name)
275 }
276 } else if image.name == frameworkBootImageName {
277 if !isApexModule || apex.IsForPlatform() {
278 // ok, this jar is part of the platform
279 } else {
280 // this jar is part of an updatable apex, fail immediately
281 ctx.Errorf("module '%s' from updatable apex '%s' is not allowed in the framework boot image", name, apex.ApexName())
282 }
283 } else {
284 panic("unknown boot image: " + image.name)
285 }
286
287 return index, jar.DexJar()
288}
289
David Srbeckyc177ebe2020-02-18 20:43:06 +0000290// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
291func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +0000292 // Collect dex jar paths for the boot image modules.
293 // This logic is tested in the apex package to avoid import cycle apex <-> java.
Colin Cross44df5812019-02-15 23:06:46 -0800294 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800295 ctx.VisitAllModules(func(module android.Module) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +0000296 if i, j := getBootImageJar(ctx, image, module); i != -1 {
297 bootDexJars[i] = j
Colin Cross800fe132019-02-11 14:21:24 -0800298 }
299 })
300
301 var missingDeps []string
302 // Ensure all modules were converted to paths
303 for i := range bootDexJars {
304 if bootDexJars[i] == nil {
305 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800306 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800307 bootDexJars[i] = android.PathForOutput(ctx, "missing")
308 } else {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +0000309 ctx.Errorf("failed to find a dex jar path for module '%s'"+
310 ", note that some jars may be filtered out by module constraints",
Colin Cross44df5812019-02-15 23:06:46 -0800311 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800312 }
313 }
314 }
315
316 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
317 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
318 // already been set up can find them.
319 for i := range bootDexJars {
320 ctx.Build(pctx, android.BuildParams{
321 Rule: android.Cp,
322 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800323 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800324 })
325 }
326
Colin Cross44df5812019-02-15 23:06:46 -0800327 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100328 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800329
Colin Crossdf8eebe2019-04-09 15:29:41 -0700330 var allFiles android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000331 for _, variant := range image.variants {
332 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000333 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800334 }
Colin Cross44df5812019-02-15 23:06:46 -0800335
Colin Crossdf8eebe2019-04-09 15:29:41 -0700336 if image.zip != nil {
337 rule := android.NewRuleBuilder()
338 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700339 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700340 FlagWithOutput("-o ", image.zip).
341 FlagWithArg("-C ", image.dir.String()).
342 FlagWithInputList("-f ", allFiles, " -f ")
343
344 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
345 }
346
Colin Cross44df5812019-02-15 23:06:46 -0800347 return image
Colin Cross800fe132019-02-11 14:21:24 -0800348}
349
David Srbeckyc177ebe2020-02-18 20:43:06 +0000350func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
351 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800352
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000353 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000354 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800355
David Srbeckyc177ebe2020-02-18 20:43:06 +0000356 arch := image.target.Arch.ArchType
David Srbecky7f8dac12020-02-13 16:00:45 +0000357 os := image.target.Os.String() // We need to distinguish host-x86 and device-x86.
358 symbolsDir := image.symbolsDir.Join(ctx, os, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000359 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
David Srbecky7f8dac12020-02-13 16:00:45 +0000360 outputDir := image.dir.Join(ctx, os, image.installSubdir, arch.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000361 outputPath := outputDir.Join(ctx, image.stem+".oat")
362 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
363 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800364
365 rule := android.NewRuleBuilder()
366 rule.MissingDeps(missingDeps)
367
368 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
369 rule.Command().Text("rm").Flag("-f").
370 Flag(symbolsDir.Join(ctx, "*.art").String()).
371 Flag(symbolsDir.Join(ctx, "*.oat").String()).
372 Flag(symbolsDir.Join(ctx, "*.invocation").String())
373 rule.Command().Text("rm").Flag("-f").
374 Flag(outputDir.Join(ctx, "*.art").String()).
375 Flag(outputDir.Join(ctx, "*.oat").String()).
376 Flag(outputDir.Join(ctx, "*.invocation").String())
377
378 cmd := rule.Command()
379
380 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
381 if extraFlags == "" {
382 // Use ANDROID_LOG_TAGS to suppress most logging by default...
383 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
384 } else {
385 // ...unless the boot image is generated specifically for testing, then allow all logging.
386 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
387 }
388
389 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
390
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000391 cmd.Tool(globalSoong.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800392 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800393 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800394 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
395 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800396
Colin Cross69f59a32019-02-15 10:39:37 -0800397 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800398 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800399 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800400 }
401
Colin Cross44df5812019-02-15 23:06:46 -0800402 if global.DirtyImageObjects.Valid() {
403 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800404 }
405
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000406 if image.extension {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000407 artImage := image.primaryImages
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000408 cmd.
409 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
410 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
411 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
412 } else {
413 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
414 }
415
Colin Cross800fe132019-02-11 14:21:24 -0800416 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800417 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
418 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800419 Flag("--generate-debug-info").
420 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700421 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000422 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800423 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000424 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800425 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000426 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800427 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800428 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800429 FlagWithArg("--no-inline-from=", "core-oj.jar").
Ulya Trafimovichc0c98d52020-03-09 12:46:06 +0000430 Flag("--force-determinism").
Colin Cross800fe132019-02-11 14:21:24 -0800431 Flag("--abort-on-hard-verifier-error")
432
David Srbecky7f8dac12020-02-13 16:00:45 +0000433 // Use the default variant/features for host builds.
434 // The map below contains only device CPU info (which might be x86 on some devices).
435 if image.target.Os == android.Android {
436 cmd.FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch])
437 cmd.FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch])
438 }
439
Colin Cross44df5812019-02-15 23:06:46 -0800440 if global.BootFlags != "" {
441 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800442 }
443
444 if extraFlags != "" {
445 cmd.Flag(extraFlags)
446 }
447
Colin Cross0b9f31f2019-02-28 11:00:01 -0800448 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800449
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000450 installDir := filepath.Join("/", image.installSubdir, arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800451
Colin Cross800fe132019-02-11 14:21:24 -0800452 var vdexInstalls android.RuleBuilderInstalls
453 var unstrippedInstalls android.RuleBuilderInstalls
454
Colin Crossdf8eebe2019-04-09 15:29:41 -0700455 var zipFiles android.WritablePaths
456
Dan Willemsen0f416782019-06-13 21:44:53 +0000457 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
458 cmd.ImplicitOutput(artOrOat)
459 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800460
Dan Willemsen0f416782019-06-13 21:44:53 +0000461 // Install the .oat and .art files
462 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
463 }
Colin Cross800fe132019-02-11 14:21:24 -0800464
Dan Willemsen0f416782019-06-13 21:44:53 +0000465 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
466 cmd.ImplicitOutput(vdex)
467 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800468
David Srbecky7f8dac12020-02-13 16:00:45 +0000469 // Note that the vdex files are identical between architectures.
470 // Make rules will create symlinks to share them between architectures.
Colin Cross800fe132019-02-11 14:21:24 -0800471 vdexInstalls = append(vdexInstalls,
David Srbecky7f8dac12020-02-13 16:00:45 +0000472 android.RuleBuilderInstall{vdex, filepath.Join(installDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000473 }
474
475 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
476 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800477
478 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
479 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800480 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800481 }
482
David Srbecky7f8dac12020-02-13 16:00:45 +0000483 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+image.target.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800484
485 // save output and installed files for makevars
David Srbeckyc177ebe2020-02-18 20:43:06 +0000486 image.installs = rule.Installs()
487 image.vdexInstalls = vdexInstalls
488 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700489
490 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800491}
492
493const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
494It is likely that the boot classpath is inconsistent.
495Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
496
David Srbeckyc177ebe2020-02-18 20:43:06 +0000497func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000498 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000499 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000500
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700501 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000502 return nil
503 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000504 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000505 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800506
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000507 rule := android.NewRuleBuilder()
508 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800509
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000510 var bootImageProfile android.Path
511 if len(global.BootImageProfiles) > 1 {
512 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
513 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
514 bootImageProfile = combinedBootImageProfile
515 } else if len(global.BootImageProfiles) == 1 {
516 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000517 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
518 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000519 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000520 // No profile (not even a default one, which is the case on some branches
521 // like master-art-host that don't have frameworks/base).
522 // Return nil and continue without profile.
523 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000524 }
Colin Cross800fe132019-02-11 14:21:24 -0800525
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000526 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800527
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000528 rule.Command().
529 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000530 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000531 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000532 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
533 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000534 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800535
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000536 rule.Install(profile, "/system/etc/boot-image.prof")
537
538 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
539
540 image.profileInstalls = rule.Installs()
541
542 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000543 })
544 if profile == nil {
545 return nil // wrap nil into a typed pointer with value nil
546 }
547 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800548}
549
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000550var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
551
David Srbeckyc177ebe2020-02-18 20:43:06 +0000552func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000553 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000554 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100555
556 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
557 return nil
558 }
559 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100560 rule := android.NewRuleBuilder()
561 rule.MissingDeps(missingDeps)
562
563 // Some branches like master-art-host don't have frameworks/base, so manually
564 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
565 // and if they do they'll get a missing deps error.
566 defaultProfile := "frameworks/base/config/boot-profile.txt"
567 path := android.ExistentPathForSource(ctx, defaultProfile)
568 var bootFrameworkProfile android.Path
569 if path.Valid() {
570 bootFrameworkProfile = path.Path()
571 } else {
572 missingDeps = append(missingDeps, defaultProfile)
573 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
574 }
575
576 profile := image.dir.Join(ctx, "boot.bprof")
577
578 rule.Command().
579 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000580 Tool(globalSoong.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100581 Flag("--generate-boot-profile").
582 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000583 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
584 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100585 FlagWithOutput("--reference-profile-file=", profile)
586
587 rule.Install(profile, "/system/etc/boot-image.bprof")
588 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
589 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
590
591 return profile
592 }).(android.WritablePath)
593}
594
595var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
596
David Srbeckyc177ebe2020-02-18 20:43:06 +0000597func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800598 var allPhonies android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000599 for _, image := range image.variants {
600 arch := image.target.Arch.ArchType
David Srbecky46672322020-03-16 13:27:55 +0000601 suffix := arch.String()
602 // Host and target might both use x86 arch. We need to ensure the names are unique.
603 if image.target.Os.Class == android.Host {
604 suffix = "host-" + suffix
605 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800606 // Create a rule to call oatdump.
David Srbecky7f8dac12020-02-13 16:00:45 +0000607 output := android.PathForOutput(ctx, "boot."+suffix+".oatdump.txt")
Colin Crossc9a4c362019-02-26 21:13:48 -0800608 rule := android.NewRuleBuilder()
609 rule.Command().
610 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700611 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000612 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
613 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbeckyc177ebe2020-02-18 20:43:06 +0000614 FlagWithArg("--image=", strings.Join(image.imageLocations, ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800615 FlagWithOutput("--output=", output).
616 FlagWithArg("--instruction-set=", arch.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000617 rule.Build(pctx, ctx, "dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800618
619 // Create a phony rule that depends on the output file and prints the path.
David Srbecky7f8dac12020-02-13 16:00:45 +0000620 phony := android.PathForPhony(ctx, "dump-oat-boot-"+suffix)
Colin Crossc9a4c362019-02-26 21:13:48 -0800621 rule = android.NewRuleBuilder()
622 rule.Command().
623 Implicit(output).
624 ImplicitOutput(phony).
625 Text("echo").FlagWithArg("Output in ", output.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000626 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800627
David Srbecky46672322020-03-16 13:27:55 +0000628 // TODO: We need to make imageLocations per-variant to make oatdump work on host.
629 if image.target.Os == android.Android {
630 allPhonies = append(allPhonies, phony)
631 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800632 }
633
634 phony := android.PathForPhony(ctx, "dump-oat-boot")
635 ctx.Build(pctx, android.BuildParams{
636 Rule: android.Phony,
637 Output: phony,
638 Inputs: allPhonies,
639 Description: "dump-oat-boot",
640 })
641
642}
643
Colin Cross2d00f0d2019-05-09 21:50:00 -0700644func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000645 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700646
647 ctx.Build(pctx, android.BuildParams{
648 Rule: android.WriteFile,
649 Output: path,
650 Args: map[string]string{
651 "content": string(data),
652 },
653 })
654}
655
Colin Cross44df5812019-02-15 23:06:46 -0800656// Export paths for default boot image to Make
657func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700658 if d.dexpreoptConfigForMake != nil {
659 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000660 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700661 }
662
Colin Cross44df5812019-02-15 23:06:46 -0800663 image := d.defaultBootImage
664 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800665 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000666 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
667 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000668
669 var imageNames []string
670 for _, current := range append(d.otherImages, image) {
671 imageNames = append(imageNames, current.name)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000672 for _, current := range current.variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000673 suffix := ""
674 if current.target.Os.Class == android.Host {
675 suffix = "_host"
676 }
677 sfx := current.name + suffix + "_" + current.target.Arch.ArchType.String()
David Srbeckyc177ebe2020-02-18 20:43:06 +0000678 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, current.vdexInstalls.String())
679 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, current.images.String())
680 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(current.imagesDeps.Strings(), " "))
681 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, current.installs.String())
682 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, current.unstrippedInstalls.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000683 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800684
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000685 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(current.imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800686 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000687 }
688 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800689 }
Colin Cross800fe132019-02-11 14:21:24 -0800690}