blob: 4db62b3479e28238cd4ea7b6cb6ef5bb279c9041 [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
David Srbeckyc177ebe2020-02-18 20:43:06 +000031// Target-independent description of pre-compiled boot image.
Colin Cross44df5812019-02-15 23:06:46 -080032type bootImageConfig struct {
David Srbecky1aacc6c2020-03-26 11:10:45 +000033 // If this image is an extension, the image that it extends.
34 extends *bootImageConfig
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000035
36 // Image name (used in directory names and ninja rule names).
37 name string
38
39 // Basename of the image: the resulting filenames are <stem>[-<jar>].{art,oat,vdex}.
40 stem string
41
42 // Output directory for the image files.
43 dir android.OutputPath
44
45 // Output directory for the image files with debug symbols.
46 symbolsDir android.OutputPath
47
48 // Subdirectory where the image files are installed.
49 installSubdir string
50
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000051 // The names of jars that constitute this image.
52 modules []string
53
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000054 // File paths to jars.
55 dexPaths android.WritablePaths // for this image
56 dexPathsDeps android.WritablePaths // for the dependency images and in this image
57
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000058 // File path to a zip archive with all image files (or nil, if not needed).
59 zip android.WritablePath
David Srbeckyc177ebe2020-02-18 20:43:06 +000060
61 // Rules which should be used in make to install the outputs.
62 profileInstalls android.RuleBuilderInstalls
63
64 // Target-dependent fields.
65 variants []*bootImageVariant
66}
67
68// Target-dependent description of pre-compiled boot image.
69type bootImageVariant struct {
70 *bootImageConfig
71
72 // Target for which the image is generated.
73 target android.Target
74
David Srbeckyab994982020-03-30 17:24:13 +010075 // The "locations" of jars.
76 dexLocations []string // for this image
77 dexLocationsDeps []string // for the dependency images and in this image
78
David Srbeckyc177ebe2020-02-18 20:43:06 +000079 // Paths to image files.
80 images android.OutputPath // first image file
81 imagesDeps android.OutputPaths // all files
82
83 // Only for extensions, paths to the primary boot images.
84 primaryImages android.OutputPath
85
86 // Rules which should be used in make to install the outputs.
87 installs android.RuleBuilderInstalls
88 vdexInstalls android.RuleBuilderInstalls
89 unstrippedInstalls android.RuleBuilderInstalls
90}
91
92func (image bootImageConfig) getVariant(target android.Target) *bootImageVariant {
93 for _, variant := range image.variants {
94 if variant.target.Os == target.Os && variant.target.Arch.ArchType == target.Arch.ArchType {
95 return variant
96 }
97 }
98 return nil
Colin Cross800fe132019-02-11 14:21:24 -080099}
100
David Srbeckyab994982020-03-30 17:24:13 +0100101// Return any (the first) variant which is for the device (as opposed to for the host)
102func (image bootImageConfig) getAnyAndroidVariant() *bootImageVariant {
103 for _, variant := range image.variants {
104 if variant.target.Os == android.Android {
105 return variant
106 }
107 }
108 return nil
109}
110
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000111func (image bootImageConfig) moduleName(idx int) string {
112 // Dexpreopt on the boot class path produces multiple files. The first dex file
113 // is converted into 'name'.art (to match the legacy assumption that 'name'.art
Dan Willemsen0f416782019-06-13 21:44:53 +0000114 // exists), and the rest are converted to 'name'-<jar>.art.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000115 m := image.modules[idx]
116 name := image.stem
David Srbecky1aacc6c2020-03-26 11:10:45 +0000117 if idx != 0 || image.extends != nil {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000118 name += "-" + stemOf(m)
119 }
120 return name
121}
Dan Willemsen0f416782019-06-13 21:44:53 +0000122
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000123func (image bootImageConfig) firstModuleNameOrStem() string {
124 if len(image.modules) > 0 {
125 return image.moduleName(0)
126 } else {
127 return image.stem
128 }
129}
130
131func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths {
132 ret := make(android.OutputPaths, 0, len(image.modules)*len(exts))
133 for i := range image.modules {
134 name := image.moduleName(i)
Dan Willemsen0f416782019-06-13 21:44:53 +0000135 for _, ext := range exts {
136 ret = append(ret, dir.Join(ctx, name+ext))
137 }
138 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000139 return ret
140}
141
David Srbecky1aacc6c2020-03-26 11:10:45 +0000142// The image "location" is a symbolic path that, with multiarchitecture support, doesn't really
143// exist on the device. Typically it is /apex/com.android.art/javalib/boot.art and should be the
144// same for all supported architectures on the device. The concrete architecture specific files
145// actually end up in architecture-specific sub-directory such as arm, arm64, x86, or x86_64.
146//
147// For example a physical file
148// "/apex/com.android.art/javalib/x86/boot.art" has "image location"
149// "/apex/com.android.art/javalib/boot.art" (which is not an actual file).
150//
151// The location is passed as an argument to the ART tools like dex2oat instead of the real path.
152// ART tools will then reconstruct the architecture-specific real path.
153func (image *bootImageVariant) imageLocations() (imageLocations []string) {
154 if image.extends != nil {
155 imageLocations = image.extends.getVariant(image.target).imageLocations()
156 }
157 return append(imageLocations, dexpreopt.PathToLocation(image.images, image.target.Arch.ArchType))
158}
159
Colin Cross800fe132019-02-11 14:21:24 -0800160func concat(lists ...[]string) []string {
161 var size int
162 for _, l := range lists {
163 size += len(l)
164 }
165 ret := make([]string, 0, size)
166 for _, l := range lists {
167 ret = append(ret, l...)
168 }
169 return ret
170}
171
Colin Cross800fe132019-02-11 14:21:24 -0800172func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800173 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800174}
175
176func skipDexpreoptBootJars(ctx android.PathContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000177 if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000178 return true
179 }
180
Colin Cross800fe132019-02-11 14:21:24 -0800181 if ctx.Config().UnbundledBuild() {
182 return true
183 }
184
Colin Cross800fe132019-02-11 14:21:24 -0800185 return false
186}
187
Colin Cross44df5812019-02-15 23:06:46 -0800188type dexpreoptBootJars struct {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000189 defaultBootImage *bootImageConfig
190 otherImages []*bootImageConfig
Colin Cross2d00f0d2019-05-09 21:50:00 -0700191
192 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800193}
Colin Cross800fe132019-02-11 14:21:24 -0800194
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000195// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000196func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000197 if skipDexpreoptBootJars(ctx) {
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000198 return nil
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000199 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000200 // Include dexpreopt files for the primary boot image.
201 files := map[android.ArchType]android.OutputPaths{}
202 for _, variant := range artBootImageConfig(ctx).variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000203 // 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 +0000204 if variant.target.Os == android.Android {
205 files[variant.target.Arch.ArchType] = variant.imagesDeps
David Srbecky7f8dac12020-02-13 16:00:45 +0000206 }
David Srbeckyc177ebe2020-02-18 20:43:06 +0000207 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000208 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000209}
210
Colin Cross800fe132019-02-11 14:21:24 -0800211// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800212func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800213 if skipDexpreoptBootJars(ctx) {
214 return
215 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000216 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
217 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
218 return
219 }
Colin Cross800fe132019-02-11 14:21:24 -0800220
Colin Cross2d00f0d2019-05-09 21:50:00 -0700221 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
222 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
223
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000224 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800225
226 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
227 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
228 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
229 // on ASAN settings.
230 if len(ctx.Config().SanitizeDevice()) == 1 &&
231 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800232 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800233 return
234 }
235
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000236 // Always create the default boot image first, to get a unique profile rule for all images.
237 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000238 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
239 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800240
241 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800242}
243
David Srbeckyc177ebe2020-02-18 20:43:06 +0000244// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
245func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Colin Cross44df5812019-02-15 23:06:46 -0800246 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800247 ctx.VisitAllModules(func(module android.Module) {
248 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800249 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800250 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800251 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800252 bootDexJars[i] = j.DexJar()
253 }
254 }
255 })
256
257 var missingDeps []string
258 // Ensure all modules were converted to paths
259 for i := range bootDexJars {
260 if bootDexJars[i] == nil {
261 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800262 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800263 bootDexJars[i] = android.PathForOutput(ctx, "missing")
264 } else {
265 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800266 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800267 }
268 }
269 }
270
271 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
272 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
273 // already been set up can find them.
274 for i := range bootDexJars {
275 ctx.Build(pctx, android.BuildParams{
276 Rule: android.Cp,
277 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800278 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800279 })
280 }
281
Colin Cross44df5812019-02-15 23:06:46 -0800282 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100283 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800284
Colin Crossdf8eebe2019-04-09 15:29:41 -0700285 var allFiles android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000286 for _, variant := range image.variants {
287 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000288 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800289 }
Colin Cross44df5812019-02-15 23:06:46 -0800290
Colin Crossdf8eebe2019-04-09 15:29:41 -0700291 if image.zip != nil {
292 rule := android.NewRuleBuilder()
293 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700294 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700295 FlagWithOutput("-o ", image.zip).
296 FlagWithArg("-C ", image.dir.String()).
297 FlagWithInputList("-f ", allFiles, " -f ")
298
299 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
300 }
301
Colin Cross44df5812019-02-15 23:06:46 -0800302 return image
Colin Cross800fe132019-02-11 14:21:24 -0800303}
304
David Srbeckyc177ebe2020-02-18 20:43:06 +0000305func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
306 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800307
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000308 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000309 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800310
David Srbeckyc177ebe2020-02-18 20:43:06 +0000311 arch := image.target.Arch.ArchType
David Srbecky7f8dac12020-02-13 16:00:45 +0000312 os := image.target.Os.String() // We need to distinguish host-x86 and device-x86.
313 symbolsDir := image.symbolsDir.Join(ctx, os, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000314 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
David Srbecky7f8dac12020-02-13 16:00:45 +0000315 outputDir := image.dir.Join(ctx, os, image.installSubdir, arch.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000316 outputPath := outputDir.Join(ctx, image.stem+".oat")
317 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
318 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800319
320 rule := android.NewRuleBuilder()
321 rule.MissingDeps(missingDeps)
322
323 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
324 rule.Command().Text("rm").Flag("-f").
325 Flag(symbolsDir.Join(ctx, "*.art").String()).
326 Flag(symbolsDir.Join(ctx, "*.oat").String()).
327 Flag(symbolsDir.Join(ctx, "*.invocation").String())
328 rule.Command().Text("rm").Flag("-f").
329 Flag(outputDir.Join(ctx, "*.art").String()).
330 Flag(outputDir.Join(ctx, "*.oat").String()).
331 Flag(outputDir.Join(ctx, "*.invocation").String())
332
333 cmd := rule.Command()
334
335 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
336 if extraFlags == "" {
337 // Use ANDROID_LOG_TAGS to suppress most logging by default...
338 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
339 } else {
340 // ...unless the boot image is generated specifically for testing, then allow all logging.
341 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
342 }
343
344 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
345
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000346 cmd.Tool(globalSoong.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800347 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800348 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800349 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
350 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800351
Colin Cross69f59a32019-02-15 10:39:37 -0800352 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800353 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800354 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800355 }
356
Colin Cross44df5812019-02-15 23:06:46 -0800357 if global.DirtyImageObjects.Valid() {
358 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800359 }
360
David Srbecky1aacc6c2020-03-26 11:10:45 +0000361 if image.extends != nil {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000362 artImage := image.primaryImages
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000363 cmd.
364 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
365 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
366 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
367 } else {
368 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
369 }
370
Colin Cross800fe132019-02-11 14:21:24 -0800371 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800372 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
373 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800374 Flag("--generate-debug-info").
375 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700376 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000377 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800378 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000379 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800380 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000381 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800382 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800383 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800384 FlagWithArg("--no-inline-from=", "core-oj.jar").
Ulya Trafimovichc0c98d52020-03-09 12:46:06 +0000385 Flag("--force-determinism").
Colin Cross800fe132019-02-11 14:21:24 -0800386 Flag("--abort-on-hard-verifier-error")
387
David Srbecky7f8dac12020-02-13 16:00:45 +0000388 // Use the default variant/features for host builds.
389 // The map below contains only device CPU info (which might be x86 on some devices).
390 if image.target.Os == android.Android {
391 cmd.FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch])
392 cmd.FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch])
393 }
394
Colin Cross44df5812019-02-15 23:06:46 -0800395 if global.BootFlags != "" {
396 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800397 }
398
399 if extraFlags != "" {
400 cmd.Flag(extraFlags)
401 }
402
Colin Cross0b9f31f2019-02-28 11:00:01 -0800403 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800404
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000405 installDir := filepath.Join("/", image.installSubdir, arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800406
Colin Cross800fe132019-02-11 14:21:24 -0800407 var vdexInstalls android.RuleBuilderInstalls
408 var unstrippedInstalls android.RuleBuilderInstalls
409
Colin Crossdf8eebe2019-04-09 15:29:41 -0700410 var zipFiles android.WritablePaths
411
Dan Willemsen0f416782019-06-13 21:44:53 +0000412 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
413 cmd.ImplicitOutput(artOrOat)
414 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800415
Dan Willemsen0f416782019-06-13 21:44:53 +0000416 // Install the .oat and .art files
417 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
418 }
Colin Cross800fe132019-02-11 14:21:24 -0800419
Dan Willemsen0f416782019-06-13 21:44:53 +0000420 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
421 cmd.ImplicitOutput(vdex)
422 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800423
David Srbecky7f8dac12020-02-13 16:00:45 +0000424 // Note that the vdex files are identical between architectures.
425 // Make rules will create symlinks to share them between architectures.
Colin Cross800fe132019-02-11 14:21:24 -0800426 vdexInstalls = append(vdexInstalls,
David Srbecky7f8dac12020-02-13 16:00:45 +0000427 android.RuleBuilderInstall{vdex, filepath.Join(installDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000428 }
429
430 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
431 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800432
433 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
434 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800435 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800436 }
437
David Srbecky7f8dac12020-02-13 16:00:45 +0000438 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+image.target.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800439
440 // save output and installed files for makevars
David Srbeckyc177ebe2020-02-18 20:43:06 +0000441 image.installs = rule.Installs()
442 image.vdexInstalls = vdexInstalls
443 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700444
445 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800446}
447
448const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
449It is likely that the boot classpath is inconsistent.
450Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
451
David Srbeckyc177ebe2020-02-18 20:43:06 +0000452func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000453 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000454 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000455
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700456 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000457 return nil
458 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000459 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000460 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800461
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000462 rule := android.NewRuleBuilder()
463 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800464
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000465 var bootImageProfile android.Path
466 if len(global.BootImageProfiles) > 1 {
467 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
468 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
469 bootImageProfile = combinedBootImageProfile
470 } else if len(global.BootImageProfiles) == 1 {
471 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000472 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
473 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000474 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000475 // No profile (not even a default one, which is the case on some branches
476 // like master-art-host that don't have frameworks/base).
477 // Return nil and continue without profile.
478 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000479 }
Colin Cross800fe132019-02-11 14:21:24 -0800480
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000481 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800482
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000483 rule.Command().
484 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000485 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000486 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000487 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
David Srbeckyab994982020-03-30 17:24:13 +0100488 FlagForEachArg("--dex-location=", image.getAnyAndroidVariant().dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000489 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800490
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000491 rule.Install(profile, "/system/etc/boot-image.prof")
492
493 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
494
495 image.profileInstalls = rule.Installs()
496
497 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000498 })
499 if profile == nil {
500 return nil // wrap nil into a typed pointer with value nil
501 }
502 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800503}
504
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000505var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
506
David Srbeckyc177ebe2020-02-18 20:43:06 +0000507func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000508 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000509 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100510
511 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
512 return nil
513 }
514 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100515 rule := android.NewRuleBuilder()
516 rule.MissingDeps(missingDeps)
517
518 // Some branches like master-art-host don't have frameworks/base, so manually
519 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
520 // and if they do they'll get a missing deps error.
521 defaultProfile := "frameworks/base/config/boot-profile.txt"
522 path := android.ExistentPathForSource(ctx, defaultProfile)
523 var bootFrameworkProfile android.Path
524 if path.Valid() {
525 bootFrameworkProfile = path.Path()
526 } else {
527 missingDeps = append(missingDeps, defaultProfile)
528 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
529 }
530
531 profile := image.dir.Join(ctx, "boot.bprof")
532
533 rule.Command().
534 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000535 Tool(globalSoong.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100536 Flag("--generate-boot-profile").
537 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000538 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
David Srbeckyab994982020-03-30 17:24:13 +0100539 FlagForEachArg("--dex-location=", image.getAnyAndroidVariant().dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100540 FlagWithOutput("--reference-profile-file=", profile)
541
542 rule.Install(profile, "/system/etc/boot-image.bprof")
543 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
544 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
545
546 return profile
547 }).(android.WritablePath)
548}
549
550var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
551
David Srbeckyc177ebe2020-02-18 20:43:06 +0000552func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800553 var allPhonies android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000554 for _, image := range image.variants {
555 arch := image.target.Arch.ArchType
David Srbecky46672322020-03-16 13:27:55 +0000556 suffix := arch.String()
557 // Host and target might both use x86 arch. We need to ensure the names are unique.
558 if image.target.Os.Class == android.Host {
559 suffix = "host-" + suffix
560 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800561 // Create a rule to call oatdump.
David Srbecky7f8dac12020-02-13 16:00:45 +0000562 output := android.PathForOutput(ctx, "boot."+suffix+".oatdump.txt")
Colin Crossc9a4c362019-02-26 21:13:48 -0800563 rule := android.NewRuleBuilder()
564 rule.Command().
565 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700566 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000567 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
568 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbecky1aacc6c2020-03-26 11:10:45 +0000569 FlagWithArg("--image=", strings.Join(image.imageLocations(), ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800570 FlagWithOutput("--output=", output).
571 FlagWithArg("--instruction-set=", arch.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000572 rule.Build(pctx, ctx, "dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800573
574 // Create a phony rule that depends on the output file and prints the path.
David Srbecky7f8dac12020-02-13 16:00:45 +0000575 phony := android.PathForPhony(ctx, "dump-oat-boot-"+suffix)
Colin Crossc9a4c362019-02-26 21:13:48 -0800576 rule = android.NewRuleBuilder()
577 rule.Command().
578 Implicit(output).
579 ImplicitOutput(phony).
580 Text("echo").FlagWithArg("Output in ", output.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000581 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800582
David Srbecky1aacc6c2020-03-26 11:10:45 +0000583 allPhonies = append(allPhonies, phony)
Colin Crossc9a4c362019-02-26 21:13:48 -0800584 }
585
586 phony := android.PathForPhony(ctx, "dump-oat-boot")
587 ctx.Build(pctx, android.BuildParams{
588 Rule: android.Phony,
589 Output: phony,
590 Inputs: allPhonies,
591 Description: "dump-oat-boot",
592 })
593
594}
595
Colin Cross2d00f0d2019-05-09 21:50:00 -0700596func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000597 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700598
599 ctx.Build(pctx, android.BuildParams{
600 Rule: android.WriteFile,
601 Output: path,
602 Args: map[string]string{
603 "content": string(data),
604 },
605 })
606}
607
Colin Cross44df5812019-02-15 23:06:46 -0800608// Export paths for default boot image to Make
609func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700610 if d.dexpreoptConfigForMake != nil {
611 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000612 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700613 }
614
Colin Cross44df5812019-02-15 23:06:46 -0800615 image := d.defaultBootImage
616 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800617 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000618 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
David Srbeckyab994982020-03-30 17:24:13 +0100619 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.getAnyAndroidVariant().dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000620
621 var imageNames []string
622 for _, current := range append(d.otherImages, image) {
623 imageNames = append(imageNames, current.name)
David Srbecky1aacc6c2020-03-26 11:10:45 +0000624 for _, variant := range current.variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000625 suffix := ""
David Srbecky1aacc6c2020-03-26 11:10:45 +0000626 if variant.target.Os.Class == android.Host {
David Srbecky7f8dac12020-02-13 16:00:45 +0000627 suffix = "_host"
628 }
David Srbecky1aacc6c2020-03-26 11:10:45 +0000629 sfx := variant.name + suffix + "_" + variant.target.Arch.ArchType.String()
630 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, variant.vdexInstalls.String())
631 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, variant.images.String())
632 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(variant.imagesDeps.Strings(), " "))
633 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, variant.installs.String())
634 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, variant.unstrippedInstalls.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000635 }
David Srbeckyab994982020-03-30 17:24:13 +0100636 imageLocations := current.getAnyAndroidVariant().imageLocations()
David Srbecky1aacc6c2020-03-26 11:10:45 +0000637 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800638 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000639 }
640 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800641 }
Colin Cross800fe132019-02-11 14:21:24 -0800642}