blob: a29665e54f572f73110ab905abd023dc84dfe31b [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 Trafimovichde534412019-11-08 10:51:01 +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 Trafimovichde534412019-11-08 10:51:01 +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
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000143func skipDexpreoptArtBootJars(ctx android.BuilderContext) bool {
144 // with EMMA_INSTRUMENT_FRAMEWORK=true ART boot class path libraries have dependencies on framework,
145 // therefore dexpreopt ART libraries cannot be dexpreopted in isolation => no ART boot image
146 return ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK")
147}
148
Colin Cross44df5812019-02-15 23:06:46 -0800149type dexpreoptBootJars struct {
150 defaultBootImage *bootImage
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000151 otherImages []*bootImage
Colin Cross2d00f0d2019-05-09 21:50:00 -0700152
153 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800154}
Colin Cross800fe132019-02-11 14:21:24 -0800155
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000156// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
157func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.Paths {
158 if skipDexpreoptBootJars(ctx) || skipDexpreoptArtBootJars(ctx) {
159 return nil
160 }
161 return artBootImageConfig(ctx).imagesDeps
162}
163
Colin Cross800fe132019-02-11 14:21:24 -0800164// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800165func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800166 if skipDexpreoptBootJars(ctx) {
167 return
168 }
169
Colin Cross2d00f0d2019-05-09 21:50:00 -0700170 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
171 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
172
Colin Cross44df5812019-02-15 23:06:46 -0800173 global := dexpreoptGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800174
175 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
176 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
177 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
178 // on ASAN settings.
179 if len(ctx.Config().SanitizeDevice()) == 1 &&
180 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800181 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800182 return
183 }
184
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000185 // Always create the default boot image first, to get a unique profile rule for all images.
Colin Cross44df5812019-02-15 23:06:46 -0800186 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000187 if !skipDexpreoptArtBootJars(ctx) {
188 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
189 buildBootImage(ctx, artBootImageConfig(ctx))
190 }
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000191 if global.GenerateApexImage {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000192 // Create boot images for the JIT-zygote experiment.
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000193 d.otherImages = append(d.otherImages, buildBootImage(ctx, apexBootImageConfig(ctx)))
194 }
Colin Crossc9a4c362019-02-26 21:13:48 -0800195
196 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800197}
198
199// buildBootImage takes a bootImageConfig, creates rules to build it, and returns a *bootImage.
200func buildBootImage(ctx android.SingletonContext, config bootImageConfig) *bootImage {
Colin Cross44df5812019-02-15 23:06:46 -0800201 image := newBootImage(ctx, config)
202
203 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800204
205 ctx.VisitAllModules(func(module android.Module) {
206 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800207 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800208 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800209 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800210 bootDexJars[i] = j.DexJar()
211 }
212 }
213 })
214
215 var missingDeps []string
216 // Ensure all modules were converted to paths
217 for i := range bootDexJars {
218 if bootDexJars[i] == nil {
219 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800220 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800221 bootDexJars[i] = android.PathForOutput(ctx, "missing")
222 } else {
223 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800224 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800225 }
226 }
227 }
228
229 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
230 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
231 // already been set up can find them.
232 for i := range bootDexJars {
233 ctx.Build(pctx, android.BuildParams{
234 Rule: android.Cp,
235 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800236 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800237 })
238 }
239
Colin Cross44df5812019-02-15 23:06:46 -0800240 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100241 bootFrameworkProfileRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800242
Colin Crossdf8eebe2019-04-09 15:29:41 -0700243 var allFiles android.Paths
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000244 for _, target := range image.targets {
245 files := buildBootImageRuleForArch(ctx, image, target.Arch.ArchType, profile, missingDeps)
246 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800247 }
Colin Cross44df5812019-02-15 23:06:46 -0800248
Colin Crossdf8eebe2019-04-09 15:29:41 -0700249 if image.zip != nil {
250 rule := android.NewRuleBuilder()
251 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700252 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700253 FlagWithOutput("-o ", image.zip).
254 FlagWithArg("-C ", image.dir.String()).
255 FlagWithInputList("-f ", allFiles, " -f ")
256
257 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
258 }
259
Colin Cross44df5812019-02-15 23:06:46 -0800260 return image
Colin Cross800fe132019-02-11 14:21:24 -0800261}
262
Colin Cross44df5812019-02-15 23:06:46 -0800263func buildBootImageRuleForArch(ctx android.SingletonContext, image *bootImage,
Colin Crossdf8eebe2019-04-09 15:29:41 -0700264 arch android.ArchType, profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800265
Colin Cross44df5812019-02-15 23:06:46 -0800266 global := dexpreoptGlobalConfig(ctx)
267
268 symbolsDir := image.symbolsDir.Join(ctx, "system/framework", arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000269 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
Colin Cross44df5812019-02-15 23:06:46 -0800270 outputDir := image.dir.Join(ctx, "system/framework", arch.String())
271 outputPath := image.images[arch]
Colin Cross69f59a32019-02-15 10:39:37 -0800272 oatLocation := pathtools.ReplaceExtension(dexpreopt.PathToLocation(outputPath, arch), "oat")
Colin Cross800fe132019-02-11 14:21:24 -0800273
274 rule := android.NewRuleBuilder()
275 rule.MissingDeps(missingDeps)
276
277 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
278 rule.Command().Text("rm").Flag("-f").
279 Flag(symbolsDir.Join(ctx, "*.art").String()).
280 Flag(symbolsDir.Join(ctx, "*.oat").String()).
281 Flag(symbolsDir.Join(ctx, "*.invocation").String())
282 rule.Command().Text("rm").Flag("-f").
283 Flag(outputDir.Join(ctx, "*.art").String()).
284 Flag(outputDir.Join(ctx, "*.oat").String()).
285 Flag(outputDir.Join(ctx, "*.invocation").String())
286
287 cmd := rule.Command()
288
289 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
290 if extraFlags == "" {
291 // Use ANDROID_LOG_TAGS to suppress most logging by default...
292 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
293 } else {
294 // ...unless the boot image is generated specifically for testing, then allow all logging.
295 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
296 }
297
298 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
299
Colin Cross44df5812019-02-15 23:06:46 -0800300 cmd.Tool(global.Tools.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800301 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800302 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800303 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
304 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800305
Colin Cross69f59a32019-02-15 10:39:37 -0800306 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800307 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800308 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800309 }
310
Colin Cross44df5812019-02-15 23:06:46 -0800311 if global.DirtyImageObjects.Valid() {
312 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800313 }
314
315 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800316 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
317 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800318 Flag("--generate-debug-info").
319 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700320 Flag("--image-format=lz4hc").
Colin Cross69f59a32019-02-15 10:39:37 -0800321 FlagWithOutput("--oat-symbols=", symbolsFile).
Colin Cross800fe132019-02-11 14:21:24 -0800322 Flag("--strip").
Colin Cross69f59a32019-02-15 10:39:37 -0800323 FlagWithOutput("--oat-file=", outputPath.ReplaceExtension(ctx, "oat")).
Colin Cross800fe132019-02-11 14:21:24 -0800324 FlagWithArg("--oat-location=", oatLocation).
Colin Cross69f59a32019-02-15 10:39:37 -0800325 FlagWithOutput("--image=", outputPath).
Colin Cross800fe132019-02-11 14:21:24 -0800326 FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress()).
327 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800328 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
329 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
330 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800331 FlagWithArg("--no-inline-from=", "core-oj.jar").
332 Flag("--abort-on-hard-verifier-error")
333
Colin Cross44df5812019-02-15 23:06:46 -0800334 if global.BootFlags != "" {
335 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800336 }
337
338 if extraFlags != "" {
339 cmd.Flag(extraFlags)
340 }
341
Colin Cross0b9f31f2019-02-28 11:00:01 -0800342 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800343
344 installDir := filepath.Join("/system/framework", arch.String())
345 vdexInstallDir := filepath.Join("/system/framework")
346
Colin Cross800fe132019-02-11 14:21:24 -0800347 var vdexInstalls android.RuleBuilderInstalls
348 var unstrippedInstalls android.RuleBuilderInstalls
349
Colin Crossdf8eebe2019-04-09 15:29:41 -0700350 var zipFiles android.WritablePaths
351
Dan Willemsen0f416782019-06-13 21:44:53 +0000352 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
353 cmd.ImplicitOutput(artOrOat)
354 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800355
Dan Willemsen0f416782019-06-13 21:44:53 +0000356 // Install the .oat and .art files
357 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
358 }
Colin Cross800fe132019-02-11 14:21:24 -0800359
Dan Willemsen0f416782019-06-13 21:44:53 +0000360 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
361 cmd.ImplicitOutput(vdex)
362 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800363
364 // The vdex files are identical between architectures, install them to a shared location. The Make rules will
365 // only use the install rules for one architecture, and will create symlinks into the architecture-specific
366 // directories.
367 vdexInstalls = append(vdexInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800368 android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000369 }
370
371 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
372 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800373
374 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
375 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800376 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800377 }
378
Colin Cross44df5812019-02-15 23:06:46 -0800379 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800380
381 // save output and installed files for makevars
Colin Cross44df5812019-02-15 23:06:46 -0800382 image.installs[arch] = rule.Installs()
383 image.vdexInstalls[arch] = vdexInstalls
384 image.unstrippedInstalls[arch] = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700385
386 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800387}
388
389const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
390It is likely that the boot classpath is inconsistent.
391Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
392
Colin Cross44df5812019-02-15 23:06:46 -0800393func bootImageProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000394 global := dexpreoptGlobalConfig(ctx)
395
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700396 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000397 return nil
398 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000399 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000400 tools := global.Tools
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000401 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800402
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000403 rule := android.NewRuleBuilder()
404 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800405
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000406 var bootImageProfile android.Path
407 if len(global.BootImageProfiles) > 1 {
408 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
409 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
410 bootImageProfile = combinedBootImageProfile
411 } else if len(global.BootImageProfiles) == 1 {
412 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000413 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
414 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000415 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000416 // No profile (not even a default one, which is the case on some branches
417 // like master-art-host that don't have frameworks/base).
418 // Return nil and continue without profile.
419 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000420 }
Colin Cross800fe132019-02-11 14:21:24 -0800421
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000422 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800423
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000424 rule.Command().
425 Text(`ANDROID_LOG_TAGS="*:e"`).
426 Tool(tools.Profman).
427 FlagWithInput("--create-profile-from=", bootImageProfile).
428 FlagForEachInput("--apk=", image.dexPaths.Paths()).
429 FlagForEachArg("--dex-location=", image.dexLocations).
430 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800431
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000432 rule.Install(profile, "/system/etc/boot-image.prof")
433
434 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
435
436 image.profileInstalls = rule.Installs()
437
438 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000439 })
440 if profile == nil {
441 return nil // wrap nil into a typed pointer with value nil
442 }
443 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800444}
445
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000446var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
447
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100448func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImage, missingDeps []string) android.WritablePath {
449 global := dexpreoptGlobalConfig(ctx)
450
451 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
452 return nil
453 }
454 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
455 tools := global.Tools
456
457 rule := android.NewRuleBuilder()
458 rule.MissingDeps(missingDeps)
459
460 // Some branches like master-art-host don't have frameworks/base, so manually
461 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
462 // and if they do they'll get a missing deps error.
463 defaultProfile := "frameworks/base/config/boot-profile.txt"
464 path := android.ExistentPathForSource(ctx, defaultProfile)
465 var bootFrameworkProfile android.Path
466 if path.Valid() {
467 bootFrameworkProfile = path.Path()
468 } else {
469 missingDeps = append(missingDeps, defaultProfile)
470 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
471 }
472
473 profile := image.dir.Join(ctx, "boot.bprof")
474
475 rule.Command().
476 Text(`ANDROID_LOG_TAGS="*:e"`).
477 Tool(tools.Profman).
478 Flag("--generate-boot-profile").
479 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
480 FlagForEachInput("--apk=", image.dexPaths.Paths()).
481 FlagForEachArg("--dex-location=", image.dexLocations).
482 FlagWithOutput("--reference-profile-file=", profile)
483
484 rule.Install(profile, "/system/etc/boot-image.bprof")
485 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
486 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
487
488 return profile
489 }).(android.WritablePath)
490}
491
492var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
493
Colin Crossc9a4c362019-02-26 21:13:48 -0800494func dumpOatRules(ctx android.SingletonContext, image *bootImage) {
495 var archs []android.ArchType
496 for arch := range image.images {
497 archs = append(archs, arch)
498 }
499 sort.Slice(archs, func(i, j int) bool { return archs[i].String() < archs[j].String() })
500
501 var allPhonies android.Paths
502 for _, arch := range archs {
503 // Create a rule to call oatdump.
504 output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt")
505 rule := android.NewRuleBuilder()
506 rule.Command().
507 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700508 BuiltTool(ctx, "oatdumpd").
Colin Crossc9a4c362019-02-26 21:13:48 -0800509 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPaths.Paths(), ":").
510 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocations, ":").
511 FlagWithArg("--image=", dexpreopt.PathToLocation(image.images[arch], arch)).Implicit(image.images[arch]).
512 FlagWithOutput("--output=", output).
513 FlagWithArg("--instruction-set=", arch.String())
514 rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
515
516 // Create a phony rule that depends on the output file and prints the path.
517 phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String())
518 rule = android.NewRuleBuilder()
519 rule.Command().
520 Implicit(output).
521 ImplicitOutput(phony).
522 Text("echo").FlagWithArg("Output in ", output.String())
523 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
524
525 allPhonies = append(allPhonies, phony)
526 }
527
528 phony := android.PathForPhony(ctx, "dump-oat-boot")
529 ctx.Build(pctx, android.BuildParams{
530 Rule: android.Phony,
531 Output: phony,
532 Inputs: allPhonies,
533 Description: "dump-oat-boot",
534 })
535
536}
537
Colin Cross2d00f0d2019-05-09 21:50:00 -0700538func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
539 data := dexpreoptGlobalConfigRaw(ctx).data
540
541 ctx.Build(pctx, android.BuildParams{
542 Rule: android.WriteFile,
543 Output: path,
544 Args: map[string]string{
545 "content": string(data),
546 },
547 })
548}
549
Colin Cross44df5812019-02-15 23:06:46 -0800550// Export paths for default boot image to Make
551func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700552 if d.dexpreoptConfigForMake != nil {
553 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
554 }
555
Colin Cross44df5812019-02-15 23:06:46 -0800556 image := d.defaultBootImage
557 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800558 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Colin Cross44df5812019-02-15 23:06:46 -0800559 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPaths.Strings(), " "))
560 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocations, " "))
Colin Crossdf8eebe2019-04-09 15:29:41 -0700561 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+image.name, image.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000562
563 var imageNames []string
564 for _, current := range append(d.otherImages, image) {
565 imageNames = append(imageNames, current.name)
Colin Cross91268c62019-04-11 14:07:04 -0700566 var arches []android.ArchType
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000567 for arch, _ := range current.images {
Colin Cross91268c62019-04-11 14:07:04 -0700568 arches = append(arches, arch)
569 }
570
571 sort.Slice(arches, func(i, j int) bool { return arches[i].String() < arches[j].String() })
572
573 for _, arch := range arches {
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000574 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.vdexInstalls[arch].String())
575 ctx.Strict("DEXPREOPT_IMAGE_"+current.name+"_"+arch.String(), current.images[arch].String())
Dan Willemsen0f416782019-06-13 21:44:53 +0000576 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+current.name+"_"+arch.String(), strings.Join(current.imagesDeps[arch].Strings(), " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000577 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.installs[arch].String())
578 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+current.name+"_"+arch.String(), current.unstrippedInstalls[arch].String())
Colin Crossdf8eebe2019-04-09 15:29:41 -0700579 if current.zip != nil {
580 }
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000581 }
582 }
583 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800584 }
Colin Cross800fe132019-02-11 14:21:24 -0800585}