blob: d00864d2375088bb8fa862c9323e111a17ef2520 [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
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000055 // File paths to jars.
56 dexPaths android.WritablePaths // for this image
57 dexPathsDeps android.WritablePaths // for the dependency images and in this image
58
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000059 // File path to a zip archive with all image files (or nil, if not needed).
60 zip android.WritablePath
David Srbeckyc177ebe2020-02-18 20:43:06 +000061
62 // Rules which should be used in make to install the outputs.
63 profileInstalls android.RuleBuilderInstalls
64
65 // Target-dependent fields.
66 variants []*bootImageVariant
67}
68
69// Target-dependent description of pre-compiled boot image.
70type bootImageVariant struct {
71 *bootImageConfig
72
73 // Target for which the image is generated.
74 target android.Target
75
David Srbeckyab994982020-03-30 17:24:13 +010076 // The "locations" of jars.
77 dexLocations []string // for this image
78 dexLocationsDeps []string // for the dependency images and in this image
79
David Srbeckyc177ebe2020-02-18 20:43:06 +000080 // 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
David Srbeckyab994982020-03-30 17:24:13 +0100102// Return any (the first) variant which is for the device (as opposed to for the host)
103func (image bootImageConfig) getAnyAndroidVariant() *bootImageVariant {
104 for _, variant := range image.variants {
105 if variant.target.Os == android.Android {
106 return variant
107 }
108 }
109 return nil
110}
111
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000112func (image bootImageConfig) moduleName(idx int) string {
113 // Dexpreopt on the boot class path produces multiple files. The first dex file
114 // is converted into 'name'.art (to match the legacy assumption that 'name'.art
Dan Willemsen0f416782019-06-13 21:44:53 +0000115 // exists), and the rest are converted to 'name'-<jar>.art.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000116 m := image.modules[idx]
117 name := image.stem
David Srbecky1aacc6c2020-03-26 11:10:45 +0000118 if idx != 0 || image.extends != nil {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000119 name += "-" + stemOf(m)
120 }
121 return name
122}
Dan Willemsen0f416782019-06-13 21:44:53 +0000123
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000124func (image bootImageConfig) firstModuleNameOrStem() string {
125 if len(image.modules) > 0 {
126 return image.moduleName(0)
127 } else {
128 return image.stem
129 }
130}
131
132func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths {
133 ret := make(android.OutputPaths, 0, len(image.modules)*len(exts))
134 for i := range image.modules {
135 name := image.moduleName(i)
Dan Willemsen0f416782019-06-13 21:44:53 +0000136 for _, ext := range exts {
137 ret = append(ret, dir.Join(ctx, name+ext))
138 }
139 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000140 return ret
141}
142
David Srbecky1aacc6c2020-03-26 11:10:45 +0000143// The image "location" is a symbolic path that, with multiarchitecture support, doesn't really
144// exist on the device. Typically it is /apex/com.android.art/javalib/boot.art and should be the
145// same for all supported architectures on the device. The concrete architecture specific files
146// actually end up in architecture-specific sub-directory such as arm, arm64, x86, or x86_64.
147//
148// For example a physical file
149// "/apex/com.android.art/javalib/x86/boot.art" has "image location"
150// "/apex/com.android.art/javalib/boot.art" (which is not an actual file).
151//
152// The location is passed as an argument to the ART tools like dex2oat instead of the real path.
153// ART tools will then reconstruct the architecture-specific real path.
154func (image *bootImageVariant) imageLocations() (imageLocations []string) {
155 if image.extends != nil {
156 imageLocations = image.extends.getVariant(image.target).imageLocations()
157 }
158 return append(imageLocations, dexpreopt.PathToLocation(image.images, image.target.Arch.ArchType))
159}
160
Colin Cross800fe132019-02-11 14:21:24 -0800161func concat(lists ...[]string) []string {
162 var size int
163 for _, l := range lists {
164 size += len(l)
165 }
166 ret := make([]string, 0, size)
167 for _, l := range lists {
168 ret = append(ret, l...)
169 }
170 return ret
171}
172
Colin Cross800fe132019-02-11 14:21:24 -0800173func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800174 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800175}
176
177func skipDexpreoptBootJars(ctx android.PathContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000178 if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000179 return true
180 }
181
Colin Cross800fe132019-02-11 14:21:24 -0800182 if ctx.Config().UnbundledBuild() {
183 return true
184 }
185
Colin Cross800fe132019-02-11 14:21:24 -0800186 return false
187}
188
Colin Cross44df5812019-02-15 23:06:46 -0800189type dexpreoptBootJars struct {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000190 defaultBootImage *bootImageConfig
191 otherImages []*bootImageConfig
Colin Cross2d00f0d2019-05-09 21:50:00 -0700192
193 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800194}
Colin Cross800fe132019-02-11 14:21:24 -0800195
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000196// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000197func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000198 if skipDexpreoptBootJars(ctx) {
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000199 return nil
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000200 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000201 // Include dexpreopt files for the primary boot image.
202 files := map[android.ArchType]android.OutputPaths{}
203 for _, variant := range artBootImageConfig(ctx).variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000204 // 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 +0000205 if variant.target.Os == android.Android {
206 files[variant.target.Arch.ArchType] = variant.imagesDeps
David Srbecky7f8dac12020-02-13 16:00:45 +0000207 }
David Srbeckyc177ebe2020-02-18 20:43:06 +0000208 }
Tim Joinesc1ef1bb2020-03-18 18:00:41 +0000209 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000210}
211
Colin Cross800fe132019-02-11 14:21:24 -0800212// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800213func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800214 if skipDexpreoptBootJars(ctx) {
215 return
216 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000217 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
218 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
219 return
220 }
Colin Cross800fe132019-02-11 14:21:24 -0800221
Colin Cross2d00f0d2019-05-09 21:50:00 -0700222 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
223 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
224
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000225 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800226
227 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
228 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
229 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
230 // on ASAN settings.
231 if len(ctx.Config().SanitizeDevice()) == 1 &&
232 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800233 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800234 return
235 }
236
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000237 // Always create the default boot image first, to get a unique profile rule for all images.
238 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000239 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
240 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800241
242 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800243}
244
David Srbeckyc177ebe2020-02-18 20:43:06 +0000245// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
246func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Colin Cross44df5812019-02-15 23:06:46 -0800247 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800248 ctx.VisitAllModules(func(module android.Module) {
249 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800250 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800251 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800252 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800253 bootDexJars[i] = j.DexJar()
254 }
255 }
256 })
257
258 var missingDeps []string
259 // Ensure all modules were converted to paths
260 for i := range bootDexJars {
261 if bootDexJars[i] == nil {
262 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800263 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800264 bootDexJars[i] = android.PathForOutput(ctx, "missing")
265 } else {
266 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800267 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800268 }
269 }
270 }
271
272 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
273 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
274 // already been set up can find them.
275 for i := range bootDexJars {
276 ctx.Build(pctx, android.BuildParams{
277 Rule: android.Cp,
278 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800279 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800280 })
281 }
282
Colin Cross44df5812019-02-15 23:06:46 -0800283 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100284 bootFrameworkProfileRule(ctx, image, missingDeps)
Vladimir Marko205e6c22020-04-01 13:52:27 +0100285 updatableBcpPackagesRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800286
Colin Crossdf8eebe2019-04-09 15:29:41 -0700287 var allFiles android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000288 for _, variant := range image.variants {
289 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000290 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800291 }
Colin Cross44df5812019-02-15 23:06:46 -0800292
Colin Crossdf8eebe2019-04-09 15:29:41 -0700293 if image.zip != nil {
294 rule := android.NewRuleBuilder()
295 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700296 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700297 FlagWithOutput("-o ", image.zip).
298 FlagWithArg("-C ", image.dir.String()).
299 FlagWithInputList("-f ", allFiles, " -f ")
300
301 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
302 }
303
Colin Cross44df5812019-02-15 23:06:46 -0800304 return image
Colin Cross800fe132019-02-11 14:21:24 -0800305}
306
David Srbeckyc177ebe2020-02-18 20:43:06 +0000307func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
308 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800309
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000310 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000311 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800312
David Srbeckyc177ebe2020-02-18 20:43:06 +0000313 arch := image.target.Arch.ArchType
David Srbecky7f8dac12020-02-13 16:00:45 +0000314 os := image.target.Os.String() // We need to distinguish host-x86 and device-x86.
315 symbolsDir := image.symbolsDir.Join(ctx, os, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000316 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
David Srbecky7f8dac12020-02-13 16:00:45 +0000317 outputDir := image.dir.Join(ctx, os, image.installSubdir, arch.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000318 outputPath := outputDir.Join(ctx, image.stem+".oat")
319 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
320 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800321
322 rule := android.NewRuleBuilder()
323 rule.MissingDeps(missingDeps)
324
325 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
326 rule.Command().Text("rm").Flag("-f").
327 Flag(symbolsDir.Join(ctx, "*.art").String()).
328 Flag(symbolsDir.Join(ctx, "*.oat").String()).
329 Flag(symbolsDir.Join(ctx, "*.invocation").String())
330 rule.Command().Text("rm").Flag("-f").
331 Flag(outputDir.Join(ctx, "*.art").String()).
332 Flag(outputDir.Join(ctx, "*.oat").String()).
333 Flag(outputDir.Join(ctx, "*.invocation").String())
334
335 cmd := rule.Command()
336
337 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
338 if extraFlags == "" {
339 // Use ANDROID_LOG_TAGS to suppress most logging by default...
340 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
341 } else {
342 // ...unless the boot image is generated specifically for testing, then allow all logging.
343 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
344 }
345
346 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
347
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000348 cmd.Tool(globalSoong.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800349 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800350 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800351 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
352 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800353
Colin Cross69f59a32019-02-15 10:39:37 -0800354 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800355 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800356 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800357 }
358
Colin Cross44df5812019-02-15 23:06:46 -0800359 if global.DirtyImageObjects.Valid() {
360 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800361 }
362
David Srbecky1aacc6c2020-03-26 11:10:45 +0000363 if image.extends != nil {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000364 artImage := image.primaryImages
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000365 cmd.
366 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
367 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
368 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
369 } else {
370 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
371 }
372
Colin Cross800fe132019-02-11 14:21:24 -0800373 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800374 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
375 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800376 Flag("--generate-debug-info").
377 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700378 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000379 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800380 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000381 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800382 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000383 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800384 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800385 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800386 FlagWithArg("--no-inline-from=", "core-oj.jar").
Ulya Trafimovichc0c98d52020-03-09 12:46:06 +0000387 Flag("--force-determinism").
Colin Cross800fe132019-02-11 14:21:24 -0800388 Flag("--abort-on-hard-verifier-error")
389
David Srbecky7f8dac12020-02-13 16:00:45 +0000390 // Use the default variant/features for host builds.
391 // The map below contains only device CPU info (which might be x86 on some devices).
392 if image.target.Os == android.Android {
393 cmd.FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch])
394 cmd.FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch])
395 }
396
Colin Cross44df5812019-02-15 23:06:46 -0800397 if global.BootFlags != "" {
398 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800399 }
400
401 if extraFlags != "" {
402 cmd.Flag(extraFlags)
403 }
404
Colin Cross0b9f31f2019-02-28 11:00:01 -0800405 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800406
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000407 installDir := filepath.Join("/", image.installSubdir, arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800408
Colin Cross800fe132019-02-11 14:21:24 -0800409 var vdexInstalls android.RuleBuilderInstalls
410 var unstrippedInstalls android.RuleBuilderInstalls
411
Colin Crossdf8eebe2019-04-09 15:29:41 -0700412 var zipFiles android.WritablePaths
413
Dan Willemsen0f416782019-06-13 21:44:53 +0000414 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
415 cmd.ImplicitOutput(artOrOat)
416 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800417
Dan Willemsen0f416782019-06-13 21:44:53 +0000418 // Install the .oat and .art files
419 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
420 }
Colin Cross800fe132019-02-11 14:21:24 -0800421
Dan Willemsen0f416782019-06-13 21:44:53 +0000422 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
423 cmd.ImplicitOutput(vdex)
424 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800425
David Srbecky7f8dac12020-02-13 16:00:45 +0000426 // Note that the vdex files are identical between architectures.
427 // Make rules will create symlinks to share them between architectures.
Colin Cross800fe132019-02-11 14:21:24 -0800428 vdexInstalls = append(vdexInstalls,
David Srbecky7f8dac12020-02-13 16:00:45 +0000429 android.RuleBuilderInstall{vdex, filepath.Join(installDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000430 }
431
432 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
433 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800434
435 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
436 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800437 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800438 }
439
David Srbecky7f8dac12020-02-13 16:00:45 +0000440 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+image.target.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800441
442 // save output and installed files for makevars
David Srbeckyc177ebe2020-02-18 20:43:06 +0000443 image.installs = rule.Installs()
444 image.vdexInstalls = vdexInstalls
445 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700446
447 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800448}
449
450const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
451It is likely that the boot classpath is inconsistent.
452Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
453
David Srbeckyc177ebe2020-02-18 20:43:06 +0000454func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000455 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000456 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000457
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700458 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000459 return nil
460 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000461 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000462 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800463
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000464 rule := android.NewRuleBuilder()
465 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800466
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000467 var bootImageProfile android.Path
468 if len(global.BootImageProfiles) > 1 {
469 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
470 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
471 bootImageProfile = combinedBootImageProfile
472 } else if len(global.BootImageProfiles) == 1 {
473 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000474 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
475 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000476 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000477 // No profile (not even a default one, which is the case on some branches
478 // like master-art-host that don't have frameworks/base).
479 // Return nil and continue without profile.
480 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000481 }
Colin Cross800fe132019-02-11 14:21:24 -0800482
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000483 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800484
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000485 rule.Command().
486 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000487 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000488 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000489 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
David Srbeckyab994982020-03-30 17:24:13 +0100490 FlagForEachArg("--dex-location=", image.getAnyAndroidVariant().dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000491 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800492
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000493 rule.Install(profile, "/system/etc/boot-image.prof")
494
495 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
496
497 image.profileInstalls = rule.Installs()
498
499 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000500 })
501 if profile == nil {
502 return nil // wrap nil into a typed pointer with value nil
503 }
504 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800505}
506
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000507var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
508
David Srbeckyc177ebe2020-02-18 20:43:06 +0000509func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000510 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000511 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100512
513 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
514 return nil
515 }
516 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100517 rule := android.NewRuleBuilder()
518 rule.MissingDeps(missingDeps)
519
520 // Some branches like master-art-host don't have frameworks/base, so manually
521 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
522 // and if they do they'll get a missing deps error.
523 defaultProfile := "frameworks/base/config/boot-profile.txt"
524 path := android.ExistentPathForSource(ctx, defaultProfile)
525 var bootFrameworkProfile android.Path
526 if path.Valid() {
527 bootFrameworkProfile = path.Path()
528 } else {
529 missingDeps = append(missingDeps, defaultProfile)
530 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
531 }
532
533 profile := image.dir.Join(ctx, "boot.bprof")
534
535 rule.Command().
536 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000537 Tool(globalSoong.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100538 Flag("--generate-boot-profile").
539 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000540 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
David Srbeckyab994982020-03-30 17:24:13 +0100541 FlagForEachArg("--dex-location=", image.getAnyAndroidVariant().dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100542 FlagWithOutput("--reference-profile-file=", profile)
543
544 rule.Install(profile, "/system/etc/boot-image.bprof")
545 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
546 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
547
548 return profile
549 }).(android.WritablePath)
550}
551
552var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
553
Vladimir Marko205e6c22020-04-01 13:52:27 +0100554func updatableBcpPackagesRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
555 if ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
556 return nil
557 }
558
559 return ctx.Config().Once(updatableBcpPackagesRuleKey, func() interface{} {
560 global := dexpreopt.GetGlobalConfig(ctx)
561 updatableModules := dexpreopt.GetJarsFromApexJarPairs(global.UpdatableBootJars)
562
563 // Collect `permitted_packages` for updatable boot jars.
564 var updatablePackages []string
565 ctx.VisitAllModules(func(module android.Module) {
566 if j, ok := module.(*Library); ok {
567 name := ctx.ModuleName(module)
568 if i := android.IndexList(name, updatableModules); i != -1 {
569 pp := j.properties.Permitted_packages
570 if len(pp) > 0 {
571 updatablePackages = append(updatablePackages, pp...)
572 } else {
573 ctx.Errorf("Missing permitted_packages for %s", name)
574 }
575 // Do not match the same library repeatedly.
576 updatableModules = append(updatableModules[:i], updatableModules[i+1:]...)
577 }
578 }
579 })
580
581 // Sort updatable packages to ensure deterministic ordering.
582 sort.Strings(updatablePackages)
583
584 updatableBcpPackagesName := "updatable-bcp-packages.txt"
585 updatableBcpPackages := image.dir.Join(ctx, updatableBcpPackagesName)
586
587 ctx.Build(pctx, android.BuildParams{
588 Rule: android.WriteFile,
589 Output: updatableBcpPackages,
590 Args: map[string]string{
591 // WriteFile automatically adds the last end-of-line.
592 "content": strings.Join(updatablePackages, "\\n"),
593 },
594 })
595
596 rule := android.NewRuleBuilder()
597 rule.MissingDeps(missingDeps)
598 rule.Install(updatableBcpPackages, "/system/etc/"+updatableBcpPackagesName)
599 // TODO: Rename `profileInstalls` to `extraInstalls`?
600 // Maybe even move the field out of the bootImageConfig into some higher level type?
601 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
602
603 return updatableBcpPackages
604 }).(android.WritablePath)
605}
606
607var updatableBcpPackagesRuleKey = android.NewOnceKey("updatableBcpPackagesRule")
608
David Srbeckyc177ebe2020-02-18 20:43:06 +0000609func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800610 var allPhonies android.Paths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000611 for _, image := range image.variants {
612 arch := image.target.Arch.ArchType
David Srbecky46672322020-03-16 13:27:55 +0000613 suffix := arch.String()
614 // Host and target might both use x86 arch. We need to ensure the names are unique.
615 if image.target.Os.Class == android.Host {
616 suffix = "host-" + suffix
617 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800618 // Create a rule to call oatdump.
David Srbecky7f8dac12020-02-13 16:00:45 +0000619 output := android.PathForOutput(ctx, "boot."+suffix+".oatdump.txt")
Colin Crossc9a4c362019-02-26 21:13:48 -0800620 rule := android.NewRuleBuilder()
621 rule.Command().
622 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700623 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000624 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
625 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbecky1aacc6c2020-03-26 11:10:45 +0000626 FlagWithArg("--image=", strings.Join(image.imageLocations(), ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800627 FlagWithOutput("--output=", output).
628 FlagWithArg("--instruction-set=", arch.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000629 rule.Build(pctx, ctx, "dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800630
631 // Create a phony rule that depends on the output file and prints the path.
David Srbecky7f8dac12020-02-13 16:00:45 +0000632 phony := android.PathForPhony(ctx, "dump-oat-boot-"+suffix)
Colin Crossc9a4c362019-02-26 21:13:48 -0800633 rule = android.NewRuleBuilder()
634 rule.Command().
635 Implicit(output).
636 ImplicitOutput(phony).
637 Text("echo").FlagWithArg("Output in ", output.String())
David Srbecky7f8dac12020-02-13 16:00:45 +0000638 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800639
David Srbecky1aacc6c2020-03-26 11:10:45 +0000640 allPhonies = append(allPhonies, phony)
Colin Crossc9a4c362019-02-26 21:13:48 -0800641 }
642
643 phony := android.PathForPhony(ctx, "dump-oat-boot")
644 ctx.Build(pctx, android.BuildParams{
645 Rule: android.Phony,
646 Output: phony,
647 Inputs: allPhonies,
648 Description: "dump-oat-boot",
649 })
650
651}
652
Colin Cross2d00f0d2019-05-09 21:50:00 -0700653func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000654 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700655
656 ctx.Build(pctx, android.BuildParams{
657 Rule: android.WriteFile,
658 Output: path,
659 Args: map[string]string{
660 "content": string(data),
661 },
662 })
663}
664
Colin Cross44df5812019-02-15 23:06:46 -0800665// Export paths for default boot image to Make
666func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700667 if d.dexpreoptConfigForMake != nil {
668 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000669 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700670 }
671
Colin Cross44df5812019-02-15 23:06:46 -0800672 image := d.defaultBootImage
673 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800674 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000675 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
David Srbeckyab994982020-03-30 17:24:13 +0100676 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.getAnyAndroidVariant().dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000677
678 var imageNames []string
679 for _, current := range append(d.otherImages, image) {
680 imageNames = append(imageNames, current.name)
David Srbecky1aacc6c2020-03-26 11:10:45 +0000681 for _, variant := range current.variants {
David Srbecky7f8dac12020-02-13 16:00:45 +0000682 suffix := ""
David Srbecky1aacc6c2020-03-26 11:10:45 +0000683 if variant.target.Os.Class == android.Host {
David Srbecky7f8dac12020-02-13 16:00:45 +0000684 suffix = "_host"
685 }
David Srbecky1aacc6c2020-03-26 11:10:45 +0000686 sfx := variant.name + suffix + "_" + variant.target.Arch.ArchType.String()
687 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, variant.vdexInstalls.String())
688 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, variant.images.String())
689 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(variant.imagesDeps.Strings(), " "))
690 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, variant.installs.String())
691 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, variant.unstrippedInstalls.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000692 }
David Srbeckyab994982020-03-30 17:24:13 +0100693 imageLocations := current.getAnyAndroidVariant().imageLocations()
David Srbecky1aacc6c2020-03-26 11:10:45 +0000694 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800695 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000696 }
697 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800698 }
Colin Cross800fe132019-02-11 14:21:24 -0800699}