blob: 8833bdb70c4d9d4a4ebd0e874a97bc0d17d4cf00 [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 Markob92ae272020-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() {
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +000029 RegisterDexpreoptBootJarsComponents(android.InitRegistrationContext)
Colin Cross800fe132019-02-11 14:21:24 -080030}
31
32// The image "location" is a symbolic path that with multiarchitecture
33// support doesn't really exist on the device. Typically it is
34// /system/framework/boot.art and should be the same for all supported
35// architectures on the device. The concrete architecture specific
36// content actually ends up in a "filename" that contains an
37// architecture specific directory name such as arm, arm64, mips,
38// mips64, x86, x86_64.
39//
40// Here are some example values for an x86_64 / x86 configuration:
41//
42// bootImages["x86_64"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86_64/boot.art"
43// dexpreopt.PathToLocation(bootImages["x86_64"], "x86_64") = "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
44//
45// bootImages["x86"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86/boot.art"
46// dexpreopt.PathToLocation(bootImages["x86"])= "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
47//
48// The location is passed as an argument to the ART tools like dex2oat instead of the real path. The ART tools
49// will then reconstruct the real path, so the rules must have a dependency on the real path.
50
David Srbecky163bda62020-02-18 20:43:06 +000051// Target-independent description of pre-compiled boot image.
Colin Cross44df5812019-02-15 23:06:46 -080052type bootImageConfig struct {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000053 // Whether this image is an extension.
54 extension bool
55
56 // Image name (used in directory names and ninja rule names).
57 name string
58
59 // Basename of the image: the resulting filenames are <stem>[-<jar>].{art,oat,vdex}.
60 stem string
61
62 // Output directory for the image files.
63 dir android.OutputPath
64
65 // Output directory for the image files with debug symbols.
66 symbolsDir android.OutputPath
67
68 // Subdirectory where the image files are installed.
69 installSubdir string
70
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000071 // The names of jars that constitute this image.
72 modules []string
73
74 // The "locations" of jars.
75 dexLocations []string // for this image
76 dexLocationsDeps []string // for the dependency images and in this image
77
78 // File paths to jars.
79 dexPaths android.WritablePaths // for this image
80 dexPathsDeps android.WritablePaths // for the dependency images and in this image
81
82 // The "locations" of the dependency images and in this image.
83 imageLocations []string
84
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000085 // File path to a zip archive with all image files (or nil, if not needed).
86 zip android.WritablePath
David Srbecky163bda62020-02-18 20:43:06 +000087
88 // Rules which should be used in make to install the outputs.
89 profileInstalls android.RuleBuilderInstalls
90
91 // Target-dependent fields.
92 variants []*bootImageVariant
93}
94
95// Target-dependent description of pre-compiled boot image.
96type bootImageVariant struct {
97 *bootImageConfig
98
99 // Target for which the image is generated.
100 target android.Target
101
102 // Paths to image files.
103 images android.OutputPath // first image file
104 imagesDeps android.OutputPaths // all files
105
106 // Only for extensions, paths to the primary boot images.
107 primaryImages android.OutputPath
108
109 // Rules which should be used in make to install the outputs.
110 installs android.RuleBuilderInstalls
111 vdexInstalls android.RuleBuilderInstalls
112 unstrippedInstalls android.RuleBuilderInstalls
113}
114
115func (image bootImageConfig) getVariant(target android.Target) *bootImageVariant {
116 for _, variant := range image.variants {
117 if variant.target.Os == target.Os && variant.target.Arch.ArchType == target.Arch.ArchType {
118 return variant
119 }
120 }
121 return nil
Colin Cross800fe132019-02-11 14:21:24 -0800122}
123
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000124func (image bootImageConfig) moduleName(idx int) string {
125 // Dexpreopt on the boot class path produces multiple files. The first dex file
126 // is converted into 'name'.art (to match the legacy assumption that 'name'.art
Dan Willemsen0f416782019-06-13 21:44:53 +0000127 // exists), and the rest are converted to 'name'-<jar>.art.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000128 m := image.modules[idx]
129 name := image.stem
130 if idx != 0 || image.extension {
131 name += "-" + stemOf(m)
132 }
133 return name
134}
Dan Willemsen0f416782019-06-13 21:44:53 +0000135
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000136func (image bootImageConfig) firstModuleNameOrStem() string {
137 if len(image.modules) > 0 {
138 return image.moduleName(0)
139 } else {
140 return image.stem
141 }
142}
143
144func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths {
145 ret := make(android.OutputPaths, 0, len(image.modules)*len(exts))
146 for i := range image.modules {
147 name := image.moduleName(i)
Dan Willemsen0f416782019-06-13 21:44:53 +0000148 for _, ext := range exts {
149 ret = append(ret, dir.Join(ctx, name+ext))
150 }
151 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000152 return ret
153}
154
Colin Cross800fe132019-02-11 14:21:24 -0800155func concat(lists ...[]string) []string {
156 var size int
157 for _, l := range lists {
158 size += len(l)
159 }
160 ret := make([]string, 0, size)
161 for _, l := range lists {
162 ret = append(ret, l...)
163 }
164 return ret
165}
166
Colin Cross800fe132019-02-11 14:21:24 -0800167func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800168 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800169}
170
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000171func RegisterDexpreoptBootJarsComponents(ctx android.RegistrationContext) {
172 ctx.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory)
173}
174
Colin Cross800fe132019-02-11 14:21:24 -0800175func skipDexpreoptBootJars(ctx android.PathContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000176 if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000177 return true
178 }
179
Colin Cross800fe132019-02-11 14:21:24 -0800180 if ctx.Config().UnbundledBuild() {
181 return true
182 }
183
Colin Cross800fe132019-02-11 14:21:24 -0800184 return false
185}
186
Colin Cross44df5812019-02-15 23:06:46 -0800187type dexpreoptBootJars struct {
David Srbecky163bda62020-02-18 20:43:06 +0000188 defaultBootImage *bootImageConfig
189 otherImages []*bootImageConfig
Colin Cross2d00f0d2019-05-09 21:50:00 -0700190
191 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800192}
Colin Cross800fe132019-02-11 14:21:24 -0800193
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000194// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000195func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000196 if skipDexpreoptBootJars(ctx) {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000197 return nil
198 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000199 // Include dexpreopt files for the primary boot image.
David Srbecky163bda62020-02-18 20:43:06 +0000200 files := map[android.ArchType]android.OutputPaths{}
201 for _, variant := range artBootImageConfig(ctx).variants {
David Srbeckye920d2d2020-02-13 16:00:45 +0000202 // We also generate boot images for host (for testing), but we don't need those in the apex.
203 if variant.target.Os == android.Android {
204 files[variant.target.Arch.ArchType] = variant.imagesDeps
205 }
David Srbecky163bda62020-02-18 20:43:06 +0000206 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000207 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000208}
209
Colin Cross800fe132019-02-11 14:21:24 -0800210// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800211func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800212 if skipDexpreoptBootJars(ctx) {
213 return
214 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000215 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
216 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
217 return
218 }
Colin Cross800fe132019-02-11 14:21:24 -0800219
Colin Cross2d00f0d2019-05-09 21:50:00 -0700220 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
221 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
222
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000223 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800224
225 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
226 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
227 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
228 // on ASAN settings.
229 if len(ctx.Config().SanitizeDevice()) == 1 &&
230 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800231 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800232 return
233 }
234
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000235 // Always create the default boot image first, to get a unique profile rule for all images.
236 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000237 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
238 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800239
240 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800241}
242
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000243// Inspect this module to see if it contains a bootclasspath dex jar.
244// Note that the same jar may occur in multiple modules.
245// This logic is tested in the apex package to avoid import cycle apex <-> java.
246func getBootImageJar(ctx android.SingletonContext, image *bootImageConfig, module android.Module) (int, android.Path) {
247 // All apex Java libraries have non-installable platform variants, skip them.
248 if module.IsSkipInstall() {
249 return -1, nil
250 }
251
252 jar, hasJar := module.(interface{ DexJar() android.Path })
253 if !hasJar {
254 return -1, nil
255 }
256
257 name := ctx.ModuleName(module)
258 index := android.IndexList(name, image.modules)
259 if index == -1 {
260 return -1, nil
261 }
262
263 // Check that this module satisfies constraints for a particular boot image.
264 apex, isApexModule := module.(android.ApexModule)
265 if image.name == artBootImageName {
266 if isApexModule && strings.HasPrefix(apex.ApexName(), "com.android.art.") {
267 // ok, found the jar in the ART apex
268 } else if isApexModule && !apex.IsForPlatform() {
269 // this jar is part of an updatable apex other than ART, fail immediately
270 ctx.Errorf("module '%s' from updatable apex '%s' is not allowed in the ART boot image", name, apex.ApexName())
271 } else if isApexModule && apex.IsForPlatform() && Bool(module.(*Library).deviceProperties.Hostdex) {
272 // this is a special "hostdex" variant, skip it and resume search
273 return -1, nil
Ulya Trafimovichb4d816e2020-04-08 15:00:49 +0100274 } else if name == "jacocoagent" && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
275 // this is Jacoco platform variant for a coverage build, skip it and resume search
276 return -1, nil
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000277 } else {
278 // this (installable) jar is part of the platform, fail immediately
279 ctx.Errorf("module '%s' is part of the platform and not allowed in the ART boot image", name)
280 }
281 } else if image.name == frameworkBootImageName {
282 if !isApexModule || apex.IsForPlatform() {
283 // ok, this jar is part of the platform
284 } else {
285 // this jar is part of an updatable apex, fail immediately
286 ctx.Errorf("module '%s' from updatable apex '%s' is not allowed in the framework boot image", name, apex.ApexName())
287 }
288 } else {
289 panic("unknown boot image: " + image.name)
290 }
291
292 return index, jar.DexJar()
293}
294
David Srbecky163bda62020-02-18 20:43:06 +0000295// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
296func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000297 // Collect dex jar paths for the boot image modules.
298 // This logic is tested in the apex package to avoid import cycle apex <-> java.
Colin Cross44df5812019-02-15 23:06:46 -0800299 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800300 ctx.VisitAllModules(func(module android.Module) {
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000301 if i, j := getBootImageJar(ctx, image, module); i != -1 {
302 bootDexJars[i] = j
Colin Cross800fe132019-02-11 14:21:24 -0800303 }
304 })
305
306 var missingDeps []string
307 // Ensure all modules were converted to paths
308 for i := range bootDexJars {
309 if bootDexJars[i] == nil {
310 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800311 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800312 bootDexJars[i] = android.PathForOutput(ctx, "missing")
313 } else {
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000314 ctx.Errorf("failed to find a dex jar path for module '%s'"+
315 ", note that some jars may be filtered out by module constraints",
Colin Cross44df5812019-02-15 23:06:46 -0800316 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800317 }
318 }
319 }
320
321 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
322 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
323 // already been set up can find them.
324 for i := range bootDexJars {
325 ctx.Build(pctx, android.BuildParams{
326 Rule: android.Cp,
327 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800328 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800329 })
330 }
331
Colin Cross44df5812019-02-15 23:06:46 -0800332 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100333 bootFrameworkProfileRule(ctx, image, missingDeps)
Vladimir Markob92ae272020-04-01 13:52:27 +0100334 updatableBcpPackagesRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800335
Colin Crossdf8eebe2019-04-09 15:29:41 -0700336 var allFiles android.Paths
David Srbecky163bda62020-02-18 20:43:06 +0000337 for _, variant := range image.variants {
338 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000339 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800340 }
Colin Cross44df5812019-02-15 23:06:46 -0800341
Colin Crossdf8eebe2019-04-09 15:29:41 -0700342 if image.zip != nil {
343 rule := android.NewRuleBuilder()
344 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700345 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700346 FlagWithOutput("-o ", image.zip).
347 FlagWithArg("-C ", image.dir.String()).
348 FlagWithInputList("-f ", allFiles, " -f ")
349
350 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
351 }
352
Colin Cross44df5812019-02-15 23:06:46 -0800353 return image
Colin Cross800fe132019-02-11 14:21:24 -0800354}
355
David Srbecky163bda62020-02-18 20:43:06 +0000356func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
357 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800358
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000359 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000360 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800361
David Srbecky163bda62020-02-18 20:43:06 +0000362 arch := image.target.Arch.ArchType
David Srbeckye920d2d2020-02-13 16:00:45 +0000363 os := image.target.Os.String() // We need to distinguish host-x86 and device-x86.
364 symbolsDir := image.symbolsDir.Join(ctx, os, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000365 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
David Srbeckye920d2d2020-02-13 16:00:45 +0000366 outputDir := image.dir.Join(ctx, os, image.installSubdir, arch.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000367 outputPath := outputDir.Join(ctx, image.stem+".oat")
368 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
369 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800370
371 rule := android.NewRuleBuilder()
372 rule.MissingDeps(missingDeps)
373
374 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
375 rule.Command().Text("rm").Flag("-f").
376 Flag(symbolsDir.Join(ctx, "*.art").String()).
377 Flag(symbolsDir.Join(ctx, "*.oat").String()).
378 Flag(symbolsDir.Join(ctx, "*.invocation").String())
379 rule.Command().Text("rm").Flag("-f").
380 Flag(outputDir.Join(ctx, "*.art").String()).
381 Flag(outputDir.Join(ctx, "*.oat").String()).
382 Flag(outputDir.Join(ctx, "*.invocation").String())
383
384 cmd := rule.Command()
385
386 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
387 if extraFlags == "" {
388 // Use ANDROID_LOG_TAGS to suppress most logging by default...
389 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
390 } else {
391 // ...unless the boot image is generated specifically for testing, then allow all logging.
392 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
393 }
394
395 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
396
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000397 cmd.Tool(globalSoong.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800398 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800399 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800400 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
401 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800402
Colin Cross69f59a32019-02-15 10:39:37 -0800403 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800404 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800405 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800406 }
407
Colin Cross44df5812019-02-15 23:06:46 -0800408 if global.DirtyImageObjects.Valid() {
409 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800410 }
411
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000412 if image.extension {
David Srbecky163bda62020-02-18 20:43:06 +0000413 artImage := image.primaryImages
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000414 cmd.
415 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
416 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
417 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
418 } else {
419 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
420 }
421
Colin Cross800fe132019-02-11 14:21:24 -0800422 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800423 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
424 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800425 Flag("--generate-debug-info").
426 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700427 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000428 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800429 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000430 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800431 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000432 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800433 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800434 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800435 FlagWithArg("--no-inline-from=", "core-oj.jar").
Ulya Trafimovich4fd35a22020-03-09 12:46:06 +0000436 Flag("--force-determinism").
Colin Cross800fe132019-02-11 14:21:24 -0800437 Flag("--abort-on-hard-verifier-error")
438
David Srbeckye920d2d2020-02-13 16:00:45 +0000439 // Use the default variant/features for host builds.
440 // The map below contains only device CPU info (which might be x86 on some devices).
441 if image.target.Os == android.Android {
442 cmd.FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch])
443 cmd.FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch])
444 }
445
Colin Cross44df5812019-02-15 23:06:46 -0800446 if global.BootFlags != "" {
447 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800448 }
449
450 if extraFlags != "" {
451 cmd.Flag(extraFlags)
452 }
453
Colin Cross0b9f31f2019-02-28 11:00:01 -0800454 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800455
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000456 installDir := filepath.Join("/", image.installSubdir, arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800457
Colin Cross800fe132019-02-11 14:21:24 -0800458 var vdexInstalls android.RuleBuilderInstalls
459 var unstrippedInstalls android.RuleBuilderInstalls
460
Colin Crossdf8eebe2019-04-09 15:29:41 -0700461 var zipFiles android.WritablePaths
462
Dan Willemsen0f416782019-06-13 21:44:53 +0000463 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
464 cmd.ImplicitOutput(artOrOat)
465 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800466
Dan Willemsen0f416782019-06-13 21:44:53 +0000467 // Install the .oat and .art files
468 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
469 }
Colin Cross800fe132019-02-11 14:21:24 -0800470
Dan Willemsen0f416782019-06-13 21:44:53 +0000471 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
472 cmd.ImplicitOutput(vdex)
473 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800474
David Srbeckye920d2d2020-02-13 16:00:45 +0000475 // Note that the vdex files are identical between architectures.
476 // Make rules will create symlinks to share them between architectures.
Colin Cross800fe132019-02-11 14:21:24 -0800477 vdexInstalls = append(vdexInstalls,
David Srbeckye920d2d2020-02-13 16:00:45 +0000478 android.RuleBuilderInstall{vdex, filepath.Join(installDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000479 }
480
481 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
482 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800483
484 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
485 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800486 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800487 }
488
David Srbeckye920d2d2020-02-13 16:00:45 +0000489 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+image.target.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800490
491 // save output and installed files for makevars
David Srbecky163bda62020-02-18 20:43:06 +0000492 image.installs = rule.Installs()
493 image.vdexInstalls = vdexInstalls
494 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700495
496 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800497}
498
499const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
500It is likely that the boot classpath is inconsistent.
501Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
502
David Srbecky163bda62020-02-18 20:43:06 +0000503func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000504 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000505 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000506
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700507 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000508 return nil
509 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000510 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000511 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800512
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000513 rule := android.NewRuleBuilder()
514 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800515
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000516 var bootImageProfile android.Path
517 if len(global.BootImageProfiles) > 1 {
518 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
519 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
520 bootImageProfile = combinedBootImageProfile
521 } else if len(global.BootImageProfiles) == 1 {
522 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000523 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
524 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000525 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000526 // No profile (not even a default one, which is the case on some branches
527 // like master-art-host that don't have frameworks/base).
528 // Return nil and continue without profile.
529 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000530 }
Colin Cross800fe132019-02-11 14:21:24 -0800531
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000532 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800533
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000534 rule.Command().
535 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000536 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000537 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000538 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
539 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000540 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800541
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000542 rule.Install(profile, "/system/etc/boot-image.prof")
543
544 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
545
546 image.profileInstalls = rule.Installs()
547
548 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000549 })
550 if profile == nil {
551 return nil // wrap nil into a typed pointer with value nil
552 }
553 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800554}
555
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000556var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
557
David Srbecky163bda62020-02-18 20:43:06 +0000558func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000559 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000560 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100561
562 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
563 return nil
564 }
565 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100566 rule := android.NewRuleBuilder()
567 rule.MissingDeps(missingDeps)
568
569 // Some branches like master-art-host don't have frameworks/base, so manually
570 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
571 // and if they do they'll get a missing deps error.
572 defaultProfile := "frameworks/base/config/boot-profile.txt"
573 path := android.ExistentPathForSource(ctx, defaultProfile)
574 var bootFrameworkProfile android.Path
575 if path.Valid() {
576 bootFrameworkProfile = path.Path()
577 } else {
578 missingDeps = append(missingDeps, defaultProfile)
579 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
580 }
581
582 profile := image.dir.Join(ctx, "boot.bprof")
583
584 rule.Command().
585 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000586 Tool(globalSoong.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100587 Flag("--generate-boot-profile").
588 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000589 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
590 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100591 FlagWithOutput("--reference-profile-file=", profile)
592
593 rule.Install(profile, "/system/etc/boot-image.bprof")
594 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
595 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
596
597 return profile
598 }).(android.WritablePath)
599}
600
601var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
602
Vladimir Markob92ae272020-04-01 13:52:27 +0100603func updatableBcpPackagesRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
604 if ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
605 return nil
606 }
607
608 return ctx.Config().Once(updatableBcpPackagesRuleKey, func() interface{} {
609 global := dexpreopt.GetGlobalConfig(ctx)
610 updatableModules := dexpreopt.GetJarsFromApexJarPairs(global.UpdatableBootJars)
611
612 // Collect `permitted_packages` for updatable boot jars.
613 var updatablePackages []string
614 ctx.VisitAllModules(func(module android.Module) {
615 if j, ok := module.(*Library); ok {
616 name := ctx.ModuleName(module)
617 if i := android.IndexList(name, updatableModules); i != -1 {
618 pp := j.properties.Permitted_packages
619 if len(pp) > 0 {
620 updatablePackages = append(updatablePackages, pp...)
621 } else {
622 ctx.Errorf("Missing permitted_packages for %s", name)
623 }
624 // Do not match the same library repeatedly.
625 updatableModules = append(updatableModules[:i], updatableModules[i+1:]...)
626 }
627 }
628 })
629
630 // Sort updatable packages to ensure deterministic ordering.
631 sort.Strings(updatablePackages)
632
633 updatableBcpPackagesName := "updatable-bcp-packages.txt"
634 updatableBcpPackages := image.dir.Join(ctx, updatableBcpPackagesName)
635
636 ctx.Build(pctx, android.BuildParams{
637 Rule: android.WriteFile,
638 Output: updatableBcpPackages,
639 Args: map[string]string{
640 // WriteFile automatically adds the last end-of-line.
641 "content": strings.Join(updatablePackages, "\\n"),
642 },
643 })
644
645 rule := android.NewRuleBuilder()
646 rule.MissingDeps(missingDeps)
647 rule.Install(updatableBcpPackages, "/system/etc/"+updatableBcpPackagesName)
648 // TODO: Rename `profileInstalls` to `extraInstalls`?
649 // Maybe even move the field out of the bootImageConfig into some higher level type?
650 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
651
652 return updatableBcpPackages
653 }).(android.WritablePath)
654}
655
656var updatableBcpPackagesRuleKey = android.NewOnceKey("updatableBcpPackagesRule")
657
David Srbecky163bda62020-02-18 20:43:06 +0000658func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800659 var allPhonies android.Paths
David Srbecky163bda62020-02-18 20:43:06 +0000660 for _, image := range image.variants {
661 arch := image.target.Arch.ArchType
David Srbeckye920d2d2020-02-13 16:00:45 +0000662 suffix := image.target.String()
Colin Crossc9a4c362019-02-26 21:13:48 -0800663 // Create a rule to call oatdump.
David Srbeckye920d2d2020-02-13 16:00:45 +0000664 output := android.PathForOutput(ctx, "boot."+suffix+".oatdump.txt")
Colin Crossc9a4c362019-02-26 21:13:48 -0800665 rule := android.NewRuleBuilder()
666 rule.Command().
667 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700668 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000669 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
670 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbecky163bda62020-02-18 20:43:06 +0000671 FlagWithArg("--image=", strings.Join(image.imageLocations, ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800672 FlagWithOutput("--output=", output).
673 FlagWithArg("--instruction-set=", arch.String())
David Srbeckye920d2d2020-02-13 16:00:45 +0000674 rule.Build(pctx, ctx, "dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800675
676 // Create a phony rule that depends on the output file and prints the path.
David Srbeckye920d2d2020-02-13 16:00:45 +0000677 phony := android.PathForPhony(ctx, "dump-oat-boot-"+suffix)
Colin Crossc9a4c362019-02-26 21:13:48 -0800678 rule = android.NewRuleBuilder()
679 rule.Command().
680 Implicit(output).
681 ImplicitOutput(phony).
682 Text("echo").FlagWithArg("Output in ", output.String())
David Srbeckye920d2d2020-02-13 16:00:45 +0000683 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+suffix, "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800684
685 allPhonies = append(allPhonies, phony)
686 }
687
688 phony := android.PathForPhony(ctx, "dump-oat-boot")
689 ctx.Build(pctx, android.BuildParams{
690 Rule: android.Phony,
691 Output: phony,
692 Inputs: allPhonies,
693 Description: "dump-oat-boot",
694 })
695
696}
697
Colin Cross2d00f0d2019-05-09 21:50:00 -0700698func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000699 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700700
701 ctx.Build(pctx, android.BuildParams{
702 Rule: android.WriteFile,
703 Output: path,
704 Args: map[string]string{
705 "content": string(data),
706 },
707 })
708}
709
Colin Cross44df5812019-02-15 23:06:46 -0800710// Export paths for default boot image to Make
711func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700712 if d.dexpreoptConfigForMake != nil {
713 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000714 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700715 }
716
Colin Cross44df5812019-02-15 23:06:46 -0800717 image := d.defaultBootImage
718 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800719 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000720 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
721 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000722
723 var imageNames []string
724 for _, current := range append(d.otherImages, image) {
725 imageNames = append(imageNames, current.name)
David Srbecky163bda62020-02-18 20:43:06 +0000726 for _, current := range current.variants {
David Srbeckye920d2d2020-02-13 16:00:45 +0000727 suffix := ""
728 if current.target.Os.Class == android.Host {
729 suffix = "_host"
730 }
731 sfx := current.name + suffix + "_" + current.target.Arch.ArchType.String()
David Srbecky163bda62020-02-18 20:43:06 +0000732 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, current.vdexInstalls.String())
733 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, current.images.String())
734 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(current.imagesDeps.Strings(), " "))
735 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, current.installs.String())
736 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, current.unstrippedInstalls.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000737 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800738
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000739 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(current.imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800740 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000741 }
742 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800743 }
Colin Cross800fe132019-02-11 14:21:24 -0800744}