blob: d40d72e0931e667ea388fe8b4752dce101a57540 [file] [log] [blame]
Colin Cross800fe132019-02-11 14:21:24 -08001// Copyright 2019 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
18 "path/filepath"
Vladimir Markob92ae272020-04-01 13:52:27 +010019 "sort"
Colin Cross800fe132019-02-11 14:21:24 -080020 "strings"
21
22 "android/soong/android"
23 "android/soong/dexpreopt"
24
Colin Cross800fe132019-02-11 14:21:24 -080025 "github.com/google/blueprint/proptools"
26)
27
28func init() {
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +000029 RegisterDexpreoptBootJarsComponents(android.InitRegistrationContext)
Colin Cross800fe132019-02-11 14:21:24 -080030}
31
32// The image "location" is a symbolic path that with multiarchitecture
33// support doesn't really exist on the device. Typically it is
34// /system/framework/boot.art and should be the same for all supported
35// architectures on the device. The concrete architecture specific
36// content actually ends up in a "filename" that contains an
37// architecture specific directory name such as arm, arm64, mips,
38// mips64, x86, x86_64.
39//
40// Here are some example values for an x86_64 / x86 configuration:
41//
42// bootImages["x86_64"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86_64/boot.art"
43// dexpreopt.PathToLocation(bootImages["x86_64"], "x86_64") = "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
44//
45// bootImages["x86"] = "out/soong/generic_x86_64/dex_bootjars/system/framework/x86/boot.art"
46// dexpreopt.PathToLocation(bootImages["x86"])= "out/soong/generic_x86_64/dex_bootjars/system/framework/boot.art"
47//
48// The location is passed as an argument to the ART tools like dex2oat instead of the real path. The ART tools
49// will then reconstruct the real path, so the rules must have a dependency on the real path.
50
David Srbecky163bda62020-02-18 20:43:06 +000051// Target-independent description of pre-compiled boot image.
Colin Cross44df5812019-02-15 23:06:46 -080052type bootImageConfig struct {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000053 // Whether this image is an extension.
54 extension bool
55
56 // Image name (used in directory names and ninja rule names).
57 name string
58
59 // Basename of the image: the resulting filenames are <stem>[-<jar>].{art,oat,vdex}.
60 stem string
61
62 // Output directory for the image files.
63 dir android.OutputPath
64
65 // Output directory for the image files with debug symbols.
66 symbolsDir android.OutputPath
67
68 // Subdirectory where the image files are installed.
69 installSubdir string
70
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000071 // The names of jars that constitute this image.
72 modules []string
73
74 // The "locations" of jars.
75 dexLocations []string // for this image
76 dexLocationsDeps []string // for the dependency images and in this image
77
78 // File paths to jars.
79 dexPaths android.WritablePaths // for this image
80 dexPathsDeps android.WritablePaths // for the dependency images and in this image
81
82 // The "locations" of the dependency images and in this image.
83 imageLocations []string
84
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000085 // File path to a zip archive with all image files (or nil, if not needed).
86 zip android.WritablePath
David Srbecky163bda62020-02-18 20:43:06 +000087
88 // Rules which should be used in make to install the outputs.
89 profileInstalls android.RuleBuilderInstalls
90
91 // Target-dependent fields.
92 variants []*bootImageVariant
93}
94
95// Target-dependent description of pre-compiled boot image.
96type bootImageVariant struct {
97 *bootImageConfig
98
99 // Target for which the image is generated.
100 target android.Target
101
102 // Paths to image files.
103 images android.OutputPath // first image file
104 imagesDeps android.OutputPaths // all files
105
106 // Only for extensions, paths to the primary boot images.
107 primaryImages android.OutputPath
108
109 // Rules which should be used in make to install the outputs.
110 installs android.RuleBuilderInstalls
111 vdexInstalls android.RuleBuilderInstalls
112 unstrippedInstalls android.RuleBuilderInstalls
113}
114
115func (image bootImageConfig) getVariant(target android.Target) *bootImageVariant {
116 for _, variant := range image.variants {
117 if variant.target.Os == target.Os && variant.target.Arch.ArchType == target.Arch.ArchType {
118 return variant
119 }
120 }
121 return nil
Colin Cross800fe132019-02-11 14:21:24 -0800122}
123
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000124func (image bootImageConfig) moduleName(idx int) string {
125 // Dexpreopt on the boot class path produces multiple files. The first dex file
126 // is converted into 'name'.art (to match the legacy assumption that 'name'.art
Dan Willemsen0f416782019-06-13 21:44:53 +0000127 // exists), and the rest are converted to 'name'-<jar>.art.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000128 m := image.modules[idx]
129 name := image.stem
130 if idx != 0 || image.extension {
131 name += "-" + stemOf(m)
132 }
133 return name
134}
Dan Willemsen0f416782019-06-13 21:44:53 +0000135
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000136func (image bootImageConfig) firstModuleNameOrStem() string {
137 if len(image.modules) > 0 {
138 return image.moduleName(0)
139 } else {
140 return image.stem
141 }
142}
143
144func (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths {
145 ret := make(android.OutputPaths, 0, len(image.modules)*len(exts))
146 for i := range image.modules {
147 name := image.moduleName(i)
Dan Willemsen0f416782019-06-13 21:44:53 +0000148 for _, ext := range exts {
149 ret = append(ret, dir.Join(ctx, name+ext))
150 }
151 }
Dan Willemsen0f416782019-06-13 21:44:53 +0000152 return ret
153}
154
Colin Cross800fe132019-02-11 14:21:24 -0800155func concat(lists ...[]string) []string {
156 var size int
157 for _, l := range lists {
158 size += len(l)
159 }
160 ret := make([]string, 0, size)
161 for _, l := range lists {
162 ret = append(ret, l...)
163 }
164 return ret
165}
166
Colin Cross800fe132019-02-11 14:21:24 -0800167func dexpreoptBootJarsFactory() android.Singleton {
Colin Cross44df5812019-02-15 23:06:46 -0800168 return &dexpreoptBootJars{}
Colin Cross800fe132019-02-11 14:21:24 -0800169}
170
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000171func RegisterDexpreoptBootJarsComponents(ctx android.RegistrationContext) {
172 ctx.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory)
173}
174
Colin Cross800fe132019-02-11 14:21:24 -0800175func skipDexpreoptBootJars(ctx android.PathContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000176 if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
Ulya Trafimovichacb33e02019-11-01 17:57:29 +0000177 return true
178 }
179
Colin Cross800fe132019-02-11 14:21:24 -0800180 if ctx.Config().UnbundledBuild() {
181 return true
182 }
183
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000184 if len(ctx.Config().Targets[android.Android]) == 0 {
185 // Host-only build
186 return true
187 }
188
Colin Cross800fe132019-02-11 14:21:24 -0800189 return false
190}
191
Colin Cross44df5812019-02-15 23:06:46 -0800192type dexpreoptBootJars struct {
David Srbecky163bda62020-02-18 20:43:06 +0000193 defaultBootImage *bootImageConfig
194 otherImages []*bootImageConfig
Colin Cross2d00f0d2019-05-09 21:50:00 -0700195
196 dexpreoptConfigForMake android.WritablePath
Colin Cross44df5812019-02-15 23:06:46 -0800197}
Colin Cross800fe132019-02-11 14:21:24 -0800198
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000199// Accessor function for the apex package. Returns nil if dexpreopt is disabled.
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000200func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths {
Ulya Trafimovich44561882020-01-03 13:25:54 +0000201 if skipDexpreoptBootJars(ctx) {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000202 return nil
203 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000204 // Include dexpreopt files for the primary boot image.
David Srbecky163bda62020-02-18 20:43:06 +0000205 files := map[android.ArchType]android.OutputPaths{}
206 for _, variant := range artBootImageConfig(ctx).variants {
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000207 files[variant.target.Arch.ArchType] = variant.imagesDeps
David Srbecky163bda62020-02-18 20:43:06 +0000208 }
Ulya Trafimovich7eebb4f2020-01-22 13:41:06 +0000209 return files
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000210}
211
Colin Cross800fe132019-02-11 14:21:24 -0800212// dexpreoptBoot singleton rules
Colin Cross44df5812019-02-15 23:06:46 -0800213func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross800fe132019-02-11 14:21:24 -0800214 if skipDexpreoptBootJars(ctx) {
215 return
216 }
Martin Stjernholm6d415272020-01-31 17:10:36 +0000217 if dexpreopt.GetCachedGlobalSoongConfig(ctx) == nil {
218 // No module has enabled dexpreopting, so we assume there will be no boot image to make.
219 return
220 }
Colin Cross800fe132019-02-11 14:21:24 -0800221
Colin Cross2d00f0d2019-05-09 21:50:00 -0700222 d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config")
223 writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
224
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000225 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross800fe132019-02-11 14:21:24 -0800226
227 // Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
228 // and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
229 // Note: this is technically incorrect. Compiled code contains stack checks which may depend
230 // on ASAN settings.
231 if len(ctx.Config().SanitizeDevice()) == 1 &&
232 ctx.Config().SanitizeDevice()[0] == "address" &&
Colin Cross44df5812019-02-15 23:06:46 -0800233 global.SanitizeLite {
Colin Cross800fe132019-02-11 14:21:24 -0800234 return
235 }
236
Lingfeng Yang54191fa2019-12-19 16:40:09 +0000237 // Always create the default boot image first, to get a unique profile rule for all images.
238 d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx))
Ulya Trafimovich44561882020-01-03 13:25:54 +0000239 // Create boot image for the ART apex (build artifacts are accessed via the global boot image config).
240 d.otherImages = append(d.otherImages, buildBootImage(ctx, artBootImageConfig(ctx)))
Colin Crossc9a4c362019-02-26 21:13:48 -0800241
242 dumpOatRules(ctx, d.defaultBootImage)
Colin Cross44df5812019-02-15 23:06:46 -0800243}
244
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000245// Inspect this module to see if it contains a bootclasspath dex jar.
246// Note that the same jar may occur in multiple modules.
247// This logic is tested in the apex package to avoid import cycle apex <-> java.
248func getBootImageJar(ctx android.SingletonContext, image *bootImageConfig, module android.Module) (int, android.Path) {
249 // All apex Java libraries have non-installable platform variants, skip them.
250 if module.IsSkipInstall() {
251 return -1, nil
252 }
253
254 jar, hasJar := module.(interface{ DexJar() android.Path })
255 if !hasJar {
256 return -1, nil
257 }
258
259 name := ctx.ModuleName(module)
260 index := android.IndexList(name, image.modules)
261 if index == -1 {
262 return -1, nil
263 }
264
265 // Check that this module satisfies constraints for a particular boot image.
266 apex, isApexModule := module.(android.ApexModule)
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +0100267 fromUpdatableApex := isApexModule && apex.Updatable()
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000268 if image.name == artBootImageName {
Colin Cross274a72d2020-08-11 12:17:01 -0700269 if isApexModule && len(apex.InApexes()) > 0 && allHavePrefix(apex.InApexes(), "com.android.art.") {
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +0100270 // ok: found the jar in the ART apex
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000271 } else if isApexModule && apex.IsForPlatform() && Bool(module.(*Library).deviceProperties.Hostdex) {
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +0100272 // exception (skip and continue): special "hostdex" platform variant
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000273 return -1, nil
Ulya Trafimovichb4d816e2020-04-08 15:00:49 +0100274 } else if name == "jacocoagent" && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +0100275 // exception (skip and continue): Jacoco platform variant for a coverage build
Ulya Trafimovichb4d816e2020-04-08 15:00:49 +0100276 return -1, nil
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +0100277 } else if fromUpdatableApex {
278 // error: this jar is part of an updatable apex other than ART
Colin Cross274a72d2020-08-11 12:17:01 -0700279 ctx.Errorf("module %q from updatable apexes %q is not allowed in the ART boot image", name, apex.InApexes())
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000280 } else {
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +0100281 // error: this jar is part of the platform or a non-updatable apex
Colin Cross274a72d2020-08-11 12:17:01 -0700282 ctx.Errorf("module %q is not allowed in the ART boot image", name)
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000283 }
284 } else if image.name == frameworkBootImageName {
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +0100285 if !fromUpdatableApex {
286 // ok: this jar is part of the platform or a non-updatable apex
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000287 } else {
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +0100288 // error: this jar is part of an updatable apex
Colin Cross274a72d2020-08-11 12:17:01 -0700289 ctx.Errorf("module %q from updatable apexes %q is not allowed in the framework boot image", name, apex.InApexes())
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000290 }
291 } else {
292 panic("unknown boot image: " + image.name)
293 }
294
295 return index, jar.DexJar()
296}
297
Colin Cross274a72d2020-08-11 12:17:01 -0700298func allHavePrefix(list []string, prefix string) bool {
299 for _, s := range list {
300 if !strings.HasPrefix(s, prefix) {
301 return false
302 }
303 }
304 return true
305}
306
David Srbecky163bda62020-02-18 20:43:06 +0000307// buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image.
308func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig {
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000309 // Collect dex jar paths for the boot image modules.
310 // This logic is tested in the apex package to avoid import cycle apex <-> java.
Colin Cross44df5812019-02-15 23:06:46 -0800311 bootDexJars := make(android.Paths, len(image.modules))
Colin Cross800fe132019-02-11 14:21:24 -0800312 ctx.VisitAllModules(func(module android.Module) {
Sam Mortimer3458e6a2019-10-07 11:41:14 -0700313 if m, ok := module.(interface{ BootJarProvider() bool }); !ok ||
314 !m.BootJarProvider() {
315 return
316 }
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000317 if i, j := getBootImageJar(ctx, image, module); i != -1 {
318 bootDexJars[i] = j
Colin Cross800fe132019-02-11 14:21:24 -0800319 }
320 })
321
322 var missingDeps []string
323 // Ensure all modules were converted to paths
324 for i := range bootDexJars {
325 if bootDexJars[i] == nil {
326 if ctx.Config().AllowMissingDependencies() {
Colin Cross44df5812019-02-15 23:06:46 -0800327 missingDeps = append(missingDeps, image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800328 bootDexJars[i] = android.PathForOutput(ctx, "missing")
329 } else {
Ulya Trafimovichcc21bba2020-01-13 15:18:16 +0000330 ctx.Errorf("failed to find a dex jar path for module '%s'"+
331 ", note that some jars may be filtered out by module constraints",
Colin Cross44df5812019-02-15 23:06:46 -0800332 image.modules[i])
Colin Cross800fe132019-02-11 14:21:24 -0800333 }
334 }
335 }
336
337 // The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before
338 // the bootclasspath modules have been compiled. Copy the dex jars there so the module rules that have
339 // already been set up can find them.
340 for i := range bootDexJars {
341 ctx.Build(pctx, android.BuildParams{
342 Rule: android.Cp,
343 Input: bootDexJars[i],
Colin Cross44df5812019-02-15 23:06:46 -0800344 Output: image.dexPaths[i],
Colin Cross800fe132019-02-11 14:21:24 -0800345 })
346 }
347
Colin Cross44df5812019-02-15 23:06:46 -0800348 profile := bootImageProfileRule(ctx, image, missingDeps)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100349 bootFrameworkProfileRule(ctx, image, missingDeps)
Vladimir Markob92ae272020-04-01 13:52:27 +0100350 updatableBcpPackagesRule(ctx, image, missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800351
Colin Crossdf8eebe2019-04-09 15:29:41 -0700352 var allFiles android.Paths
David Srbecky163bda62020-02-18 20:43:06 +0000353 for _, variant := range image.variants {
354 files := buildBootImageVariant(ctx, variant, profile, missingDeps)
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000355 allFiles = append(allFiles, files.Paths()...)
Colin Cross800fe132019-02-11 14:21:24 -0800356 }
Colin Cross44df5812019-02-15 23:06:46 -0800357
Colin Crossdf8eebe2019-04-09 15:29:41 -0700358 if image.zip != nil {
359 rule := android.NewRuleBuilder()
360 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -0700361 BuiltTool(ctx, "soong_zip").
Colin Crossdf8eebe2019-04-09 15:29:41 -0700362 FlagWithOutput("-o ", image.zip).
363 FlagWithArg("-C ", image.dir.String()).
364 FlagWithInputList("-f ", allFiles, " -f ")
365
366 rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image")
367 }
368
Colin Cross44df5812019-02-15 23:06:46 -0800369 return image
Colin Cross800fe132019-02-11 14:21:24 -0800370}
371
David Srbecky163bda62020-02-18 20:43:06 +0000372func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant,
373 profile android.Path, missingDeps []string) android.WritablePaths {
Colin Cross800fe132019-02-11 14:21:24 -0800374
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000375 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000376 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross44df5812019-02-15 23:06:46 -0800377
David Srbecky163bda62020-02-18 20:43:06 +0000378 arch := image.target.Arch.ArchType
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000379 symbolsDir := image.symbolsDir.Join(ctx, image.installSubdir, arch.String())
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000380 symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000381 outputDir := image.dir.Join(ctx, image.installSubdir, arch.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000382 outputPath := outputDir.Join(ctx, image.stem+".oat")
383 oatLocation := dexpreopt.PathToLocation(outputPath, arch)
384 imagePath := outputPath.ReplaceExtension(ctx, "art")
Colin Cross800fe132019-02-11 14:21:24 -0800385
386 rule := android.NewRuleBuilder()
387 rule.MissingDeps(missingDeps)
388
389 rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
390 rule.Command().Text("rm").Flag("-f").
391 Flag(symbolsDir.Join(ctx, "*.art").String()).
392 Flag(symbolsDir.Join(ctx, "*.oat").String()).
393 Flag(symbolsDir.Join(ctx, "*.invocation").String())
394 rule.Command().Text("rm").Flag("-f").
395 Flag(outputDir.Join(ctx, "*.art").String()).
396 Flag(outputDir.Join(ctx, "*.oat").String()).
397 Flag(outputDir.Join(ctx, "*.invocation").String())
398
399 cmd := rule.Command()
400
401 extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
402 if extraFlags == "" {
403 // Use ANDROID_LOG_TAGS to suppress most logging by default...
404 cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
405 } else {
406 // ...unless the boot image is generated specifically for testing, then allow all logging.
407 cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
408 }
409
410 invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
411
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000412 cmd.Tool(globalSoong.Dex2oat).
Colin Cross800fe132019-02-11 14:21:24 -0800413 Flag("--avoid-storing-invocation").
Colin Cross69f59a32019-02-15 10:39:37 -0800414 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross44df5812019-02-15 23:06:46 -0800415 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
416 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
Colin Cross800fe132019-02-11 14:21:24 -0800417
Colin Cross69f59a32019-02-15 10:39:37 -0800418 if profile != nil {
Colin Cross800fe132019-02-11 14:21:24 -0800419 cmd.FlagWithArg("--compiler-filter=", "speed-profile")
Colin Cross69f59a32019-02-15 10:39:37 -0800420 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800421 }
422
Colin Cross44df5812019-02-15 23:06:46 -0800423 if global.DirtyImageObjects.Valid() {
424 cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path())
Colin Cross800fe132019-02-11 14:21:24 -0800425 }
426
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000427 if image.extension {
David Srbecky163bda62020-02-18 20:43:06 +0000428 artImage := image.primaryImages
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000429 cmd.
430 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
431 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
432 FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage)
433 } else {
434 cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
435 }
436
Colin Cross800fe132019-02-11 14:21:24 -0800437 cmd.
Colin Cross44df5812019-02-15 23:06:46 -0800438 FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
439 FlagForEachArg("--dex-location=", image.dexLocations).
Colin Cross800fe132019-02-11 14:21:24 -0800440 Flag("--generate-debug-info").
441 Flag("--generate-build-id").
Mathieu Chartier54fd8072019-07-26 13:50:04 -0700442 Flag("--image-format=lz4hc").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000443 FlagWithArg("--oat-symbols=", symbolsFile.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800444 Flag("--strip").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000445 FlagWithArg("--oat-file=", outputPath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800446 FlagWithArg("--oat-location=", oatLocation).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000447 FlagWithArg("--image=", imagePath.String()).
Colin Cross800fe132019-02-11 14:21:24 -0800448 FlagWithArg("--instruction-set=", arch.String()).
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000449 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
450 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
Colin Cross44df5812019-02-15 23:06:46 -0800451 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross800fe132019-02-11 14:21:24 -0800452 FlagWithArg("--no-inline-from=", "core-oj.jar").
Ulya Trafimovich4fd35a22020-03-09 12:46:06 +0000453 Flag("--force-determinism").
Colin Cross800fe132019-02-11 14:21:24 -0800454 Flag("--abort-on-hard-verifier-error")
455
Colin Cross44df5812019-02-15 23:06:46 -0800456 if global.BootFlags != "" {
457 cmd.Flag(global.BootFlags)
Colin Cross800fe132019-02-11 14:21:24 -0800458 }
459
460 if extraFlags != "" {
461 cmd.Flag(extraFlags)
462 }
463
Colin Cross0b9f31f2019-02-28 11:00:01 -0800464 cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
Colin Cross800fe132019-02-11 14:21:24 -0800465
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000466 installDir := filepath.Join("/", image.installSubdir, arch.String())
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000467 vdexInstallDir := filepath.Join("/", image.installSubdir)
Colin Cross800fe132019-02-11 14:21:24 -0800468
Colin Cross800fe132019-02-11 14:21:24 -0800469 var vdexInstalls android.RuleBuilderInstalls
470 var unstrippedInstalls android.RuleBuilderInstalls
471
Colin Crossdf8eebe2019-04-09 15:29:41 -0700472 var zipFiles android.WritablePaths
473
Dan Willemsen0f416782019-06-13 21:44:53 +0000474 for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
475 cmd.ImplicitOutput(artOrOat)
476 zipFiles = append(zipFiles, artOrOat)
Colin Cross800fe132019-02-11 14:21:24 -0800477
Dan Willemsen0f416782019-06-13 21:44:53 +0000478 // Install the .oat and .art files
479 rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
480 }
Colin Cross800fe132019-02-11 14:21:24 -0800481
Dan Willemsen0f416782019-06-13 21:44:53 +0000482 for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
483 cmd.ImplicitOutput(vdex)
484 zipFiles = append(zipFiles, vdex)
Colin Cross800fe132019-02-11 14:21:24 -0800485
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000486 // The vdex files are identical between architectures, install them to a shared location. The Make rules will
487 // only use the install rules for one architecture, and will create symlinks into the architecture-specific
488 // directories.
Colin Cross800fe132019-02-11 14:21:24 -0800489 vdexInstalls = append(vdexInstalls,
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000490 android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())})
Dan Willemsen0f416782019-06-13 21:44:53 +0000491 }
492
493 for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
494 cmd.ImplicitOutput(unstrippedOat)
Colin Cross800fe132019-02-11 14:21:24 -0800495
496 // Install the unstripped oat files. The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
497 unstrippedInstalls = append(unstrippedInstalls,
Colin Cross69f59a32019-02-15 10:39:37 -0800498 android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
Colin Cross800fe132019-02-11 14:21:24 -0800499 }
500
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000501 rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String())
Colin Cross800fe132019-02-11 14:21:24 -0800502
503 // save output and installed files for makevars
David Srbecky163bda62020-02-18 20:43:06 +0000504 image.installs = rule.Installs()
505 image.vdexInstalls = vdexInstalls
506 image.unstrippedInstalls = unstrippedInstalls
Colin Crossdf8eebe2019-04-09 15:29:41 -0700507
508 return zipFiles
Colin Cross800fe132019-02-11 14:21:24 -0800509}
510
511const failureMessage = `ERROR: Dex2oat failed to compile a boot image.
512It is likely that the boot classpath is inconsistent.
513Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
514
David Srbecky163bda62020-02-18 20:43:06 +0000515func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000516 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000517 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000518
Mathieu Chartier6adeee12019-06-26 10:01:36 -0700519 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
Nicolas Geoffray27c7cc62019-02-24 16:04:52 +0000520 return nil
521 }
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000522 profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000523 defaultProfile := "frameworks/base/config/boot-image-profile.txt"
Colin Cross800fe132019-02-11 14:21:24 -0800524
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000525 rule := android.NewRuleBuilder()
526 rule.MissingDeps(missingDeps)
Colin Cross800fe132019-02-11 14:21:24 -0800527
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000528 var bootImageProfile android.Path
529 if len(global.BootImageProfiles) > 1 {
530 combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt")
531 rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile)
532 bootImageProfile = combinedBootImageProfile
533 } else if len(global.BootImageProfiles) == 1 {
534 bootImageProfile = global.BootImageProfiles[0]
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000535 } else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
536 bootImageProfile = path.Path()
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000537 } else {
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000538 // No profile (not even a default one, which is the case on some branches
539 // like master-art-host that don't have frameworks/base).
540 // Return nil and continue without profile.
541 return nil
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000542 }
Colin Cross800fe132019-02-11 14:21:24 -0800543
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000544 profile := image.dir.Join(ctx, "boot.prof")
Colin Cross800fe132019-02-11 14:21:24 -0800545
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000546 rule.Command().
547 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000548 Tool(globalSoong.Profman).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000549 FlagWithInput("--create-profile-from=", bootImageProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000550 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
551 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000552 FlagWithOutput("--reference-profile-file=", profile)
Colin Cross800fe132019-02-11 14:21:24 -0800553
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000554 rule.Install(profile, "/system/etc/boot-image.prof")
555
556 rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars")
557
558 image.profileInstalls = rule.Installs()
559
560 return profile
Ulyana Trafimovichde534412019-11-08 10:51:01 +0000561 })
562 if profile == nil {
563 return nil // wrap nil into a typed pointer with value nil
564 }
565 return profile.(android.WritablePath)
Colin Cross800fe132019-02-11 14:21:24 -0800566}
567
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000568var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule")
569
David Srbecky163bda62020-02-18 20:43:06 +0000570func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000571 globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000572 global := dexpreopt.GetGlobalConfig(ctx)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100573
574 if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
575 return nil
576 }
577 return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100578 rule := android.NewRuleBuilder()
579 rule.MissingDeps(missingDeps)
580
581 // Some branches like master-art-host don't have frameworks/base, so manually
582 // handle the case that the default is missing. Those branches won't attempt to build the profile rule,
583 // and if they do they'll get a missing deps error.
584 defaultProfile := "frameworks/base/config/boot-profile.txt"
585 path := android.ExistentPathForSource(ctx, defaultProfile)
586 var bootFrameworkProfile android.Path
587 if path.Valid() {
588 bootFrameworkProfile = path.Path()
589 } else {
590 missingDeps = append(missingDeps, defaultProfile)
591 bootFrameworkProfile = android.PathForOutput(ctx, "missing")
592 }
593
594 profile := image.dir.Join(ctx, "boot.bprof")
595
596 rule.Command().
597 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000598 Tool(globalSoong.Profman).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100599 Flag("--generate-boot-profile").
600 FlagWithInput("--create-profile-from=", bootFrameworkProfile).
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000601 FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
602 FlagForEachArg("--dex-location=", image.dexLocationsDeps).
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100603 FlagWithOutput("--reference-profile-file=", profile)
604
605 rule.Install(profile, "/system/etc/boot-image.bprof")
606 rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars")
607 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
608
609 return profile
610 }).(android.WritablePath)
611}
612
613var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule")
614
Vladimir Markob92ae272020-04-01 13:52:27 +0100615func updatableBcpPackagesRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath {
616 if ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() {
617 return nil
618 }
619
620 return ctx.Config().Once(updatableBcpPackagesRuleKey, func() interface{} {
621 global := dexpreopt.GetGlobalConfig(ctx)
622 updatableModules := dexpreopt.GetJarsFromApexJarPairs(global.UpdatableBootJars)
623
624 // Collect `permitted_packages` for updatable boot jars.
625 var updatablePackages []string
626 ctx.VisitAllModules(func(module android.Module) {
Paul Duffina105cf92020-05-29 11:24:51 +0100627 if j, ok := module.(PermittedPackagesForUpdatableBootJars); ok {
Vladimir Markob92ae272020-04-01 13:52:27 +0100628 name := ctx.ModuleName(module)
629 if i := android.IndexList(name, updatableModules); i != -1 {
Paul Duffina105cf92020-05-29 11:24:51 +0100630 pp := j.PermittedPackagesForUpdatableBootJars()
Vladimir Markob92ae272020-04-01 13:52:27 +0100631 if len(pp) > 0 {
632 updatablePackages = append(updatablePackages, pp...)
633 } else {
634 ctx.Errorf("Missing permitted_packages for %s", name)
635 }
636 // Do not match the same library repeatedly.
637 updatableModules = append(updatableModules[:i], updatableModules[i+1:]...)
638 }
639 }
640 })
641
642 // Sort updatable packages to ensure deterministic ordering.
643 sort.Strings(updatablePackages)
644
645 updatableBcpPackagesName := "updatable-bcp-packages.txt"
646 updatableBcpPackages := image.dir.Join(ctx, updatableBcpPackagesName)
647
648 ctx.Build(pctx, android.BuildParams{
649 Rule: android.WriteFile,
650 Output: updatableBcpPackages,
651 Args: map[string]string{
652 // WriteFile automatically adds the last end-of-line.
653 "content": strings.Join(updatablePackages, "\\n"),
654 },
655 })
656
657 rule := android.NewRuleBuilder()
658 rule.MissingDeps(missingDeps)
659 rule.Install(updatableBcpPackages, "/system/etc/"+updatableBcpPackagesName)
660 // TODO: Rename `profileInstalls` to `extraInstalls`?
661 // Maybe even move the field out of the bootImageConfig into some higher level type?
662 image.profileInstalls = append(image.profileInstalls, rule.Installs()...)
663
664 return updatableBcpPackages
665 }).(android.WritablePath)
666}
667
668var updatableBcpPackagesRuleKey = android.NewOnceKey("updatableBcpPackagesRule")
669
David Srbecky163bda62020-02-18 20:43:06 +0000670func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) {
Colin Crossc9a4c362019-02-26 21:13:48 -0800671 var allPhonies android.Paths
David Srbecky163bda62020-02-18 20:43:06 +0000672 for _, image := range image.variants {
673 arch := image.target.Arch.ArchType
Colin Crossc9a4c362019-02-26 21:13:48 -0800674 // Create a rule to call oatdump.
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000675 output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt")
Colin Crossc9a4c362019-02-26 21:13:48 -0800676 rule := android.NewRuleBuilder()
677 rule.Command().
678 // TODO: for now, use the debug version for better error reporting
Colin Crossee94d6a2019-07-08 17:08:34 -0700679 BuiltTool(ctx, "oatdumpd").
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000680 FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
681 FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
David Srbecky163bda62020-02-18 20:43:06 +0000682 FlagWithArg("--image=", strings.Join(image.imageLocations, ":")).Implicits(image.imagesDeps.Paths()).
Colin Crossc9a4c362019-02-26 21:13:48 -0800683 FlagWithOutput("--output=", output).
684 FlagWithArg("--instruction-set=", arch.String())
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000685 rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800686
687 // Create a phony rule that depends on the output file and prints the path.
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000688 phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800689 rule = android.NewRuleBuilder()
690 rule.Command().
691 Implicit(output).
692 ImplicitOutput(phony).
693 Text("echo").FlagWithArg("Output in ", output.String())
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000694 rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String())
Colin Crossc9a4c362019-02-26 21:13:48 -0800695
696 allPhonies = append(allPhonies, phony)
697 }
698
699 phony := android.PathForPhony(ctx, "dump-oat-boot")
700 ctx.Build(pctx, android.BuildParams{
701 Rule: android.Phony,
702 Output: phony,
703 Inputs: allPhonies,
704 Description: "dump-oat-boot",
705 })
706
707}
708
Colin Cross2d00f0d2019-05-09 21:50:00 -0700709func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000710 data := dexpreopt.GetGlobalConfigRawData(ctx)
Colin Cross2d00f0d2019-05-09 21:50:00 -0700711
712 ctx.Build(pctx, android.BuildParams{
713 Rule: android.WriteFile,
714 Output: path,
715 Args: map[string]string{
716 "content": string(data),
717 },
718 })
719}
720
Colin Cross44df5812019-02-15 23:06:46 -0800721// Export paths for default boot image to Make
722func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
Colin Cross2d00f0d2019-05-09 21:50:00 -0700723 if d.dexpreoptConfigForMake != nil {
724 ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000725 ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
Colin Cross2d00f0d2019-05-09 21:50:00 -0700726 }
727
Colin Cross44df5812019-02-15 23:06:46 -0800728 image := d.defaultBootImage
729 if image != nil {
Colin Cross44df5812019-02-15 23:06:46 -0800730 ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String())
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000731 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " "))
732 ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " "))
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000733
734 var imageNames []string
735 for _, current := range append(d.otherImages, image) {
736 imageNames = append(imageNames, current.name)
David Srbecky163bda62020-02-18 20:43:06 +0000737 for _, current := range current.variants {
Jeff Tinker74cc81c2020-05-19 17:45:22 +0000738 sfx := current.name + "_" + current.target.Arch.ArchType.String()
David Srbecky163bda62020-02-18 20:43:06 +0000739 ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, current.vdexInstalls.String())
740 ctx.Strict("DEXPREOPT_IMAGE_"+sfx, current.images.String())
741 ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(current.imagesDeps.Strings(), " "))
742 ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, current.installs.String())
743 ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, current.unstrippedInstalls.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000744 }
Colin Cross31bf00d2019-12-04 13:16:01 -0800745
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000746 ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(current.imageLocations, ":"))
Colin Cross31bf00d2019-12-04 13:16:01 -0800747 ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
Nicolas Geoffray72892f12019-02-22 15:34:40 +0000748 }
749 ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " "))
Colin Cross800fe132019-02-11 14:21:24 -0800750 }
Colin Cross800fe132019-02-11 14:21:24 -0800751}