| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 1 | // 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 |  | 
 | 15 | package java | 
 | 16 |  | 
 | 17 | import ( | 
 | 18 | 	"path/filepath" | 
| Vladimir Marko | b92ae27 | 2020-04-01 13:52:27 +0100 | [diff] [blame] | 19 | 	"sort" | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 20 | 	"strings" | 
 | 21 |  | 
 | 22 | 	"android/soong/android" | 
 | 23 | 	"android/soong/dexpreopt" | 
 | 24 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 25 | 	"github.com/google/blueprint/proptools" | 
 | 26 | ) | 
 | 27 |  | 
 | 28 | func init() { | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 29 | 	RegisterDexpreoptBootJarsComponents(android.InitRegistrationContext) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 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 Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 51 | // Target-independent description of pre-compiled boot image. | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 52 | type bootImageConfig struct { | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 53 | 	// 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 Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 71 | 	// 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 Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 85 | 	// File path to a zip archive with all image files (or nil, if not needed). | 
 | 86 | 	zip android.WritablePath | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 87 |  | 
 | 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. | 
 | 96 | type 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 |  | 
 | 115 | func (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 Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 122 | } | 
 | 123 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 124 | func (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 Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 127 | 	// exists), and the rest are converted to 'name'-<jar>.art. | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 128 | 	m := image.modules[idx] | 
 | 129 | 	name := image.stem | 
 | 130 | 	if idx != 0 || image.extension { | 
 | 131 | 		name += "-" + stemOf(m) | 
 | 132 | 	} | 
 | 133 | 	return name | 
 | 134 | } | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 135 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 136 | func (image bootImageConfig) firstModuleNameOrStem() string { | 
 | 137 | 	if len(image.modules) > 0 { | 
 | 138 | 		return image.moduleName(0) | 
 | 139 | 	} else { | 
 | 140 | 		return image.stem | 
 | 141 | 	} | 
 | 142 | } | 
 | 143 |  | 
 | 144 | func (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 Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 148 | 		for _, ext := range exts { | 
 | 149 | 			ret = append(ret, dir.Join(ctx, name+ext)) | 
 | 150 | 		} | 
 | 151 | 	} | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 152 | 	return ret | 
 | 153 | } | 
 | 154 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 155 | func 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 Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 167 | func dexpreoptBootJarsFactory() android.Singleton { | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 168 | 	return &dexpreoptBootJars{} | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 169 | } | 
 | 170 |  | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 171 | func RegisterDexpreoptBootJarsComponents(ctx android.RegistrationContext) { | 
 | 172 | 	ctx.RegisterSingletonType("dex_bootjars", dexpreoptBootJarsFactory) | 
 | 173 | } | 
 | 174 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 175 | func skipDexpreoptBootJars(ctx android.PathContext) bool { | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 176 | 	if dexpreopt.GetGlobalConfig(ctx).DisablePreopt { | 
| Ulya Trafimovich | acb33e0 | 2019-11-01 17:57:29 +0000 | [diff] [blame] | 177 | 		return true | 
 | 178 | 	} | 
 | 179 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 180 | 	if ctx.Config().UnbundledBuild() { | 
 | 181 | 		return true | 
 | 182 | 	} | 
 | 183 |  | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 184 | 	if len(ctx.Config().Targets[android.Android]) == 0 { | 
 | 185 | 		// Host-only build | 
 | 186 | 		return true | 
 | 187 | 	} | 
 | 188 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 189 | 	return false | 
 | 190 | } | 
 | 191 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 192 | type dexpreoptBootJars struct { | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 193 | 	defaultBootImage *bootImageConfig | 
 | 194 | 	otherImages      []*bootImageConfig | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 195 |  | 
 | 196 | 	dexpreoptConfigForMake android.WritablePath | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 197 | } | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 198 |  | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 199 | // Accessor function for the apex package. Returns nil if dexpreopt is disabled. | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 200 | func DexpreoptedArtApexJars(ctx android.BuilderContext) map[android.ArchType]android.OutputPaths { | 
| Ulya Trafimovich | 4456188 | 2020-01-03 13:25:54 +0000 | [diff] [blame] | 201 | 	if skipDexpreoptBootJars(ctx) { | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 202 | 		return nil | 
 | 203 | 	} | 
| Ulya Trafimovich | 7eebb4f | 2020-01-22 13:41:06 +0000 | [diff] [blame] | 204 | 	// Include dexpreopt files for the primary boot image. | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 205 | 	files := map[android.ArchType]android.OutputPaths{} | 
 | 206 | 	for _, variant := range artBootImageConfig(ctx).variants { | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 207 | 		files[variant.target.Arch.ArchType] = variant.imagesDeps | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 208 | 	} | 
| Ulya Trafimovich | 7eebb4f | 2020-01-22 13:41:06 +0000 | [diff] [blame] | 209 | 	return files | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 210 | } | 
 | 211 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 212 | // dexpreoptBoot singleton rules | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 213 | func (d *dexpreoptBootJars) GenerateBuildActions(ctx android.SingletonContext) { | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 214 | 	if skipDexpreoptBootJars(ctx) { | 
 | 215 | 		return | 
 | 216 | 	} | 
| Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 217 | 	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 Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 221 |  | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 222 | 	d.dexpreoptConfigForMake = android.PathForOutput(ctx, ctx.Config().DeviceName(), "dexpreopt.config") | 
 | 223 | 	writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake) | 
 | 224 |  | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 225 | 	global := dexpreopt.GetGlobalConfig(ctx) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 226 |  | 
 | 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 Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 233 | 		global.SanitizeLite { | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 234 | 		return | 
 | 235 | 	} | 
 | 236 |  | 
| Lingfeng Yang | 54191fa | 2019-12-19 16:40:09 +0000 | [diff] [blame] | 237 | 	// Always create the default boot image first, to get a unique profile rule for all images. | 
 | 238 | 	d.defaultBootImage = buildBootImage(ctx, defaultBootImageConfig(ctx)) | 
| Ulya Trafimovich | 4456188 | 2020-01-03 13:25:54 +0000 | [diff] [blame] | 239 | 	// 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 Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 241 |  | 
 | 242 | 	dumpOatRules(ctx, d.defaultBootImage) | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 243 | } | 
 | 244 |  | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 245 | // 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. | 
 | 248 | func 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 Trafimovich | c0eb0b1 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 267 | 	fromUpdatableApex := isApexModule && apex.Updatable() | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 268 | 	if image.name == artBootImageName { | 
 | 269 | 		if isApexModule && strings.HasPrefix(apex.ApexName(), "com.android.art.") { | 
| Ulya Trafimovich | c0eb0b1 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 270 | 			// ok: found the jar in the ART apex | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 271 | 		} else if isApexModule && apex.IsForPlatform() && Bool(module.(*Library).deviceProperties.Hostdex) { | 
| Ulya Trafimovich | c0eb0b1 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 272 | 			// exception (skip and continue): special "hostdex" platform variant | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 273 | 			return -1, nil | 
| Ulya Trafimovich | b4d816e | 2020-04-08 15:00:49 +0100 | [diff] [blame] | 274 | 		} else if name == "jacocoagent" && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") { | 
| Ulya Trafimovich | c0eb0b1 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 275 | 			// exception (skip and continue): Jacoco platform variant for a coverage build | 
| Ulya Trafimovich | b4d816e | 2020-04-08 15:00:49 +0100 | [diff] [blame] | 276 | 			return -1, nil | 
| Ulya Trafimovich | c0eb0b1 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 277 | 		} else if fromUpdatableApex { | 
 | 278 | 			// error: this jar is part of an updatable apex other than ART | 
 | 279 | 			ctx.Errorf("module '%s' from updatable apex '%s' is not allowed in the ART boot image", name, apex.ApexName()) | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 280 | 		} else { | 
| Ulya Trafimovich | c0eb0b1 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 281 | 			// error: this jar is part of the platform or a non-updatable apex | 
 | 282 | 			ctx.Errorf("module '%s' is not allowed in the ART boot image", name) | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 283 | 		} | 
 | 284 | 	} else if image.name == frameworkBootImageName { | 
| Ulya Trafimovich | c0eb0b1 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 285 | 		if !fromUpdatableApex { | 
 | 286 | 			// ok: this jar is part of the platform or a non-updatable apex | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 287 | 		} else { | 
| Ulya Trafimovich | c0eb0b1 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 288 | 			// error: this jar is part of an updatable apex | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 289 | 			ctx.Errorf("module '%s' from updatable apex '%s' is not allowed in the framework boot image", name, apex.ApexName()) | 
 | 290 | 		} | 
 | 291 | 	} else { | 
 | 292 | 		panic("unknown boot image: " + image.name) | 
 | 293 | 	} | 
 | 294 |  | 
 | 295 | 	return index, jar.DexJar() | 
 | 296 | } | 
 | 297 |  | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 298 | // buildBootImage takes a bootImageConfig, creates rules to build it, and returns the image. | 
 | 299 | func buildBootImage(ctx android.SingletonContext, image *bootImageConfig) *bootImageConfig { | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 300 | 	// Collect dex jar paths for the boot image modules. | 
 | 301 | 	// This logic is tested in the apex package to avoid import cycle apex <-> java. | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 302 | 	bootDexJars := make(android.Paths, len(image.modules)) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 303 | 	ctx.VisitAllModules(func(module android.Module) { | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 304 | 		if i, j := getBootImageJar(ctx, image, module); i != -1 { | 
 | 305 | 			bootDexJars[i] = j | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 306 | 		} | 
 | 307 | 	}) | 
 | 308 |  | 
 | 309 | 	var missingDeps []string | 
 | 310 | 	// Ensure all modules were converted to paths | 
 | 311 | 	for i := range bootDexJars { | 
 | 312 | 		if bootDexJars[i] == nil { | 
 | 313 | 			if ctx.Config().AllowMissingDependencies() { | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 314 | 				missingDeps = append(missingDeps, image.modules[i]) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 315 | 				bootDexJars[i] = android.PathForOutput(ctx, "missing") | 
 | 316 | 			} else { | 
| Ulya Trafimovich | cc21bba | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 317 | 				ctx.Errorf("failed to find a dex jar path for module '%s'"+ | 
 | 318 | 					", note that some jars may be filtered out by module constraints", | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 319 | 					image.modules[i]) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 320 | 			} | 
 | 321 | 		} | 
 | 322 | 	} | 
 | 323 |  | 
 | 324 | 	// The path to bootclasspath dex files needs to be known at module GenerateAndroidBuildAction time, before | 
 | 325 | 	// the bootclasspath modules have been compiled.  Copy the dex jars there so the module rules that have | 
 | 326 | 	// already been set up can find them. | 
 | 327 | 	for i := range bootDexJars { | 
 | 328 | 		ctx.Build(pctx, android.BuildParams{ | 
 | 329 | 			Rule:   android.Cp, | 
 | 330 | 			Input:  bootDexJars[i], | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 331 | 			Output: image.dexPaths[i], | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 332 | 		}) | 
 | 333 | 	} | 
 | 334 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 335 | 	profile := bootImageProfileRule(ctx, image, missingDeps) | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 336 | 	bootFrameworkProfileRule(ctx, image, missingDeps) | 
| Vladimir Marko | b92ae27 | 2020-04-01 13:52:27 +0100 | [diff] [blame] | 337 | 	updatableBcpPackagesRule(ctx, image, missingDeps) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 338 |  | 
| Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 339 | 	var allFiles android.Paths | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 340 | 	for _, variant := range image.variants { | 
 | 341 | 		files := buildBootImageVariant(ctx, variant, profile, missingDeps) | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 342 | 		allFiles = append(allFiles, files.Paths()...) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 343 | 	} | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 344 |  | 
| Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 345 | 	if image.zip != nil { | 
 | 346 | 		rule := android.NewRuleBuilder() | 
 | 347 | 		rule.Command(). | 
| Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 348 | 			BuiltTool(ctx, "soong_zip"). | 
| Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 349 | 			FlagWithOutput("-o ", image.zip). | 
 | 350 | 			FlagWithArg("-C ", image.dir.String()). | 
 | 351 | 			FlagWithInputList("-f ", allFiles, " -f ") | 
 | 352 |  | 
 | 353 | 		rule.Build(pctx, ctx, "zip_"+image.name, "zip "+image.name+" image") | 
 | 354 | 	} | 
 | 355 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 356 | 	return image | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 357 | } | 
 | 358 |  | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 359 | func buildBootImageVariant(ctx android.SingletonContext, image *bootImageVariant, | 
 | 360 | 	profile android.Path, missingDeps []string) android.WritablePaths { | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 361 |  | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 362 | 	globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx) | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 363 | 	global := dexpreopt.GetGlobalConfig(ctx) | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 364 |  | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 365 | 	arch := image.target.Arch.ArchType | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 366 | 	symbolsDir := image.symbolsDir.Join(ctx, image.installSubdir, arch.String()) | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 367 | 	symbolsFile := symbolsDir.Join(ctx, image.stem+".oat") | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 368 | 	outputDir := image.dir.Join(ctx, image.installSubdir, arch.String()) | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 369 | 	outputPath := outputDir.Join(ctx, image.stem+".oat") | 
 | 370 | 	oatLocation := dexpreopt.PathToLocation(outputPath, arch) | 
 | 371 | 	imagePath := outputPath.ReplaceExtension(ctx, "art") | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 372 |  | 
 | 373 | 	rule := android.NewRuleBuilder() | 
 | 374 | 	rule.MissingDeps(missingDeps) | 
 | 375 |  | 
 | 376 | 	rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String()) | 
 | 377 | 	rule.Command().Text("rm").Flag("-f"). | 
 | 378 | 		Flag(symbolsDir.Join(ctx, "*.art").String()). | 
 | 379 | 		Flag(symbolsDir.Join(ctx, "*.oat").String()). | 
 | 380 | 		Flag(symbolsDir.Join(ctx, "*.invocation").String()) | 
 | 381 | 	rule.Command().Text("rm").Flag("-f"). | 
 | 382 | 		Flag(outputDir.Join(ctx, "*.art").String()). | 
 | 383 | 		Flag(outputDir.Join(ctx, "*.oat").String()). | 
 | 384 | 		Flag(outputDir.Join(ctx, "*.invocation").String()) | 
 | 385 |  | 
 | 386 | 	cmd := rule.Command() | 
 | 387 |  | 
 | 388 | 	extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS") | 
 | 389 | 	if extraFlags == "" { | 
 | 390 | 		// Use ANDROID_LOG_TAGS to suppress most logging by default... | 
 | 391 | 		cmd.Text(`ANDROID_LOG_TAGS="*:e"`) | 
 | 392 | 	} else { | 
 | 393 | 		// ...unless the boot image is generated specifically for testing, then allow all logging. | 
 | 394 | 		cmd.Text(`ANDROID_LOG_TAGS="*:v"`) | 
 | 395 | 	} | 
 | 396 |  | 
 | 397 | 	invocationPath := outputPath.ReplaceExtension(ctx, "invocation") | 
 | 398 |  | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 399 | 	cmd.Tool(globalSoong.Dex2oat). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 400 | 		Flag("--avoid-storing-invocation"). | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 401 | 		FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath). | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 402 | 		Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms). | 
 | 403 | 		Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 404 |  | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 405 | 	if profile != nil { | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 406 | 		cmd.FlagWithArg("--compiler-filter=", "speed-profile") | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 407 | 		cmd.FlagWithInput("--profile-file=", profile) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 408 | 	} | 
 | 409 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 410 | 	if global.DirtyImageObjects.Valid() { | 
 | 411 | 		cmd.FlagWithInput("--dirty-image-objects=", global.DirtyImageObjects.Path()) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 412 | 	} | 
 | 413 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 414 | 	if image.extension { | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 415 | 		artImage := image.primaryImages | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 416 | 		cmd. | 
 | 417 | 			Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":"). | 
 | 418 | 			Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":"). | 
 | 419 | 			FlagWithArg("--boot-image=", dexpreopt.PathToLocation(artImage, arch)).Implicit(artImage) | 
 | 420 | 	} else { | 
 | 421 | 		cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress()) | 
 | 422 | 	} | 
 | 423 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 424 | 	cmd. | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 425 | 		FlagForEachInput("--dex-file=", image.dexPaths.Paths()). | 
 | 426 | 		FlagForEachArg("--dex-location=", image.dexLocations). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 427 | 		Flag("--generate-debug-info"). | 
 | 428 | 		Flag("--generate-build-id"). | 
| Mathieu Chartier | 54fd807 | 2019-07-26 13:50:04 -0700 | [diff] [blame] | 429 | 		Flag("--image-format=lz4hc"). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 430 | 		FlagWithArg("--oat-symbols=", symbolsFile.String()). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 431 | 		Flag("--strip"). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 432 | 		FlagWithArg("--oat-file=", outputPath.String()). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 433 | 		FlagWithArg("--oat-location=", oatLocation). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 434 | 		FlagWithArg("--image=", imagePath.String()). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 435 | 		FlagWithArg("--instruction-set=", arch.String()). | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 436 | 		FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]). | 
 | 437 | 		FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]). | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 438 | 		FlagWithArg("--android-root=", global.EmptyDirectory). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 439 | 		FlagWithArg("--no-inline-from=", "core-oj.jar"). | 
| Ulya Trafimovich | 4fd35a2 | 2020-03-09 12:46:06 +0000 | [diff] [blame] | 440 | 		Flag("--force-determinism"). | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 441 | 		Flag("--abort-on-hard-verifier-error") | 
 | 442 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 443 | 	if global.BootFlags != "" { | 
 | 444 | 		cmd.Flag(global.BootFlags) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 445 | 	} | 
 | 446 |  | 
 | 447 | 	if extraFlags != "" { | 
 | 448 | 		cmd.Flag(extraFlags) | 
 | 449 | 	} | 
 | 450 |  | 
| Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 451 | 	cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage)) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 452 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 453 | 	installDir := filepath.Join("/", image.installSubdir, arch.String()) | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 454 | 	vdexInstallDir := filepath.Join("/", image.installSubdir) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 455 |  | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 456 | 	var vdexInstalls android.RuleBuilderInstalls | 
 | 457 | 	var unstrippedInstalls android.RuleBuilderInstalls | 
 | 458 |  | 
| Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 459 | 	var zipFiles android.WritablePaths | 
 | 460 |  | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 461 | 	for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") { | 
 | 462 | 		cmd.ImplicitOutput(artOrOat) | 
 | 463 | 		zipFiles = append(zipFiles, artOrOat) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 464 |  | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 465 | 		// Install the .oat and .art files | 
 | 466 | 		rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base())) | 
 | 467 | 	} | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 468 |  | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 469 | 	for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") { | 
 | 470 | 		cmd.ImplicitOutput(vdex) | 
 | 471 | 		zipFiles = append(zipFiles, vdex) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 472 |  | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 473 | 		// The vdex files are identical between architectures, install them to a shared location.  The Make rules will | 
 | 474 | 		// only use the install rules for one architecture, and will create symlinks into the architecture-specific | 
 | 475 | 		// directories. | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 476 | 		vdexInstalls = append(vdexInstalls, | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 477 | 			android.RuleBuilderInstall{vdex, filepath.Join(vdexInstallDir, vdex.Base())}) | 
| Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 478 | 	} | 
 | 479 |  | 
 | 480 | 	for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") { | 
 | 481 | 		cmd.ImplicitOutput(unstrippedOat) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 482 |  | 
 | 483 | 		// Install the unstripped oat files.  The Make rules will put these in $(TARGET_OUT_UNSTRIPPED) | 
 | 484 | 		unstrippedInstalls = append(unstrippedInstalls, | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 485 | 			android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())}) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 486 | 	} | 
 | 487 |  | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 488 | 	rule.Build(pctx, ctx, image.name+"JarsDexpreopt_"+arch.String(), "dexpreopt "+image.name+" jars "+arch.String()) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 489 |  | 
 | 490 | 	// save output and installed files for makevars | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 491 | 	image.installs = rule.Installs() | 
 | 492 | 	image.vdexInstalls = vdexInstalls | 
 | 493 | 	image.unstrippedInstalls = unstrippedInstalls | 
| Colin Cross | df8eebe | 2019-04-09 15:29:41 -0700 | [diff] [blame] | 494 |  | 
 | 495 | 	return zipFiles | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 496 | } | 
 | 497 |  | 
 | 498 | const failureMessage = `ERROR: Dex2oat failed to compile a boot image. | 
 | 499 | It is likely that the boot classpath is inconsistent. | 
 | 500 | Rebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.` | 
 | 501 |  | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 502 | func bootImageProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath { | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 503 | 	globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx) | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 504 | 	global := dexpreopt.GetGlobalConfig(ctx) | 
| Nicolas Geoffray | 27c7cc6 | 2019-02-24 16:04:52 +0000 | [diff] [blame] | 505 |  | 
| Mathieu Chartier | 6adeee1 | 2019-06-26 10:01:36 -0700 | [diff] [blame] | 506 | 	if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() { | 
| Nicolas Geoffray | 27c7cc6 | 2019-02-24 16:04:52 +0000 | [diff] [blame] | 507 | 		return nil | 
 | 508 | 	} | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 509 | 	profile := ctx.Config().Once(bootImageProfileRuleKey, func() interface{} { | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 510 | 		defaultProfile := "frameworks/base/config/boot-image-profile.txt" | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 511 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 512 | 		rule := android.NewRuleBuilder() | 
 | 513 | 		rule.MissingDeps(missingDeps) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 514 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 515 | 		var bootImageProfile android.Path | 
 | 516 | 		if len(global.BootImageProfiles) > 1 { | 
 | 517 | 			combinedBootImageProfile := image.dir.Join(ctx, "boot-image-profile.txt") | 
 | 518 | 			rule.Command().Text("cat").Inputs(global.BootImageProfiles).Text(">").Output(combinedBootImageProfile) | 
 | 519 | 			bootImageProfile = combinedBootImageProfile | 
 | 520 | 		} else if len(global.BootImageProfiles) == 1 { | 
 | 521 | 			bootImageProfile = global.BootImageProfiles[0] | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 522 | 		} else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() { | 
 | 523 | 			bootImageProfile = path.Path() | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 524 | 		} else { | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 525 | 			// No profile (not even a default one, which is the case on some branches | 
 | 526 | 			// like master-art-host that don't have frameworks/base). | 
 | 527 | 			// Return nil and continue without profile. | 
 | 528 | 			return nil | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 529 | 		} | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 530 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 531 | 		profile := image.dir.Join(ctx, "boot.prof") | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 532 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 533 | 		rule.Command(). | 
 | 534 | 			Text(`ANDROID_LOG_TAGS="*:e"`). | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 535 | 			Tool(globalSoong.Profman). | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 536 | 			FlagWithInput("--create-profile-from=", bootImageProfile). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 537 | 			FlagForEachInput("--apk=", image.dexPathsDeps.Paths()). | 
 | 538 | 			FlagForEachArg("--dex-location=", image.dexLocationsDeps). | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 539 | 			FlagWithOutput("--reference-profile-file=", profile) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 540 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 541 | 		rule.Install(profile, "/system/etc/boot-image.prof") | 
 | 542 |  | 
 | 543 | 		rule.Build(pctx, ctx, "bootJarsProfile", "profile boot jars") | 
 | 544 |  | 
 | 545 | 		image.profileInstalls = rule.Installs() | 
 | 546 |  | 
 | 547 | 		return profile | 
| Ulyana Trafimovich | de53441 | 2019-11-08 10:51:01 +0000 | [diff] [blame] | 548 | 	}) | 
 | 549 | 	if profile == nil { | 
 | 550 | 		return nil // wrap nil into a typed pointer with value nil | 
 | 551 | 	} | 
 | 552 | 	return profile.(android.WritablePath) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 553 | } | 
 | 554 |  | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 555 | var bootImageProfileRuleKey = android.NewOnceKey("bootImageProfileRule") | 
 | 556 |  | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 557 | func bootFrameworkProfileRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath { | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 558 | 	globalSoong := dexpreopt.GetCachedGlobalSoongConfig(ctx) | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 559 | 	global := dexpreopt.GetGlobalConfig(ctx) | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 560 |  | 
 | 561 | 	if global.DisableGenerateProfile || ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() { | 
 | 562 | 		return nil | 
 | 563 | 	} | 
 | 564 | 	return ctx.Config().Once(bootFrameworkProfileRuleKey, func() interface{} { | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 565 | 		rule := android.NewRuleBuilder() | 
 | 566 | 		rule.MissingDeps(missingDeps) | 
 | 567 |  | 
 | 568 | 		// Some branches like master-art-host don't have frameworks/base, so manually | 
 | 569 | 		// handle the case that the default is missing.  Those branches won't attempt to build the profile rule, | 
 | 570 | 		// and if they do they'll get a missing deps error. | 
 | 571 | 		defaultProfile := "frameworks/base/config/boot-profile.txt" | 
 | 572 | 		path := android.ExistentPathForSource(ctx, defaultProfile) | 
 | 573 | 		var bootFrameworkProfile android.Path | 
 | 574 | 		if path.Valid() { | 
 | 575 | 			bootFrameworkProfile = path.Path() | 
 | 576 | 		} else { | 
 | 577 | 			missingDeps = append(missingDeps, defaultProfile) | 
 | 578 | 			bootFrameworkProfile = android.PathForOutput(ctx, "missing") | 
 | 579 | 		} | 
 | 580 |  | 
 | 581 | 		profile := image.dir.Join(ctx, "boot.bprof") | 
 | 582 |  | 
 | 583 | 		rule.Command(). | 
 | 584 | 			Text(`ANDROID_LOG_TAGS="*:e"`). | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 585 | 			Tool(globalSoong.Profman). | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 586 | 			Flag("--generate-boot-profile"). | 
 | 587 | 			FlagWithInput("--create-profile-from=", bootFrameworkProfile). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 588 | 			FlagForEachInput("--apk=", image.dexPathsDeps.Paths()). | 
 | 589 | 			FlagForEachArg("--dex-location=", image.dexLocationsDeps). | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 590 | 			FlagWithOutput("--reference-profile-file=", profile) | 
 | 591 |  | 
 | 592 | 		rule.Install(profile, "/system/etc/boot-image.bprof") | 
 | 593 | 		rule.Build(pctx, ctx, "bootFrameworkProfile", "profile boot framework jars") | 
 | 594 | 		image.profileInstalls = append(image.profileInstalls, rule.Installs()...) | 
 | 595 |  | 
 | 596 | 		return profile | 
 | 597 | 	}).(android.WritablePath) | 
 | 598 | } | 
 | 599 |  | 
 | 600 | var bootFrameworkProfileRuleKey = android.NewOnceKey("bootFrameworkProfileRule") | 
 | 601 |  | 
| Vladimir Marko | b92ae27 | 2020-04-01 13:52:27 +0100 | [diff] [blame] | 602 | func updatableBcpPackagesRule(ctx android.SingletonContext, image *bootImageConfig, missingDeps []string) android.WritablePath { | 
 | 603 | 	if ctx.Config().IsPdkBuild() || ctx.Config().UnbundledBuild() { | 
 | 604 | 		return nil | 
 | 605 | 	} | 
 | 606 |  | 
 | 607 | 	return ctx.Config().Once(updatableBcpPackagesRuleKey, func() interface{} { | 
 | 608 | 		global := dexpreopt.GetGlobalConfig(ctx) | 
 | 609 | 		updatableModules := dexpreopt.GetJarsFromApexJarPairs(global.UpdatableBootJars) | 
 | 610 |  | 
 | 611 | 		// Collect `permitted_packages` for updatable boot jars. | 
 | 612 | 		var updatablePackages []string | 
 | 613 | 		ctx.VisitAllModules(func(module android.Module) { | 
| Paul Duffin | a105cf9 | 2020-05-29 11:24:51 +0100 | [diff] [blame] | 614 | 			if j, ok := module.(PermittedPackagesForUpdatableBootJars); ok { | 
| Vladimir Marko | b92ae27 | 2020-04-01 13:52:27 +0100 | [diff] [blame] | 615 | 				name := ctx.ModuleName(module) | 
 | 616 | 				if i := android.IndexList(name, updatableModules); i != -1 { | 
| Paul Duffin | a105cf9 | 2020-05-29 11:24:51 +0100 | [diff] [blame] | 617 | 					pp := j.PermittedPackagesForUpdatableBootJars() | 
| Vladimir Marko | b92ae27 | 2020-04-01 13:52:27 +0100 | [diff] [blame] | 618 | 					if len(pp) > 0 { | 
 | 619 | 						updatablePackages = append(updatablePackages, pp...) | 
 | 620 | 					} else { | 
 | 621 | 						ctx.Errorf("Missing permitted_packages for %s", name) | 
 | 622 | 					} | 
 | 623 | 					// Do not match the same library repeatedly. | 
 | 624 | 					updatableModules = append(updatableModules[:i], updatableModules[i+1:]...) | 
 | 625 | 				} | 
 | 626 | 			} | 
 | 627 | 		}) | 
 | 628 |  | 
 | 629 | 		// Sort updatable packages to ensure deterministic ordering. | 
 | 630 | 		sort.Strings(updatablePackages) | 
 | 631 |  | 
 | 632 | 		updatableBcpPackagesName := "updatable-bcp-packages.txt" | 
 | 633 | 		updatableBcpPackages := image.dir.Join(ctx, updatableBcpPackagesName) | 
 | 634 |  | 
 | 635 | 		ctx.Build(pctx, android.BuildParams{ | 
 | 636 | 			Rule:   android.WriteFile, | 
 | 637 | 			Output: updatableBcpPackages, | 
 | 638 | 			Args: map[string]string{ | 
 | 639 | 				// WriteFile automatically adds the last end-of-line. | 
 | 640 | 				"content": strings.Join(updatablePackages, "\\n"), | 
 | 641 | 			}, | 
 | 642 | 		}) | 
 | 643 |  | 
 | 644 | 		rule := android.NewRuleBuilder() | 
 | 645 | 		rule.MissingDeps(missingDeps) | 
 | 646 | 		rule.Install(updatableBcpPackages, "/system/etc/"+updatableBcpPackagesName) | 
 | 647 | 		// TODO: Rename `profileInstalls` to `extraInstalls`? | 
 | 648 | 		// Maybe even move the field out of the bootImageConfig into some higher level type? | 
 | 649 | 		image.profileInstalls = append(image.profileInstalls, rule.Installs()...) | 
 | 650 |  | 
 | 651 | 		return updatableBcpPackages | 
 | 652 | 	}).(android.WritablePath) | 
 | 653 | } | 
 | 654 |  | 
 | 655 | var updatableBcpPackagesRuleKey = android.NewOnceKey("updatableBcpPackagesRule") | 
 | 656 |  | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 657 | func dumpOatRules(ctx android.SingletonContext, image *bootImageConfig) { | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 658 | 	var allPhonies android.Paths | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 659 | 	for _, image := range image.variants { | 
 | 660 | 		arch := image.target.Arch.ArchType | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 661 | 		// Create a rule to call oatdump. | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 662 | 		output := android.PathForOutput(ctx, "boot."+arch.String()+".oatdump.txt") | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 663 | 		rule := android.NewRuleBuilder() | 
 | 664 | 		rule.Command(). | 
 | 665 | 			// TODO: for now, use the debug version for better error reporting | 
| Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 666 | 			BuiltTool(ctx, "oatdumpd"). | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 667 | 			FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":"). | 
 | 668 | 			FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":"). | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 669 | 			FlagWithArg("--image=", strings.Join(image.imageLocations, ":")).Implicits(image.imagesDeps.Paths()). | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 670 | 			FlagWithOutput("--output=", output). | 
 | 671 | 			FlagWithArg("--instruction-set=", arch.String()) | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 672 | 		rule.Build(pctx, ctx, "dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String()) | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 673 |  | 
 | 674 | 		// Create a phony rule that depends on the output file and prints the path. | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 675 | 		phony := android.PathForPhony(ctx, "dump-oat-boot-"+arch.String()) | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 676 | 		rule = android.NewRuleBuilder() | 
 | 677 | 		rule.Command(). | 
 | 678 | 			Implicit(output). | 
 | 679 | 			ImplicitOutput(phony). | 
 | 680 | 			Text("echo").FlagWithArg("Output in ", output.String()) | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 681 | 		rule.Build(pctx, ctx, "phony-dump-oat-boot-"+arch.String(), "dump oat boot "+arch.String()) | 
| Colin Cross | c9a4c36 | 2019-02-26 21:13:48 -0800 | [diff] [blame] | 682 |  | 
 | 683 | 		allPhonies = append(allPhonies, phony) | 
 | 684 | 	} | 
 | 685 |  | 
 | 686 | 	phony := android.PathForPhony(ctx, "dump-oat-boot") | 
 | 687 | 	ctx.Build(pctx, android.BuildParams{ | 
 | 688 | 		Rule:        android.Phony, | 
 | 689 | 		Output:      phony, | 
 | 690 | 		Inputs:      allPhonies, | 
 | 691 | 		Description: "dump-oat-boot", | 
 | 692 | 	}) | 
 | 693 |  | 
 | 694 | } | 
 | 695 |  | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 696 | func writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) { | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 697 | 	data := dexpreopt.GetGlobalConfigRawData(ctx) | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 698 |  | 
 | 699 | 	ctx.Build(pctx, android.BuildParams{ | 
 | 700 | 		Rule:   android.WriteFile, | 
 | 701 | 		Output: path, | 
 | 702 | 		Args: map[string]string{ | 
 | 703 | 			"content": string(data), | 
 | 704 | 		}, | 
 | 705 | 	}) | 
 | 706 | } | 
 | 707 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 708 | // Export paths for default boot image to Make | 
 | 709 | func (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) { | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 710 | 	if d.dexpreoptConfigForMake != nil { | 
 | 711 | 		ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String()) | 
| Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 712 | 		ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String()) | 
| Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 713 | 	} | 
 | 714 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 715 | 	image := d.defaultBootImage | 
 | 716 | 	if image != nil { | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 717 | 		ctx.Strict("DEXPREOPT_IMAGE_PROFILE_BUILT_INSTALLED", image.profileInstalls.String()) | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 718 | 		ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(image.dexPathsDeps.Strings(), " ")) | 
 | 719 | 		ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(image.dexLocationsDeps, " ")) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 720 |  | 
 | 721 | 		var imageNames []string | 
 | 722 | 		for _, current := range append(d.otherImages, image) { | 
 | 723 | 			imageNames = append(imageNames, current.name) | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 724 | 			for _, current := range current.variants { | 
| Jeff Tinker | 74cc81c | 2020-05-19 17:45:22 +0000 | [diff] [blame] | 725 | 				sfx := current.name + "_" + current.target.Arch.ArchType.String() | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 726 | 				ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, current.vdexInstalls.String()) | 
 | 727 | 				ctx.Strict("DEXPREOPT_IMAGE_"+sfx, current.images.String()) | 
 | 728 | 				ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(current.imagesDeps.Strings(), " ")) | 
 | 729 | 				ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, current.installs.String()) | 
 | 730 | 				ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, current.unstrippedInstalls.String()) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 731 | 			} | 
| Colin Cross | 31bf00d | 2019-12-04 13:16:01 -0800 | [diff] [blame] | 732 |  | 
| Ulya Trafimovich | 3391a1e | 2020-01-03 17:33:17 +0000 | [diff] [blame] | 733 | 			ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_"+current.name, strings.Join(current.imageLocations, ":")) | 
| Colin Cross | 31bf00d | 2019-12-04 13:16:01 -0800 | [diff] [blame] | 734 | 			ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String()) | 
| Nicolas Geoffray | 72892f1 | 2019-02-22 15:34:40 +0000 | [diff] [blame] | 735 | 		} | 
 | 736 | 		ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(imageNames, " ")) | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 737 | 	} | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 738 | } |