blob: ae3cd06cd33bef53e07149234455f105df40373c [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
54 // The "locations" of jars.
55 dexLocations []string // for this image
56 dexLocationsDeps []string // for the dependency images and in this image
57
58 // File paths to jars.
59 dexPaths android.WritablePaths // for this image
60 dexPathsDeps android.WritablePaths // for the dependency images and in this image
61
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000062 // File path to a zip archive with all image files (or nil, if not needed).
63 zip android.WritablePath
David Srbeckyc177ebe2020-02-18 20:43:06 +000064
65 // Rules which should be used in make to install the outputs.
66 profileInstalls android.RuleBuilderInstalls
67
68 // Target-dependent fields.
69 variants []*bootImageVariant
70}
71
72// Target-dependent description of pre-compiled boot image.
73type bootImageVariant struct {
74 *bootImageConfig
75
76 // Target for which the image is generated.
77 target android.Target
78
79 // 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
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000101func (image bootImageConfig) moduleName(idx int) string {
102 // Dexpreopt on the boot class path produces multiple files. The first dex file
103 // is converted into 'name'.art (to match the legacy assumption that 'name'.art
Dan Willemsen0f416782019-06-13 21:44:53 +0000104 // exists), and the rest are converted to 'name'-<jar>.art.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000105 m := image.modules[idx]
106 name := image.stem
David Srbecky1aacc6c2020-03-26 11:10:45 +0000107 if idx != 0 || image.extends != nil {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000108 name += "-" + stemOf(m)
109 }
110 return name
111}
Dan Willemsen0f416782019-06-13 21:44:53 +0000112
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000113func (image bootImageConfig) firstModuleNameOrStem() string {
114 if len(image.modules) > 0 {
115 return image.moduleName(0)
116 } else {
117 return image.stem
118 }
119}
120
121func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths {
122 ret := make(android.OutputPaths, 0, len(image.modules)*len(exts))
123 for i := range image.modules {
124 name := image.moduleName(i)
Dan Willemsen0f416782019-06-13 21:44:53 +0000125 for _, ext := range exts {
126 ret = append(ret, dir.Join(ctx, name+ext))
127 }
128 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000129 return ret
130}
131
David Srbecky1aacc6c2020-03-26 11:10:45 +0000132// The image "location" is a symbolic path that, with multiarchitecture support, doesn't really
133// exist on the device. Typically it is /apex/com.android.art/javalib/boot.art and should be the
134// same for all supported architectures on the device. The concrete architecture specific files
135// actually end up in architecture-specific sub-directory such as arm, arm64, x86, or x86_64.
136//
137// For example a physical file
138// "/apex/com.android.art/javalib/x86/boot.art" has "image location"
139// "/apex/com.android.art/javalib/boot.art" (which is not an actual file).
140//
141// The location is passed as an argument to the ART tools like dex2oat instead of the real path.
142// ART tools will then reconstruct the architecture-specific real path.
143func (image *bootImageVariant) imageLocations() (imageLocations []string) {
144 if image.extends != nil {
145 imageLocations = image.extends.getVariant(image.target).imageLocations()
146 }
147 return append(imageLocations, dexpreopt.PathToLocation(image.images, image.target.Arch.ArchType))
148}
149
Colin Cross800fe132019-02-11 14:21:24 -0800150func concat(lists ...[]string) []string {
151 var size int
152 for _, l := range lists {
153 size += len(l)
154 }
155 ret := make([]string, 0, size)
156 for _, l := range lists {
157 ret = append(ret, l...)
158 }
159 return ret
160}
161
Colin Cross800fe132019-02-11 14:21:24 -0800162func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800163 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800164}
165
166func skipDexpreoptBootJars(ctx android.PathContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000167 if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000168 return true
169 }
170
Colin Cross800fe132019-02-11 14:21:24 -0800171 if ctx.Config().UnbundledBuild() {
172 return true
173 }
174
Colin Cross800fe132019-02-11 14:21:24 -0800175 return false
176}
177
Colin Cross44df5812019-02-15 23:06:46 -0800178type dexpreoptBootJars struct {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000179 defaultBootImage *bootImageConfig
180 otherImages []*bootImageConfig
Colin Cross2d00f0d2019-05-09 21:50:00 -0700181
182 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800183}
Colin Cross800fe132019-02-11 14:21:24 -0800184
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000185// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000186func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000187 if skipDexpreoptBootJars(ctx) {
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000188 return nil
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000189 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000190 // Include dexpreopt files for the primary boot image.
191 files := map[android.ArchType]android.OutputPaths{}
192 for _, variant := range artBootImageConfig(ctx).variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000193 // 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 +0000194 if variant.target.Os == android.Android {
195 files[variant.target.Arch.ArchType] = variant.imagesDeps
David Srbecky7f8dac12020-02-13 16:00:45 +0000196 }
David Srbeckyc177ebe2020-02-18 20:43:06 +0000197 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000198 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000199}
200
Colin Cross800fe132019-02-11 14:21:24 -0800201// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800202func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800203 if skipDexpreoptBootJars(ctx) {
204 return
205 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000206 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
207 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
208 return
209 }
Colin Cross800fe132019-02-11 14:21:24 -0800210
Colin Cross2d00f0d2019-05-09 21:50:00 -0700211 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
212 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
213
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000214 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800215
216 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
217 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
218 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
219 // on ASAN settings.
220 if len(ctx.Config().SanitizeDevice()) == 1 &&
221 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800222 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800223 return
224 }
225
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000226 // Always create the default boot image first, to get a unique profile rule for all images.
227 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000228 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
229 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800230
231 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800232}
233
David Srbeckyc177ebe2020-02-18 20:43:06 +0000234// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
235func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Colin Cross44df5812019-02-15 23:06:46 -0800236 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800237 ctx.VisitAllModules(func(module android.Module) {
238 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800239 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800240 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800241 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800242 bootDexJars[i] = j.DexJar()
243 }
244 }
245 })
246
247 var missingDeps []string
248 // Ensure all modules were converted to paths
249 for i := range bootDexJars {
250 if bootDexJars[i] == nil {
251 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800252 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800253 bootDexJars[i] = android.PathForOutput(ctx, "missing")
254 } else {
255 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800256 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800257 }
258 }
259 }
260
261 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
262 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
263 // already been set up can find them.
264 for i := range bootDexJars {
265 ctx.Build(pctx, android.BuildParams{
266 Rule: android.Cp,
267 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800268 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800269 })
270 }
271
Colin Cross44df5812019-02-15 23:06:46 -0800272 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100273 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800274
Colin Crossdf8eebe2019-04-09 15:29:41 -0700275 var allFiles android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000276 for _, variant := range image.variants {
277 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000278 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800279 }
Colin Cross44df5812019-02-15 23:06:46 -0800280
Colin Crossdf8eebe2019-04-09 15:29:41 -0700281 if image.zip != nil {
282 rule := android.NewRuleBuilder()
283 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700284 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700285 FlagWithOutput("-o ", image.zip).
286 FlagWithArg("-C ", image.dir.String()).
287 FlagWithInputList("-f ", allFiles, " -f ")
288
289 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
290 }
291
Colin Cross44df5812019-02-15 23:06:46 -0800292 return image
Colin Cross800fe132019-02-11 14:21:24 -0800293}
294
David Srbeckyc177ebe2020-02-18 20:43:06 +0000295func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
296 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800297
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000298 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000299 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800300
David Srbeckyc177ebe2020-02-18 20:43:06 +0000301 arch := image.target.Arch.ArchType
David Srbecky7f8dac12020-02-13 16:00:45 +0000302 os := image.target.Os.String() // We need to distinguish host-x86 and device-x86.
303 symbolsDir := image.symbolsDir.Join(ctx, os, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000304 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
David Srbecky7f8dac12020-02-13 16:00:45 +0000305 outputDir := image.dir.Join(ctx, os, image.installSubdir, arch.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000306 outputPath := outputDir.Join(ctx, image.stem+".oat")
307 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
308 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800309
310 rule := android.NewRuleBuilder()
311 rule.MissingDeps(missingDeps)
312
313 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
314 rule.Command().Text("rm").Flag("-f").
315 Flag(symbolsDir.Join(ctx, "*.art").String()).
316 Flag(symbolsDir.Join(ctx, "*.oat").String()).
317 Flag(symbolsDir.Join(ctx, "*.invocation").String())
318 rule.Command().Text("rm").Flag("-f").
319 Flag(outputDir.Join(ctx, "*.art").String()).
320 Flag(outputDir.Join(ctx, "*.oat").String()).
321 Flag(outputDir.Join(ctx, "*.invocation").String())
322
323 cmd := rule.Command()
324
325 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
326 if extraFlags == "" {
327 // Use ANDROID_LOG_TAGS to suppress most logging by default...
328 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
329 } else {
330 // ...unless the boot image is generated specifically for testing, then allow all logging.
331 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
332 }
333
334 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
335
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000336 cmd.Tool(globalSoong.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800337 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800338 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800339 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
340 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800341
Colin Cross69f59a32019-02-15 10:39:37 -0800342 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800343 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800344 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800345 }
346
Colin Cross44df5812019-02-15 23:06:46 -0800347 if global.DirtyImageObjects.Valid() {
348 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800349 }
350
David Srbecky1aacc6c2020-03-26 11:10:45 +0000351 if image.extends != nil {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000352 artImage := image.primaryImages
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000353 cmd.
354 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
355 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
356 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
357 } else {
358 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
359 }
360
Colin Cross800fe132019-02-11 14:21:24 -0800361 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800362 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
363 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800364 Flag("--generate-debug-info").
365 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700366 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000367 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800368 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000369 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800370 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000371 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800372 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800373 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800374 FlagWithArg("--no-inline-from=", "core-oj.jar").
Ulya Trafimovichc0c98d52020-03-09 12:46:06 +0000375 Flag("--force-determinism").
Colin Cross800fe132019-02-11 14:21:24 -0800376 Flag("--abort-on-hard-verifier-error")
377
David Srbecky7f8dac12020-02-13 16:00:45 +0000378 // Use the default variant/features for host builds.
379 // The map below contains only device CPU info (which might be x86 on some devices).
380 if image.target.Os == android.Android {
381 cmd.FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch])
382 cmd.FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch])
383 }
384
Colin Cross44df5812019-02-15 23:06:46 -0800385 if global.BootFlags != "" {
386 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800387 }
388
389 if extraFlags != "" {
390 cmd.Flag(extraFlags)
391 }
392
Colin Cross0b9f31f2019-02-28 11:00:01 -0800393 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800394
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000395 installDir := filepath.Join("/", image.installSubdir, arch.String())
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
David Srbecky7f8dac12020-02-13 16:00:45 +0000414 // Note that the vdex files are identical between architectures.
415 // Make rules will create symlinks to share them between architectures.
Colin Cross800fe132019-02-11 14:21:24 -0800416 vdexInstalls = append(vdexInstalls,
David Srbecky7f8dac12020-02-13 16:00:45 +0000417 android.RuleBuilderInstall{vdex, filepath.Join(installDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000418 }
419
420 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
421 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800422
423 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
424 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800425 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800426 }
427
David Srbecky7f8dac12020-02-13 16:00:45 +0000428 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+image.target.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800429
430 // save output and installed files for makevars
David Srbeckyc177ebe2020-02-18 20:43:06 +0000431 image.installs = rule.Installs()
432 image.vdexInstalls = vdexInstalls
433 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700434
435 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800436}
437
438const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
439It is likely that the boot classpath is inconsistent.
440Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
441
David Srbeckyc177ebe2020-02-18 20:43:06 +0000442func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000443 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000444 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000445
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700446 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000447 return nil
448 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000449 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000450 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800451
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000452 rule := android.NewRuleBuilder()
453 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800454
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000455 var bootImageProfile android.Path
456 if len(global.BootImageProfiles) > 1 {
457 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
458 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
459 bootImageProfile = combinedBootImageProfile
460 } else if len(global.BootImageProfiles) == 1 {
461 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000462 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
463 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000464 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000465 // No profile (not even a default one, which is the case on some branches
466 // like master-art-host that don't have frameworks/base).
467 // Return nil and continue without profile.
468 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000469 }
Colin Cross800fe132019-02-11 14:21:24 -0800470
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000471 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800472
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000473 rule.Command().
474 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000475 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000476 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000477 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
478 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000479 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800480
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000481 rule.Install(profile, "/system/etc/boot-image.prof")
482
483 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
484
485 image.profileInstalls = rule.Installs()
486
487 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000488 })
489 if profile == nil {
490 return nil // wrap nil into a typed pointer with value nil
491 }
492 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800493}
494
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000495var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
496
David Srbeckyc177ebe2020-02-18 20:43:06 +0000497func bootFrameworkProfileRule(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 Geoffraye7102422019-07-24 13:19:29 +0100500
501 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
502 return nil
503 }
504 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100505 rule := android.NewRuleBuilder()
506 rule.MissingDeps(missingDeps)
507
508 // Some branches like master-art-host don't have frameworks/base, so manually
509 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
510 // and if they do they'll get a missing deps error.
511 defaultProfile := "frameworks/base/config/boot-profile.txt"
512 path := android.ExistentPathForSource(ctx, defaultProfile)
513 var bootFrameworkProfile android.Path
514 if path.Valid() {
515 bootFrameworkProfile = path.Path()
516 } else {
517 missingDeps = append(missingDeps, defaultProfile)
518 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
519 }
520
521 profile := image.dir.Join(ctx, "boot.bprof")
522
523 rule.Command().
524 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000525 Tool(globalSoong.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100526 Flag("--generate-boot-profile").
527 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000528 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
529 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100530 FlagWithOutput("--reference-profile-file=", profile)
531
532 rule.Install(profile, "/system/etc/boot-image.bprof")
533 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
534 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
535
536 return profile
537 }).(android.WritablePath)
538}
539
540var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
541
David Srbeckyc177ebe2020-02-18 20:43:06 +0000542func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800543 var allPhonies android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000544 for _, image := range image.variants {
545 arch := image.target.Arch.ArchType
David Srbecky46672322020-03-16 13:27:55 +0000546 suffix := arch.String()
547 // Host and target might both use x86 arch. We need to ensure the names are unique.
548 if image.target.Os.Class == android.Host {
549 suffix = "host-" + suffix
550 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800551 // Create a rule to call oatdump.
David Srbecky7f8dac12020-02-13 16:00:45 +0000552 output := android.PathForOutput(ctx, "boot."+suffix+".oatdump.txt")
Colin Crossc9a4c362019-02-26 21:13:48 -0800553 rule := android.NewRuleBuilder()
554 rule.Command().
555 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700556 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000557 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
558 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbecky1aacc6c2020-03-26 11:10:45 +0000559 FlagWithArg("--image=", strings.Join(image.imageLocations(), ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800560 FlagWithOutput("--output=", output).
561 FlagWithArg("--instruction-set=", arch.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000562 rule.Build(pctx, ctx, "dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800563
564 // Create a phony rule that depends on the output file and prints the path.
David Srbecky7f8dac12020-02-13 16:00:45 +0000565 phony := android.PathForPhony(ctx, "dump-oat-boot-"+suffix)
Colin Crossc9a4c362019-02-26 21:13:48 -0800566 rule = android.NewRuleBuilder()
567 rule.Command().
568 Implicit(output).
569 ImplicitOutput(phony).
570 Text("echo").FlagWithArg("Output in ", output.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000571 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800572
David Srbecky1aacc6c2020-03-26 11:10:45 +0000573 allPhonies = append(allPhonies, phony)
Colin Crossc9a4c362019-02-26 21:13:48 -0800574 }
575
576 phony := android.PathForPhony(ctx, "dump-oat-boot")
577 ctx.Build(pctx, android.BuildParams{
578 Rule: android.Phony,
579 Output: phony,
580 Inputs: allPhonies,
581 Description: "dump-oat-boot",
582 })
583
584}
585
Colin Cross2d00f0d2019-05-09 21:50:00 -0700586func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000587 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700588
589 ctx.Build(pctx, android.BuildParams{
590 Rule: android.WriteFile,
591 Output: path,
592 Args: map[string]string{
593 "content": string(data),
594 },
595 })
596}
597
Colin Cross44df5812019-02-15 23:06:46 -0800598// Export paths for default boot image to Make
599func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700600 if d.dexpreoptConfigForMake != nil {
601 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000602 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700603 }
604
Colin Cross44df5812019-02-15 23:06:46 -0800605 image := d.defaultBootImage
606 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800607 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000608 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
609 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000610
611 var imageNames []string
612 for _, current := range append(d.otherImages, image) {
613 imageNames = append(imageNames, current.name)
David Srbecky1aacc6c2020-03-26 11:10:45 +0000614 imageLocations := []string{}
615 for _, variant := range current.variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000616 suffix := ""
David Srbecky1aacc6c2020-03-26 11:10:45 +0000617 if variant.target.Os.Class == android.Host {
David Srbecky7f8dac12020-02-13 16:00:45 +0000618 suffix = "_host"
619 }
David Srbecky1aacc6c2020-03-26 11:10:45 +0000620 sfx := variant.name + suffix + "_" + variant.target.Arch.ArchType.String()
621 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, variant.vdexInstalls.String())
622 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, variant.images.String())
623 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(variant.imagesDeps.Strings(), " "))
624 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, variant.installs.String())
625 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, variant.unstrippedInstalls.String())
626 if variant.target.Os == android.Android {
627 // The locations for all Android targets are identical. Pick one.
628 imageLocations = variant.imageLocations()
629 }
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000630 }
David Srbecky1aacc6c2020-03-26 11:10:45 +0000631 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800632 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000633 }
634 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800635 }
Colin Cross800fe132019-02-11 14:21:24 -0800636}