blob: e008f7f43feee0dd0703ff1e0064b21a97c45de2 [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"
Vladimir Marko205e6c22020-04-01 13:52:27 +010019 "sort"
Colin Cross800fe132019-02-11 14:21:24 -080020 "strings"
21
22 "android/soong/android"
23 "android/soong/dexpreopt"
24
Colin Cross800fe132019-02-11 14:21:24 -080025 "github.com/google/blueprint/proptools"
26)
27
28func init() {
29 android.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory)
30}
31
David Srbeckyc177ebe2020-02-18 20:43:06 +000032// Target-independent description of pre-compiled boot image.
Colin Cross44df5812019-02-15 23:06:46 -080033type bootImageConfig struct {
David Srbecky1aacc6c2020-03-26 11:10:45 +000034 // If this image is an extension, the image that it extends.
35 extends *bootImageConfig
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000036
37 // Image name (used in directory names and ninja rule names).
38 name string
39
40 // Basename of the image: the resulting filenames are <stem>[-<jar>].{art,oat,vdex}.
41 stem string
42
43 // Output directory for the image files.
44 dir android.OutputPath
45
46 // Output directory for the image files with debug symbols.
47 symbolsDir android.OutputPath
48
49 // Subdirectory where the image files are installed.
50 installSubdir string
51
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000052 // The names of jars that constitute this image.
53 modules []string
54
55 // The "locations" of jars.
56 dexLocations []string // for this image
57 dexLocationsDeps []string // for the dependency images and in this image
58
59 // File paths to jars.
60 dexPaths android.WritablePaths // for this image
61 dexPathsDeps android.WritablePaths // for the dependency images and in this image
62
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000063 // File path to a zip archive with all image files (or nil, if not needed).
64 zip android.WritablePath
David Srbeckyc177ebe2020-02-18 20:43:06 +000065
66 // Rules which should be used in make to install the outputs.
67 profileInstalls android.RuleBuilderInstalls
68
69 // Target-dependent fields.
70 variants []*bootImageVariant
71}
72
73// Target-dependent description of pre-compiled boot image.
74type bootImageVariant struct {
75 *bootImageConfig
76
77 // Target for which the image is generated.
78 target android.Target
79
80 // Paths to image files.
81 images android.OutputPath // first image file
82 imagesDeps android.OutputPaths // all files
83
84 // Only for extensions, paths to the primary boot images.
85 primaryImages android.OutputPath
86
87 // Rules which should be used in make to install the outputs.
88 installs android.RuleBuilderInstalls
89 vdexInstalls android.RuleBuilderInstalls
90 unstrippedInstalls android.RuleBuilderInstalls
91}
92
93func (image bootImageConfig) getVariant(target android.Target) *bootImageVariant {
94 for _, variant := range image.variants {
95 if variant.target.Os == target.Os && variant.target.Arch.ArchType == target.Arch.ArchType {
96 return variant
97 }
98 }
99 return nil
Colin Cross800fe132019-02-11 14:21:24 -0800100}
101
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000102func (image bootImageConfig) moduleName(idx int) string {
103 // Dexpreopt on the boot class path produces multiple files. The first dex file
104 // is converted into 'name'.art (to match the legacy assumption that 'name'.art
Dan Willemsen0f416782019-06-13 21:44:53 +0000105 // exists), and the rest are converted to 'name'-<jar>.art.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000106 m := image.modules[idx]
107 name := image.stem
David Srbecky1aacc6c2020-03-26 11:10:45 +0000108 if idx != 0 || image.extends != nil {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000109 name += "-" + stemOf(m)
110 }
111 return name
112}
Dan Willemsen0f416782019-06-13 21:44:53 +0000113
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000114func (image bootImageConfig) firstModuleNameOrStem() string {
115 if len(image.modules) > 0 {
116 return image.moduleName(0)
117 } else {
118 return image.stem
119 }
120}
121
122func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths {
123 ret := make(android.OutputPaths, 0, len(image.modules)*len(exts))
124 for i := range image.modules {
125 name := image.moduleName(i)
Dan Willemsen0f416782019-06-13 21:44:53 +0000126 for _, ext := range exts {
127 ret = append(ret, dir.Join(ctx, name+ext))
128 }
129 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000130 return ret
131}
132
David Srbecky1aacc6c2020-03-26 11:10:45 +0000133// The image "location" is a symbolic path that, with multiarchitecture support, doesn't really
134// exist on the device. Typically it is /apex/com.android.art/javalib/boot.art and should be the
135// same for all supported architectures on the device. The concrete architecture specific files
136// actually end up in architecture-specific sub-directory such as arm, arm64, x86, or x86_64.
137//
138// For example a physical file
139// "/apex/com.android.art/javalib/x86/boot.art" has "image location"
140// "/apex/com.android.art/javalib/boot.art" (which is not an actual file).
141//
142// The location is passed as an argument to the ART tools like dex2oat instead of the real path.
143// ART tools will then reconstruct the architecture-specific real path.
144func (image *bootImageVariant) imageLocations() (imageLocations []string) {
145 if image.extends != nil {
146 imageLocations = image.extends.getVariant(image.target).imageLocations()
147 }
148 return append(imageLocations, dexpreopt.PathToLocation(image.images, image.target.Arch.ArchType))
149}
150
Colin Cross800fe132019-02-11 14:21:24 -0800151func concat(lists ...[]string) []string {
152 var size int
153 for _, l := range lists {
154 size += len(l)
155 }
156 ret := make([]string, 0, size)
157 for _, l := range lists {
158 ret = append(ret, l...)
159 }
160 return ret
161}
162
Colin Cross800fe132019-02-11 14:21:24 -0800163func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800164 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800165}
166
167func skipDexpreoptBootJars(ctx android.PathContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000168 if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000169 return true
170 }
171
Colin Cross800fe132019-02-11 14:21:24 -0800172 if ctx.Config().UnbundledBuild() {
173 return true
174 }
175
Colin Cross800fe132019-02-11 14:21:24 -0800176 return false
177}
178
Colin Cross44df5812019-02-15 23:06:46 -0800179type dexpreoptBootJars struct {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000180 defaultBootImage *bootImageConfig
181 otherImages []*bootImageConfig
Colin Cross2d00f0d2019-05-09 21:50:00 -0700182
183 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800184}
Colin Cross800fe132019-02-11 14:21:24 -0800185
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000186// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000187func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000188 if skipDexpreoptBootJars(ctx) {
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000189 return nil
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000190 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000191 // Include dexpreopt files for the primary boot image.
192 files := map[android.ArchType]android.OutputPaths{}
193 for _, variant := range artBootImageConfig(ctx).variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000194 // 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 +0000195 if variant.target.Os == android.Android {
196 files[variant.target.Arch.ArchType] = variant.imagesDeps
David Srbecky7f8dac12020-02-13 16:00:45 +0000197 }
David Srbeckyc177ebe2020-02-18 20:43:06 +0000198 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000199 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000200}
201
Colin Cross800fe132019-02-11 14:21:24 -0800202// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800203func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800204 if skipDexpreoptBootJars(ctx) {
205 return
206 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000207 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
208 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
209 return
210 }
Colin Cross800fe132019-02-11 14:21:24 -0800211
Colin Cross2d00f0d2019-05-09 21:50:00 -0700212 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
213 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
214
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000215 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800216
217 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
218 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
219 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
220 // on ASAN settings.
221 if len(ctx.Config().SanitizeDevice()) == 1 &&
222 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800223 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800224 return
225 }
226
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000227 // Always create the default boot image first, to get a unique profile rule for all images.
228 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000229 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
230 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800231
232 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800233}
234
David Srbeckyc177ebe2020-02-18 20:43:06 +0000235// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
236func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Colin Cross44df5812019-02-15 23:06:46 -0800237 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800238 ctx.VisitAllModules(func(module android.Module) {
239 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800240 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800241 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800242 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800243 bootDexJars[i] = j.DexJar()
244 }
245 }
246 })
247
248 var missingDeps []string
249 // Ensure all modules were converted to paths
250 for i := range bootDexJars {
251 if bootDexJars[i] == nil {
252 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800253 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800254 bootDexJars[i] = android.PathForOutput(ctx, "missing")
255 } else {
256 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800257 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800258 }
259 }
260 }
261
262 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
263 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
264 // already been set up can find them.
265 for i := range bootDexJars {
266 ctx.Build(pctx, android.BuildParams{
267 Rule: android.Cp,
268 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800269 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800270 })
271 }
272
Colin Cross44df5812019-02-15 23:06:46 -0800273 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100274 bootFrameworkProfileRule(ctx, image, missingDeps)
Vladimir Marko205e6c22020-04-01 13:52:27 +0100275 updatableBcpPackagesRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800276
Colin Crossdf8eebe2019-04-09 15:29:41 -0700277 var allFiles android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000278 for _, variant := range image.variants {
279 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000280 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800281 }
Colin Cross44df5812019-02-15 23:06:46 -0800282
Colin Crossdf8eebe2019-04-09 15:29:41 -0700283 if image.zip != nil {
284 rule := android.NewRuleBuilder()
285 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700286 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700287 FlagWithOutput("-o ", image.zip).
288 FlagWithArg("-C ", image.dir.String()).
289 FlagWithInputList("-f ", allFiles, " -f ")
290
291 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
292 }
293
Colin Cross44df5812019-02-15 23:06:46 -0800294 return image
Colin Cross800fe132019-02-11 14:21:24 -0800295}
296
David Srbeckyc177ebe2020-02-18 20:43:06 +0000297func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
298 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800299
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000300 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000301 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800302
David Srbeckyc177ebe2020-02-18 20:43:06 +0000303 arch := image.target.Arch.ArchType
David Srbecky7f8dac12020-02-13 16:00:45 +0000304 os := image.target.Os.String() // We need to distinguish host-x86 and device-x86.
305 symbolsDir := image.symbolsDir.Join(ctx, os, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000306 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
David Srbecky7f8dac12020-02-13 16:00:45 +0000307 outputDir := image.dir.Join(ctx, os, image.installSubdir, arch.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000308 outputPath := outputDir.Join(ctx, image.stem+".oat")
309 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
310 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800311
312 rule := android.NewRuleBuilder()
313 rule.MissingDeps(missingDeps)
314
315 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
316 rule.Command().Text("rm").Flag("-f").
317 Flag(symbolsDir.Join(ctx, "*.art").String()).
318 Flag(symbolsDir.Join(ctx, "*.oat").String()).
319 Flag(symbolsDir.Join(ctx, "*.invocation").String())
320 rule.Command().Text("rm").Flag("-f").
321 Flag(outputDir.Join(ctx, "*.art").String()).
322 Flag(outputDir.Join(ctx, "*.oat").String()).
323 Flag(outputDir.Join(ctx, "*.invocation").String())
324
325 cmd := rule.Command()
326
327 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
328 if extraFlags == "" {
329 // Use ANDROID_LOG_TAGS to suppress most logging by default...
330 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
331 } else {
332 // ...unless the boot image is generated specifically for testing, then allow all logging.
333 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
334 }
335
336 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
337
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000338 cmd.Tool(globalSoong.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800339 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800340 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800341 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
342 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800343
Colin Cross69f59a32019-02-15 10:39:37 -0800344 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800345 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800346 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800347 }
348
Colin Cross44df5812019-02-15 23:06:46 -0800349 if global.DirtyImageObjects.Valid() {
350 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800351 }
352
David Srbecky1aacc6c2020-03-26 11:10:45 +0000353 if image.extends != nil {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000354 artImage := image.primaryImages
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000355 cmd.
356 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
357 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
358 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
359 } else {
360 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
361 }
362
Colin Cross800fe132019-02-11 14:21:24 -0800363 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800364 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
365 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800366 Flag("--generate-debug-info").
367 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700368 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000369 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800370 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000371 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800372 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000373 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800374 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800375 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800376 FlagWithArg("--no-inline-from=", "core-oj.jar").
Ulya Trafimovichc0c98d52020-03-09 12:46:06 +0000377 Flag("--force-determinism").
Colin Cross800fe132019-02-11 14:21:24 -0800378 Flag("--abort-on-hard-verifier-error")
379
David Srbecky7f8dac12020-02-13 16:00:45 +0000380 // Use the default variant/features for host builds.
381 // The map below contains only device CPU info (which might be x86 on some devices).
382 if image.target.Os == android.Android {
383 cmd.FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch])
384 cmd.FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch])
385 }
386
Colin Cross44df5812019-02-15 23:06:46 -0800387 if global.BootFlags != "" {
388 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800389 }
390
391 if extraFlags != "" {
392 cmd.Flag(extraFlags)
393 }
394
Colin Cross0b9f31f2019-02-28 11:00:01 -0800395 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800396
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000397 installDir := filepath.Join("/", image.installSubdir, arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800398
Colin Cross800fe132019-02-11 14:21:24 -0800399 var vdexInstalls android.RuleBuilderInstalls
400 var unstrippedInstalls android.RuleBuilderInstalls
401
Colin Crossdf8eebe2019-04-09 15:29:41 -0700402 var zipFiles android.WritablePaths
403
Dan Willemsen0f416782019-06-13 21:44:53 +0000404 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
405 cmd.ImplicitOutput(artOrOat)
406 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800407
Dan Willemsen0f416782019-06-13 21:44:53 +0000408 // Install the .oat and .art files
409 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
410 }
Colin Cross800fe132019-02-11 14:21:24 -0800411
Dan Willemsen0f416782019-06-13 21:44:53 +0000412 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
413 cmd.ImplicitOutput(vdex)
414 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800415
David Srbecky7f8dac12020-02-13 16:00:45 +0000416 // Note that the vdex files are identical between architectures.
417 // Make rules will create symlinks to share them between architectures.
Colin Cross800fe132019-02-11 14:21:24 -0800418 vdexInstalls = append(vdexInstalls,
David Srbecky7f8dac12020-02-13 16:00:45 +0000419 android.RuleBuilderInstall{vdex, filepath.Join(installDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000420 }
421
422 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
423 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800424
425 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
426 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800427 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800428 }
429
David Srbecky7f8dac12020-02-13 16:00:45 +0000430 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+image.target.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800431
432 // save output and installed files for makevars
David Srbeckyc177ebe2020-02-18 20:43:06 +0000433 image.installs = rule.Installs()
434 image.vdexInstalls = vdexInstalls
435 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700436
437 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800438}
439
440const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
441It is likely that the boot classpath is inconsistent.
442Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
443
David Srbeckyc177ebe2020-02-18 20:43:06 +0000444func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000445 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000446 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000447
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700448 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000449 return nil
450 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000451 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000452 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800453
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000454 rule := android.NewRuleBuilder()
455 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800456
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000457 var bootImageProfile android.Path
458 if len(global.BootImageProfiles) > 1 {
459 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
460 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
461 bootImageProfile = combinedBootImageProfile
462 } else if len(global.BootImageProfiles) == 1 {
463 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000464 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
465 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000466 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000467 // No profile (not even a default one, which is the case on some branches
468 // like master-art-host that don't have frameworks/base).
469 // Return nil and continue without profile.
470 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000471 }
Colin Cross800fe132019-02-11 14:21:24 -0800472
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000473 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800474
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000475 rule.Command().
476 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000477 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000478 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000479 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
480 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000481 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800482
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000483 rule.Install(profile, "/system/etc/boot-image.prof")
484
485 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
486
487 image.profileInstalls = rule.Installs()
488
489 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000490 })
491 if profile == nil {
492 return nil // wrap nil into a typed pointer with value nil
493 }
494 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800495}
496
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000497var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
498
David Srbeckyc177ebe2020-02-18 20:43:06 +0000499func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000500 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000501 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100502
503 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
504 return nil
505 }
506 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100507 rule := android.NewRuleBuilder()
508 rule.MissingDeps(missingDeps)
509
510 // Some branches like master-art-host don't have frameworks/base, so manually
511 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
512 // and if they do they'll get a missing deps error.
513 defaultProfile := "frameworks/base/config/boot-profile.txt"
514 path := android.ExistentPathForSource(ctx, defaultProfile)
515 var bootFrameworkProfile android.Path
516 if path.Valid() {
517 bootFrameworkProfile = path.Path()
518 } else {
519 missingDeps = append(missingDeps, defaultProfile)
520 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
521 }
522
523 profile := image.dir.Join(ctx, "boot.bprof")
524
525 rule.Command().
526 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000527 Tool(globalSoong.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100528 Flag("--generate-boot-profile").
529 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000530 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
531 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100532 FlagWithOutput("--reference-profile-file=", profile)
533
534 rule.Install(profile, "/system/etc/boot-image.bprof")
535 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
536 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
537
538 return profile
539 }).(android.WritablePath)
540}
541
542var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
543
Vladimir Marko205e6c22020-04-01 13:52:27 +0100544func updatableBcpPackagesRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
545 if ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
546 return nil
547 }
548
549 return ctx.Config().Once(updatableBcpPackagesRuleKey, func() interface{} {
550 global := dexpreopt.GetGlobalConfig(ctx)
551 updatableModules := dexpreopt.GetJarsFromApexJarPairs(global.UpdatableBootJars)
552
553 // Collect `permitted_packages` for updatable boot jars.
554 var updatablePackages []string
555 ctx.VisitAllModules(func(module android.Module) {
556 if j, ok := module.(*Library); ok {
557 name := ctx.ModuleName(module)
558 if i := android.IndexList(name, updatableModules); i != -1 {
559 pp := j.properties.Permitted_packages
560 if len(pp) > 0 {
561 updatablePackages = append(updatablePackages, pp...)
562 } else {
563 ctx.Errorf("Missing permitted_packages for %s", name)
564 }
565 // Do not match the same library repeatedly.
566 updatableModules = append(updatableModules[:i], updatableModules[i+1:]...)
567 }
568 }
569 })
570
571 // Sort updatable packages to ensure deterministic ordering.
572 sort.Strings(updatablePackages)
573
574 updatableBcpPackagesName := "updatable-bcp-packages.txt"
575 updatableBcpPackages := image.dir.Join(ctx, updatableBcpPackagesName)
576
577 ctx.Build(pctx, android.BuildParams{
578 Rule: android.WriteFile,
579 Output: updatableBcpPackages,
580 Args: map[string]string{
581 // WriteFile automatically adds the last end-of-line.
582 "content": strings.Join(updatablePackages, "\\n"),
583 },
584 })
585
586 rule := android.NewRuleBuilder()
587 rule.MissingDeps(missingDeps)
588 rule.Install(updatableBcpPackages, "/system/etc/"+updatableBcpPackagesName)
589 // TODO: Rename `profileInstalls` to `extraInstalls`?
590 // Maybe even move the field out of the bootImageConfig into some higher level type?
591 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
592
593 return updatableBcpPackages
594 }).(android.WritablePath)
595}
596
597var updatableBcpPackagesRuleKey = android.NewOnceKey("updatableBcpPackagesRule")
598
David Srbeckyc177ebe2020-02-18 20:43:06 +0000599func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800600 var allPhonies android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000601 for _, image := range image.variants {
602 arch := image.target.Arch.ArchType
David Srbecky46672322020-03-16 13:27:55 +0000603 suffix := arch.String()
604 // Host and target might both use x86 arch. We need to ensure the names are unique.
605 if image.target.Os.Class == android.Host {
606 suffix = "host-" + suffix
607 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800608 // Create a rule to call oatdump.
David Srbecky7f8dac12020-02-13 16:00:45 +0000609 output := android.PathForOutput(ctx, "boot."+suffix+".oatdump.txt")
Colin Crossc9a4c362019-02-26 21:13:48 -0800610 rule := android.NewRuleBuilder()
611 rule.Command().
612 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700613 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000614 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
615 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbecky1aacc6c2020-03-26 11:10:45 +0000616 FlagWithArg("--image=", strings.Join(image.imageLocations(), ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800617 FlagWithOutput("--output=", output).
618 FlagWithArg("--instruction-set=", arch.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000619 rule.Build(pctx, ctx, "dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800620
621 // Create a phony rule that depends on the output file and prints the path.
David Srbecky7f8dac12020-02-13 16:00:45 +0000622 phony := android.PathForPhony(ctx, "dump-oat-boot-"+suffix)
Colin Crossc9a4c362019-02-26 21:13:48 -0800623 rule = android.NewRuleBuilder()
624 rule.Command().
625 Implicit(output).
626 ImplicitOutput(phony).
627 Text("echo").FlagWithArg("Output in ", output.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000628 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800629
David Srbecky1aacc6c2020-03-26 11:10:45 +0000630 allPhonies = append(allPhonies, phony)
Colin Crossc9a4c362019-02-26 21:13:48 -0800631 }
632
633 phony := android.PathForPhony(ctx, "dump-oat-boot")
634 ctx.Build(pctx, android.BuildParams{
635 Rule: android.Phony,
636 Output: phony,
637 Inputs: allPhonies,
638 Description: "dump-oat-boot",
639 })
640
641}
642
Colin Cross2d00f0d2019-05-09 21:50:00 -0700643func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000644 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700645
646 ctx.Build(pctx, android.BuildParams{
647 Rule: android.WriteFile,
648 Output: path,
649 Args: map[string]string{
650 "content": string(data),
651 },
652 })
653}
654
Colin Cross44df5812019-02-15 23:06:46 -0800655// Export paths for default boot image to Make
656func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700657 if d.dexpreoptConfigForMake != nil {
658 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000659 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700660 }
661
Colin Cross44df5812019-02-15 23:06:46 -0800662 image := d.defaultBootImage
663 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800664 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000665 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
666 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000667
668 var imageNames []string
669 for _, current := range append(d.otherImages, image) {
670 imageNames = append(imageNames, current.name)
David Srbecky1aacc6c2020-03-26 11:10:45 +0000671 imageLocations := []string{}
672 for _, variant := range current.variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000673 suffix := ""
David Srbecky1aacc6c2020-03-26 11:10:45 +0000674 if variant.target.Os.Class == android.Host {
David Srbecky7f8dac12020-02-13 16:00:45 +0000675 suffix = "_host"
676 }
David Srbecky1aacc6c2020-03-26 11:10:45 +0000677 sfx := variant.name + suffix + "_" + variant.target.Arch.ArchType.String()
678 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, variant.vdexInstalls.String())
679 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, variant.images.String())
680 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(variant.imagesDeps.Strings(), " "))
681 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, variant.installs.String())
682 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, variant.unstrippedInstalls.String())
683 if variant.target.Os == android.Android {
684 // The locations for all Android targets are identical. Pick one.
685 imageLocations = variant.imageLocations()
686 }
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000687 }
David Srbecky1aacc6c2020-03-26 11:10:45 +0000688 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800689 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000690 }
691 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800692 }
Colin Cross800fe132019-02-11 14:21:24 -0800693}