blob: dbf61151aed202825dfbfd02e0f4f3ca7f5cfadd [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
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +000054 stem string
Colin Cross44df5812019-02-15 23:06:46 -080055 modules []string
56 dexLocations []string
57 dexPaths android.WritablePaths
58 dir android.OutputPath
59 symbolsDir android.OutputPath
Colin Crossc11e0c52019-05-08 15:18:22 -070060 targets []android.Target
Colin Cross44df5812019-02-15 23:06:46 -080061 images map[android.ArchType]android.OutputPath
Dan Willemsen0f416782019-06-13 21:44:53 +000062 imagesDeps map[android.ArchType]android.Paths
Colin Crossdf8eebe2019-04-09 15:29:41 -070063 zip android.WritablePath
Colin Cross800fe132019-02-11 14:21:24 -080064}
65
Dan Willemsen0f416782019-06-13 21:44:53 +000066func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) []android.OutputPath {
67 ret := make([]android.OutputPath, 0, len(image.modules)*len(exts))
68
69 // dex preopt on the bootclasspath produces multiple files. The first dex file
70 // is converted into to 'name'.art (to match the legacy assumption that 'name'.art
71 // exists), and the rest are converted to 'name'-<jar>.art.
72 // In addition, each .art file has an associated .oat and .vdex file, and an
73 // unstripped .oat file
74 for i, m := range image.modules {
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +000075 name := image.stem
Dan Willemsen0f416782019-06-13 21:44:53 +000076 if i != 0 {
Jiyong Park0b238752019-10-29 11:23:10 +090077 name += "-" + stemOf(m)
Dan Willemsen0f416782019-06-13 21:44:53 +000078 }
79
80 for _, ext := range exts {
81 ret = append(ret, dir.Join(ctx, name+ext))
82 }
83 }
84
85 return ret
86}
87
Colin Cross44df5812019-02-15 23:06:46 -080088type bootImage struct {
89 bootImageConfig
Colin Cross800fe132019-02-11 14:21:24 -080090
Colin Cross44df5812019-02-15 23:06:46 -080091 installs map[android.ArchType]android.RuleBuilderInstalls
92 vdexInstalls map[android.ArchType]android.RuleBuilderInstalls
93 unstrippedInstalls map[android.ArchType]android.RuleBuilderInstalls
Colin Cross800fe132019-02-11 14:21:24 -080094
Colin Cross44df5812019-02-15 23:06:46 -080095 profileInstalls android.RuleBuilderInstalls
96}
Colin Cross800fe132019-02-11 14:21:24 -080097
Colin Cross44df5812019-02-15 23:06:46 -080098func newBootImage(ctx android.PathContext, config bootImageConfig) *bootImage {
99 image := &bootImage{
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000100 bootImageConfig: config,
Colin Cross800fe132019-02-11 14:21:24 -0800101
Colin Cross44df5812019-02-15 23:06:46 -0800102 installs: make(map[android.ArchType]android.RuleBuilderInstalls),
103 vdexInstalls: make(map[android.ArchType]android.RuleBuilderInstalls),
104 unstrippedInstalls: make(map[android.ArchType]android.RuleBuilderInstalls),
105 }
Colin Cross800fe132019-02-11 14:21:24 -0800106
Colin Cross44df5812019-02-15 23:06:46 -0800107 return image
Colin Cross800fe132019-02-11 14:21:24 -0800108}
109
110func concat(lists ...[]string) []string {
111 var size int
112 for _, l := range lists {
113 size += len(l)
114 }
115 ret := make([]string, 0, size)
116 for _, l := range lists {
117 ret = append(ret, l...)
118 }
119 return ret
120}
121
Colin Cross800fe132019-02-11 14:21:24 -0800122func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800123 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800124}
125
126func skipDexpreoptBootJars(ctx android.PathContext) bool {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000127 if dexpreoptGlobalConfig(ctx).DisablePreopt {
128 return true
129 }
130
Colin Cross800fe132019-02-11 14:21:24 -0800131 if ctx.Config().UnbundledBuild() {
132 return true
133 }
134
135 if len(ctx.Config().Targets[android.Android]) == 0 {
136 // Host-only build
137 return true
138 }
139
140 return false
141}
142
Colin Cross44df5812019-02-15 23:06:46 -0800143type dexpreoptBootJars struct {
144 defaultBootImage *bootImage
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000145 otherImages []*bootImage
Colin Cross2d00f0d2019-05-09 21:50:00 -0700146
147 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800148}
Colin Cross800fe132019-02-11 14:21:24 -0800149
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000150// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
151func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.Paths {
152 if skipDexpreoptBootJars(ctx) {
153 return nil
154 }
155 return artBootImageConfig(ctx).imagesDeps
156}
157
Colin Cross800fe132019-02-11 14:21:24 -0800158// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800159func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800160 if skipDexpreoptBootJars(ctx) {
161 return
162 }
163
Colin Cross2d00f0d2019-05-09 21:50:00 -0700164 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
165 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
166
Colin Cross44df5812019-02-15 23:06:46 -0800167 global := dexpreoptGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800168
169 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
170 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
171 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
172 // on ASAN settings.
173 if len(ctx.Config().SanitizeDevice()) == 1 &&
174 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800175 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800176 return
177 }
178
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000179 // Always create the default boot image first, to get a unique profile rule for all images.
Colin Cross44df5812019-02-15 23:06:46 -0800180 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000181 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
182 buildBootImage(ctx, artBootImageConfig(ctx))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000183 if global.GenerateApexImage {
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000184 // Create boot images for the JIT-zygote experiment.
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000185 d.otherImages = append(d.otherImages, buildBootImage(ctx, apexBootImageConfig(ctx)))
186 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800187
188 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800189}
190
191// buildBootImage takes a bootImageConfig, creates rules to build it, and returns a *bootImage.
192func buildBootImage(ctx android.SingletonContext, config bootImageConfig) *bootImage {
Colin Cross44df5812019-02-15 23:06:46 -0800193 image := newBootImage(ctx, config)
194
195 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800196
197 ctx.VisitAllModules(func(module android.Module) {
198 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800199 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800200 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800201 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800202 bootDexJars[i] = j.DexJar()
203 }
204 }
205 })
206
207 var missingDeps []string
208 // Ensure all modules were converted to paths
209 for i := range bootDexJars {
210 if bootDexJars[i] == nil {
211 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800212 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800213 bootDexJars[i] = android.PathForOutput(ctx, "missing")
214 } else {
215 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800216 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800217 }
218 }
219 }
220
221 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
222 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
223 // already been set up can find them.
224 for i := range bootDexJars {
225 ctx.Build(pctx, android.BuildParams{
226 Rule: android.Cp,
227 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800228 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800229 })
230 }
231
Colin Cross44df5812019-02-15 23:06:46 -0800232 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100233 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800234
Colin Crossdf8eebe2019-04-09 15:29:41 -0700235 var allFiles android.Paths
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000236 for _, target := range image.targets {
237 files := buildBootImageRuleForArch(ctx, image, target.Arch.ArchType, profile, missingDeps)
238 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800239 }
Colin Cross44df5812019-02-15 23:06:46 -0800240
Colin Crossdf8eebe2019-04-09 15:29:41 -0700241 if image.zip != nil {
242 rule := android.NewRuleBuilder()
243 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700244 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700245 FlagWithOutput("-o ", image.zip).
246 FlagWithArg("-C ", image.dir.String()).
247 FlagWithInputList("-f ", allFiles, " -f ")
248
249 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
250 }
251
Colin Cross44df5812019-02-15 23:06:46 -0800252 return image
Colin Cross800fe132019-02-11 14:21:24 -0800253}
254
Colin Cross44df5812019-02-15 23:06:46 -0800255func buildBootImageRuleForArch(ctx android.SingletonContext, image *bootImage,
Colin Crossdf8eebe2019-04-09 15:29:41 -0700256 arch android.ArchType, profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800257
Colin Cross44df5812019-02-15 23:06:46 -0800258 global := dexpreoptGlobalConfig(ctx)
259
260 symbolsDir := image.symbolsDir.Join(ctx, "system/framework", arch.String())
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000261 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
Colin Cross44df5812019-02-15 23:06:46 -0800262 outputDir := image.dir.Join(ctx, "system/framework", arch.String())
263 outputPath := image.images[arch]
Colin Cross69f59a32019-02-15 10:39:37 -0800264 oatLocation := pathtools.ReplaceExtension(dexpreopt.PathToLocation(outputPath, arch), "oat")
Colin Cross800fe132019-02-11 14:21:24 -0800265
266 rule := android.NewRuleBuilder()
267 rule.MissingDeps(missingDeps)
268
269 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
270 rule.Command().Text("rm").Flag("-f").
271 Flag(symbolsDir.Join(ctx, "*.art").String()).
272 Flag(symbolsDir.Join(ctx, "*.oat").String()).
273 Flag(symbolsDir.Join(ctx, "*.invocation").String())
274 rule.Command().Text("rm").Flag("-f").
275 Flag(outputDir.Join(ctx, "*.art").String()).
276 Flag(outputDir.Join(ctx, "*.oat").String()).
277 Flag(outputDir.Join(ctx, "*.invocation").String())
278
279 cmd := rule.Command()
280
281 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
282 if extraFlags == "" {
283 // Use ANDROID_LOG_TAGS to suppress most logging by default...
284 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
285 } else {
286 // ...unless the boot image is generated specifically for testing, then allow all logging.
287 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
288 }
289
290 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
291
Colin Cross44df5812019-02-15 23:06:46 -0800292 cmd.Tool(global.Tools.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800293 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800294 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800295 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
296 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800297
Colin Cross69f59a32019-02-15 10:39:37 -0800298 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800299 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800300 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800301 }
302
Colin Cross44df5812019-02-15 23:06:46 -0800303 if global.DirtyImageObjects.Valid() {
304 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800305 }
306
307 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800308 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
309 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800310 Flag("--generate-debug-info").
311 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700312 Flag("--image-format=lz4hc").
Colin Cross69f59a32019-02-15 10:39:37 -0800313 FlagWithOutput("--oat-symbols=", symbolsFile).
Colin Cross800fe132019-02-11 14:21:24 -0800314 Flag("--strip").
Colin Cross69f59a32019-02-15 10:39:37 -0800315 FlagWithOutput("--oat-file=", outputPath.ReplaceExtension(ctx, "oat")).
Colin Cross800fe132019-02-11 14:21:24 -0800316 FlagWithArg("--oat-location=", oatLocation).
Colin Cross69f59a32019-02-15 10:39:37 -0800317 FlagWithOutput("--image=", outputPath).
Colin Cross800fe132019-02-11 14:21:24 -0800318 FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress()).
319 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800320 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
321 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
322 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800323 FlagWithArg("--no-inline-from=", "core-oj.jar").
324 Flag("--abort-on-hard-verifier-error")
325
Colin Cross44df5812019-02-15 23:06:46 -0800326 if global.BootFlags != "" {
327 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800328 }
329
330 if extraFlags != "" {
331 cmd.Flag(extraFlags)
332 }
333
Colin Cross0b9f31f2019-02-28 11:00:01 -0800334 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800335
336 installDir := filepath.Join("/system/framework", arch.String())
337 vdexInstallDir := filepath.Join("/system/framework")
338
Colin Cross800fe132019-02-11 14:21:24 -0800339 var vdexInstalls android.RuleBuilderInstalls
340 var unstrippedInstalls android.RuleBuilderInstalls
341
Colin Crossdf8eebe2019-04-09 15:29:41 -0700342 var zipFiles android.WritablePaths
343
Dan Willemsen0f416782019-06-13 21:44:53 +0000344 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
345 cmd.ImplicitOutput(artOrOat)
346 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800347
Dan Willemsen0f416782019-06-13 21:44:53 +0000348 // Install the .oat and .art files
349 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
350 }
Colin Cross800fe132019-02-11 14:21:24 -0800351
Dan Willemsen0f416782019-06-13 21:44:53 +0000352 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
353 cmd.ImplicitOutput(vdex)
354 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800355
356 // The vdex files are identical between architectures, install them to a shared location. The Make rules will
357 // only use the install rules for one architecture, and will create symlinks into the architecture-specific
358 // directories.
359 vdexInstalls = append(vdexInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800360 android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000361 }
362
363 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
364 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800365
366 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
367 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800368 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800369 }
370
Colin Cross44df5812019-02-15 23:06:46 -0800371 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800372
373 // save output and installed files for makevars
Colin Cross44df5812019-02-15 23:06:46 -0800374 image.installs[arch] = rule.Installs()
375 image.vdexInstalls[arch] = vdexInstalls
376 image.unstrippedInstalls[arch] = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700377
378 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800379}
380
381const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
382It is likely that the boot classpath is inconsistent.
383Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
384
Colin Cross44df5812019-02-15 23:06:46 -0800385func bootImageProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000386 global := dexpreoptGlobalConfig(ctx)
387
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700388 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000389 return nil
390 }
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000391 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000392 tools := global.Tools
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000393 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800394
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000395 rule := android.NewRuleBuilder()
396 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800397
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000398 var bootImageProfile android.Path
399 if len(global.BootImageProfiles) > 1 {
400 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
401 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
402 bootImageProfile = combinedBootImageProfile
403 } else if len(global.BootImageProfiles) == 1 {
404 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000405 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
406 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000407 } else {
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000408 // No profile (not even a default one, which is the case on some branches
409 // like master-art-host that don't have frameworks/base).
410 // Return nil and continue without profile.
411 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000412 }
Colin Cross800fe132019-02-11 14:21:24 -0800413
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000414 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800415
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000416 rule.Command().
417 Text(`ANDROID_LOG_TAGS="*:e"`).
418 Tool(tools.Profman).
419 FlagWithInput("--create-profile-from=", bootImageProfile).
420 FlagForEachInput("--apk=", image.dexPaths.Paths()).
421 FlagForEachArg("--dex-location=", image.dexLocations).
422 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800423
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000424 rule.Install(profile, "/system/etc/boot-image.prof")
425
426 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
427
428 image.profileInstalls = rule.Installs()
429
430 return profile
Ulyana Trafimovich66b3e992019-11-06 17:20:49 +0000431 })
432 if profile == nil {
433 return nil // wrap nil into a typed pointer with value nil
434 }
435 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800436}
437
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000438var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
439
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100440func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
441 global := dexpreoptGlobalConfig(ctx)
442
443 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
444 return nil
445 }
446 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
447 tools := global.Tools
448
449 rule := android.NewRuleBuilder()
450 rule.MissingDeps(missingDeps)
451
452 // Some branches like master-art-host don't have frameworks/base, so manually
453 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
454 // and if they do they'll get a missing deps error.
455 defaultProfile := "frameworks/base/config/boot-profile.txt"
456 path := android.ExistentPathForSource(ctx, defaultProfile)
457 var bootFrameworkProfile android.Path
458 if path.Valid() {
459 bootFrameworkProfile = path.Path()
460 } else {
461 missingDeps = append(missingDeps, defaultProfile)
462 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
463 }
464
465 profile := image.dir.Join(ctx, "boot.bprof")
466
467 rule.Command().
468 Text(`ANDROID_LOG_TAGS="*:e"`).
469 Tool(tools.Profman).
470 Flag("--generate-boot-profile").
471 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
472 FlagForEachInput("--apk=", image.dexPaths.Paths()).
473 FlagForEachArg("--dex-location=", image.dexLocations).
474 FlagWithOutput("--reference-profile-file=", profile)
475
476 rule.Install(profile, "/system/etc/boot-image.bprof")
477 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
478 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
479
480 return profile
481 }).(android.WritablePath)
482}
483
484var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
485
Colin Crossc9a4c362019-02-26 21:13:48 -0800486func dumpOatRules(ctx android.SingletonContext, image *bootImage) {
487 var archs []android.ArchType
488 for arch := range image.images {
489 archs = append(archs, arch)
490 }
491 sort.Slice(archs, func(i, j int) bool { return archs[i].String() < archs[j].String() })
492
493 var allPhonies android.Paths
494 for _, arch := range archs {
495 // Create a rule to call oatdump.
496 output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt")
497 rule := android.NewRuleBuilder()
498 rule.Command().
499 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700500 BuiltTool(ctx, "oatdumpd").
Colin Crossc9a4c362019-02-26 21:13:48 -0800501 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPaths.Paths(), ":").
502 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocations, ":").
503 FlagWithArg("--image=", dexpreopt.PathToLocation(image.images[arch], arch)).Implicit(image.images[arch]).
504 FlagWithOutput("--output=", output).
505 FlagWithArg("--instruction-set=", arch.String())
506 rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
507
508 // Create a phony rule that depends on the output file and prints the path.
509 phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String())
510 rule = android.NewRuleBuilder()
511 rule.Command().
512 Implicit(output).
513 ImplicitOutput(phony).
514 Text("echo").FlagWithArg("Output in ", output.String())
515 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
516
517 allPhonies = append(allPhonies, phony)
518 }
519
520 phony := android.PathForPhony(ctx, "dump-oat-boot")
521 ctx.Build(pctx, android.BuildParams{
522 Rule: android.Phony,
523 Output: phony,
524 Inputs: allPhonies,
525 Description: "dump-oat-boot",
526 })
527
528}
529
Colin Cross2d00f0d2019-05-09 21:50:00 -0700530func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
531 data := dexpreoptGlobalConfigRaw(ctx).data
532
533 ctx.Build(pctx, android.BuildParams{
534 Rule: android.WriteFile,
535 Output: path,
536 Args: map[string]string{
537 "content": string(data),
538 },
539 })
540}
541
Colin Cross44df5812019-02-15 23:06:46 -0800542// Export paths for default boot image to Make
543func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700544 if d.dexpreoptConfigForMake != nil {
545 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
546 }
547
Colin Cross44df5812019-02-15 23:06:46 -0800548 image := d.defaultBootImage
549 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800550 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Colin Cross44df5812019-02-15 23:06:46 -0800551 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPaths.Strings(), " "))
552 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocations, " "))
Colin Crossdf8eebe2019-04-09 15:29:41 -0700553 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+image.name, image.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000554
555 var imageNames []string
556 for _, current := range append(d.otherImages, image) {
557 imageNames = append(imageNames, current.name)
Colin Cross91268c62019-04-11 14:07:04 -0700558 var arches []android.ArchType
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000559 for arch, _ := range current.images {
Colin Cross91268c62019-04-11 14:07:04 -0700560 arches = append(arches, arch)
561 }
562
563 sort.Slice(arches, func(i, j int) bool { return arches[i].String() < arches[j].String() })
564
565 for _, arch := range arches {
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000566 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.vdexInstalls[arch].String())
567 ctx.Strict("DEXPREOPT_IMAGE_"+current.name+"_"+arch.String(), current.images[arch].String())
Dan Willemsen0f416782019-06-13 21:44:53 +0000568 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+current.name+"_"+arch.String(), strings.Join(current.imagesDeps[arch].Strings(), " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000569 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.installs[arch].String())
570 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.unstrippedInstalls[arch].String())
Colin Crossdf8eebe2019-04-09 15:29:41 -0700571 if current.zip != nil {
572 }
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000573 }
574 }
575 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800576 }
Colin Cross800fe132019-02-11 14:21:24 -0800577}