blob: 508443a7ed4aede4ed5c74f05f1c70700f22833c [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"
Colin Crossc9a4c362019-02-26 21:13:48 -080019 "sort"
Colin Cross800fe132019-02-11 14:21:24 -080020 "strings"
21
22 "android/soong/android"
23 "android/soong/dexpreopt"
24
25 "github.com/google/blueprint/pathtools"
26 "github.com/google/blueprint/proptools"
27)
28
29func init() {
30 android.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory)
31}
32
33// The image "location" is a symbolic path that with multiarchitecture
34// support doesn't really exist on the device. Typically it is
35// /system/framework/boot.art and should be the same for all supported
36// architectures on the device. The concrete architecture specific
37// content actually ends up in a "filename" that contains an
38// architecture specific directory name such as arm, arm64, mips,
39// mips64, x86, x86_64.
40//
41// Here are some example values for an x86_64 / x86 configuration:
42//
43// bootImages["x86_64"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86_64/boot.art"
44// dexpreopt.PathToLocation(bootImages["x86_64"], "x86_64") = "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
45//
46// bootImages["x86"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86/boot.art"
47// dexpreopt.PathToLocation(bootImages["x86"])= "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
48//
49// The location is passed as an argument to the ART tools like dex2oat instead of the real path. The ART tools
50// will then reconstruct the real path, so the rules must have a dependency on the real path.
51
Colin Cross44df5812019-02-15 23:06:46 -080052type bootImageConfig struct {
53 name string
54 modules []string
55 dexLocations []string
56 dexPaths android.WritablePaths
57 dir android.OutputPath
58 symbolsDir android.OutputPath
Colin Crossc11e0c52019-05-08 15:18:22 -070059 targets []android.Target
Colin Cross44df5812019-02-15 23:06:46 -080060 images map[android.ArchType]android.OutputPath
Dan Willemsen0f416782019-06-13 21:44:53 +000061 imagesDeps map[android.ArchType]android.Paths
Colin Crossdf8eebe2019-04-09 15:29:41 -070062 zip android.WritablePath
Colin Cross800fe132019-02-11 14:21:24 -080063}
64
Dan Willemsen0f416782019-06-13 21:44:53 +000065func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) []android.OutputPath {
66 ret := make([]android.OutputPath, 0, len(image.modules)*len(exts))
67
68 // dex preopt on the bootclasspath produces multiple files. The first dex file
69 // is converted into to 'name'.art (to match the legacy assumption that 'name'.art
70 // exists), and the rest are converted to 'name'-<jar>.art.
71 // In addition, each .art file has an associated .oat and .vdex file, and an
72 // unstripped .oat file
73 for i, m := range image.modules {
74 name := image.name
75 if i != 0 {
76 name += "-" + m
77 }
78
79 for _, ext := range exts {
80 ret = append(ret, dir.Join(ctx, name+ext))
81 }
82 }
83
84 return ret
85}
86
Colin Cross44df5812019-02-15 23:06:46 -080087type bootImage struct {
88 bootImageConfig
Colin Cross800fe132019-02-11 14:21:24 -080089
Colin Cross44df5812019-02-15 23:06:46 -080090 installs map[android.ArchType]android.RuleBuilderInstalls
91 vdexInstalls map[android.ArchType]android.RuleBuilderInstalls
92 unstrippedInstalls map[android.ArchType]android.RuleBuilderInstalls
Colin Cross800fe132019-02-11 14:21:24 -080093
Colin Cross44df5812019-02-15 23:06:46 -080094 profileInstalls android.RuleBuilderInstalls
95}
Colin Cross800fe132019-02-11 14:21:24 -080096
Colin Cross44df5812019-02-15 23:06:46 -080097func newBootImage(ctx android.PathContext, config bootImageConfig) *bootImage {
98 image := &bootImage{
Nicolas Geoffray72892f12019-02-22 15:34:40 +000099 bootImageConfig: config,
Colin Cross800fe132019-02-11 14:21:24 -0800100
Colin Cross44df5812019-02-15 23:06:46 -0800101 installs: make(map[android.ArchType]android.RuleBuilderInstalls),
102 vdexInstalls: make(map[android.ArchType]android.RuleBuilderInstalls),
103 unstrippedInstalls: make(map[android.ArchType]android.RuleBuilderInstalls),
104 }
Colin Cross800fe132019-02-11 14:21:24 -0800105
Colin Cross44df5812019-02-15 23:06:46 -0800106 return image
Colin Cross800fe132019-02-11 14:21:24 -0800107}
108
109func concat(lists ...[]string) []string {
110 var size int
111 for _, l := range lists {
112 size += len(l)
113 }
114 ret := make([]string, 0, size)
115 for _, l := range lists {
116 ret = append(ret, l...)
117 }
118 return ret
119}
120
Colin Cross800fe132019-02-11 14:21:24 -0800121func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800122 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800123}
124
125func skipDexpreoptBootJars(ctx android.PathContext) bool {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000126 if dexpreoptGlobalConfig(ctx).DisablePreopt {
127 return true
128 }
129
Colin Cross800fe132019-02-11 14:21:24 -0800130 if ctx.Config().UnbundledBuild() {
131 return true
132 }
133
134 if len(ctx.Config().Targets[android.Android]) == 0 {
135 // Host-only build
136 return true
137 }
138
139 return false
140}
141
Colin Cross44df5812019-02-15 23:06:46 -0800142type dexpreoptBootJars struct {
143 defaultBootImage *bootImage
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000144 otherImages []*bootImage
Colin Cross2d00f0d2019-05-09 21:50:00 -0700145
146 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800147}
Colin Cross800fe132019-02-11 14:21:24 -0800148
149// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800150func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800151 if skipDexpreoptBootJars(ctx) {
152 return
153 }
154
Colin Cross2d00f0d2019-05-09 21:50:00 -0700155 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
156 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
157
Colin Cross44df5812019-02-15 23:06:46 -0800158 global := dexpreoptGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800159
160 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
161 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
162 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
163 // on ASAN settings.
164 if len(ctx.Config().SanitizeDevice()) == 1 &&
165 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800166 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800167 return
168 }
169
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000170 // Always create the default boot image first, to get a unique profile rule for all images.
Colin Cross44df5812019-02-15 23:06:46 -0800171 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000172 if global.GenerateApexImage {
173 d.otherImages = append(d.otherImages, buildBootImage(ctx, apexBootImageConfig(ctx)))
174 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800175
176 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800177}
178
179// buildBootImage takes a bootImageConfig, creates rules to build it, and returns a *bootImage.
180func buildBootImage(ctx android.SingletonContext, config bootImageConfig) *bootImage {
181 global := dexpreoptGlobalConfig(ctx)
182
183 image := newBootImage(ctx, config)
184
185 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800186
187 ctx.VisitAllModules(func(module android.Module) {
188 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800189 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800190 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800191 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800192 bootDexJars[i] = j.DexJar()
193 }
194 }
195 })
196
197 var missingDeps []string
198 // Ensure all modules were converted to paths
199 for i := range bootDexJars {
200 if bootDexJars[i] == nil {
201 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800202 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800203 bootDexJars[i] = android.PathForOutput(ctx, "missing")
204 } else {
205 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800206 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800207 }
208 }
209 }
210
211 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
212 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
213 // already been set up can find them.
214 for i := range bootDexJars {
215 ctx.Build(pctx, android.BuildParams{
216 Rule: android.Cp,
217 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800218 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800219 })
220 }
221
Colin Cross44df5812019-02-15 23:06:46 -0800222 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100223 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800224
Colin Crossdf8eebe2019-04-09 15:29:41 -0700225 var allFiles android.Paths
226
Colin Cross44df5812019-02-15 23:06:46 -0800227 if !global.DisablePreopt {
Colin Crossc11e0c52019-05-08 15:18:22 -0700228 for _, target := range image.targets {
229 files := buildBootImageRuleForArch(ctx, image, target.Arch.ArchType, profile, missingDeps)
230 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800231 }
232 }
Colin Cross44df5812019-02-15 23:06:46 -0800233
Colin Crossdf8eebe2019-04-09 15:29:41 -0700234 if image.zip != nil {
235 rule := android.NewRuleBuilder()
236 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700237 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700238 FlagWithOutput("-o ", image.zip).
239 FlagWithArg("-C ", image.dir.String()).
240 FlagWithInputList("-f ", allFiles, " -f ")
241
242 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
243 }
244
Colin Cross44df5812019-02-15 23:06:46 -0800245 return image
Colin Cross800fe132019-02-11 14:21:24 -0800246}
247
Colin Cross44df5812019-02-15 23:06:46 -0800248func buildBootImageRuleForArch(ctx android.SingletonContext, image *bootImage,
Colin Crossdf8eebe2019-04-09 15:29:41 -0700249 arch android.ArchType, profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800250
Colin Cross44df5812019-02-15 23:06:46 -0800251 global := dexpreoptGlobalConfig(ctx)
252
253 symbolsDir := image.symbolsDir.Join(ctx, "system/framework", arch.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000254 symbolsFile := symbolsDir.Join(ctx, image.name+".oat")
Colin Cross44df5812019-02-15 23:06:46 -0800255 outputDir := image.dir.Join(ctx, "system/framework", arch.String())
256 outputPath := image.images[arch]
Colin Cross69f59a32019-02-15 10:39:37 -0800257 oatLocation := pathtools.ReplaceExtension(dexpreopt.PathToLocation(outputPath, arch), "oat")
Colin Cross800fe132019-02-11 14:21:24 -0800258
259 rule := android.NewRuleBuilder()
260 rule.MissingDeps(missingDeps)
261
262 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
263 rule.Command().Text("rm").Flag("-f").
264 Flag(symbolsDir.Join(ctx, "*.art").String()).
265 Flag(symbolsDir.Join(ctx, "*.oat").String()).
266 Flag(symbolsDir.Join(ctx, "*.invocation").String())
267 rule.Command().Text("rm").Flag("-f").
268 Flag(outputDir.Join(ctx, "*.art").String()).
269 Flag(outputDir.Join(ctx, "*.oat").String()).
270 Flag(outputDir.Join(ctx, "*.invocation").String())
271
272 cmd := rule.Command()
273
274 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
275 if extraFlags == "" {
276 // Use ANDROID_LOG_TAGS to suppress most logging by default...
277 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
278 } else {
279 // ...unless the boot image is generated specifically for testing, then allow all logging.
280 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
281 }
282
283 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
284
Colin Cross44df5812019-02-15 23:06:46 -0800285 cmd.Tool(global.Tools.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800286 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800287 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800288 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
289 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800290
Colin Cross69f59a32019-02-15 10:39:37 -0800291 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800292 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800293 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800294 }
295
Colin Cross44df5812019-02-15 23:06:46 -0800296 if global.DirtyImageObjects.Valid() {
297 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800298 }
299
300 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800301 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
302 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800303 Flag("--generate-debug-info").
304 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700305 Flag("--image-format=lz4hc").
Colin Cross69f59a32019-02-15 10:39:37 -0800306 FlagWithOutput("--oat-symbols=", symbolsFile).
Colin Cross800fe132019-02-11 14:21:24 -0800307 Flag("--strip").
Colin Cross69f59a32019-02-15 10:39:37 -0800308 FlagWithOutput("--oat-file=", outputPath.ReplaceExtension(ctx, "oat")).
Colin Cross800fe132019-02-11 14:21:24 -0800309 FlagWithArg("--oat-location=", oatLocation).
Colin Cross69f59a32019-02-15 10:39:37 -0800310 FlagWithOutput("--image=", outputPath).
Colin Cross800fe132019-02-11 14:21:24 -0800311 FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress()).
312 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800313 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
314 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
315 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800316 FlagWithArg("--no-inline-from=", "core-oj.jar").
317 Flag("--abort-on-hard-verifier-error")
318
Colin Cross44df5812019-02-15 23:06:46 -0800319 if global.BootFlags != "" {
320 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800321 }
322
323 if extraFlags != "" {
324 cmd.Flag(extraFlags)
325 }
326
Colin Cross0b9f31f2019-02-28 11:00:01 -0800327 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800328
329 installDir := filepath.Join("/system/framework", arch.String())
330 vdexInstallDir := filepath.Join("/system/framework")
331
Colin Cross800fe132019-02-11 14:21:24 -0800332 var vdexInstalls android.RuleBuilderInstalls
333 var unstrippedInstalls android.RuleBuilderInstalls
334
Colin Crossdf8eebe2019-04-09 15:29:41 -0700335 var zipFiles android.WritablePaths
336
Dan Willemsen0f416782019-06-13 21:44:53 +0000337 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
338 cmd.ImplicitOutput(artOrOat)
339 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800340
Dan Willemsen0f416782019-06-13 21:44:53 +0000341 // Install the .oat and .art files
342 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
343 }
Colin Cross800fe132019-02-11 14:21:24 -0800344
Dan Willemsen0f416782019-06-13 21:44:53 +0000345 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
346 cmd.ImplicitOutput(vdex)
347 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800348
349 // The vdex files are identical between architectures, install them to a shared location. The Make rules will
350 // only use the install rules for one architecture, and will create symlinks into the architecture-specific
351 // directories.
352 vdexInstalls = append(vdexInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800353 android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000354 }
355
356 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
357 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800358
359 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
360 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800361 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800362 }
363
Colin Cross44df5812019-02-15 23:06:46 -0800364 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800365
366 // save output and installed files for makevars
Colin Cross44df5812019-02-15 23:06:46 -0800367 image.installs[arch] = rule.Installs()
368 image.vdexInstalls[arch] = vdexInstalls
369 image.unstrippedInstalls[arch] = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700370
371 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800372}
373
374const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
375It is likely that the boot classpath is inconsistent.
376Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
377
Colin Cross44df5812019-02-15 23:06:46 -0800378func bootImageProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000379 global := dexpreoptGlobalConfig(ctx)
380
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700381 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000382 return nil
383 }
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000384 return ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000385 tools := global.Tools
Colin Cross800fe132019-02-11 14:21:24 -0800386
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000387 rule := android.NewRuleBuilder()
388 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800389
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000390 var bootImageProfile android.Path
391 if len(global.BootImageProfiles) > 1 {
392 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
393 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
394 bootImageProfile = combinedBootImageProfile
395 } else if len(global.BootImageProfiles) == 1 {
396 bootImageProfile = global.BootImageProfiles[0]
397 } else {
398 // If not set, use the default. Some branches like master-art-host don't have frameworks/base, so manually
399 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
400 // and if they do they'll get a missing deps error.
401 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
402 path := android.ExistentPathForSource(ctx, defaultProfile)
403 if path.Valid() {
404 bootImageProfile = path.Path()
405 } else {
406 missingDeps = append(missingDeps, defaultProfile)
407 bootImageProfile = android.PathForOutput(ctx, "missing")
408 }
409 }
Colin Cross800fe132019-02-11 14:21:24 -0800410
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000411 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800412
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000413 rule.Command().
414 Text(`ANDROID_LOG_TAGS="*:e"`).
415 Tool(tools.Profman).
416 FlagWithInput("--create-profile-from=", bootImageProfile).
417 FlagForEachInput("--apk=", image.dexPaths.Paths()).
418 FlagForEachArg("--dex-location=", image.dexLocations).
419 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800420
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000421 rule.Install(profile, "/system/etc/boot-image.prof")
422
423 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
424
425 image.profileInstalls = rule.Installs()
426
427 return profile
428 }).(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800429}
430
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000431var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
432
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100433func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
434 global := dexpreoptGlobalConfig(ctx)
435
436 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
437 return nil
438 }
439 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
440 tools := global.Tools
441
442 rule := android.NewRuleBuilder()
443 rule.MissingDeps(missingDeps)
444
445 // Some branches like master-art-host don't have frameworks/base, so manually
446 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
447 // and if they do they'll get a missing deps error.
448 defaultProfile := "frameworks/base/config/boot-profile.txt"
449 path := android.ExistentPathForSource(ctx, defaultProfile)
450 var bootFrameworkProfile android.Path
451 if path.Valid() {
452 bootFrameworkProfile = path.Path()
453 } else {
454 missingDeps = append(missingDeps, defaultProfile)
455 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
456 }
457
458 profile := image.dir.Join(ctx, "boot.bprof")
459
460 rule.Command().
461 Text(`ANDROID_LOG_TAGS="*:e"`).
462 Tool(tools.Profman).
463 Flag("--generate-boot-profile").
464 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
465 FlagForEachInput("--apk=", image.dexPaths.Paths()).
466 FlagForEachArg("--dex-location=", image.dexLocations).
467 FlagWithOutput("--reference-profile-file=", profile)
468
469 rule.Install(profile, "/system/etc/boot-image.bprof")
470 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
471 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
472
473 return profile
474 }).(android.WritablePath)
475}
476
477var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
478
Colin Crossc9a4c362019-02-26 21:13:48 -0800479func dumpOatRules(ctx android.SingletonContext, image *bootImage) {
480 var archs []android.ArchType
481 for arch := range image.images {
482 archs = append(archs, arch)
483 }
484 sort.Slice(archs, func(i, j int) bool { return archs[i].String() < archs[j].String() })
485
486 var allPhonies android.Paths
487 for _, arch := range archs {
488 // Create a rule to call oatdump.
489 output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt")
490 rule := android.NewRuleBuilder()
491 rule.Command().
492 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700493 BuiltTool(ctx, "oatdumpd").
Colin Crossc9a4c362019-02-26 21:13:48 -0800494 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPaths.Paths(), ":").
495 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocations, ":").
496 FlagWithArg("--image=", dexpreopt.PathToLocation(image.images[arch], arch)).Implicit(image.images[arch]).
497 FlagWithOutput("--output=", output).
498 FlagWithArg("--instruction-set=", arch.String())
499 rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
500
501 // Create a phony rule that depends on the output file and prints the path.
502 phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String())
503 rule = android.NewRuleBuilder()
504 rule.Command().
505 Implicit(output).
506 ImplicitOutput(phony).
507 Text("echo").FlagWithArg("Output in ", output.String())
508 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
509
510 allPhonies = append(allPhonies, phony)
511 }
512
513 phony := android.PathForPhony(ctx, "dump-oat-boot")
514 ctx.Build(pctx, android.BuildParams{
515 Rule: android.Phony,
516 Output: phony,
517 Inputs: allPhonies,
518 Description: "dump-oat-boot",
519 })
520
521}
522
Colin Cross2d00f0d2019-05-09 21:50:00 -0700523func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
524 data := dexpreoptGlobalConfigRaw(ctx).data
525
526 ctx.Build(pctx, android.BuildParams{
527 Rule: android.WriteFile,
528 Output: path,
529 Args: map[string]string{
530 "content": string(data),
531 },
532 })
533}
534
Colin Cross44df5812019-02-15 23:06:46 -0800535// Export paths for default boot image to Make
536func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700537 if d.dexpreoptConfigForMake != nil {
538 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
539 }
540
Colin Cross44df5812019-02-15 23:06:46 -0800541 image := d.defaultBootImage
542 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800543 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Colin Cross44df5812019-02-15 23:06:46 -0800544 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPaths.Strings(), " "))
545 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocations, " "))
Colin Crossdf8eebe2019-04-09 15:29:41 -0700546 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+image.name, image.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000547
548 var imageNames []string
549 for _, current := range append(d.otherImages, image) {
550 imageNames = append(imageNames, current.name)
Colin Cross91268c62019-04-11 14:07:04 -0700551 var arches []android.ArchType
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000552 for arch, _ := range current.images {
Colin Cross91268c62019-04-11 14:07:04 -0700553 arches = append(arches, arch)
554 }
555
556 sort.Slice(arches, func(i, j int) bool { return arches[i].String() < arches[j].String() })
557
558 for _, arch := range arches {
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000559 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.vdexInstalls[arch].String())
560 ctx.Strict("DEXPREOPT_IMAGE_"+current.name+"_"+arch.String(), current.images[arch].String())
Dan Willemsen0f416782019-06-13 21:44:53 +0000561 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+current.name+"_"+arch.String(), strings.Join(current.imagesDeps[arch].Strings(), " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000562 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.installs[arch].String())
563 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.unstrippedInstalls[arch].String())
Colin Crossdf8eebe2019-04-09 15:29:41 -0700564 if current.zip != nil {
565 }
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000566 }
567 }
568 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800569 }
Colin Cross800fe132019-02-11 14:21:24 -0800570}