blob: 76b1d69c3b02e52c6aa9a719c0c59b083155adbd [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() {
29 android.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory)
30}
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
171func skipDexpreoptBootJars(ctx android.PathContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000172 if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000173 return true
174 }
175
Colin Cross800fe132019-02-11 14:21:24 -0800176 if ctx.Config().UnbundledBuild() {
177 return true
178 }
179
180 if len(ctx.Config().Targets[android.Android]) == 0 {
181 // Host-only build
182 return true
183 }
184
185 return false
186}
187
Colin Cross44df5812019-02-15 23:06:46 -0800188type dexpreoptBootJars struct {
David Srbecky163bda62020-02-18 20:43:06 +0000189 defaultBootImage *bootImageConfig
190 otherImages []*bootImageConfig
Colin Cross2d00f0d2019-05-09 21:50:00 -0700191
192 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800193}
Colin Cross800fe132019-02-11 14:21:24 -0800194
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000195// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000196func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000197 if skipDexpreoptBootJars(ctx) {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000198 return nil
199 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000200 // Include dexpreopt files for the primary boot image.
David Srbecky163bda62020-02-18 20:43:06 +0000201 files := map[android.ArchType]android.OutputPaths{}
202 for _, variant := range artBootImageConfig(ctx).variants {
203 files[variant.target.Arch.ArchType] = variant.imagesDeps
204 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000205 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000206}
207
Colin Cross800fe132019-02-11 14:21:24 -0800208// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800209func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800210 if skipDexpreoptBootJars(ctx) {
211 return
212 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000213 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
214 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
215 return
216 }
Colin Cross800fe132019-02-11 14:21:24 -0800217
Colin Cross2d00f0d2019-05-09 21:50:00 -0700218 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
219 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
220
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000221 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800222
223 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
224 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
225 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
226 // on ASAN settings.
227 if len(ctx.Config().SanitizeDevice()) == 1 &&
228 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800229 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800230 return
231 }
232
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000233 // Always create the default boot image first, to get a unique profile rule for all images.
234 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000235 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
236 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800237
238 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800239}
240
David Srbecky163bda62020-02-18 20:43:06 +0000241// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
242func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Colin Cross44df5812019-02-15 23:06:46 -0800243 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800244 ctx.VisitAllModules(func(module android.Module) {
245 // Collect dex jar paths for the modules listed above.
Colin Cross42be7612019-02-21 18:12:14 -0800246 if j, ok := module.(interface{ DexJar() android.Path }); ok {
Colin Cross800fe132019-02-11 14:21:24 -0800247 name := ctx.ModuleName(module)
Colin Cross44df5812019-02-15 23:06:46 -0800248 if i := android.IndexList(name, image.modules); i != -1 {
Colin Cross800fe132019-02-11 14:21:24 -0800249 bootDexJars[i] = j.DexJar()
250 }
251 }
252 })
253
254 var missingDeps []string
255 // Ensure all modules were converted to paths
256 for i := range bootDexJars {
257 if bootDexJars[i] == nil {
258 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800259 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800260 bootDexJars[i] = android.PathForOutput(ctx, "missing")
261 } else {
262 ctx.Errorf("failed to find dex jar path for module %q",
Colin Cross44df5812019-02-15 23:06:46 -0800263 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800264 }
265 }
266 }
267
268 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
269 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
270 // already been set up can find them.
271 for i := range bootDexJars {
272 ctx.Build(pctx, android.BuildParams{
273 Rule: android.Cp,
274 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800275 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800276 })
277 }
278
Colin Cross44df5812019-02-15 23:06:46 -0800279 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100280 bootFrameworkProfileRule(ctx, image, missingDeps)
Vladimir Markob92ae272020-04-01 13:52:27 +0100281 updatableBcpPackagesRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800282
Colin Crossdf8eebe2019-04-09 15:29:41 -0700283 var allFiles android.Paths
David Srbecky163bda62020-02-18 20:43:06 +0000284 for _, variant := range image.variants {
285 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000286 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800287 }
Colin Cross44df5812019-02-15 23:06:46 -0800288
Colin Crossdf8eebe2019-04-09 15:29:41 -0700289 if image.zip != nil {
290 rule := android.NewRuleBuilder()
291 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700292 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700293 FlagWithOutput("-o ", image.zip).
294 FlagWithArg("-C ", image.dir.String()).
295 FlagWithInputList("-f ", allFiles, " -f ")
296
297 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
298 }
299
Colin Cross44df5812019-02-15 23:06:46 -0800300 return image
Colin Cross800fe132019-02-11 14:21:24 -0800301}
302
David Srbecky163bda62020-02-18 20:43:06 +0000303func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
304 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800305
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000306 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000307 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800308
David Srbecky163bda62020-02-18 20:43:06 +0000309 arch := image.target.Arch.ArchType
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000310 symbolsDir := image.symbolsDir.Join(ctx, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000311 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000312 outputDir := image.dir.Join(ctx, image.installSubdir, arch.String())
313 outputPath := outputDir.Join(ctx, image.stem+".oat")
314 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
315 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800316
317 rule := android.NewRuleBuilder()
318 rule.MissingDeps(missingDeps)
319
320 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
321 rule.Command().Text("rm").Flag("-f").
322 Flag(symbolsDir.Join(ctx, "*.art").String()).
323 Flag(symbolsDir.Join(ctx, "*.oat").String()).
324 Flag(symbolsDir.Join(ctx, "*.invocation").String())
325 rule.Command().Text("rm").Flag("-f").
326 Flag(outputDir.Join(ctx, "*.art").String()).
327 Flag(outputDir.Join(ctx, "*.oat").String()).
328 Flag(outputDir.Join(ctx, "*.invocation").String())
329
330 cmd := rule.Command()
331
332 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
333 if extraFlags == "" {
334 // Use ANDROID_LOG_TAGS to suppress most logging by default...
335 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
336 } else {
337 // ...unless the boot image is generated specifically for testing, then allow all logging.
338 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
339 }
340
341 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
342
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000343 cmd.Tool(globalSoong.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800344 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800345 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800346 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
347 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800348
Colin Cross69f59a32019-02-15 10:39:37 -0800349 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800350 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800351 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800352 }
353
Colin Cross44df5812019-02-15 23:06:46 -0800354 if global.DirtyImageObjects.Valid() {
355 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800356 }
357
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000358 if image.extension {
David Srbecky163bda62020-02-18 20:43:06 +0000359 artImage := image.primaryImages
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000360 cmd.
361 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
362 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
363 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
364 } else {
365 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
366 }
367
Colin Cross800fe132019-02-11 14:21:24 -0800368 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800369 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
370 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800371 Flag("--generate-debug-info").
372 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700373 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000374 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800375 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000376 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800377 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000378 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800379 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross44df5812019-02-15 23:06:46 -0800380 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
381 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
382 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800383 FlagWithArg("--no-inline-from=", "core-oj.jar").
Ulya Trafimovich4fd35a22020-03-09 12:46:06 +0000384 Flag("--force-determinism").
Colin Cross800fe132019-02-11 14:21:24 -0800385 Flag("--abort-on-hard-verifier-error")
386
Colin Cross44df5812019-02-15 23:06:46 -0800387 if global.BootFlags != "" {
388 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800389 }
390
391 if extraFlags != "" {
392 cmd.Flag(extraFlags)
393 }
394
Colin Cross0b9f31f2019-02-28 11:00:01 -0800395 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800396
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000397 installDir := filepath.Join("/", image.installSubdir, arch.String())
398 vdexInstallDir := filepath.Join("/", image.installSubdir)
Colin Cross800fe132019-02-11 14:21:24 -0800399
Colin Cross800fe132019-02-11 14:21:24 -0800400 var vdexInstalls android.RuleBuilderInstalls
401 var unstrippedInstalls android.RuleBuilderInstalls
402
Colin Crossdf8eebe2019-04-09 15:29:41 -0700403 var zipFiles android.WritablePaths
404
Dan Willemsen0f416782019-06-13 21:44:53 +0000405 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
406 cmd.ImplicitOutput(artOrOat)
407 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800408
Dan Willemsen0f416782019-06-13 21:44:53 +0000409 // Install the .oat and .art files
410 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
411 }
Colin Cross800fe132019-02-11 14:21:24 -0800412
Dan Willemsen0f416782019-06-13 21:44:53 +0000413 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
414 cmd.ImplicitOutput(vdex)
415 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800416
417 // The vdex files are identical between architectures, install them to a shared location. The Make rules will
418 // only use the install rules for one architecture, and will create symlinks into the architecture-specific
419 // directories.
420 vdexInstalls = append(vdexInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800421 android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000422 }
423
424 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
425 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800426
427 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
428 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800429 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800430 }
431
Colin Cross44df5812019-02-15 23:06:46 -0800432 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800433
434 // save output and installed files for makevars
David Srbecky163bda62020-02-18 20:43:06 +0000435 image.installs = rule.Installs()
436 image.vdexInstalls = vdexInstalls
437 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700438
439 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800440}
441
442const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
443It is likely that the boot classpath is inconsistent.
444Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
445
David Srbecky163bda62020-02-18 20:43:06 +0000446func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000447 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000448 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000449
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700450 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000451 return nil
452 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000453 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000454 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800455
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000456 rule := android.NewRuleBuilder()
457 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800458
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000459 var bootImageProfile android.Path
460 if len(global.BootImageProfiles) > 1 {
461 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
462 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
463 bootImageProfile = combinedBootImageProfile
464 } else if len(global.BootImageProfiles) == 1 {
465 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000466 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
467 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000468 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000469 // No profile (not even a default one, which is the case on some branches
470 // like master-art-host that don't have frameworks/base).
471 // Return nil and continue without profile.
472 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000473 }
Colin Cross800fe132019-02-11 14:21:24 -0800474
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000475 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800476
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000477 rule.Command().
478 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000479 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000480 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000481 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
482 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000483 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800484
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000485 rule.Install(profile, "/system/etc/boot-image.prof")
486
487 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
488
489 image.profileInstalls = rule.Installs()
490
491 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000492 })
493 if profile == nil {
494 return nil // wrap nil into a typed pointer with value nil
495 }
496 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800497}
498
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000499var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
500
David Srbecky163bda62020-02-18 20:43:06 +0000501func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000502 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000503 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100504
505 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
506 return nil
507 }
508 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100509 rule := android.NewRuleBuilder()
510 rule.MissingDeps(missingDeps)
511
512 // Some branches like master-art-host don't have frameworks/base, so manually
513 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
514 // and if they do they'll get a missing deps error.
515 defaultProfile := "frameworks/base/config/boot-profile.txt"
516 path := android.ExistentPathForSource(ctx, defaultProfile)
517 var bootFrameworkProfile android.Path
518 if path.Valid() {
519 bootFrameworkProfile = path.Path()
520 } else {
521 missingDeps = append(missingDeps, defaultProfile)
522 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
523 }
524
525 profile := image.dir.Join(ctx, "boot.bprof")
526
527 rule.Command().
528 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000529 Tool(globalSoong.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100530 Flag("--generate-boot-profile").
531 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000532 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
533 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100534 FlagWithOutput("--reference-profile-file=", profile)
535
536 rule.Install(profile, "/system/etc/boot-image.bprof")
537 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
538 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
539
540 return profile
541 }).(android.WritablePath)
542}
543
544var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
545
Vladimir Markob92ae272020-04-01 13:52:27 +0100546func updatableBcpPackagesRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
547 if ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
548 return nil
549 }
550
551 return ctx.Config().Once(updatableBcpPackagesRuleKey, func() interface{} {
552 global := dexpreopt.GetGlobalConfig(ctx)
553 updatableModules := dexpreopt.GetJarsFromApexJarPairs(global.UpdatableBootJars)
554
555 // Collect `permitted_packages` for updatable boot jars.
556 var updatablePackages []string
557 ctx.VisitAllModules(func(module android.Module) {
558 if j, ok := module.(*Library); ok {
559 name := ctx.ModuleName(module)
560 if i := android.IndexList(name, updatableModules); i != -1 {
561 pp := j.properties.Permitted_packages
562 if len(pp) > 0 {
563 updatablePackages = append(updatablePackages, pp...)
564 } else {
565 ctx.Errorf("Missing permitted_packages for %s", name)
566 }
567 // Do not match the same library repeatedly.
568 updatableModules = append(updatableModules[:i], updatableModules[i+1:]...)
569 }
570 }
571 })
572
573 // Sort updatable packages to ensure deterministic ordering.
574 sort.Strings(updatablePackages)
575
576 updatableBcpPackagesName := "updatable-bcp-packages.txt"
577 updatableBcpPackages := image.dir.Join(ctx, updatableBcpPackagesName)
578
579 ctx.Build(pctx, android.BuildParams{
580 Rule: android.WriteFile,
581 Output: updatableBcpPackages,
582 Args: map[string]string{
583 // WriteFile automatically adds the last end-of-line.
584 "content": strings.Join(updatablePackages, "\\n"),
585 },
586 })
587
588 rule := android.NewRuleBuilder()
589 rule.MissingDeps(missingDeps)
590 rule.Install(updatableBcpPackages, "/system/etc/"+updatableBcpPackagesName)
591 // TODO: Rename `profileInstalls` to `extraInstalls`?
592 // Maybe even move the field out of the bootImageConfig into some higher level type?
593 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
594
595 return updatableBcpPackages
596 }).(android.WritablePath)
597}
598
599var updatableBcpPackagesRuleKey = android.NewOnceKey("updatableBcpPackagesRule")
600
David Srbecky163bda62020-02-18 20:43:06 +0000601func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800602 var allPhonies android.Paths
David Srbecky163bda62020-02-18 20:43:06 +0000603 for _, image := range image.variants {
604 arch := image.target.Arch.ArchType
Colin Crossc9a4c362019-02-26 21:13:48 -0800605 // Create a rule to call oatdump.
606 output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt")
607 rule := android.NewRuleBuilder()
608 rule.Command().
609 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700610 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000611 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
612 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbecky163bda62020-02-18 20:43:06 +0000613 FlagWithArg("--image=", strings.Join(image.imageLocations, ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800614 FlagWithOutput("--output=", output).
615 FlagWithArg("--instruction-set=", arch.String())
616 rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
617
618 // Create a phony rule that depends on the output file and prints the path.
619 phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String())
620 rule = android.NewRuleBuilder()
621 rule.Command().
622 Implicit(output).
623 ImplicitOutput(phony).
624 Text("echo").FlagWithArg("Output in ", output.String())
625 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
626
627 allPhonies = append(allPhonies, phony)
628 }
629
630 phony := android.PathForPhony(ctx, "dump-oat-boot")
631 ctx.Build(pctx, android.BuildParams{
632 Rule: android.Phony,
633 Output: phony,
634 Inputs: allPhonies,
635 Description: "dump-oat-boot",
636 })
637
638}
639
Colin Cross2d00f0d2019-05-09 21:50:00 -0700640func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000641 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700642
643 ctx.Build(pctx, android.BuildParams{
644 Rule: android.WriteFile,
645 Output: path,
646 Args: map[string]string{
647 "content": string(data),
648 },
649 })
650}
651
Colin Cross44df5812019-02-15 23:06:46 -0800652// Export paths for default boot image to Make
653func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700654 if d.dexpreoptConfigForMake != nil {
655 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000656 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700657 }
658
Colin Cross44df5812019-02-15 23:06:46 -0800659 image := d.defaultBootImage
660 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800661 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000662 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
663 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000664
665 var imageNames []string
666 for _, current := range append(d.otherImages, image) {
667 imageNames = append(imageNames, current.name)
David Srbecky163bda62020-02-18 20:43:06 +0000668 for _, current := range current.variants {
669 sfx := current.name + "_" + current.target.Arch.ArchType.String()
670 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, current.vdexInstalls.String())
671 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, current.images.String())
672 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(current.imagesDeps.Strings(), " "))
673 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, current.installs.String())
674 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, current.unstrippedInstalls.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000675 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800676
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000677 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(current.imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800678 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000679 }
680 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800681 }
Colin Cross800fe132019-02-11 14:21:24 -0800682}