xref: /aosp_15_r20/build/soong/java/dexpreopt_bootjars.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2019 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage java
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"path/filepath"
19*333d2b36SAndroid Build Coastguard Worker	"strings"
20*333d2b36SAndroid Build Coastguard Worker
21*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
22*333d2b36SAndroid Build Coastguard Worker	"android/soong/dexpreopt"
23*333d2b36SAndroid Build Coastguard Worker
24*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint"
25*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint/proptools"
26*333d2b36SAndroid Build Coastguard Worker)
27*333d2b36SAndroid Build Coastguard Worker
28*333d2b36SAndroid Build Coastguard Worker// =================================================================================================
29*333d2b36SAndroid Build Coastguard Worker// WIP - see http://b/177892522 for details
30*333d2b36SAndroid Build Coastguard Worker//
31*333d2b36SAndroid Build Coastguard Worker// The build support for boot images is currently being migrated away from singleton to modules so
32*333d2b36SAndroid Build Coastguard Worker// the documentation may not be strictly accurate. Rather than update the documentation at every
33*333d2b36SAndroid Build Coastguard Worker// step which will create a lot of churn the changes that have been made will be listed here and the
34*333d2b36SAndroid Build Coastguard Worker// documentation will be updated once it is closer to the final result.
35*333d2b36SAndroid Build Coastguard Worker//
36*333d2b36SAndroid Build Coastguard Worker// Changes:
37*333d2b36SAndroid Build Coastguard Worker// 1) dex_bootjars is now a singleton module and not a plain singleton.
38*333d2b36SAndroid Build Coastguard Worker// 2) Boot images are now represented by the boot_image module type.
39*333d2b36SAndroid Build Coastguard Worker// 3) The art boot image is called "art-boot-image", the framework boot image is called
40*333d2b36SAndroid Build Coastguard Worker//    "framework-boot-image".
41*333d2b36SAndroid Build Coastguard Worker// 4) They are defined in art/build/boot/Android.bp and frameworks/base/boot/Android.bp
42*333d2b36SAndroid Build Coastguard Worker//    respectively.
43*333d2b36SAndroid Build Coastguard Worker// 5) Each boot_image retrieves the appropriate boot image configuration from the map returned by
44*333d2b36SAndroid Build Coastguard Worker//    genBootImageConfigs() using the image_name specified in the boot_image module.
45*333d2b36SAndroid Build Coastguard Worker// =================================================================================================
46*333d2b36SAndroid Build Coastguard Worker
47*333d2b36SAndroid Build Coastguard Worker// This comment describes:
48*333d2b36SAndroid Build Coastguard Worker//   1. ART boot images in general (their types, structure, file layout, etc.)
49*333d2b36SAndroid Build Coastguard Worker//   2. build system support for boot images
50*333d2b36SAndroid Build Coastguard Worker//
51*333d2b36SAndroid Build Coastguard Worker// 1. ART boot images
52*333d2b36SAndroid Build Coastguard Worker// ------------------
53*333d2b36SAndroid Build Coastguard Worker//
54*333d2b36SAndroid Build Coastguard Worker// A boot image in ART is a set of files that contain AOT-compiled native code and a heap snapshot
55*333d2b36SAndroid Build Coastguard Worker// of AOT-initialized classes for the bootclasspath Java libraries. A boot image is compiled from a
56*333d2b36SAndroid Build Coastguard Worker// set of DEX jars by the dex2oat compiler. A boot image is used for two purposes: 1) it is
57*333d2b36SAndroid Build Coastguard Worker// installed on device and loaded at runtime, and 2) other Java libraries and apps are compiled
58*333d2b36SAndroid Build Coastguard Worker// against it (compilation may take place either on host, known as "dexpreopt", or on device, known
59*333d2b36SAndroid Build Coastguard Worker// as "dexopt").
60*333d2b36SAndroid Build Coastguard Worker//
61*333d2b36SAndroid Build Coastguard Worker// A boot image is not a single file, but a collection of interrelated files. Each boot image has a
62*333d2b36SAndroid Build Coastguard Worker// number of components that correspond to the Java libraries that constitute it. For each component
63*333d2b36SAndroid Build Coastguard Worker// there are multiple files:
64*333d2b36SAndroid Build Coastguard Worker//   - *.oat or *.odex file with native code (architecture-specific, one per instruction set)
65*333d2b36SAndroid Build Coastguard Worker//   - *.art file with pre-initialized Java classes (architecture-specific, one per instruction set)
66*333d2b36SAndroid Build Coastguard Worker//   - *.vdex file with verification metadata for the DEX bytecode (architecture independent)
67*333d2b36SAndroid Build Coastguard Worker//
68*333d2b36SAndroid Build Coastguard Worker// *.vdex files for the boot images do not contain the DEX bytecode itself, because the
69*333d2b36SAndroid Build Coastguard Worker// bootclasspath DEX files are stored on disk in uncompressed and aligned form. Consequently a boot
70*333d2b36SAndroid Build Coastguard Worker// image is not self-contained and cannot be used without its DEX files. To simplify the management
71*333d2b36SAndroid Build Coastguard Worker// of boot image files, ART uses a certain naming scheme and associates the following metadata with
72*333d2b36SAndroid Build Coastguard Worker// each boot image:
73*333d2b36SAndroid Build Coastguard Worker//   - A stem, which is a symbolic name that is prepended to boot image file names.
74*333d2b36SAndroid Build Coastguard Worker//   - A location (on-device path to the boot image files).
75*333d2b36SAndroid Build Coastguard Worker//   - A list of boot image locations (on-device paths to dependency boot images).
76*333d2b36SAndroid Build Coastguard Worker//   - A set of DEX locations (on-device paths to the DEX files, one location for one DEX file used
77*333d2b36SAndroid Build Coastguard Worker//     to compile the boot image).
78*333d2b36SAndroid Build Coastguard Worker//
79*333d2b36SAndroid Build Coastguard Worker// There are two kinds of boot images:
80*333d2b36SAndroid Build Coastguard Worker//   - primary boot images
81*333d2b36SAndroid Build Coastguard Worker//   - boot image extensions
82*333d2b36SAndroid Build Coastguard Worker//
83*333d2b36SAndroid Build Coastguard Worker// 1.1. Primary boot images
84*333d2b36SAndroid Build Coastguard Worker// ------------------------
85*333d2b36SAndroid Build Coastguard Worker//
86*333d2b36SAndroid Build Coastguard Worker// A primary boot image is compiled for a core subset of bootclasspath Java libraries. It does not
87*333d2b36SAndroid Build Coastguard Worker// depend on any other images, and other boot images may depend on it.
88*333d2b36SAndroid Build Coastguard Worker//
89*333d2b36SAndroid Build Coastguard Worker// For example, assuming that the stem is "boot", the location is /apex/com.android.art/javalib/,
90*333d2b36SAndroid Build Coastguard Worker// the set of core bootclasspath libraries is A B C, and the boot image is compiled for ARM targets
91*333d2b36SAndroid Build Coastguard Worker// (32 and 64 bits), it will have three components with the following files:
92*333d2b36SAndroid Build Coastguard Worker//   - /apex/com.android.art/javalib/{arm,arm64}/boot.{art,oat,vdex}
93*333d2b36SAndroid Build Coastguard Worker//   - /apex/com.android.art/javalib/{arm,arm64}/boot-B.{art,oat,vdex}
94*333d2b36SAndroid Build Coastguard Worker//   - /apex/com.android.art/javalib/{arm,arm64}/boot-C.{art,oat,vdex}
95*333d2b36SAndroid Build Coastguard Worker//
96*333d2b36SAndroid Build Coastguard Worker// The files of the first component are special: they do not have the component name appended after
97*333d2b36SAndroid Build Coastguard Worker// the stem. This naming convention dates back to the times when the boot image was not split into
98*333d2b36SAndroid Build Coastguard Worker// components, and there were just boot.oat and boot.art. The decision to split was motivated by
99*333d2b36SAndroid Build Coastguard Worker// licensing reasons for one of the bootclasspath libraries.
100*333d2b36SAndroid Build Coastguard Worker//
101*333d2b36SAndroid Build Coastguard Worker// As of November 2020 the only primary boot image in Android is the image in the ART APEX
102*333d2b36SAndroid Build Coastguard Worker// com.android.art. The primary ART boot image contains the Core libraries that are part of the ART
103*333d2b36SAndroid Build Coastguard Worker// module. When the ART module gets updated, the primary boot image will be updated with it, and all
104*333d2b36SAndroid Build Coastguard Worker// dependent images will get invalidated (the checksum of the primary image stored in dependent
105*333d2b36SAndroid Build Coastguard Worker// images will not match), unless they are updated in sync with the ART module.
106*333d2b36SAndroid Build Coastguard Worker//
107*333d2b36SAndroid Build Coastguard Worker// 1.2. Boot image extensions
108*333d2b36SAndroid Build Coastguard Worker// --------------------------
109*333d2b36SAndroid Build Coastguard Worker//
110*333d2b36SAndroid Build Coastguard Worker// A boot image extension is compiled for a subset of bootclasspath Java libraries (in particular,
111*333d2b36SAndroid Build Coastguard Worker// this subset does not include the Core bootclasspath libraries that go into the primary boot
112*333d2b36SAndroid Build Coastguard Worker// image). A boot image extension depends on the primary boot image and optionally some other boot
113*333d2b36SAndroid Build Coastguard Worker// image extensions. Other images may depend on it. In other words, boot image extensions can form
114*333d2b36SAndroid Build Coastguard Worker// acyclic dependency graphs.
115*333d2b36SAndroid Build Coastguard Worker//
116*333d2b36SAndroid Build Coastguard Worker// The motivation for boot image extensions comes from the Mainline project. Consider a situation
117*333d2b36SAndroid Build Coastguard Worker// when the list of bootclasspath libraries is A B C, and both A and B are parts of the Android
118*333d2b36SAndroid Build Coastguard Worker// platform, but C is part of an updatable APEX com.android.C. When the APEX is updated, the Java
119*333d2b36SAndroid Build Coastguard Worker// code for C might have changed compared to the code that was used to compile the boot image.
120*333d2b36SAndroid Build Coastguard Worker// Consequently, the whole boot image is obsolete and invalidated (even though the code for A and B
121*333d2b36SAndroid Build Coastguard Worker// that does not depend on C is up to date). To avoid this, the original monolithic boot image is
122*333d2b36SAndroid Build Coastguard Worker// split in two parts: the primary boot image that contains A B, and the boot image extension that
123*333d2b36SAndroid Build Coastguard Worker// contains C and depends on the primary boot image (extends it).
124*333d2b36SAndroid Build Coastguard Worker//
125*333d2b36SAndroid Build Coastguard Worker// For example, assuming that the stem is "boot", the location is /system/framework, the set of
126*333d2b36SAndroid Build Coastguard Worker// bootclasspath libraries is D E (where D is part of the platform and is located in
127*333d2b36SAndroid Build Coastguard Worker// /system/framework, and E is part of a non-updatable APEX com.android.E and is located in
128*333d2b36SAndroid Build Coastguard Worker// /apex/com.android.E/javalib), and the boot image is compiled for ARM targets (32 and 64 bits),
129*333d2b36SAndroid Build Coastguard Worker// it will have two components with the following files:
130*333d2b36SAndroid Build Coastguard Worker//   - /system/framework/{arm,arm64}/boot-D.{art,oat,vdex}
131*333d2b36SAndroid Build Coastguard Worker//   - /system/framework/{arm,arm64}/boot-E.{art,oat,vdex}
132*333d2b36SAndroid Build Coastguard Worker//
133*333d2b36SAndroid Build Coastguard Worker// As of November 2020 the only boot image extension in Android is the Framework boot image
134*333d2b36SAndroid Build Coastguard Worker// extension. It extends the primary ART boot image and contains Framework libraries and other
135*333d2b36SAndroid Build Coastguard Worker// bootclasspath libraries from the platform and non-updatable APEXes that are not included in the
136*333d2b36SAndroid Build Coastguard Worker// ART image. The Framework boot image extension is updated together with the platform. In the
137*333d2b36SAndroid Build Coastguard Worker// future other boot image extensions may be added for some updatable modules.
138*333d2b36SAndroid Build Coastguard Worker//
139*333d2b36SAndroid Build Coastguard Worker//
140*333d2b36SAndroid Build Coastguard Worker// 2. Build system support for boot images
141*333d2b36SAndroid Build Coastguard Worker// ---------------------------------------
142*333d2b36SAndroid Build Coastguard Worker//
143*333d2b36SAndroid Build Coastguard Worker// The primary ART boot image needs to be compiled with one dex2oat invocation that depends on DEX
144*333d2b36SAndroid Build Coastguard Worker// jars for the core libraries. Framework boot image extension needs to be compiled with one dex2oat
145*333d2b36SAndroid Build Coastguard Worker// invocation that depends on the primary ART boot image and all bootclasspath DEX jars except the
146*333d2b36SAndroid Build Coastguard Worker// core libraries as they are already part of the primary ART boot image.
147*333d2b36SAndroid Build Coastguard Worker//
148*333d2b36SAndroid Build Coastguard Worker// 2.1. Libraries that go in the boot images
149*333d2b36SAndroid Build Coastguard Worker// -----------------------------------------
150*333d2b36SAndroid Build Coastguard Worker//
151*333d2b36SAndroid Build Coastguard Worker// The contents of each boot image are determined by the PRODUCT variables. The primary ART APEX
152*333d2b36SAndroid Build Coastguard Worker// boot image contains libraries listed in the ART_APEX_JARS variable in the AOSP makefiles. The
153*333d2b36SAndroid Build Coastguard Worker// Framework boot image extension contains libraries specified in the PRODUCT_BOOT_JARS and
154*333d2b36SAndroid Build Coastguard Worker// PRODUCT_BOOT_JARS_EXTRA variables. The AOSP makefiles specify some common Framework libraries,
155*333d2b36SAndroid Build Coastguard Worker// but more product-specific libraries can be added in the product makefiles.
156*333d2b36SAndroid Build Coastguard Worker//
157*333d2b36SAndroid Build Coastguard Worker// Each component of the PRODUCT_BOOT_JARS and PRODUCT_BOOT_JARS_EXTRA variables is a
158*333d2b36SAndroid Build Coastguard Worker// colon-separated pair <apex>:<library>, where <apex> is the variant name of a non-updatable APEX,
159*333d2b36SAndroid Build Coastguard Worker// "platform" if the library is a part of the platform in the system partition, or "system_ext" if
160*333d2b36SAndroid Build Coastguard Worker// it's in the system_ext partition.
161*333d2b36SAndroid Build Coastguard Worker//
162*333d2b36SAndroid Build Coastguard Worker// In these variables APEXes are identified by their "variant names", i.e. the names they get
163*333d2b36SAndroid Build Coastguard Worker// mounted as in /apex on device. In Soong modules that is the name set in the "apex_name"
164*333d2b36SAndroid Build Coastguard Worker// properties, which default to the "name" values. For example, many APEXes have both
165*333d2b36SAndroid Build Coastguard Worker// com.android.xxx and com.google.android.xxx modules in Soong, but take the same place
166*333d2b36SAndroid Build Coastguard Worker// /apex/com.android.xxx at runtime. In these cases the variant name is always com.android.xxx,
167*333d2b36SAndroid Build Coastguard Worker// regardless which APEX goes into the product. See also android.ApexInfo.ApexVariationName and
168*333d2b36SAndroid Build Coastguard Worker// apex.apexBundleProperties.Apex_name.
169*333d2b36SAndroid Build Coastguard Worker//
170*333d2b36SAndroid Build Coastguard Worker// A related variable PRODUCT_APEX_BOOT_JARS contains bootclasspath libraries that are in APEXes.
171*333d2b36SAndroid Build Coastguard Worker// They are not included in the boot image. The only exception here are ART jars and core-icu4j.jar
172*333d2b36SAndroid Build Coastguard Worker// that have been historically part of the boot image and are now in apexes; they are in boot images
173*333d2b36SAndroid Build Coastguard Worker// and core-icu4j.jar is generally treated as being part of PRODUCT_BOOT_JARS.
174*333d2b36SAndroid Build Coastguard Worker//
175*333d2b36SAndroid Build Coastguard Worker// One exception to the above rules are "coverage" builds (a special build flavor which requires
176*333d2b36SAndroid Build Coastguard Worker// setting environment variable EMMA_INSTRUMENT_FRAMEWORK=true). In coverage builds the Java code in
177*333d2b36SAndroid Build Coastguard Worker// boot image libraries is instrumented, which means that the instrumentation library (jacocoagent)
178*333d2b36SAndroid Build Coastguard Worker// needs to be added to the list of bootclasspath DEX jars.
179*333d2b36SAndroid Build Coastguard Worker//
180*333d2b36SAndroid Build Coastguard Worker// In general, there is a requirement that the source code for a boot image library must be
181*333d2b36SAndroid Build Coastguard Worker// available at build time (e.g. it cannot be a stub that has a separate implementation library).
182*333d2b36SAndroid Build Coastguard Worker//
183*333d2b36SAndroid Build Coastguard Worker// 2.2. Static configs
184*333d2b36SAndroid Build Coastguard Worker// -------------------
185*333d2b36SAndroid Build Coastguard Worker//
186*333d2b36SAndroid Build Coastguard Worker// Because boot images are used to dexpreopt other Java modules, the paths to boot image files must
187*333d2b36SAndroid Build Coastguard Worker// be known by the time dexpreopt build rules for the dependent modules are generated. Boot image
188*333d2b36SAndroid Build Coastguard Worker// configs are constructed very early during the build, before build rule generation. The configs
189*333d2b36SAndroid Build Coastguard Worker// provide predefined paths to boot image files (these paths depend only on static build
190*333d2b36SAndroid Build Coastguard Worker// configuration, such as PRODUCT variables, and use hard-coded directory names).
191*333d2b36SAndroid Build Coastguard Worker//
192*333d2b36SAndroid Build Coastguard Worker// 2.3. Singleton
193*333d2b36SAndroid Build Coastguard Worker// --------------
194*333d2b36SAndroid Build Coastguard Worker//
195*333d2b36SAndroid Build Coastguard Worker// Build rules for the boot images are generated with a Soong singleton. Because a singleton has no
196*333d2b36SAndroid Build Coastguard Worker// dependencies on other modules, it has to find the modules for the DEX jars using VisitAllModules.
197*333d2b36SAndroid Build Coastguard Worker// Soong loops through all modules and compares each module against a list of bootclasspath library
198*333d2b36SAndroid Build Coastguard Worker// names. Then it generates build rules that copy DEX jars from their intermediate module-specific
199*333d2b36SAndroid Build Coastguard Worker// locations to the hard-coded locations predefined in the boot image configs.
200*333d2b36SAndroid Build Coastguard Worker//
201*333d2b36SAndroid Build Coastguard Worker// It would be possible to use a module with proper dependencies instead, but that would require
202*333d2b36SAndroid Build Coastguard Worker// changes in the way Soong generates variables for Make: a singleton can use one MakeVars() method
203*333d2b36SAndroid Build Coastguard Worker// that writes variables to out/soong/make_vars-*.mk, which is included early by the main makefile,
204*333d2b36SAndroid Build Coastguard Worker// but module(s) would have to use out/soong/Android-*.mk which has a group of LOCAL_* variables
205*333d2b36SAndroid Build Coastguard Worker// for each module, and is included later.
206*333d2b36SAndroid Build Coastguard Worker//
207*333d2b36SAndroid Build Coastguard Worker// 2.4. Install rules
208*333d2b36SAndroid Build Coastguard Worker// ------------------
209*333d2b36SAndroid Build Coastguard Worker//
210*333d2b36SAndroid Build Coastguard Worker// The primary boot image and the Framework extension are installed in different ways. The primary
211*333d2b36SAndroid Build Coastguard Worker// boot image is part of the ART APEX: it is copied into the APEX intermediate files, packaged
212*333d2b36SAndroid Build Coastguard Worker// together with other APEX contents, extracted and mounted on device. The Framework boot image
213*333d2b36SAndroid Build Coastguard Worker// extension is installed by the rules defined in makefiles (make/core/dex_preopt_libart.mk). Soong
214*333d2b36SAndroid Build Coastguard Worker// writes out a few DEXPREOPT_IMAGE_* variables for Make; these variables contain boot image names,
215*333d2b36SAndroid Build Coastguard Worker// paths and so on.
216*333d2b36SAndroid Build Coastguard Worker//
217*333d2b36SAndroid Build Coastguard Worker
218*333d2b36SAndroid Build Coastguard Workervar artApexNames = []string{
219*333d2b36SAndroid Build Coastguard Worker	"com.android.art",
220*333d2b36SAndroid Build Coastguard Worker	"com.android.art.debug",
221*333d2b36SAndroid Build Coastguard Worker	"com.android.art.testing",
222*333d2b36SAndroid Build Coastguard Worker	"com.google.android.art",
223*333d2b36SAndroid Build Coastguard Worker	"com.google.android.art.debug",
224*333d2b36SAndroid Build Coastguard Worker	"com.google.android.art.testing",
225*333d2b36SAndroid Build Coastguard Worker}
226*333d2b36SAndroid Build Coastguard Worker
227*333d2b36SAndroid Build Coastguard Workervar (
228*333d2b36SAndroid Build Coastguard Worker	dexpreoptBootJarDepTag          = bootclasspathDependencyTag{name: "dexpreopt-boot-jar"}
229*333d2b36SAndroid Build Coastguard Worker	dexBootJarsFragmentsKey         = android.NewOnceKey("dexBootJarsFragments")
230*333d2b36SAndroid Build Coastguard Worker	apexContributionsMetadataDepTag = dependencyTag{name: "all_apex_contributions"}
231*333d2b36SAndroid Build Coastguard Worker)
232*333d2b36SAndroid Build Coastguard Worker
233*333d2b36SAndroid Build Coastguard Workerfunc init() {
234*333d2b36SAndroid Build Coastguard Worker	RegisterDexpreoptBootJarsComponents(android.InitRegistrationContext)
235*333d2b36SAndroid Build Coastguard Worker}
236*333d2b36SAndroid Build Coastguard Worker
237*333d2b36SAndroid Build Coastguard Worker// Target-independent description of a boot image.
238*333d2b36SAndroid Build Coastguard Worker//
239*333d2b36SAndroid Build Coastguard Worker// WARNING: All fields in this struct should be initialized in the genBootImageConfigs function.
240*333d2b36SAndroid Build Coastguard Worker// Failure to do so can lead to data races if there is no synchronization enforced ordering between
241*333d2b36SAndroid Build Coastguard Worker// the writer and the reader.
242*333d2b36SAndroid Build Coastguard Workertype bootImageConfig struct {
243*333d2b36SAndroid Build Coastguard Worker	// If this image is an extension, the image that it extends.
244*333d2b36SAndroid Build Coastguard Worker	extends *bootImageConfig
245*333d2b36SAndroid Build Coastguard Worker
246*333d2b36SAndroid Build Coastguard Worker	// Image name (used in directory names and ninja rule names).
247*333d2b36SAndroid Build Coastguard Worker	name string
248*333d2b36SAndroid Build Coastguard Worker
249*333d2b36SAndroid Build Coastguard Worker	// If the module with the given name exists, this config is enabled.
250*333d2b36SAndroid Build Coastguard Worker	enabledIfExists string
251*333d2b36SAndroid Build Coastguard Worker
252*333d2b36SAndroid Build Coastguard Worker	// Basename of the image: the resulting filenames are <stem>[-<jar>].{art,oat,vdex}.
253*333d2b36SAndroid Build Coastguard Worker	stem string
254*333d2b36SAndroid Build Coastguard Worker
255*333d2b36SAndroid Build Coastguard Worker	// Output directory for the image files.
256*333d2b36SAndroid Build Coastguard Worker	dir android.OutputPath
257*333d2b36SAndroid Build Coastguard Worker
258*333d2b36SAndroid Build Coastguard Worker	// Output directory for the image files with debug symbols.
259*333d2b36SAndroid Build Coastguard Worker	symbolsDir android.OutputPath
260*333d2b36SAndroid Build Coastguard Worker
261*333d2b36SAndroid Build Coastguard Worker	// The relative location where the image files are installed. On host, the location is relative to
262*333d2b36SAndroid Build Coastguard Worker	// $ANDROID_PRODUCT_OUT.
263*333d2b36SAndroid Build Coastguard Worker	//
264*333d2b36SAndroid Build Coastguard Worker	// Only the configs that are built by platform_bootclasspath are installable on device. On device,
265*333d2b36SAndroid Build Coastguard Worker	// the location is relative to "/".
266*333d2b36SAndroid Build Coastguard Worker	installDir string
267*333d2b36SAndroid Build Coastguard Worker
268*333d2b36SAndroid Build Coastguard Worker	// A list of (location, jar) pairs for the Java modules in this image.
269*333d2b36SAndroid Build Coastguard Worker	modules android.ConfiguredJarList
270*333d2b36SAndroid Build Coastguard Worker
271*333d2b36SAndroid Build Coastguard Worker	// File paths to jars.
272*333d2b36SAndroid Build Coastguard Worker	dexPaths     android.WritablePaths // for this image
273*333d2b36SAndroid Build Coastguard Worker	dexPathsDeps android.WritablePaths // for the dependency images and in this image
274*333d2b36SAndroid Build Coastguard Worker
275*333d2b36SAndroid Build Coastguard Worker	// Map from module name (without prebuilt_ prefix) to the predefined build path.
276*333d2b36SAndroid Build Coastguard Worker	dexPathsByModule map[string]android.WritablePath
277*333d2b36SAndroid Build Coastguard Worker
278*333d2b36SAndroid Build Coastguard Worker	// File path to a zip archive with all image files (or nil, if not needed).
279*333d2b36SAndroid Build Coastguard Worker	zip android.WritablePath
280*333d2b36SAndroid Build Coastguard Worker
281*333d2b36SAndroid Build Coastguard Worker	// Target-dependent fields.
282*333d2b36SAndroid Build Coastguard Worker	variants []*bootImageVariant
283*333d2b36SAndroid Build Coastguard Worker
284*333d2b36SAndroid Build Coastguard Worker	// Path of the preloaded classes file.
285*333d2b36SAndroid Build Coastguard Worker	preloadedClassesFile string
286*333d2b36SAndroid Build Coastguard Worker
287*333d2b36SAndroid Build Coastguard Worker	// The "--compiler-filter" argument.
288*333d2b36SAndroid Build Coastguard Worker	compilerFilter string
289*333d2b36SAndroid Build Coastguard Worker
290*333d2b36SAndroid Build Coastguard Worker	// The "--single-image" argument.
291*333d2b36SAndroid Build Coastguard Worker	singleImage bool
292*333d2b36SAndroid Build Coastguard Worker
293*333d2b36SAndroid Build Coastguard Worker	// Profiles imported from APEXes, in addition to the profile at the default path. Each entry must
294*333d2b36SAndroid Build Coastguard Worker	// be the name of an APEX module.
295*333d2b36SAndroid Build Coastguard Worker	profileImports []string
296*333d2b36SAndroid Build Coastguard Worker}
297*333d2b36SAndroid Build Coastguard Worker
298*333d2b36SAndroid Build Coastguard Worker// Target-dependent description of a boot image.
299*333d2b36SAndroid Build Coastguard Worker//
300*333d2b36SAndroid Build Coastguard Worker// WARNING: The warning comment on bootImageConfig applies here too.
301*333d2b36SAndroid Build Coastguard Workertype bootImageVariant struct {
302*333d2b36SAndroid Build Coastguard Worker	*bootImageConfig
303*333d2b36SAndroid Build Coastguard Worker
304*333d2b36SAndroid Build Coastguard Worker	// Target for which the image is generated.
305*333d2b36SAndroid Build Coastguard Worker	target android.Target
306*333d2b36SAndroid Build Coastguard Worker
307*333d2b36SAndroid Build Coastguard Worker	// The "locations" of jars.
308*333d2b36SAndroid Build Coastguard Worker	dexLocations     []string // for this image
309*333d2b36SAndroid Build Coastguard Worker	dexLocationsDeps []string // for the dependency images and in this image
310*333d2b36SAndroid Build Coastguard Worker
311*333d2b36SAndroid Build Coastguard Worker	// Paths to image files.
312*333d2b36SAndroid Build Coastguard Worker	imagePathOnHost   android.OutputPath // first image file path on host
313*333d2b36SAndroid Build Coastguard Worker	imagePathOnDevice string             // first image file path on device
314*333d2b36SAndroid Build Coastguard Worker
315*333d2b36SAndroid Build Coastguard Worker	// All the files that constitute this image variant, i.e. .art, .oat and .vdex files.
316*333d2b36SAndroid Build Coastguard Worker	imagesDeps android.OutputPaths
317*333d2b36SAndroid Build Coastguard Worker
318*333d2b36SAndroid Build Coastguard Worker	// The path to the base image variant's imagePathOnHost field, where base image variant
319*333d2b36SAndroid Build Coastguard Worker	// means the image variant that this extends.
320*333d2b36SAndroid Build Coastguard Worker	//
321*333d2b36SAndroid Build Coastguard Worker	// This is only set for a variant of an image that extends another image.
322*333d2b36SAndroid Build Coastguard Worker	baseImages android.OutputPaths
323*333d2b36SAndroid Build Coastguard Worker
324*333d2b36SAndroid Build Coastguard Worker	// The paths to the base image variant's imagesDeps field, where base image variant
325*333d2b36SAndroid Build Coastguard Worker	// means the image variant that this extends.
326*333d2b36SAndroid Build Coastguard Worker	//
327*333d2b36SAndroid Build Coastguard Worker	// This is only set for a variant of an image that extends another image.
328*333d2b36SAndroid Build Coastguard Worker	baseImagesDeps android.Paths
329*333d2b36SAndroid Build Coastguard Worker
330*333d2b36SAndroid Build Coastguard Worker	// Rules which should be used in make to install the outputs on host.
331*333d2b36SAndroid Build Coastguard Worker	//
332*333d2b36SAndroid Build Coastguard Worker	// Deprecated: Not initialized correctly, see struct comment.
333*333d2b36SAndroid Build Coastguard Worker	installs android.RuleBuilderInstalls
334*333d2b36SAndroid Build Coastguard Worker
335*333d2b36SAndroid Build Coastguard Worker	// Rules which should be used in make to install the vdex outputs on host.
336*333d2b36SAndroid Build Coastguard Worker	//
337*333d2b36SAndroid Build Coastguard Worker	// Deprecated: Not initialized correctly, see struct comment.
338*333d2b36SAndroid Build Coastguard Worker	vdexInstalls android.RuleBuilderInstalls
339*333d2b36SAndroid Build Coastguard Worker
340*333d2b36SAndroid Build Coastguard Worker	// Rules which should be used in make to install the unstripped outputs on host.
341*333d2b36SAndroid Build Coastguard Worker	//
342*333d2b36SAndroid Build Coastguard Worker	// Deprecated: Not initialized correctly, see struct comment.
343*333d2b36SAndroid Build Coastguard Worker	unstrippedInstalls android.RuleBuilderInstalls
344*333d2b36SAndroid Build Coastguard Worker
345*333d2b36SAndroid Build Coastguard Worker	// Path to the license metadata file for the module that built the image.
346*333d2b36SAndroid Build Coastguard Worker	//
347*333d2b36SAndroid Build Coastguard Worker	// Deprecated: Not initialized correctly, see struct comment.
348*333d2b36SAndroid Build Coastguard Worker	licenseMetadataFile android.OptionalPath
349*333d2b36SAndroid Build Coastguard Worker}
350*333d2b36SAndroid Build Coastguard Worker
351*333d2b36SAndroid Build Coastguard Worker// Get target-specific boot image variant for the given boot image config and target.
352*333d2b36SAndroid Build Coastguard Workerfunc (image bootImageConfig) getVariant(target android.Target) *bootImageVariant {
353*333d2b36SAndroid Build Coastguard Worker	for _, variant := range image.variants {
354*333d2b36SAndroid Build Coastguard Worker		if variant.target.Os == target.Os && variant.target.Arch.ArchType == target.Arch.ArchType {
355*333d2b36SAndroid Build Coastguard Worker			return variant
356*333d2b36SAndroid Build Coastguard Worker		}
357*333d2b36SAndroid Build Coastguard Worker	}
358*333d2b36SAndroid Build Coastguard Worker	return nil
359*333d2b36SAndroid Build Coastguard Worker}
360*333d2b36SAndroid Build Coastguard Worker
361*333d2b36SAndroid Build Coastguard Worker// Return any (the first) variant which is for the device (as opposed to for the host).
362*333d2b36SAndroid Build Coastguard Workerfunc (image bootImageConfig) getAnyAndroidVariant() *bootImageVariant {
363*333d2b36SAndroid Build Coastguard Worker	for _, variant := range image.variants {
364*333d2b36SAndroid Build Coastguard Worker		if variant.target.Os == android.Android {
365*333d2b36SAndroid Build Coastguard Worker			return variant
366*333d2b36SAndroid Build Coastguard Worker		}
367*333d2b36SAndroid Build Coastguard Worker	}
368*333d2b36SAndroid Build Coastguard Worker	return nil
369*333d2b36SAndroid Build Coastguard Worker}
370*333d2b36SAndroid Build Coastguard Worker
371*333d2b36SAndroid Build Coastguard Worker// Return the name of a boot image module given a boot image config and a component (module) index.
372*333d2b36SAndroid Build Coastguard Worker// A module name is a combination of the Java library name, and the boot image stem (that is stored
373*333d2b36SAndroid Build Coastguard Worker// in the config).
374*333d2b36SAndroid Build Coastguard Workerfunc (image bootImageConfig) moduleName(ctx android.PathContext, idx int) string {
375*333d2b36SAndroid Build Coastguard Worker	// The first module of the primary boot image is special: its module name has only the stem, but
376*333d2b36SAndroid Build Coastguard Worker	// not the library name. All other module names are of the form <stem>-<library name>
377*333d2b36SAndroid Build Coastguard Worker	m := image.modules.Jar(idx)
378*333d2b36SAndroid Build Coastguard Worker	name := image.stem
379*333d2b36SAndroid Build Coastguard Worker	if idx != 0 || image.extends != nil {
380*333d2b36SAndroid Build Coastguard Worker		name += "-" + android.ModuleStem(ctx.Config(), image.modules.Apex(idx), m)
381*333d2b36SAndroid Build Coastguard Worker	}
382*333d2b36SAndroid Build Coastguard Worker	return name
383*333d2b36SAndroid Build Coastguard Worker}
384*333d2b36SAndroid Build Coastguard Worker
385*333d2b36SAndroid Build Coastguard Worker// Return the name of the first boot image module, or stem if the list of modules is empty.
386*333d2b36SAndroid Build Coastguard Workerfunc (image bootImageConfig) firstModuleNameOrStem(ctx android.PathContext) string {
387*333d2b36SAndroid Build Coastguard Worker	if image.modules.Len() > 0 {
388*333d2b36SAndroid Build Coastguard Worker		return image.moduleName(ctx, 0)
389*333d2b36SAndroid Build Coastguard Worker	} else {
390*333d2b36SAndroid Build Coastguard Worker		return image.stem
391*333d2b36SAndroid Build Coastguard Worker	}
392*333d2b36SAndroid Build Coastguard Worker}
393*333d2b36SAndroid Build Coastguard Worker
394*333d2b36SAndroid Build Coastguard Worker// Return filenames for the given boot image component, given the output directory and a list of
395*333d2b36SAndroid Build Coastguard Worker// extensions.
396*333d2b36SAndroid Build Coastguard Workerfunc (image bootImageConfig) moduleFiles(ctx android.PathContext, dir android.OutputPath, exts ...string) android.OutputPaths {
397*333d2b36SAndroid Build Coastguard Worker	ret := make(android.OutputPaths, 0, image.modules.Len()*len(exts))
398*333d2b36SAndroid Build Coastguard Worker	for i := 0; i < image.modules.Len(); i++ {
399*333d2b36SAndroid Build Coastguard Worker		name := image.moduleName(ctx, i)
400*333d2b36SAndroid Build Coastguard Worker		for _, ext := range exts {
401*333d2b36SAndroid Build Coastguard Worker			ret = append(ret, dir.Join(ctx, name+ext))
402*333d2b36SAndroid Build Coastguard Worker		}
403*333d2b36SAndroid Build Coastguard Worker		if image.singleImage {
404*333d2b36SAndroid Build Coastguard Worker			break
405*333d2b36SAndroid Build Coastguard Worker		}
406*333d2b36SAndroid Build Coastguard Worker	}
407*333d2b36SAndroid Build Coastguard Worker	return ret
408*333d2b36SAndroid Build Coastguard Worker}
409*333d2b36SAndroid Build Coastguard Worker
410*333d2b36SAndroid Build Coastguard Worker// apexVariants returns a list of all *bootImageVariant that could be included in an apex.
411*333d2b36SAndroid Build Coastguard Workerfunc (image *bootImageConfig) apexVariants() []*bootImageVariant {
412*333d2b36SAndroid Build Coastguard Worker	variants := []*bootImageVariant{}
413*333d2b36SAndroid Build Coastguard Worker	for _, variant := range image.variants {
414*333d2b36SAndroid Build Coastguard Worker		// We also generate boot images for host (for testing), but we don't need those in the apex.
415*333d2b36SAndroid Build Coastguard Worker		// TODO(b/177892522) - consider changing this to check Os.OsClass = android.Device
416*333d2b36SAndroid Build Coastguard Worker		if variant.target.Os == android.Android {
417*333d2b36SAndroid Build Coastguard Worker			variants = append(variants, variant)
418*333d2b36SAndroid Build Coastguard Worker		}
419*333d2b36SAndroid Build Coastguard Worker	}
420*333d2b36SAndroid Build Coastguard Worker	return variants
421*333d2b36SAndroid Build Coastguard Worker}
422*333d2b36SAndroid Build Coastguard Worker
423*333d2b36SAndroid Build Coastguard Worker// Return boot image locations (as a list of symbolic paths).
424*333d2b36SAndroid Build Coastguard Worker//
425*333d2b36SAndroid Build Coastguard Worker// The image "location" is a symbolic path that, with multiarchitecture support, doesn't really
426*333d2b36SAndroid Build Coastguard Worker// exist on the device. Typically it is /apex/com.android.art/javalib/boot.art and should be the
427*333d2b36SAndroid Build Coastguard Worker// same for all supported architectures on the device. The concrete architecture specific files
428*333d2b36SAndroid Build Coastguard Worker// actually end up in architecture-specific sub-directory such as arm, arm64, x86, or x86_64.
429*333d2b36SAndroid Build Coastguard Worker//
430*333d2b36SAndroid Build Coastguard Worker// For example a physical file /apex/com.android.art/javalib/x86/boot.art has "image location"
431*333d2b36SAndroid Build Coastguard Worker// /apex/com.android.art/javalib/boot.art (which is not an actual file).
432*333d2b36SAndroid Build Coastguard Worker//
433*333d2b36SAndroid Build Coastguard Worker// For a primary boot image the list of locations has a single element.
434*333d2b36SAndroid Build Coastguard Worker//
435*333d2b36SAndroid Build Coastguard Worker// For a boot image extension the list of locations contains a location for all dependency images
436*333d2b36SAndroid Build Coastguard Worker// (including the primary image) and the location of the extension itself. For example, for the
437*333d2b36SAndroid Build Coastguard Worker// Framework boot image extension that depends on the primary ART boot image the list contains two
438*333d2b36SAndroid Build Coastguard Worker// elements.
439*333d2b36SAndroid Build Coastguard Worker//
440*333d2b36SAndroid Build Coastguard Worker// The location is passed as an argument to the ART tools like dex2oat instead of the real path.
441*333d2b36SAndroid Build Coastguard Worker// ART tools will then reconstruct the architecture-specific real path.
442*333d2b36SAndroid Build Coastguard Workerfunc (image *bootImageVariant) imageLocations() (imageLocationsOnHost []string, imageLocationsOnDevice []string) {
443*333d2b36SAndroid Build Coastguard Worker	if image.extends != nil {
444*333d2b36SAndroid Build Coastguard Worker		imageLocationsOnHost, imageLocationsOnDevice = image.extends.getVariant(image.target).imageLocations()
445*333d2b36SAndroid Build Coastguard Worker	}
446*333d2b36SAndroid Build Coastguard Worker	return append(imageLocationsOnHost, dexpreopt.PathToLocation(image.imagePathOnHost, image.target.Arch.ArchType)),
447*333d2b36SAndroid Build Coastguard Worker		append(imageLocationsOnDevice, dexpreopt.PathStringToLocation(image.imagePathOnDevice, image.target.Arch.ArchType))
448*333d2b36SAndroid Build Coastguard Worker}
449*333d2b36SAndroid Build Coastguard Worker
450*333d2b36SAndroid Build Coastguard Workerfunc (image *bootImageConfig) isProfileGuided() bool {
451*333d2b36SAndroid Build Coastguard Worker	return image.compilerFilter == "speed-profile"
452*333d2b36SAndroid Build Coastguard Worker}
453*333d2b36SAndroid Build Coastguard Worker
454*333d2b36SAndroid Build Coastguard Workerfunc (image *bootImageConfig) isEnabled(ctx android.BaseModuleContext) bool {
455*333d2b36SAndroid Build Coastguard Worker	return ctx.OtherModuleExists(image.enabledIfExists)
456*333d2b36SAndroid Build Coastguard Worker}
457*333d2b36SAndroid Build Coastguard Worker
458*333d2b36SAndroid Build Coastguard Workerfunc dexpreoptBootJarsFactory() android.SingletonModule {
459*333d2b36SAndroid Build Coastguard Worker	m := &dexpreoptBootJars{}
460*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
461*333d2b36SAndroid Build Coastguard Worker	return m
462*333d2b36SAndroid Build Coastguard Worker}
463*333d2b36SAndroid Build Coastguard Worker
464*333d2b36SAndroid Build Coastguard Workerfunc RegisterDexpreoptBootJarsComponents(ctx android.RegistrationContext) {
465*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterParallelSingletonModuleType("dex_bootjars", dexpreoptBootJarsFactory)
466*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterModuleType("art_boot_images", artBootImagesFactory)
467*333d2b36SAndroid Build Coastguard Worker	ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
468*333d2b36SAndroid Build Coastguard Worker		ctx.BottomUp("dex_bootjars_deps", DexpreoptBootJarsMutator)
469*333d2b36SAndroid Build Coastguard Worker	})
470*333d2b36SAndroid Build Coastguard Worker}
471*333d2b36SAndroid Build Coastguard Worker
472*333d2b36SAndroid Build Coastguard Workerfunc SkipDexpreoptBootJars(ctx android.PathContext) bool {
473*333d2b36SAndroid Build Coastguard Worker	global := dexpreopt.GetGlobalConfig(ctx)
474*333d2b36SAndroid Build Coastguard Worker	return global.DisablePreoptBootImages || !shouldBuildBootImages(ctx.Config(), global)
475*333d2b36SAndroid Build Coastguard Worker}
476*333d2b36SAndroid Build Coastguard Worker
477*333d2b36SAndroid Build Coastguard Worker// Singleton module for generating boot image build rules.
478*333d2b36SAndroid Build Coastguard Workertype dexpreoptBootJars struct {
479*333d2b36SAndroid Build Coastguard Worker	android.SingletonModuleBase
480*333d2b36SAndroid Build Coastguard Worker
481*333d2b36SAndroid Build Coastguard Worker	// Default boot image config (currently always the Framework boot image extension). It should be
482*333d2b36SAndroid Build Coastguard Worker	// noted that JIT-Zygote builds use ART APEX image instead of the Framework boot image extension,
483*333d2b36SAndroid Build Coastguard Worker	// but the switch is handled not here, but in the makefiles (triggered with
484*333d2b36SAndroid Build Coastguard Worker	// DEXPREOPT_USE_ART_IMAGE=true).
485*333d2b36SAndroid Build Coastguard Worker	defaultBootImage *bootImageConfig
486*333d2b36SAndroid Build Coastguard Worker
487*333d2b36SAndroid Build Coastguard Worker	// Other boot image configs (currently the list contains only the primary ART APEX image. It
488*333d2b36SAndroid Build Coastguard Worker	// used to contain an experimental JIT-Zygote image (now replaced with the ART APEX image). In
489*333d2b36SAndroid Build Coastguard Worker	// the future other boot image extensions may be added.
490*333d2b36SAndroid Build Coastguard Worker	otherImages []*bootImageConfig
491*333d2b36SAndroid Build Coastguard Worker
492*333d2b36SAndroid Build Coastguard Worker	// Build path to a config file that Soong writes for Make (to be used in makefiles that install
493*333d2b36SAndroid Build Coastguard Worker	// the default boot image).
494*333d2b36SAndroid Build Coastguard Worker	dexpreoptConfigForMake android.WritablePath
495*333d2b36SAndroid Build Coastguard Worker
496*333d2b36SAndroid Build Coastguard Worker	// Build path to the boot framework profile.
497*333d2b36SAndroid Build Coastguard Worker	// This is used as the `OutputFile` in `AndroidMkEntries`.
498*333d2b36SAndroid Build Coastguard Worker	// A non-nil value ensures that this singleton module does not get skipped in AndroidMkEntries processing.
499*333d2b36SAndroid Build Coastguard Worker	bootFrameworkProfile android.WritablePath
500*333d2b36SAndroid Build Coastguard Worker}
501*333d2b36SAndroid Build Coastguard Worker
502*333d2b36SAndroid Build Coastguard Workerfunc (dbj *dexpreoptBootJars) DepsMutator(ctx android.BottomUpMutatorContext) {
503*333d2b36SAndroid Build Coastguard Worker	// Create a dependency on all_apex_contributions to determine the selected mainline module
504*333d2b36SAndroid Build Coastguard Worker	ctx.AddDependency(ctx.Module(), apexContributionsMetadataDepTag, "all_apex_contributions")
505*333d2b36SAndroid Build Coastguard Worker}
506*333d2b36SAndroid Build Coastguard Worker
507*333d2b36SAndroid Build Coastguard Workerfunc DexpreoptBootJarsMutator(ctx android.BottomUpMutatorContext) {
508*333d2b36SAndroid Build Coastguard Worker	if _, ok := ctx.Module().(*dexpreoptBootJars); !ok {
509*333d2b36SAndroid Build Coastguard Worker		return
510*333d2b36SAndroid Build Coastguard Worker	}
511*333d2b36SAndroid Build Coastguard Worker
512*333d2b36SAndroid Build Coastguard Worker	if dexpreopt.IsDex2oatNeeded(ctx) {
513*333d2b36SAndroid Build Coastguard Worker		// Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
514*333d2b36SAndroid Build Coastguard Worker		// path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
515*333d2b36SAndroid Build Coastguard Worker		dexpreopt.RegisterToolDeps(ctx)
516*333d2b36SAndroid Build Coastguard Worker	}
517*333d2b36SAndroid Build Coastguard Worker
518*333d2b36SAndroid Build Coastguard Worker	imageConfigs := genBootImageConfigs(ctx)
519*333d2b36SAndroid Build Coastguard Worker	for _, config := range imageConfigs {
520*333d2b36SAndroid Build Coastguard Worker		if !config.isEnabled(ctx) {
521*333d2b36SAndroid Build Coastguard Worker			continue
522*333d2b36SAndroid Build Coastguard Worker		}
523*333d2b36SAndroid Build Coastguard Worker		// For accessing the boot jars.
524*333d2b36SAndroid Build Coastguard Worker		addDependenciesOntoBootImageModules(ctx, config.modules, dexpreoptBootJarDepTag)
525*333d2b36SAndroid Build Coastguard Worker		// Create a dependency on the apex selected using RELEASE_APEX_CONTRIBUTIONS_*
526*333d2b36SAndroid Build Coastguard Worker		// TODO: b/308174306 - Remove the direct depedendency edge to the java_library (source/prebuilt) once all mainline modules
527*333d2b36SAndroid Build Coastguard Worker		// have been flagged using RELEASE_APEX_CONTRIBUTIONS_*
528*333d2b36SAndroid Build Coastguard Worker		apexes := []string{}
529*333d2b36SAndroid Build Coastguard Worker		for i := 0; i < config.modules.Len(); i++ {
530*333d2b36SAndroid Build Coastguard Worker			apexes = append(apexes, config.modules.Apex(i))
531*333d2b36SAndroid Build Coastguard Worker		}
532*333d2b36SAndroid Build Coastguard Worker		addDependenciesOntoSelectedBootImageApexes(ctx, android.FirstUniqueStrings(apexes)...)
533*333d2b36SAndroid Build Coastguard Worker	}
534*333d2b36SAndroid Build Coastguard Worker
535*333d2b36SAndroid Build Coastguard Worker	if ctx.OtherModuleExists("platform-bootclasspath") {
536*333d2b36SAndroid Build Coastguard Worker		// For accessing all bootclasspath fragments.
537*333d2b36SAndroid Build Coastguard Worker		addDependencyOntoApexModulePair(ctx, "platform", "platform-bootclasspath", platformBootclasspathDepTag)
538*333d2b36SAndroid Build Coastguard Worker	} else if ctx.OtherModuleExists("art-bootclasspath-fragment") {
539*333d2b36SAndroid Build Coastguard Worker		// For accessing the ART bootclasspath fragment on a thin manifest (e.g., master-art) where
540*333d2b36SAndroid Build Coastguard Worker		// platform-bootclasspath doesn't exist.
541*333d2b36SAndroid Build Coastguard Worker		addDependencyOntoApexModulePair(ctx, "com.android.art", "art-bootclasspath-fragment", bootclasspathFragmentDepTag)
542*333d2b36SAndroid Build Coastguard Worker	}
543*333d2b36SAndroid Build Coastguard Worker}
544*333d2b36SAndroid Build Coastguard Worker
545*333d2b36SAndroid Build Coastguard Worker// Create a dependency from dex_bootjars to the specific apexes selected using all_apex_contributions
546*333d2b36SAndroid Build Coastguard Worker// This dependency will be used to get the path to the deapexed dex boot jars and profile (via a provider)
547*333d2b36SAndroid Build Coastguard Workerfunc addDependenciesOntoSelectedBootImageApexes(ctx android.BottomUpMutatorContext, apexes ...string) {
548*333d2b36SAndroid Build Coastguard Worker	psi := android.PrebuiltSelectionInfoMap{}
549*333d2b36SAndroid Build Coastguard Worker	ctx.VisitDirectDepsWithTag(apexContributionsMetadataDepTag, func(am android.Module) {
550*333d2b36SAndroid Build Coastguard Worker		if info, exists := android.OtherModuleProvider(ctx, am, android.PrebuiltSelectionInfoProvider); exists {
551*333d2b36SAndroid Build Coastguard Worker			psi = info
552*333d2b36SAndroid Build Coastguard Worker		}
553*333d2b36SAndroid Build Coastguard Worker	})
554*333d2b36SAndroid Build Coastguard Worker	for _, apex := range apexes {
555*333d2b36SAndroid Build Coastguard Worker		for _, selected := range psi.GetSelectedModulesForApiDomain(apex) {
556*333d2b36SAndroid Build Coastguard Worker			// We need to add a dep on only the apex listed in `contents` of the selected apex_contributions module
557*333d2b36SAndroid Build Coastguard Worker			// This is not available in a structured format in `apex_contributions`, so this hack adds a dep on all `contents`
558*333d2b36SAndroid Build Coastguard Worker			// (some modules like art.module.public.api do not have an apex variation since it is a pure stub module that does not get installed)
559*333d2b36SAndroid Build Coastguard Worker			apexVariationOfSelected := append(ctx.Target().Variations(), blueprint.Variation{Mutator: "apex", Variation: apex})
560*333d2b36SAndroid Build Coastguard Worker			if ctx.OtherModuleDependencyVariantExists(apexVariationOfSelected, selected) {
561*333d2b36SAndroid Build Coastguard Worker				ctx.AddFarVariationDependencies(apexVariationOfSelected, dexpreoptBootJarDepTag, selected)
562*333d2b36SAndroid Build Coastguard Worker			} else if ctx.OtherModuleDependencyVariantExists(apexVariationOfSelected, android.RemoveOptionalPrebuiltPrefix(selected)) {
563*333d2b36SAndroid Build Coastguard Worker				// The prebuilt might have been renamed by prebuilt_rename mutator if the source module does not exist.
564*333d2b36SAndroid Build Coastguard Worker				// Remove the prebuilt_ prefix.
565*333d2b36SAndroid Build Coastguard Worker				ctx.AddFarVariationDependencies(apexVariationOfSelected, dexpreoptBootJarDepTag, android.RemoveOptionalPrebuiltPrefix(selected))
566*333d2b36SAndroid Build Coastguard Worker			}
567*333d2b36SAndroid Build Coastguard Worker		}
568*333d2b36SAndroid Build Coastguard Worker	}
569*333d2b36SAndroid Build Coastguard Worker}
570*333d2b36SAndroid Build Coastguard Worker
571*333d2b36SAndroid Build Coastguard Workerfunc gatherBootclasspathFragments(ctx android.ModuleContext) map[string]android.Module {
572*333d2b36SAndroid Build Coastguard Worker	return ctx.Config().Once(dexBootJarsFragmentsKey, func() interface{} {
573*333d2b36SAndroid Build Coastguard Worker		fragments := make(map[string]android.Module)
574*333d2b36SAndroid Build Coastguard Worker		ctx.WalkDeps(func(child, parent android.Module) bool {
575*333d2b36SAndroid Build Coastguard Worker			if !isActiveModule(ctx, child) {
576*333d2b36SAndroid Build Coastguard Worker				return false
577*333d2b36SAndroid Build Coastguard Worker			}
578*333d2b36SAndroid Build Coastguard Worker			tag := ctx.OtherModuleDependencyTag(child)
579*333d2b36SAndroid Build Coastguard Worker			if tag == platformBootclasspathDepTag {
580*333d2b36SAndroid Build Coastguard Worker				return true
581*333d2b36SAndroid Build Coastguard Worker			}
582*333d2b36SAndroid Build Coastguard Worker			if tag == bootclasspathFragmentDepTag {
583*333d2b36SAndroid Build Coastguard Worker				apexInfo, _ := android.OtherModuleProvider(ctx, child, android.ApexInfoProvider)
584*333d2b36SAndroid Build Coastguard Worker				for _, apex := range apexInfo.InApexVariants {
585*333d2b36SAndroid Build Coastguard Worker					fragments[apex] = child
586*333d2b36SAndroid Build Coastguard Worker				}
587*333d2b36SAndroid Build Coastguard Worker				return false
588*333d2b36SAndroid Build Coastguard Worker			}
589*333d2b36SAndroid Build Coastguard Worker			return false
590*333d2b36SAndroid Build Coastguard Worker		})
591*333d2b36SAndroid Build Coastguard Worker		return fragments
592*333d2b36SAndroid Build Coastguard Worker	}).(map[string]android.Module)
593*333d2b36SAndroid Build Coastguard Worker}
594*333d2b36SAndroid Build Coastguard Worker
595*333d2b36SAndroid Build Coastguard Workerfunc getBootclasspathFragmentByApex(ctx android.ModuleContext, apexName string) android.Module {
596*333d2b36SAndroid Build Coastguard Worker	return gatherBootclasspathFragments(ctx)[apexName]
597*333d2b36SAndroid Build Coastguard Worker}
598*333d2b36SAndroid Build Coastguard Worker
599*333d2b36SAndroid Build Coastguard Worker// GenerateAndroidBuildActions generates the build rules for boot images.
600*333d2b36SAndroid Build Coastguard Workerfunc (d *dexpreoptBootJars) GenerateAndroidBuildActions(ctx android.ModuleContext) {
601*333d2b36SAndroid Build Coastguard Worker	imageConfigs := genBootImageConfigs(ctx)
602*333d2b36SAndroid Build Coastguard Worker	d.defaultBootImage = defaultBootImageConfig(ctx)
603*333d2b36SAndroid Build Coastguard Worker	d.otherImages = make([]*bootImageConfig, 0, len(imageConfigs)-1)
604*333d2b36SAndroid Build Coastguard Worker	var profileInstalls android.RuleBuilderInstalls
605*333d2b36SAndroid Build Coastguard Worker	var artBootImageHostInstalls android.RuleBuilderInstalls
606*333d2b36SAndroid Build Coastguard Worker	for _, name := range getImageNames() {
607*333d2b36SAndroid Build Coastguard Worker		config := imageConfigs[name]
608*333d2b36SAndroid Build Coastguard Worker		if config != d.defaultBootImage {
609*333d2b36SAndroid Build Coastguard Worker			d.otherImages = append(d.otherImages, config)
610*333d2b36SAndroid Build Coastguard Worker		}
611*333d2b36SAndroid Build Coastguard Worker		if !config.isEnabled(ctx) {
612*333d2b36SAndroid Build Coastguard Worker			continue
613*333d2b36SAndroid Build Coastguard Worker		}
614*333d2b36SAndroid Build Coastguard Worker		installs := generateBootImage(ctx, config)
615*333d2b36SAndroid Build Coastguard Worker		profileInstalls = append(profileInstalls, installs...)
616*333d2b36SAndroid Build Coastguard Worker		if config == d.defaultBootImage {
617*333d2b36SAndroid Build Coastguard Worker			bootProfile, installs := bootFrameworkProfileRule(ctx, config)
618*333d2b36SAndroid Build Coastguard Worker			d.bootFrameworkProfile = bootProfile
619*333d2b36SAndroid Build Coastguard Worker			profileInstalls = append(profileInstalls, installs...)
620*333d2b36SAndroid Build Coastguard Worker		}
621*333d2b36SAndroid Build Coastguard Worker		// Gather the install files of the host variant of the ART boot image.
622*333d2b36SAndroid Build Coastguard Worker		// These installed files will be used in ART tests.
623*333d2b36SAndroid Build Coastguard Worker		if config.name == "art" {
624*333d2b36SAndroid Build Coastguard Worker			for _, variant := range config.variants {
625*333d2b36SAndroid Build Coastguard Worker				if variant.target.Os != ctx.Config().BuildOS {
626*333d2b36SAndroid Build Coastguard Worker					// not a host variant
627*333d2b36SAndroid Build Coastguard Worker					continue
628*333d2b36SAndroid Build Coastguard Worker				}
629*333d2b36SAndroid Build Coastguard Worker				artBootImageHostInstalls = append(artBootImageHostInstalls, variant.installs...)
630*333d2b36SAndroid Build Coastguard Worker				artBootImageHostInstalls = append(artBootImageHostInstalls, variant.vdexInstalls...)
631*333d2b36SAndroid Build Coastguard Worker			}
632*333d2b36SAndroid Build Coastguard Worker		}
633*333d2b36SAndroid Build Coastguard Worker	}
634*333d2b36SAndroid Build Coastguard Worker	if len(profileInstalls) > 0 {
635*333d2b36SAndroid Build Coastguard Worker		android.SetProvider(ctx, profileInstallInfoProvider, profileInstallInfo{
636*333d2b36SAndroid Build Coastguard Worker			profileInstalls:            profileInstalls,
637*333d2b36SAndroid Build Coastguard Worker			profileLicenseMetadataFile: android.OptionalPathForPath(ctx.LicenseMetadataFile()),
638*333d2b36SAndroid Build Coastguard Worker		})
639*333d2b36SAndroid Build Coastguard Worker		for _, install := range profileInstalls {
640*333d2b36SAndroid Build Coastguard Worker			installFile(ctx, install)
641*333d2b36SAndroid Build Coastguard Worker		}
642*333d2b36SAndroid Build Coastguard Worker	}
643*333d2b36SAndroid Build Coastguard Worker	// Set a provider containing the install files of the host variant of the ART boot image.
644*333d2b36SAndroid Build Coastguard Worker	// The actual install rules will be created by `art_boot_images`
645*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(
646*333d2b36SAndroid Build Coastguard Worker		ctx,
647*333d2b36SAndroid Build Coastguard Worker		artBootImageHostInfoProvider,
648*333d2b36SAndroid Build Coastguard Worker		artBootImageHostInfo{
649*333d2b36SAndroid Build Coastguard Worker			installs: artBootImageHostInstalls,
650*333d2b36SAndroid Build Coastguard Worker		},
651*333d2b36SAndroid Build Coastguard Worker	)
652*333d2b36SAndroid Build Coastguard Worker}
653*333d2b36SAndroid Build Coastguard Worker
654*333d2b36SAndroid Build Coastguard Worker// GenerateSingletonBuildActions generates build rules for the dexpreopt config for Make.
655*333d2b36SAndroid Build Coastguard Workerfunc (d *dexpreoptBootJars) GenerateSingletonBuildActions(ctx android.SingletonContext) {
656*333d2b36SAndroid Build Coastguard Worker	d.dexpreoptConfigForMake =
657*333d2b36SAndroid Build Coastguard Worker		android.PathForOutput(ctx, dexpreopt.GetDexpreoptDirName(ctx), "dexpreopt.config")
658*333d2b36SAndroid Build Coastguard Worker	writeGlobalConfigForMake(ctx, d.dexpreoptConfigForMake)
659*333d2b36SAndroid Build Coastguard Worker}
660*333d2b36SAndroid Build Coastguard Worker
661*333d2b36SAndroid Build Coastguard Worker// shouldBuildBootImages determines whether boot images should be built.
662*333d2b36SAndroid Build Coastguard Workerfunc shouldBuildBootImages(config android.Config, global *dexpreopt.GlobalConfig) bool {
663*333d2b36SAndroid Build Coastguard Worker	// Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
664*333d2b36SAndroid Build Coastguard Worker	// and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
665*333d2b36SAndroid Build Coastguard Worker	// Note: this is technically incorrect. Compiled code contains stack checks which may depend
666*333d2b36SAndroid Build Coastguard Worker	//       on ASAN settings.
667*333d2b36SAndroid Build Coastguard Worker	if len(config.SanitizeDevice()) == 1 && config.SanitizeDevice()[0] == "address" && global.SanitizeLite {
668*333d2b36SAndroid Build Coastguard Worker		return false
669*333d2b36SAndroid Build Coastguard Worker	}
670*333d2b36SAndroid Build Coastguard Worker	return true
671*333d2b36SAndroid Build Coastguard Worker}
672*333d2b36SAndroid Build Coastguard Worker
673*333d2b36SAndroid Build Coastguard Workerfunc generateBootImage(ctx android.ModuleContext, imageConfig *bootImageConfig) android.RuleBuilderInstalls {
674*333d2b36SAndroid Build Coastguard Worker	apexJarModulePairs := getModulesForImage(ctx, imageConfig)
675*333d2b36SAndroid Build Coastguard Worker
676*333d2b36SAndroid Build Coastguard Worker	// Copy module dex jars to their predefined locations.
677*333d2b36SAndroid Build Coastguard Worker	bootDexJarsByModule := extractEncodedDexJarsFromModulesOrBootclasspathFragments(ctx, apexJarModulePairs)
678*333d2b36SAndroid Build Coastguard Worker	copyBootJarsToPredefinedLocations(ctx, bootDexJarsByModule, imageConfig.dexPathsByModule)
679*333d2b36SAndroid Build Coastguard Worker
680*333d2b36SAndroid Build Coastguard Worker	// Build a profile for the image config from the profile at the default path. The profile will
681*333d2b36SAndroid Build Coastguard Worker	// then be used along with profiles imported from APEXes to build the boot image.
682*333d2b36SAndroid Build Coastguard Worker	profile, profileInstalls := bootImageProfileRule(ctx, imageConfig)
683*333d2b36SAndroid Build Coastguard Worker
684*333d2b36SAndroid Build Coastguard Worker	// If dexpreopt of boot image jars should be skipped, stop after generating a profile.
685*333d2b36SAndroid Build Coastguard Worker	global := dexpreopt.GetGlobalConfig(ctx)
686*333d2b36SAndroid Build Coastguard Worker	if SkipDexpreoptBootJars(ctx) || (global.OnlyPreoptArtBootImage && imageConfig.name != "art") {
687*333d2b36SAndroid Build Coastguard Worker		return profileInstalls
688*333d2b36SAndroid Build Coastguard Worker	}
689*333d2b36SAndroid Build Coastguard Worker
690*333d2b36SAndroid Build Coastguard Worker	// Build boot image files for the android variants.
691*333d2b36SAndroid Build Coastguard Worker	androidBootImageFiles := buildBootImageVariantsForAndroidOs(ctx, imageConfig, profile)
692*333d2b36SAndroid Build Coastguard Worker
693*333d2b36SAndroid Build Coastguard Worker	// Zip the android variant boot image files up.
694*333d2b36SAndroid Build Coastguard Worker	buildBootImageZipInPredefinedLocation(ctx, imageConfig, androidBootImageFiles.byArch)
695*333d2b36SAndroid Build Coastguard Worker
696*333d2b36SAndroid Build Coastguard Worker	// Build boot image files for the host variants. There are use directly by ART host side tests.
697*333d2b36SAndroid Build Coastguard Worker	buildBootImageVariantsForBuildOs(ctx, imageConfig, profile)
698*333d2b36SAndroid Build Coastguard Worker
699*333d2b36SAndroid Build Coastguard Worker	// Create a `dump-oat-<image-name>` rule that runs `oatdump` for debugging purposes.
700*333d2b36SAndroid Build Coastguard Worker	dumpOatRules(ctx, imageConfig)
701*333d2b36SAndroid Build Coastguard Worker
702*333d2b36SAndroid Build Coastguard Worker	return profileInstalls
703*333d2b36SAndroid Build Coastguard Worker}
704*333d2b36SAndroid Build Coastguard Worker
705*333d2b36SAndroid Build Coastguard Workertype apexJarModulePair struct {
706*333d2b36SAndroid Build Coastguard Worker	apex      string
707*333d2b36SAndroid Build Coastguard Worker	jarModule android.Module
708*333d2b36SAndroid Build Coastguard Worker}
709*333d2b36SAndroid Build Coastguard Worker
710*333d2b36SAndroid Build Coastguard Workerfunc getModulesForImage(ctx android.ModuleContext, imageConfig *bootImageConfig) []apexJarModulePair {
711*333d2b36SAndroid Build Coastguard Worker	modules := make([]apexJarModulePair, 0, imageConfig.modules.Len())
712*333d2b36SAndroid Build Coastguard Worker	for i := 0; i < imageConfig.modules.Len(); i++ {
713*333d2b36SAndroid Build Coastguard Worker		found := false
714*333d2b36SAndroid Build Coastguard Worker		for _, module := range gatherApexModulePairDepsWithTag(ctx, dexpreoptBootJarDepTag) {
715*333d2b36SAndroid Build Coastguard Worker			name := android.RemoveOptionalPrebuiltPrefix(module.Name())
716*333d2b36SAndroid Build Coastguard Worker			if name == imageConfig.modules.Jar(i) {
717*333d2b36SAndroid Build Coastguard Worker				modules = append(modules, apexJarModulePair{
718*333d2b36SAndroid Build Coastguard Worker					apex:      imageConfig.modules.Apex(i),
719*333d2b36SAndroid Build Coastguard Worker					jarModule: module,
720*333d2b36SAndroid Build Coastguard Worker				})
721*333d2b36SAndroid Build Coastguard Worker				found = true
722*333d2b36SAndroid Build Coastguard Worker				break
723*333d2b36SAndroid Build Coastguard Worker			}
724*333d2b36SAndroid Build Coastguard Worker		}
725*333d2b36SAndroid Build Coastguard Worker		if !found && !ctx.Config().AllowMissingDependencies() {
726*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf(
727*333d2b36SAndroid Build Coastguard Worker				"Boot image '%s' module '%s' not added as a dependency of dex_bootjars",
728*333d2b36SAndroid Build Coastguard Worker				imageConfig.name,
729*333d2b36SAndroid Build Coastguard Worker				imageConfig.modules.Jar(i))
730*333d2b36SAndroid Build Coastguard Worker			return []apexJarModulePair{}
731*333d2b36SAndroid Build Coastguard Worker		}
732*333d2b36SAndroid Build Coastguard Worker	}
733*333d2b36SAndroid Build Coastguard Worker	return modules
734*333d2b36SAndroid Build Coastguard Worker}
735*333d2b36SAndroid Build Coastguard Worker
736*333d2b36SAndroid Build Coastguard Worker// extractEncodedDexJarsFromModulesOrBootclasspathFragments gets the hidden API encoded dex jars for
737*333d2b36SAndroid Build Coastguard Worker// the given modules.
738*333d2b36SAndroid Build Coastguard Workerfunc extractEncodedDexJarsFromModulesOrBootclasspathFragments(ctx android.ModuleContext, apexJarModulePairs []apexJarModulePair) bootDexJarByModule {
739*333d2b36SAndroid Build Coastguard Worker	apexNameToApexExportInfoMap := getApexNameToApexExportsInfoMap(ctx)
740*333d2b36SAndroid Build Coastguard Worker	encodedDexJarsByModuleName := bootDexJarByModule{}
741*333d2b36SAndroid Build Coastguard Worker	for _, pair := range apexJarModulePairs {
742*333d2b36SAndroid Build Coastguard Worker		dexJarPath := getDexJarForApex(ctx, pair, apexNameToApexExportInfoMap)
743*333d2b36SAndroid Build Coastguard Worker		encodedDexJarsByModuleName.addPath(pair.jarModule, dexJarPath)
744*333d2b36SAndroid Build Coastguard Worker	}
745*333d2b36SAndroid Build Coastguard Worker	return encodedDexJarsByModuleName
746*333d2b36SAndroid Build Coastguard Worker}
747*333d2b36SAndroid Build Coastguard Worker
748*333d2b36SAndroid Build Coastguard Workertype apexNameToApexExportsInfoMap map[string]android.ApexExportsInfo
749*333d2b36SAndroid Build Coastguard Worker
750*333d2b36SAndroid Build Coastguard Worker// javaLibraryPathOnHost returns the path to the java library which is exported by the apex for hiddenapi and dexpreopt and a boolean indicating whether the java library exists
751*333d2b36SAndroid Build Coastguard Worker// For prebuilt apexes, this is created by deapexing the prebuilt apex
752*333d2b36SAndroid Build Coastguard Workerfunc (m *apexNameToApexExportsInfoMap) javaLibraryDexPathOnHost(ctx android.ModuleContext, apex string, javalib string) (android.Path, bool) {
753*333d2b36SAndroid Build Coastguard Worker	if info, exists := (*m)[apex]; exists {
754*333d2b36SAndroid Build Coastguard Worker		if dex, exists := info.LibraryNameToDexJarPathOnHost[javalib]; exists {
755*333d2b36SAndroid Build Coastguard Worker			return dex, true
756*333d2b36SAndroid Build Coastguard Worker		} else {
757*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf("Apex %s does not provide a dex boot jar for library %s\n", apex, javalib)
758*333d2b36SAndroid Build Coastguard Worker		}
759*333d2b36SAndroid Build Coastguard Worker	}
760*333d2b36SAndroid Build Coastguard Worker	// An apex entry could not be found. Return false.
761*333d2b36SAndroid Build Coastguard Worker	// TODO: b/308174306 - When all the mainline modules have been flagged, make this a hard error
762*333d2b36SAndroid Build Coastguard Worker	return nil, false
763*333d2b36SAndroid Build Coastguard Worker}
764*333d2b36SAndroid Build Coastguard Worker
765*333d2b36SAndroid Build Coastguard Worker// Returns the stem of an artifact inside a prebuilt apex
766*333d2b36SAndroid Build Coastguard Workerfunc ModuleStemForDeapexing(m android.Module) string {
767*333d2b36SAndroid Build Coastguard Worker	bmn, _ := m.(interface{ BaseModuleName() string })
768*333d2b36SAndroid Build Coastguard Worker	return bmn.BaseModuleName()
769*333d2b36SAndroid Build Coastguard Worker}
770*333d2b36SAndroid Build Coastguard Worker
771*333d2b36SAndroid Build Coastguard Worker// Returns the java libraries exported by the apex for hiddenapi and dexpreopt
772*333d2b36SAndroid Build Coastguard Worker// This information can come from two mechanisms
773*333d2b36SAndroid Build Coastguard Worker// 1. New: Direct deps to _selected_ apexes. The apexes return a ApexExportsInfo
774*333d2b36SAndroid Build Coastguard Worker// 2. Legacy: An edge to java_library or java_import (java_sdk_library) module. For prebuilt apexes, this serves as a hook and is populated by deapexers of prebuilt apxes
775*333d2b36SAndroid Build Coastguard Worker// TODO: b/308174306 - Once all mainline modules have been flagged, drop (2)
776*333d2b36SAndroid Build Coastguard Workerfunc getDexJarForApex(ctx android.ModuleContext, pair apexJarModulePair, apexNameToApexExportsInfoMap apexNameToApexExportsInfoMap) android.Path {
777*333d2b36SAndroid Build Coastguard Worker	if dex, found := apexNameToApexExportsInfoMap.javaLibraryDexPathOnHost(ctx, pair.apex, ModuleStemForDeapexing(pair.jarModule)); found {
778*333d2b36SAndroid Build Coastguard Worker		return dex
779*333d2b36SAndroid Build Coastguard Worker	}
780*333d2b36SAndroid Build Coastguard Worker	// TODO: b/308174306 - Remove the legacy mechanism
781*333d2b36SAndroid Build Coastguard Worker	if android.IsConfiguredJarForPlatform(pair.apex) || android.IsModulePrebuilt(pair.jarModule) {
782*333d2b36SAndroid Build Coastguard Worker		// This gives us the dex jar with the hidden API flags encoded from the monolithic hidden API
783*333d2b36SAndroid Build Coastguard Worker		// files or the dex jar extracted from a prebuilt APEX. We can't use this for a boot jar for
784*333d2b36SAndroid Build Coastguard Worker		// a source APEX because there is no guarantee that it is the same as the jar packed into the
785*333d2b36SAndroid Build Coastguard Worker		// APEX. In practice, they are the same when we are building from a full source tree, but they
786*333d2b36SAndroid Build Coastguard Worker		// are different when we are building from a thin manifest (e.g., master-art), where there is
787*333d2b36SAndroid Build Coastguard Worker		// no monolithic hidden API files at all.
788*333d2b36SAndroid Build Coastguard Worker		return retrieveEncodedBootDexJarFromModule(ctx, pair.jarModule)
789*333d2b36SAndroid Build Coastguard Worker	} else {
790*333d2b36SAndroid Build Coastguard Worker		// Use exactly the same jar that is packed into the APEX.
791*333d2b36SAndroid Build Coastguard Worker		fragment := getBootclasspathFragmentByApex(ctx, pair.apex)
792*333d2b36SAndroid Build Coastguard Worker		if fragment == nil {
793*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf("Boot jar '%[1]s' is from APEX '%[2]s', but a bootclasspath_fragment for "+
794*333d2b36SAndroid Build Coastguard Worker				"APEX '%[2]s' doesn't exist or is not added as a dependency of dex_bootjars",
795*333d2b36SAndroid Build Coastguard Worker				pair.jarModule.Name(),
796*333d2b36SAndroid Build Coastguard Worker				pair.apex)
797*333d2b36SAndroid Build Coastguard Worker		}
798*333d2b36SAndroid Build Coastguard Worker		bootclasspathFragmentInfo, _ := android.OtherModuleProvider(ctx, fragment, BootclasspathFragmentApexContentInfoProvider)
799*333d2b36SAndroid Build Coastguard Worker		jar, err := bootclasspathFragmentInfo.DexBootJarPathForContentModule(pair.jarModule)
800*333d2b36SAndroid Build Coastguard Worker		if err != nil {
801*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf("%s", err)
802*333d2b36SAndroid Build Coastguard Worker		}
803*333d2b36SAndroid Build Coastguard Worker		return jar
804*333d2b36SAndroid Build Coastguard Worker	}
805*333d2b36SAndroid Build Coastguard Worker	return nil
806*333d2b36SAndroid Build Coastguard Worker}
807*333d2b36SAndroid Build Coastguard Worker
808*333d2b36SAndroid Build Coastguard Worker// copyBootJarsToPredefinedLocations generates commands that will copy boot jars to predefined
809*333d2b36SAndroid Build Coastguard Worker// paths in the global config.
810*333d2b36SAndroid Build Coastguard Workerfunc copyBootJarsToPredefinedLocations(ctx android.ModuleContext, srcBootDexJarsByModule bootDexJarByModule, dstBootJarsByModule map[string]android.WritablePath) {
811*333d2b36SAndroid Build Coastguard Worker	// Create the super set of module names.
812*333d2b36SAndroid Build Coastguard Worker	names := []string{}
813*333d2b36SAndroid Build Coastguard Worker	names = append(names, android.SortedKeys(srcBootDexJarsByModule)...)
814*333d2b36SAndroid Build Coastguard Worker	names = append(names, android.SortedKeys(dstBootJarsByModule)...)
815*333d2b36SAndroid Build Coastguard Worker	names = android.SortedUniqueStrings(names)
816*333d2b36SAndroid Build Coastguard Worker	for _, name := range names {
817*333d2b36SAndroid Build Coastguard Worker		src := srcBootDexJarsByModule[name]
818*333d2b36SAndroid Build Coastguard Worker		dst := dstBootJarsByModule[name]
819*333d2b36SAndroid Build Coastguard Worker
820*333d2b36SAndroid Build Coastguard Worker		if src == nil {
821*333d2b36SAndroid Build Coastguard Worker			// A dex boot jar should be provided by the source java module. It needs to be installable or
822*333d2b36SAndroid Build Coastguard Worker			// have compile_dex=true - cf. assignments to java.Module.dexJarFile.
823*333d2b36SAndroid Build Coastguard Worker			//
824*333d2b36SAndroid Build Coastguard Worker			// However, the source java module may be either replaced or overridden (using prefer:true) by
825*333d2b36SAndroid Build Coastguard Worker			// a prebuilt java module with the same name. In that case the dex boot jar needs to be
826*333d2b36SAndroid Build Coastguard Worker			// provided by the corresponding prebuilt APEX module. That APEX is the one that refers
827*333d2b36SAndroid Build Coastguard Worker			// through a exported_(boot|systemserver)classpath_fragments property to a
828*333d2b36SAndroid Build Coastguard Worker			// prebuilt_(boot|systemserver)classpath_fragment module, which in turn lists the prebuilt
829*333d2b36SAndroid Build Coastguard Worker			// java module in the contents property. If that chain is broken then this dependency will
830*333d2b36SAndroid Build Coastguard Worker			// fail.
831*333d2b36SAndroid Build Coastguard Worker			if !ctx.Config().AllowMissingDependencies() {
832*333d2b36SAndroid Build Coastguard Worker				ctx.ModuleErrorf("module %s does not provide a dex boot jar (see comment next to this message in Soong for details)", name)
833*333d2b36SAndroid Build Coastguard Worker			} else {
834*333d2b36SAndroid Build Coastguard Worker				ctx.AddMissingDependencies([]string{name})
835*333d2b36SAndroid Build Coastguard Worker			}
836*333d2b36SAndroid Build Coastguard Worker		} else if dst == nil {
837*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf("module %s is not part of the boot configuration", name)
838*333d2b36SAndroid Build Coastguard Worker		} else {
839*333d2b36SAndroid Build Coastguard Worker			ctx.Build(pctx, android.BuildParams{
840*333d2b36SAndroid Build Coastguard Worker				Rule:   android.Cp,
841*333d2b36SAndroid Build Coastguard Worker				Input:  src,
842*333d2b36SAndroid Build Coastguard Worker				Output: dst,
843*333d2b36SAndroid Build Coastguard Worker			})
844*333d2b36SAndroid Build Coastguard Worker		}
845*333d2b36SAndroid Build Coastguard Worker	}
846*333d2b36SAndroid Build Coastguard Worker}
847*333d2b36SAndroid Build Coastguard Worker
848*333d2b36SAndroid Build Coastguard Worker// buildBootImageVariantsForAndroidOs generates rules to build the boot image variants for the
849*333d2b36SAndroid Build Coastguard Worker// android.Android OsType and returns a map from the architectures to the paths of the generated
850*333d2b36SAndroid Build Coastguard Worker// boot image files.
851*333d2b36SAndroid Build Coastguard Worker//
852*333d2b36SAndroid Build Coastguard Worker// The paths are returned because they are needed elsewhere in Soong, e.g. for populating an APEX.
853*333d2b36SAndroid Build Coastguard Workerfunc buildBootImageVariantsForAndroidOs(ctx android.ModuleContext, image *bootImageConfig, profile android.WritablePath) bootImageOutputs {
854*333d2b36SAndroid Build Coastguard Worker	return buildBootImageForOsType(ctx, image, profile, android.Android)
855*333d2b36SAndroid Build Coastguard Worker}
856*333d2b36SAndroid Build Coastguard Worker
857*333d2b36SAndroid Build Coastguard Worker// buildBootImageVariantsForBuildOs generates rules to build the boot image variants for the
858*333d2b36SAndroid Build Coastguard Worker// config.BuildOS OsType, i.e. the type of OS on which the build is being running.
859*333d2b36SAndroid Build Coastguard Worker//
860*333d2b36SAndroid Build Coastguard Worker// The files need to be generated into their predefined location because they are used from there
861*333d2b36SAndroid Build Coastguard Worker// both within Soong and outside, e.g. for ART based host side testing and also for use by some
862*333d2b36SAndroid Build Coastguard Worker// cloud based tools. However, they are not needed by callers of this function and so the paths do
863*333d2b36SAndroid Build Coastguard Worker// not need to be returned from this func, unlike the buildBootImageVariantsForAndroidOs func.
864*333d2b36SAndroid Build Coastguard Workerfunc buildBootImageVariantsForBuildOs(ctx android.ModuleContext, image *bootImageConfig, profile android.WritablePath) {
865*333d2b36SAndroid Build Coastguard Worker	buildBootImageForOsType(ctx, image, profile, ctx.Config().BuildOS)
866*333d2b36SAndroid Build Coastguard Worker}
867*333d2b36SAndroid Build Coastguard Worker
868*333d2b36SAndroid Build Coastguard Worker// bootImageFilesByArch is a map from android.ArchType to the paths to the boot image files.
869*333d2b36SAndroid Build Coastguard Worker//
870*333d2b36SAndroid Build Coastguard Worker// The paths include the .art, .oat and .vdex files, one for each of the modules from which the boot
871*333d2b36SAndroid Build Coastguard Worker// image is created.
872*333d2b36SAndroid Build Coastguard Workertype bootImageFilesByArch map[android.ArchType]android.Paths
873*333d2b36SAndroid Build Coastguard Worker
874*333d2b36SAndroid Build Coastguard Worker// bootImageOutputs encapsulates information about boot images that were created/obtained by
875*333d2b36SAndroid Build Coastguard Worker// commonBootclasspathFragment.produceBootImageFiles.
876*333d2b36SAndroid Build Coastguard Workertype bootImageOutputs struct {
877*333d2b36SAndroid Build Coastguard Worker	// Map from arch to the paths to the boot image files created/obtained for that arch.
878*333d2b36SAndroid Build Coastguard Worker	byArch bootImageFilesByArch
879*333d2b36SAndroid Build Coastguard Worker
880*333d2b36SAndroid Build Coastguard Worker	variants []bootImageVariantOutputs
881*333d2b36SAndroid Build Coastguard Worker
882*333d2b36SAndroid Build Coastguard Worker	// The path to the profile file created/obtained for the boot image.
883*333d2b36SAndroid Build Coastguard Worker	profile android.WritablePath
884*333d2b36SAndroid Build Coastguard Worker}
885*333d2b36SAndroid Build Coastguard Worker
886*333d2b36SAndroid Build Coastguard Worker// buildBootImageForOsType takes a bootImageConfig, a profile file and an android.OsType
887*333d2b36SAndroid Build Coastguard Worker// boot image files are required for and it creates rules to build the boot image
888*333d2b36SAndroid Build Coastguard Worker// files for all the required architectures for them.
889*333d2b36SAndroid Build Coastguard Worker//
890*333d2b36SAndroid Build Coastguard Worker// It returns a map from android.ArchType to the predefined paths of the boot image files.
891*333d2b36SAndroid Build Coastguard Workerfunc buildBootImageForOsType(ctx android.ModuleContext, image *bootImageConfig, profile android.WritablePath, requiredOsType android.OsType) bootImageOutputs {
892*333d2b36SAndroid Build Coastguard Worker	filesByArch := bootImageFilesByArch{}
893*333d2b36SAndroid Build Coastguard Worker	imageOutputs := bootImageOutputs{
894*333d2b36SAndroid Build Coastguard Worker		byArch:  filesByArch,
895*333d2b36SAndroid Build Coastguard Worker		profile: profile,
896*333d2b36SAndroid Build Coastguard Worker	}
897*333d2b36SAndroid Build Coastguard Worker	for _, variant := range image.variants {
898*333d2b36SAndroid Build Coastguard Worker		if variant.target.Os == requiredOsType {
899*333d2b36SAndroid Build Coastguard Worker			variantOutputs := buildBootImageVariant(ctx, variant, profile)
900*333d2b36SAndroid Build Coastguard Worker			imageOutputs.variants = append(imageOutputs.variants, variantOutputs)
901*333d2b36SAndroid Build Coastguard Worker			filesByArch[variant.target.Arch.ArchType] = variant.imagesDeps.Paths()
902*333d2b36SAndroid Build Coastguard Worker		}
903*333d2b36SAndroid Build Coastguard Worker	}
904*333d2b36SAndroid Build Coastguard Worker
905*333d2b36SAndroid Build Coastguard Worker	return imageOutputs
906*333d2b36SAndroid Build Coastguard Worker}
907*333d2b36SAndroid Build Coastguard Worker
908*333d2b36SAndroid Build Coastguard Worker// buildBootImageZipInPredefinedLocation generates a zip file containing all the boot image files.
909*333d2b36SAndroid Build Coastguard Worker//
910*333d2b36SAndroid Build Coastguard Worker// The supplied filesByArch is nil when the boot image files have not been generated. Otherwise, it
911*333d2b36SAndroid Build Coastguard Worker// is a map from android.ArchType to the predefined locations.
912*333d2b36SAndroid Build Coastguard Workerfunc buildBootImageZipInPredefinedLocation(ctx android.ModuleContext, image *bootImageConfig, filesByArch bootImageFilesByArch) {
913*333d2b36SAndroid Build Coastguard Worker	if filesByArch == nil {
914*333d2b36SAndroid Build Coastguard Worker		return
915*333d2b36SAndroid Build Coastguard Worker	}
916*333d2b36SAndroid Build Coastguard Worker
917*333d2b36SAndroid Build Coastguard Worker	// Compute the list of files from all the architectures.
918*333d2b36SAndroid Build Coastguard Worker	zipFiles := android.Paths{}
919*333d2b36SAndroid Build Coastguard Worker	for _, archType := range android.ArchTypeList() {
920*333d2b36SAndroid Build Coastguard Worker		zipFiles = append(zipFiles, filesByArch[archType]...)
921*333d2b36SAndroid Build Coastguard Worker	}
922*333d2b36SAndroid Build Coastguard Worker
923*333d2b36SAndroid Build Coastguard Worker	rule := android.NewRuleBuilder(pctx, ctx)
924*333d2b36SAndroid Build Coastguard Worker	rule.Command().
925*333d2b36SAndroid Build Coastguard Worker		BuiltTool("soong_zip").
926*333d2b36SAndroid Build Coastguard Worker		FlagWithOutput("-o ", image.zip).
927*333d2b36SAndroid Build Coastguard Worker		FlagWithArg("-C ", image.dir.Join(ctx, android.Android.String()).String()).
928*333d2b36SAndroid Build Coastguard Worker		FlagWithInputList("-f ", zipFiles, " -f ")
929*333d2b36SAndroid Build Coastguard Worker
930*333d2b36SAndroid Build Coastguard Worker	rule.Build("zip_"+image.name, "zip "+image.name+" image")
931*333d2b36SAndroid Build Coastguard Worker}
932*333d2b36SAndroid Build Coastguard Worker
933*333d2b36SAndroid Build Coastguard Workertype bootImageVariantOutputs struct {
934*333d2b36SAndroid Build Coastguard Worker	config *bootImageVariant
935*333d2b36SAndroid Build Coastguard Worker}
936*333d2b36SAndroid Build Coastguard Worker
937*333d2b36SAndroid Build Coastguard Worker// Returns the profile file for an apex
938*333d2b36SAndroid Build Coastguard Worker// This information can come from two mechanisms
939*333d2b36SAndroid Build Coastguard Worker// 1. New: Direct deps to _selected_ apexes. The apexes return a BootclasspathFragmentApexContentInfo
940*333d2b36SAndroid Build Coastguard Worker// 2. Legacy: An edge to bootclasspath_fragment module. For prebuilt apexes, this serves as a hook and is populated by deapexers of prebuilt apxes
941*333d2b36SAndroid Build Coastguard Worker// TODO: b/308174306 - Once all mainline modules have been flagged, drop (2)
942*333d2b36SAndroid Build Coastguard Workerfunc getProfilePathForApex(ctx android.ModuleContext, apexName string, apexNameToBcpInfoMap map[string]android.ApexExportsInfo) android.Path {
943*333d2b36SAndroid Build Coastguard Worker	if info, exists := apexNameToBcpInfoMap[apexName]; exists {
944*333d2b36SAndroid Build Coastguard Worker		return info.ProfilePathOnHost
945*333d2b36SAndroid Build Coastguard Worker	}
946*333d2b36SAndroid Build Coastguard Worker	// TODO: b/308174306 - Remove the legacy mechanism
947*333d2b36SAndroid Build Coastguard Worker	fragment := getBootclasspathFragmentByApex(ctx, apexName)
948*333d2b36SAndroid Build Coastguard Worker	if fragment == nil {
949*333d2b36SAndroid Build Coastguard Worker		ctx.ModuleErrorf("Boot image config imports profile from '%[2]s', but a "+
950*333d2b36SAndroid Build Coastguard Worker			"bootclasspath_fragment for APEX '%[2]s' doesn't exist or is not added as a "+
951*333d2b36SAndroid Build Coastguard Worker			"dependency of dex_bootjars",
952*333d2b36SAndroid Build Coastguard Worker			apexName)
953*333d2b36SAndroid Build Coastguard Worker		return nil
954*333d2b36SAndroid Build Coastguard Worker	}
955*333d2b36SAndroid Build Coastguard Worker	return fragment.(commonBootclasspathFragment).getProfilePath()
956*333d2b36SAndroid Build Coastguard Worker}
957*333d2b36SAndroid Build Coastguard Worker
958*333d2b36SAndroid Build Coastguard Workerfunc getApexNameToApexExportsInfoMap(ctx android.ModuleContext) apexNameToApexExportsInfoMap {
959*333d2b36SAndroid Build Coastguard Worker	apexNameToApexExportsInfoMap := apexNameToApexExportsInfoMap{}
960*333d2b36SAndroid Build Coastguard Worker	ctx.VisitDirectDepsWithTag(dexpreoptBootJarDepTag, func(am android.Module) {
961*333d2b36SAndroid Build Coastguard Worker		if info, exists := android.OtherModuleProvider(ctx, am, android.ApexExportsInfoProvider); exists {
962*333d2b36SAndroid Build Coastguard Worker			apexNameToApexExportsInfoMap[info.ApexName] = info
963*333d2b36SAndroid Build Coastguard Worker		}
964*333d2b36SAndroid Build Coastguard Worker	})
965*333d2b36SAndroid Build Coastguard Worker	return apexNameToApexExportsInfoMap
966*333d2b36SAndroid Build Coastguard Worker}
967*333d2b36SAndroid Build Coastguard Worker
968*333d2b36SAndroid Build Coastguard Workerfunc packageFileForTargetImage(ctx android.ModuleContext, image *bootImageVariant) {
969*333d2b36SAndroid Build Coastguard Worker	if image.target.Os != ctx.Os() {
970*333d2b36SAndroid Build Coastguard Worker		// This is not for the target device.
971*333d2b36SAndroid Build Coastguard Worker		return
972*333d2b36SAndroid Build Coastguard Worker	}
973*333d2b36SAndroid Build Coastguard Worker
974*333d2b36SAndroid Build Coastguard Worker	for _, install := range image.installs {
975*333d2b36SAndroid Build Coastguard Worker		installFile(ctx, install)
976*333d2b36SAndroid Build Coastguard Worker	}
977*333d2b36SAndroid Build Coastguard Worker
978*333d2b36SAndroid Build Coastguard Worker	for _, install := range image.vdexInstalls {
979*333d2b36SAndroid Build Coastguard Worker		installPath, relDir, name := getModuleInstallPathInfo(ctx, install.To)
980*333d2b36SAndroid Build Coastguard Worker		if name == "" {
981*333d2b36SAndroid Build Coastguard Worker			continue
982*333d2b36SAndroid Build Coastguard Worker		}
983*333d2b36SAndroid Build Coastguard Worker		// Note that the vdex files are identical between architectures. Copy the vdex to a no arch directory
984*333d2b36SAndroid Build Coastguard Worker		// and create symlinks for both the primary and secondary arches.
985*333d2b36SAndroid Build Coastguard Worker		ctx.InstallSymlink(installPath.Join(ctx, relDir), name, installPath.Join(ctx, "framework", name))
986*333d2b36SAndroid Build Coastguard Worker		if image.target.Arch.ArchType.Name == ctx.DeviceConfig().DeviceArch() {
987*333d2b36SAndroid Build Coastguard Worker			// Copy the vdex from the primary arch to the no-arch directory
988*333d2b36SAndroid Build Coastguard Worker			// e.g. /system/framework/$bootjar.vdex
989*333d2b36SAndroid Build Coastguard Worker			ctx.InstallFile(installPath.Join(ctx, "framework"), name, install.From)
990*333d2b36SAndroid Build Coastguard Worker		}
991*333d2b36SAndroid Build Coastguard Worker	}
992*333d2b36SAndroid Build Coastguard Worker}
993*333d2b36SAndroid Build Coastguard Worker
994*333d2b36SAndroid Build Coastguard Workervar artBootImageHostInfoProvider = blueprint.NewProvider[artBootImageHostInfo]()
995*333d2b36SAndroid Build Coastguard Worker
996*333d2b36SAndroid Build Coastguard Worker// artBootImageHostInfo contains the install locations of the host variant of ART boot image
997*333d2b36SAndroid Build Coastguard Worker// this contains both the primary and secondary arch locations
998*333d2b36SAndroid Build Coastguard Workertype artBootImageHostInfo struct {
999*333d2b36SAndroid Build Coastguard Worker	installs android.RuleBuilderInstalls
1000*333d2b36SAndroid Build Coastguard Worker}
1001*333d2b36SAndroid Build Coastguard Worker
1002*333d2b36SAndroid Build Coastguard Worker// Generate boot image build rules for a specific target.
1003*333d2b36SAndroid Build Coastguard Workerfunc buildBootImageVariant(ctx android.ModuleContext, image *bootImageVariant, profile android.Path) bootImageVariantOutputs {
1004*333d2b36SAndroid Build Coastguard Worker
1005*333d2b36SAndroid Build Coastguard Worker	globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
1006*333d2b36SAndroid Build Coastguard Worker	global := dexpreopt.GetGlobalConfig(ctx)
1007*333d2b36SAndroid Build Coastguard Worker
1008*333d2b36SAndroid Build Coastguard Worker	arch := image.target.Arch.ArchType
1009*333d2b36SAndroid Build Coastguard Worker	os := image.target.Os.String() // We need to distinguish host-x86 and device-x86.
1010*333d2b36SAndroid Build Coastguard Worker	symbolsDir := image.symbolsDir.Join(ctx, os, image.installDir, arch.String())
1011*333d2b36SAndroid Build Coastguard Worker	symbolsFile := symbolsDir.Join(ctx, image.stem+".oat")
1012*333d2b36SAndroid Build Coastguard Worker	outputDir := image.dir.Join(ctx, os, image.installDir, arch.String())
1013*333d2b36SAndroid Build Coastguard Worker	outputPath := outputDir.Join(ctx, image.stem+".oat")
1014*333d2b36SAndroid Build Coastguard Worker	oatLocation := dexpreopt.PathToLocation(outputPath, arch)
1015*333d2b36SAndroid Build Coastguard Worker	imagePath := outputPath.ReplaceExtension(ctx, "art")
1016*333d2b36SAndroid Build Coastguard Worker
1017*333d2b36SAndroid Build Coastguard Worker	rule := android.NewRuleBuilder(pctx, ctx)
1018*333d2b36SAndroid Build Coastguard Worker
1019*333d2b36SAndroid Build Coastguard Worker	rule.Command().Text("mkdir").Flag("-p").Flag(symbolsDir.String())
1020*333d2b36SAndroid Build Coastguard Worker	rule.Command().Text("rm").Flag("-f").
1021*333d2b36SAndroid Build Coastguard Worker		Flag(symbolsDir.Join(ctx, "*.art").String()).
1022*333d2b36SAndroid Build Coastguard Worker		Flag(symbolsDir.Join(ctx, "*.oat").String()).
1023*333d2b36SAndroid Build Coastguard Worker		Flag(symbolsDir.Join(ctx, "*.vdex").String()).
1024*333d2b36SAndroid Build Coastguard Worker		Flag(symbolsDir.Join(ctx, "*.invocation").String())
1025*333d2b36SAndroid Build Coastguard Worker	rule.Command().Text("rm").Flag("-f").
1026*333d2b36SAndroid Build Coastguard Worker		Flag(outputDir.Join(ctx, "*.art").String()).
1027*333d2b36SAndroid Build Coastguard Worker		Flag(outputDir.Join(ctx, "*.oat").String()).
1028*333d2b36SAndroid Build Coastguard Worker		Flag(outputDir.Join(ctx, "*.vdex").String()).
1029*333d2b36SAndroid Build Coastguard Worker		Flag(outputDir.Join(ctx, "*.invocation").String())
1030*333d2b36SAndroid Build Coastguard Worker
1031*333d2b36SAndroid Build Coastguard Worker	cmd := rule.Command()
1032*333d2b36SAndroid Build Coastguard Worker
1033*333d2b36SAndroid Build Coastguard Worker	extraFlags := ctx.Config().Getenv("ART_BOOT_IMAGE_EXTRA_ARGS")
1034*333d2b36SAndroid Build Coastguard Worker	if extraFlags == "" {
1035*333d2b36SAndroid Build Coastguard Worker		// Use ANDROID_LOG_TAGS to suppress most logging by default...
1036*333d2b36SAndroid Build Coastguard Worker		cmd.Text(`ANDROID_LOG_TAGS="*:e"`)
1037*333d2b36SAndroid Build Coastguard Worker	} else {
1038*333d2b36SAndroid Build Coastguard Worker		// ...unless the boot image is generated specifically for testing, then allow all logging.
1039*333d2b36SAndroid Build Coastguard Worker		cmd.Text(`ANDROID_LOG_TAGS="*:v"`)
1040*333d2b36SAndroid Build Coastguard Worker	}
1041*333d2b36SAndroid Build Coastguard Worker
1042*333d2b36SAndroid Build Coastguard Worker	invocationPath := outputPath.ReplaceExtension(ctx, "invocation")
1043*333d2b36SAndroid Build Coastguard Worker
1044*333d2b36SAndroid Build Coastguard Worker	apexNameToApexExportsInfoMap := getApexNameToApexExportsInfoMap(ctx)
1045*333d2b36SAndroid Build Coastguard Worker
1046*333d2b36SAndroid Build Coastguard Worker	cmd.Tool(globalSoong.Dex2oat).
1047*333d2b36SAndroid Build Coastguard Worker		Flag("--avoid-storing-invocation").
1048*333d2b36SAndroid Build Coastguard Worker		FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
1049*333d2b36SAndroid Build Coastguard Worker		Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatImageXms).
1050*333d2b36SAndroid Build Coastguard Worker		Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatImageXmx)
1051*333d2b36SAndroid Build Coastguard Worker
1052*333d2b36SAndroid Build Coastguard Worker	if image.isProfileGuided() && !global.DisableGenerateProfile {
1053*333d2b36SAndroid Build Coastguard Worker		if profile != nil {
1054*333d2b36SAndroid Build Coastguard Worker			cmd.FlagWithInput("--profile-file=", profile)
1055*333d2b36SAndroid Build Coastguard Worker		}
1056*333d2b36SAndroid Build Coastguard Worker
1057*333d2b36SAndroid Build Coastguard Worker		for _, apex := range image.profileImports {
1058*333d2b36SAndroid Build Coastguard Worker			importedProfile := getProfilePathForApex(ctx, apex, apexNameToApexExportsInfoMap)
1059*333d2b36SAndroid Build Coastguard Worker			if importedProfile == nil {
1060*333d2b36SAndroid Build Coastguard Worker				ctx.ModuleErrorf("Boot image config '%[1]s' imports profile from '%[2]s', but '%[2]s' "+
1061*333d2b36SAndroid Build Coastguard Worker					"doesn't provide a profile",
1062*333d2b36SAndroid Build Coastguard Worker					image.name,
1063*333d2b36SAndroid Build Coastguard Worker					apex)
1064*333d2b36SAndroid Build Coastguard Worker				return bootImageVariantOutputs{}
1065*333d2b36SAndroid Build Coastguard Worker			}
1066*333d2b36SAndroid Build Coastguard Worker			cmd.FlagWithInput("--profile-file=", importedProfile)
1067*333d2b36SAndroid Build Coastguard Worker		}
1068*333d2b36SAndroid Build Coastguard Worker	}
1069*333d2b36SAndroid Build Coastguard Worker
1070*333d2b36SAndroid Build Coastguard Worker	dirtyImageFile := "frameworks/base/config/dirty-image-objects"
1071*333d2b36SAndroid Build Coastguard Worker	dirtyImagePath := android.ExistentPathForSource(ctx, dirtyImageFile)
1072*333d2b36SAndroid Build Coastguard Worker	if dirtyImagePath.Valid() {
1073*333d2b36SAndroid Build Coastguard Worker		cmd.FlagWithInput("--dirty-image-objects=", dirtyImagePath.Path())
1074*333d2b36SAndroid Build Coastguard Worker	}
1075*333d2b36SAndroid Build Coastguard Worker
1076*333d2b36SAndroid Build Coastguard Worker	if image.extends != nil {
1077*333d2b36SAndroid Build Coastguard Worker		// It is a boot image extension, so it needs the boot images that it depends on.
1078*333d2b36SAndroid Build Coastguard Worker		baseImageLocations := make([]string, 0, len(image.baseImages))
1079*333d2b36SAndroid Build Coastguard Worker		for _, image := range image.baseImages {
1080*333d2b36SAndroid Build Coastguard Worker			baseImageLocations = append(baseImageLocations, dexpreopt.PathToLocation(image, arch))
1081*333d2b36SAndroid Build Coastguard Worker		}
1082*333d2b36SAndroid Build Coastguard Worker		cmd.
1083*333d2b36SAndroid Build Coastguard Worker			Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
1084*333d2b36SAndroid Build Coastguard Worker			Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", image.dexLocationsDeps, ":").
1085*333d2b36SAndroid Build Coastguard Worker			// Add the path to the first file in the boot image with the arch specific directory removed,
1086*333d2b36SAndroid Build Coastguard Worker			// dex2oat will reconstruct the path to the actual file when it needs it. As the actual path
1087*333d2b36SAndroid Build Coastguard Worker			// to the file cannot be passed to the command make sure to add the actual path as an Implicit
1088*333d2b36SAndroid Build Coastguard Worker			// dependency to ensure that it is built before the command runs.
1089*333d2b36SAndroid Build Coastguard Worker			FlagWithList("--boot-image=", baseImageLocations, ":").Implicits(image.baseImages.Paths()).
1090*333d2b36SAndroid Build Coastguard Worker			// Similarly, the dex2oat tool will automatically find the paths to other files in the base
1091*333d2b36SAndroid Build Coastguard Worker			// boot image so make sure to add them as implicit dependencies to ensure that they are built
1092*333d2b36SAndroid Build Coastguard Worker			// before this command is run.
1093*333d2b36SAndroid Build Coastguard Worker			Implicits(image.baseImagesDeps)
1094*333d2b36SAndroid Build Coastguard Worker	} else {
1095*333d2b36SAndroid Build Coastguard Worker		// It is a primary image, so it needs a base address.
1096*333d2b36SAndroid Build Coastguard Worker		cmd.FlagWithArg("--base=", ctx.Config().LibartImgDeviceBaseAddress())
1097*333d2b36SAndroid Build Coastguard Worker	}
1098*333d2b36SAndroid Build Coastguard Worker
1099*333d2b36SAndroid Build Coastguard Worker	if len(image.preloadedClassesFile) > 0 {
1100*333d2b36SAndroid Build Coastguard Worker		// We always expect a preloaded classes file to be available. However, if we cannot find it, it's
1101*333d2b36SAndroid Build Coastguard Worker		// OK to not pass the flag to dex2oat.
1102*333d2b36SAndroid Build Coastguard Worker		preloadedClassesPath := android.ExistentPathForSource(ctx, image.preloadedClassesFile)
1103*333d2b36SAndroid Build Coastguard Worker		if preloadedClassesPath.Valid() {
1104*333d2b36SAndroid Build Coastguard Worker			cmd.FlagWithInput("--preloaded-classes=", preloadedClassesPath.Path())
1105*333d2b36SAndroid Build Coastguard Worker		}
1106*333d2b36SAndroid Build Coastguard Worker	}
1107*333d2b36SAndroid Build Coastguard Worker
1108*333d2b36SAndroid Build Coastguard Worker	cmd.
1109*333d2b36SAndroid Build Coastguard Worker		FlagForEachInput("--dex-file=", image.dexPaths.Paths()).
1110*333d2b36SAndroid Build Coastguard Worker		FlagForEachArg("--dex-location=", image.dexLocations).
1111*333d2b36SAndroid Build Coastguard Worker		Flag("--generate-debug-info").
1112*333d2b36SAndroid Build Coastguard Worker		Flag("--generate-build-id").
1113*333d2b36SAndroid Build Coastguard Worker		Flag("--image-format=lz4hc").
1114*333d2b36SAndroid Build Coastguard Worker		FlagWithArg("--oat-symbols=", symbolsFile.String()).
1115*333d2b36SAndroid Build Coastguard Worker		FlagWithArg("--oat-file=", outputPath.String()).
1116*333d2b36SAndroid Build Coastguard Worker		FlagWithArg("--oat-location=", oatLocation).
1117*333d2b36SAndroid Build Coastguard Worker		FlagWithArg("--image=", imagePath.String()).
1118*333d2b36SAndroid Build Coastguard Worker		FlagWithArg("--instruction-set=", arch.String()).
1119*333d2b36SAndroid Build Coastguard Worker		FlagWithArg("--android-root=", global.EmptyDirectory).
1120*333d2b36SAndroid Build Coastguard Worker		FlagWithArg("--no-inline-from=", "core-oj.jar").
1121*333d2b36SAndroid Build Coastguard Worker		Flag("--force-determinism").
1122*333d2b36SAndroid Build Coastguard Worker		Flag("--abort-on-hard-verifier-error")
1123*333d2b36SAndroid Build Coastguard Worker
1124*333d2b36SAndroid Build Coastguard Worker	// We don't strip on host to make perf tools work.
1125*333d2b36SAndroid Build Coastguard Worker	if image.target.Os == android.Android {
1126*333d2b36SAndroid Build Coastguard Worker		cmd.Flag("--strip")
1127*333d2b36SAndroid Build Coastguard Worker	}
1128*333d2b36SAndroid Build Coastguard Worker
1129*333d2b36SAndroid Build Coastguard Worker	// If the image is profile-guided but the profile is disabled, we omit "--compiler-filter" to
1130*333d2b36SAndroid Build Coastguard Worker	// leave the decision to dex2oat to pick the compiler filter.
1131*333d2b36SAndroid Build Coastguard Worker	if !(image.isProfileGuided() && global.DisableGenerateProfile) {
1132*333d2b36SAndroid Build Coastguard Worker		cmd.FlagWithArg("--compiler-filter=", image.compilerFilter)
1133*333d2b36SAndroid Build Coastguard Worker	}
1134*333d2b36SAndroid Build Coastguard Worker
1135*333d2b36SAndroid Build Coastguard Worker	if image.singleImage {
1136*333d2b36SAndroid Build Coastguard Worker		cmd.Flag("--single-image")
1137*333d2b36SAndroid Build Coastguard Worker	}
1138*333d2b36SAndroid Build Coastguard Worker
1139*333d2b36SAndroid Build Coastguard Worker	// Use the default variant/features for host builds.
1140*333d2b36SAndroid Build Coastguard Worker	// The map below contains only device CPU info (which might be x86 on some devices).
1141*333d2b36SAndroid Build Coastguard Worker	if image.target.Os == android.Android {
1142*333d2b36SAndroid Build Coastguard Worker		cmd.FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch])
1143*333d2b36SAndroid Build Coastguard Worker		cmd.FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch])
1144*333d2b36SAndroid Build Coastguard Worker	}
1145*333d2b36SAndroid Build Coastguard Worker
1146*333d2b36SAndroid Build Coastguard Worker	if image.target.Os == android.Android {
1147*333d2b36SAndroid Build Coastguard Worker		cmd.Text("$(cat").Input(globalSoong.UffdGcFlag).Text(")")
1148*333d2b36SAndroid Build Coastguard Worker	}
1149*333d2b36SAndroid Build Coastguard Worker
1150*333d2b36SAndroid Build Coastguard Worker	if global.BootFlags != "" {
1151*333d2b36SAndroid Build Coastguard Worker		cmd.Flag(global.BootFlags)
1152*333d2b36SAndroid Build Coastguard Worker	}
1153*333d2b36SAndroid Build Coastguard Worker
1154*333d2b36SAndroid Build Coastguard Worker	if extraFlags != "" {
1155*333d2b36SAndroid Build Coastguard Worker		cmd.Flag(extraFlags)
1156*333d2b36SAndroid Build Coastguard Worker	}
1157*333d2b36SAndroid Build Coastguard Worker
1158*333d2b36SAndroid Build Coastguard Worker	cmd.Textf(`|| ( echo %s ; false )`, proptools.ShellEscape(failureMessage))
1159*333d2b36SAndroid Build Coastguard Worker
1160*333d2b36SAndroid Build Coastguard Worker	installDir := filepath.Dir(image.imagePathOnDevice)
1161*333d2b36SAndroid Build Coastguard Worker
1162*333d2b36SAndroid Build Coastguard Worker	var vdexInstalls android.RuleBuilderInstalls
1163*333d2b36SAndroid Build Coastguard Worker	var unstrippedInstalls android.RuleBuilderInstalls
1164*333d2b36SAndroid Build Coastguard Worker
1165*333d2b36SAndroid Build Coastguard Worker	for _, artOrOat := range image.moduleFiles(ctx, outputDir, ".art", ".oat") {
1166*333d2b36SAndroid Build Coastguard Worker		cmd.ImplicitOutput(artOrOat)
1167*333d2b36SAndroid Build Coastguard Worker
1168*333d2b36SAndroid Build Coastguard Worker		// Install the .oat and .art files
1169*333d2b36SAndroid Build Coastguard Worker		rule.Install(artOrOat, filepath.Join(installDir, artOrOat.Base()))
1170*333d2b36SAndroid Build Coastguard Worker	}
1171*333d2b36SAndroid Build Coastguard Worker
1172*333d2b36SAndroid Build Coastguard Worker	for _, vdex := range image.moduleFiles(ctx, outputDir, ".vdex") {
1173*333d2b36SAndroid Build Coastguard Worker		cmd.ImplicitOutput(vdex)
1174*333d2b36SAndroid Build Coastguard Worker
1175*333d2b36SAndroid Build Coastguard Worker		// Note that the vdex files are identical between architectures.
1176*333d2b36SAndroid Build Coastguard Worker		// Make rules will create symlinks to share them between architectures.
1177*333d2b36SAndroid Build Coastguard Worker		vdexInstalls = append(vdexInstalls,
1178*333d2b36SAndroid Build Coastguard Worker			android.RuleBuilderInstall{vdex, filepath.Join(installDir, vdex.Base())})
1179*333d2b36SAndroid Build Coastguard Worker	}
1180*333d2b36SAndroid Build Coastguard Worker
1181*333d2b36SAndroid Build Coastguard Worker	for _, unstrippedOat := range image.moduleFiles(ctx, symbolsDir, ".oat") {
1182*333d2b36SAndroid Build Coastguard Worker		cmd.ImplicitOutput(unstrippedOat)
1183*333d2b36SAndroid Build Coastguard Worker
1184*333d2b36SAndroid Build Coastguard Worker		// Install the unstripped oat files.  The Make rules will put these in $(TARGET_OUT_UNSTRIPPED)
1185*333d2b36SAndroid Build Coastguard Worker		unstrippedInstalls = append(unstrippedInstalls,
1186*333d2b36SAndroid Build Coastguard Worker			android.RuleBuilderInstall{unstrippedOat, filepath.Join(installDir, unstrippedOat.Base())})
1187*333d2b36SAndroid Build Coastguard Worker	}
1188*333d2b36SAndroid Build Coastguard Worker
1189*333d2b36SAndroid Build Coastguard Worker	rule.Build(image.name+"JarsDexpreopt_"+image.target.String(), "dexpreopt "+image.name+" jars "+arch.String())
1190*333d2b36SAndroid Build Coastguard Worker
1191*333d2b36SAndroid Build Coastguard Worker	// save output and installed files for makevars
1192*333d2b36SAndroid Build Coastguard Worker	// TODO - these are always the same and so should be initialized in genBootImageConfigs
1193*333d2b36SAndroid Build Coastguard Worker	image.installs = rule.Installs()
1194*333d2b36SAndroid Build Coastguard Worker	image.vdexInstalls = vdexInstalls
1195*333d2b36SAndroid Build Coastguard Worker	image.unstrippedInstalls = unstrippedInstalls
1196*333d2b36SAndroid Build Coastguard Worker	packageFileForTargetImage(ctx, image)
1197*333d2b36SAndroid Build Coastguard Worker
1198*333d2b36SAndroid Build Coastguard Worker	// Only set the licenseMetadataFile from the active module.
1199*333d2b36SAndroid Build Coastguard Worker	if isActiveModule(ctx, ctx.Module()) {
1200*333d2b36SAndroid Build Coastguard Worker		image.licenseMetadataFile = android.OptionalPathForPath(ctx.LicenseMetadataFile())
1201*333d2b36SAndroid Build Coastguard Worker	}
1202*333d2b36SAndroid Build Coastguard Worker
1203*333d2b36SAndroid Build Coastguard Worker	return bootImageVariantOutputs{
1204*333d2b36SAndroid Build Coastguard Worker		image,
1205*333d2b36SAndroid Build Coastguard Worker	}
1206*333d2b36SAndroid Build Coastguard Worker}
1207*333d2b36SAndroid Build Coastguard Worker
1208*333d2b36SAndroid Build Coastguard Workerconst failureMessage = `ERROR: Dex2oat failed to compile a boot image.
1209*333d2b36SAndroid Build Coastguard WorkerIt is likely that the boot classpath is inconsistent.
1210*333d2b36SAndroid Build Coastguard WorkerRebuild with ART_BOOT_IMAGE_EXTRA_ARGS="--runtime-arg -verbose:verifier" to see verification errors.`
1211*333d2b36SAndroid Build Coastguard Worker
1212*333d2b36SAndroid Build Coastguard Workerfunc bootImageProfileRuleCommon(ctx android.ModuleContext, name string, dexFiles android.Paths, dexLocations []string) android.WritablePath {
1213*333d2b36SAndroid Build Coastguard Worker	globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
1214*333d2b36SAndroid Build Coastguard Worker	global := dexpreopt.GetGlobalConfig(ctx)
1215*333d2b36SAndroid Build Coastguard Worker
1216*333d2b36SAndroid Build Coastguard Worker	if global.DisableGenerateProfile {
1217*333d2b36SAndroid Build Coastguard Worker		return nil
1218*333d2b36SAndroid Build Coastguard Worker	}
1219*333d2b36SAndroid Build Coastguard Worker
1220*333d2b36SAndroid Build Coastguard Worker	defaultProfile := "frameworks/base/boot/boot-image-profile.txt"
1221*333d2b36SAndroid Build Coastguard Worker	// If ART is prebuilt, primarily in next release configs, this will still use
1222*333d2b36SAndroid Build Coastguard Worker	// the profile from source which represent the latest code, so it may not
1223*333d2b36SAndroid Build Coastguard Worker	// correspond to the BCP jars in the prebuilt APEX, but this is the profile we
1224*333d2b36SAndroid Build Coastguard Worker	// have access to.
1225*333d2b36SAndroid Build Coastguard Worker	artProfile := "art/build/boot/boot-image-profile.txt"
1226*333d2b36SAndroid Build Coastguard Worker	extraProfile := "frameworks/base/boot/boot-image-profile-extra.txt"
1227*333d2b36SAndroid Build Coastguard Worker
1228*333d2b36SAndroid Build Coastguard Worker	rule := android.NewRuleBuilder(pctx, ctx)
1229*333d2b36SAndroid Build Coastguard Worker
1230*333d2b36SAndroid Build Coastguard Worker	var profiles android.Paths
1231*333d2b36SAndroid Build Coastguard Worker	if len(global.BootImageProfiles) > 0 {
1232*333d2b36SAndroid Build Coastguard Worker		profiles = append(profiles, global.BootImageProfiles...)
1233*333d2b36SAndroid Build Coastguard Worker	} else if path := android.ExistentPathForSource(ctx, defaultProfile); path.Valid() {
1234*333d2b36SAndroid Build Coastguard Worker		profiles = append(profiles, path.Path())
1235*333d2b36SAndroid Build Coastguard Worker	} else {
1236*333d2b36SAndroid Build Coastguard Worker		// No profile (not even a default one, which is the case on some branches
1237*333d2b36SAndroid Build Coastguard Worker		// like master-art-host that don't have frameworks/base).
1238*333d2b36SAndroid Build Coastguard Worker		// Return nil and continue without profile.
1239*333d2b36SAndroid Build Coastguard Worker		return nil
1240*333d2b36SAndroid Build Coastguard Worker	}
1241*333d2b36SAndroid Build Coastguard Worker	if path := android.ExistentPathForSource(ctx, artProfile); path.Valid() {
1242*333d2b36SAndroid Build Coastguard Worker		profiles = append(profiles, path.Path())
1243*333d2b36SAndroid Build Coastguard Worker	}
1244*333d2b36SAndroid Build Coastguard Worker	if path := android.ExistentPathForSource(ctx, extraProfile); path.Valid() {
1245*333d2b36SAndroid Build Coastguard Worker		profiles = append(profiles, path.Path())
1246*333d2b36SAndroid Build Coastguard Worker	}
1247*333d2b36SAndroid Build Coastguard Worker	bootImageProfile := android.PathForModuleOut(ctx, name, "boot-image-profile.txt")
1248*333d2b36SAndroid Build Coastguard Worker	rule.Command().Text("cat").Inputs(profiles).Text(">").Output(bootImageProfile)
1249*333d2b36SAndroid Build Coastguard Worker
1250*333d2b36SAndroid Build Coastguard Worker	profile := android.PathForModuleOut(ctx, name, "boot.prof")
1251*333d2b36SAndroid Build Coastguard Worker
1252*333d2b36SAndroid Build Coastguard Worker	rule.Command().
1253*333d2b36SAndroid Build Coastguard Worker		Text(`ANDROID_LOG_TAGS="*:e"`).
1254*333d2b36SAndroid Build Coastguard Worker		Tool(globalSoong.Profman).
1255*333d2b36SAndroid Build Coastguard Worker		Flag("--output-profile-type=boot").
1256*333d2b36SAndroid Build Coastguard Worker		FlagWithInput("--create-profile-from=", bootImageProfile).
1257*333d2b36SAndroid Build Coastguard Worker		FlagForEachInput("--apk=", dexFiles).
1258*333d2b36SAndroid Build Coastguard Worker		FlagForEachArg("--dex-location=", dexLocations).
1259*333d2b36SAndroid Build Coastguard Worker		FlagWithOutput("--reference-profile-file=", profile)
1260*333d2b36SAndroid Build Coastguard Worker
1261*333d2b36SAndroid Build Coastguard Worker	rule.Build("bootJarsProfile_"+name, "profile boot jars "+name)
1262*333d2b36SAndroid Build Coastguard Worker
1263*333d2b36SAndroid Build Coastguard Worker	return profile
1264*333d2b36SAndroid Build Coastguard Worker}
1265*333d2b36SAndroid Build Coastguard Worker
1266*333d2b36SAndroid Build Coastguard Workertype profileInstallInfo struct {
1267*333d2b36SAndroid Build Coastguard Worker	// Rules which should be used in make to install the outputs.
1268*333d2b36SAndroid Build Coastguard Worker	profileInstalls android.RuleBuilderInstalls
1269*333d2b36SAndroid Build Coastguard Worker
1270*333d2b36SAndroid Build Coastguard Worker	// Path to the license metadata file for the module that built the profile.
1271*333d2b36SAndroid Build Coastguard Worker	profileLicenseMetadataFile android.OptionalPath
1272*333d2b36SAndroid Build Coastguard Worker}
1273*333d2b36SAndroid Build Coastguard Worker
1274*333d2b36SAndroid Build Coastguard Workervar profileInstallInfoProvider = blueprint.NewProvider[profileInstallInfo]()
1275*333d2b36SAndroid Build Coastguard Worker
1276*333d2b36SAndroid Build Coastguard Workerfunc bootImageProfileRule(ctx android.ModuleContext, image *bootImageConfig) (android.WritablePath, android.RuleBuilderInstalls) {
1277*333d2b36SAndroid Build Coastguard Worker	if !image.isProfileGuided() {
1278*333d2b36SAndroid Build Coastguard Worker		return nil, nil
1279*333d2b36SAndroid Build Coastguard Worker	}
1280*333d2b36SAndroid Build Coastguard Worker
1281*333d2b36SAndroid Build Coastguard Worker	profile := bootImageProfileRuleCommon(ctx, image.name, image.dexPathsDeps.Paths(), image.getAnyAndroidVariant().dexLocationsDeps)
1282*333d2b36SAndroid Build Coastguard Worker
1283*333d2b36SAndroid Build Coastguard Worker	if image == defaultBootImageConfig(ctx) && profile != nil {
1284*333d2b36SAndroid Build Coastguard Worker		rule := android.NewRuleBuilder(pctx, ctx)
1285*333d2b36SAndroid Build Coastguard Worker		rule.Install(profile, "/system/etc/boot-image.prof")
1286*333d2b36SAndroid Build Coastguard Worker		return profile, rule.Installs()
1287*333d2b36SAndroid Build Coastguard Worker	}
1288*333d2b36SAndroid Build Coastguard Worker	return profile, nil
1289*333d2b36SAndroid Build Coastguard Worker}
1290*333d2b36SAndroid Build Coastguard Worker
1291*333d2b36SAndroid Build Coastguard Worker// bootFrameworkProfileRule generates the rule to create the boot framework profile and
1292*333d2b36SAndroid Build Coastguard Worker// returns a path to the generated file.
1293*333d2b36SAndroid Build Coastguard Workerfunc bootFrameworkProfileRule(ctx android.ModuleContext, image *bootImageConfig) (android.WritablePath, android.RuleBuilderInstalls) {
1294*333d2b36SAndroid Build Coastguard Worker	globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
1295*333d2b36SAndroid Build Coastguard Worker	global := dexpreopt.GetGlobalConfig(ctx)
1296*333d2b36SAndroid Build Coastguard Worker
1297*333d2b36SAndroid Build Coastguard Worker	if global.DisableGenerateProfile || ctx.Config().UnbundledBuild() {
1298*333d2b36SAndroid Build Coastguard Worker		return nil, nil
1299*333d2b36SAndroid Build Coastguard Worker	}
1300*333d2b36SAndroid Build Coastguard Worker
1301*333d2b36SAndroid Build Coastguard Worker	defaultProfile := "frameworks/base/boot/boot-profile.txt"
1302*333d2b36SAndroid Build Coastguard Worker	bootFrameworkProfile := android.PathForSource(ctx, defaultProfile)
1303*333d2b36SAndroid Build Coastguard Worker
1304*333d2b36SAndroid Build Coastguard Worker	profile := image.dir.Join(ctx, "boot.bprof")
1305*333d2b36SAndroid Build Coastguard Worker
1306*333d2b36SAndroid Build Coastguard Worker	rule := android.NewRuleBuilder(pctx, ctx)
1307*333d2b36SAndroid Build Coastguard Worker	rule.Command().
1308*333d2b36SAndroid Build Coastguard Worker		Text(`ANDROID_LOG_TAGS="*:e"`).
1309*333d2b36SAndroid Build Coastguard Worker		Tool(globalSoong.Profman).
1310*333d2b36SAndroid Build Coastguard Worker		Flag("--output-profile-type=bprof").
1311*333d2b36SAndroid Build Coastguard Worker		FlagWithInput("--create-profile-from=", bootFrameworkProfile).
1312*333d2b36SAndroid Build Coastguard Worker		FlagForEachInput("--apk=", image.dexPathsDeps.Paths()).
1313*333d2b36SAndroid Build Coastguard Worker		FlagForEachArg("--dex-location=", image.getAnyAndroidVariant().dexLocationsDeps).
1314*333d2b36SAndroid Build Coastguard Worker		FlagWithOutput("--reference-profile-file=", profile)
1315*333d2b36SAndroid Build Coastguard Worker
1316*333d2b36SAndroid Build Coastguard Worker	rule.Install(profile, "/system/etc/boot-image.bprof")
1317*333d2b36SAndroid Build Coastguard Worker	rule.Build("bootFrameworkProfile", "profile boot framework jars")
1318*333d2b36SAndroid Build Coastguard Worker	return profile, rule.Installs()
1319*333d2b36SAndroid Build Coastguard Worker}
1320*333d2b36SAndroid Build Coastguard Worker
1321*333d2b36SAndroid Build Coastguard Workerfunc dumpOatRules(ctx android.ModuleContext, image *bootImageConfig) {
1322*333d2b36SAndroid Build Coastguard Worker	var allPhonies android.Paths
1323*333d2b36SAndroid Build Coastguard Worker	name := image.name
1324*333d2b36SAndroid Build Coastguard Worker	globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
1325*333d2b36SAndroid Build Coastguard Worker	for _, image := range image.variants {
1326*333d2b36SAndroid Build Coastguard Worker		arch := image.target.Arch.ArchType
1327*333d2b36SAndroid Build Coastguard Worker		suffix := arch.String()
1328*333d2b36SAndroid Build Coastguard Worker		// Host and target might both use x86 arch. We need to ensure the names are unique.
1329*333d2b36SAndroid Build Coastguard Worker		if image.target.Os.Class == android.Host {
1330*333d2b36SAndroid Build Coastguard Worker			suffix = "host-" + suffix
1331*333d2b36SAndroid Build Coastguard Worker		}
1332*333d2b36SAndroid Build Coastguard Worker		// Create a rule to call oatdump.
1333*333d2b36SAndroid Build Coastguard Worker		output := android.PathForOutput(ctx, name+"."+suffix+".oatdump.txt")
1334*333d2b36SAndroid Build Coastguard Worker		rule := android.NewRuleBuilder(pctx, ctx)
1335*333d2b36SAndroid Build Coastguard Worker		imageLocationsOnHost, _ := image.imageLocations()
1336*333d2b36SAndroid Build Coastguard Worker
1337*333d2b36SAndroid Build Coastguard Worker		cmd := rule.Command().
1338*333d2b36SAndroid Build Coastguard Worker			BuiltTool("oatdump").
1339*333d2b36SAndroid Build Coastguard Worker			FlagWithInputList("--runtime-arg -Xbootclasspath:", image.dexPathsDeps.Paths(), ":").
1340*333d2b36SAndroid Build Coastguard Worker			FlagWithList("--runtime-arg -Xbootclasspath-locations:", image.dexLocationsDeps, ":").
1341*333d2b36SAndroid Build Coastguard Worker			FlagWithArg("--image=", strings.Join(imageLocationsOnHost, ":")).Implicits(image.imagesDeps.Paths()).
1342*333d2b36SAndroid Build Coastguard Worker			FlagWithOutput("--output=", output).
1343*333d2b36SAndroid Build Coastguard Worker			FlagWithArg("--instruction-set=", arch.String())
1344*333d2b36SAndroid Build Coastguard Worker		if image.target.Os == android.Android {
1345*333d2b36SAndroid Build Coastguard Worker			cmd.Text("$(cat").Input(globalSoong.UffdGcFlag).Text(")")
1346*333d2b36SAndroid Build Coastguard Worker		}
1347*333d2b36SAndroid Build Coastguard Worker		rule.Build("dump-oat-"+name+"-"+suffix, "dump oat "+name+" "+arch.String())
1348*333d2b36SAndroid Build Coastguard Worker
1349*333d2b36SAndroid Build Coastguard Worker		// Create a phony rule that depends on the output file and prints the path.
1350*333d2b36SAndroid Build Coastguard Worker		phony := android.PathForPhony(ctx, "dump-oat-"+name+"-"+suffix)
1351*333d2b36SAndroid Build Coastguard Worker		rule = android.NewRuleBuilder(pctx, ctx)
1352*333d2b36SAndroid Build Coastguard Worker		rule.Command().
1353*333d2b36SAndroid Build Coastguard Worker			Implicit(output).
1354*333d2b36SAndroid Build Coastguard Worker			ImplicitOutput(phony).
1355*333d2b36SAndroid Build Coastguard Worker			Text("echo").FlagWithArg("Output in ", output.String())
1356*333d2b36SAndroid Build Coastguard Worker		rule.Build("phony-dump-oat-"+name+"-"+suffix, "dump oat "+name+" "+arch.String())
1357*333d2b36SAndroid Build Coastguard Worker
1358*333d2b36SAndroid Build Coastguard Worker		allPhonies = append(allPhonies, phony)
1359*333d2b36SAndroid Build Coastguard Worker	}
1360*333d2b36SAndroid Build Coastguard Worker
1361*333d2b36SAndroid Build Coastguard Worker	phony := android.PathForPhony(ctx, "dump-oat-"+name)
1362*333d2b36SAndroid Build Coastguard Worker	ctx.Build(pctx, android.BuildParams{
1363*333d2b36SAndroid Build Coastguard Worker		Rule:        android.Phony,
1364*333d2b36SAndroid Build Coastguard Worker		Output:      phony,
1365*333d2b36SAndroid Build Coastguard Worker		Inputs:      allPhonies,
1366*333d2b36SAndroid Build Coastguard Worker		Description: "dump-oat-" + name,
1367*333d2b36SAndroid Build Coastguard Worker	})
1368*333d2b36SAndroid Build Coastguard Worker}
1369*333d2b36SAndroid Build Coastguard Worker
1370*333d2b36SAndroid Build Coastguard Workerfunc writeGlobalConfigForMake(ctx android.SingletonContext, path android.WritablePath) {
1371*333d2b36SAndroid Build Coastguard Worker	data := dexpreopt.GetGlobalConfigRawData(ctx)
1372*333d2b36SAndroid Build Coastguard Worker
1373*333d2b36SAndroid Build Coastguard Worker	android.WriteFileRule(ctx, path, string(data))
1374*333d2b36SAndroid Build Coastguard Worker}
1375*333d2b36SAndroid Build Coastguard Worker
1376*333d2b36SAndroid Build Coastguard Worker// Define Make variables for boot image names, paths, etc. These variables are used in makefiles
1377*333d2b36SAndroid Build Coastguard Worker// (make/core/dex_preopt_libart.mk) to generate install rules that copy boot image files to the
1378*333d2b36SAndroid Build Coastguard Worker// correct output directories.
1379*333d2b36SAndroid Build Coastguard Workerfunc (d *dexpreoptBootJars) MakeVars(ctx android.MakeVarsContext) {
1380*333d2b36SAndroid Build Coastguard Worker	if d.dexpreoptConfigForMake != nil && !SkipDexpreoptBootJars(ctx) {
1381*333d2b36SAndroid Build Coastguard Worker		ctx.Strict("DEX_PREOPT_CONFIG_FOR_MAKE", d.dexpreoptConfigForMake.String())
1382*333d2b36SAndroid Build Coastguard Worker		ctx.Strict("DEX_PREOPT_SOONG_CONFIG_FOR_MAKE", android.PathForOutput(ctx, "dexpreopt_soong.config").String())
1383*333d2b36SAndroid Build Coastguard Worker	}
1384*333d2b36SAndroid Build Coastguard Worker
1385*333d2b36SAndroid Build Coastguard Worker	image := d.defaultBootImage
1386*333d2b36SAndroid Build Coastguard Worker	if image != nil && !SkipDexpreoptBootJars(ctx) {
1387*333d2b36SAndroid Build Coastguard Worker		global := dexpreopt.GetGlobalConfig(ctx)
1388*333d2b36SAndroid Build Coastguard Worker		dexPaths, dexLocations := bcpForDexpreopt(ctx, global.PreoptWithUpdatableBcp)
1389*333d2b36SAndroid Build Coastguard Worker		ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_FILES", strings.Join(dexPaths.Strings(), " "))
1390*333d2b36SAndroid Build Coastguard Worker		ctx.Strict("DEXPREOPT_BOOTCLASSPATH_DEX_LOCATIONS", strings.Join(dexLocations, " "))
1391*333d2b36SAndroid Build Coastguard Worker
1392*333d2b36SAndroid Build Coastguard Worker		// The primary ART boot image is exposed to Make for testing (gtests) and benchmarking
1393*333d2b36SAndroid Build Coastguard Worker		// (golem) purposes.
1394*333d2b36SAndroid Build Coastguard Worker		for _, current := range append(d.otherImages, image) {
1395*333d2b36SAndroid Build Coastguard Worker			for _, variant := range current.variants {
1396*333d2b36SAndroid Build Coastguard Worker				suffix := ""
1397*333d2b36SAndroid Build Coastguard Worker				if variant.target.Os.Class == android.Host {
1398*333d2b36SAndroid Build Coastguard Worker					suffix = "_host"
1399*333d2b36SAndroid Build Coastguard Worker				}
1400*333d2b36SAndroid Build Coastguard Worker				sfx := variant.name + suffix + "_" + variant.target.Arch.ArchType.String()
1401*333d2b36SAndroid Build Coastguard Worker				ctx.Strict("DEXPREOPT_IMAGE_VDEX_BUILT_INSTALLED_"+sfx, variant.vdexInstalls.String())
1402*333d2b36SAndroid Build Coastguard Worker				ctx.Strict("DEXPREOPT_IMAGE_"+sfx, variant.imagePathOnHost.String())
1403*333d2b36SAndroid Build Coastguard Worker				ctx.Strict("DEXPREOPT_IMAGE_DEPS_"+sfx, strings.Join(variant.imagesDeps.Strings(), " "))
1404*333d2b36SAndroid Build Coastguard Worker				ctx.Strict("DEXPREOPT_IMAGE_BUILT_INSTALLED_"+sfx, variant.installs.String())
1405*333d2b36SAndroid Build Coastguard Worker				ctx.Strict("DEXPREOPT_IMAGE_UNSTRIPPED_BUILT_INSTALLED_"+sfx, variant.unstrippedInstalls.String())
1406*333d2b36SAndroid Build Coastguard Worker				if variant.licenseMetadataFile.Valid() {
1407*333d2b36SAndroid Build Coastguard Worker					ctx.Strict("DEXPREOPT_IMAGE_LICENSE_METADATA_"+sfx, variant.licenseMetadataFile.String())
1408*333d2b36SAndroid Build Coastguard Worker				}
1409*333d2b36SAndroid Build Coastguard Worker			}
1410*333d2b36SAndroid Build Coastguard Worker			imageLocationsOnHost, imageLocationsOnDevice := current.getAnyAndroidVariant().imageLocations()
1411*333d2b36SAndroid Build Coastguard Worker			ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_ON_HOST"+current.name, strings.Join(imageLocationsOnHost, ":"))
1412*333d2b36SAndroid Build Coastguard Worker			ctx.Strict("DEXPREOPT_IMAGE_LOCATIONS_ON_DEVICE"+current.name, strings.Join(imageLocationsOnDevice, ":"))
1413*333d2b36SAndroid Build Coastguard Worker			ctx.Strict("DEXPREOPT_IMAGE_ZIP_"+current.name, current.zip.String())
1414*333d2b36SAndroid Build Coastguard Worker		}
1415*333d2b36SAndroid Build Coastguard Worker		ctx.Strict("DEXPREOPT_IMAGE_NAMES", strings.Join(getImageNames(), " "))
1416*333d2b36SAndroid Build Coastguard Worker	}
1417*333d2b36SAndroid Build Coastguard Worker}
1418*333d2b36SAndroid Build Coastguard Worker
1419*333d2b36SAndroid Build Coastguard Worker// Add one of the outputs in `OutputFile`
1420*333d2b36SAndroid Build Coastguard Worker// This ensures that this singleton module does not get skipped when writing out/soong/Android-*.mk
1421*333d2b36SAndroid Build Coastguard Workerfunc (d *dexpreoptBootJars) AndroidMkEntries() []android.AndroidMkEntries {
1422*333d2b36SAndroid Build Coastguard Worker	return []android.AndroidMkEntries{{
1423*333d2b36SAndroid Build Coastguard Worker		Class:      "ETC",
1424*333d2b36SAndroid Build Coastguard Worker		OutputFile: android.OptionalPathForPath(d.bootFrameworkProfile),
1425*333d2b36SAndroid Build Coastguard Worker	}}
1426*333d2b36SAndroid Build Coastguard Worker}
1427*333d2b36SAndroid Build Coastguard Worker
1428*333d2b36SAndroid Build Coastguard Worker// artBootImages is a thin wrapper around `dex_bootjars`.
1429*333d2b36SAndroid Build Coastguard Worker// it creates the installation rules for the host variant of the ART boot image.
1430*333d2b36SAndroid Build Coastguard Workertype artBootImages struct {
1431*333d2b36SAndroid Build Coastguard Worker	android.ModuleBase
1432*333d2b36SAndroid Build Coastguard Worker
1433*333d2b36SAndroid Build Coastguard Worker	// A non-empty file that will be written as `LOCAL_SOONG_INSTALLED_MODULE` in out/soong/Android-*.mk
1434*333d2b36SAndroid Build Coastguard Worker	outputFile android.OptionalPath
1435*333d2b36SAndroid Build Coastguard Worker}
1436*333d2b36SAndroid Build Coastguard Worker
1437*333d2b36SAndroid Build Coastguard Workerfunc artBootImagesFactory() android.Module {
1438*333d2b36SAndroid Build Coastguard Worker	m := &artBootImages{}
1439*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidMultiTargetsArchModule(m, android.HostSupported, android.MultilibCommon)
1440*333d2b36SAndroid Build Coastguard Worker	return m
1441*333d2b36SAndroid Build Coastguard Worker}
1442*333d2b36SAndroid Build Coastguard Worker
1443*333d2b36SAndroid Build Coastguard Workerfunc (dbj *artBootImages) DepsMutator(ctx android.BottomUpMutatorContext) {
1444*333d2b36SAndroid Build Coastguard Worker	// Create a dependency on `dex_bootjars` to access the intermediate locations of host art boot image.
1445*333d2b36SAndroid Build Coastguard Worker	ctx.AddVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), dexpreoptBootJarDepTag, "dex_bootjars")
1446*333d2b36SAndroid Build Coastguard Worker}
1447*333d2b36SAndroid Build Coastguard Worker
1448*333d2b36SAndroid Build Coastguard Workerfunc (d *artBootImages) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1449*333d2b36SAndroid Build Coastguard Worker	ctx.VisitDirectDepsWithTag(dexpreoptBootJarDepTag, func(m android.Module) {
1450*333d2b36SAndroid Build Coastguard Worker		hostInstallsInfo, ok := android.OtherModuleProvider(ctx, m, artBootImageHostInfoProvider)
1451*333d2b36SAndroid Build Coastguard Worker		if !ok {
1452*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf("Could not find information about the host variant of ART boot image")
1453*333d2b36SAndroid Build Coastguard Worker		}
1454*333d2b36SAndroid Build Coastguard Worker		installs := d.installFile(ctx, hostInstallsInfo.installs)
1455*333d2b36SAndroid Build Coastguard Worker		if len(installs) > 0 {
1456*333d2b36SAndroid Build Coastguard Worker			d.outputFile = android.OptionalPathForPath(installs[0])
1457*333d2b36SAndroid Build Coastguard Worker			// Create a phony target that can ART run-tests can depend on.
1458*333d2b36SAndroid Build Coastguard Worker			ctx.Phony(d.Name(), installs...)
1459*333d2b36SAndroid Build Coastguard Worker		} else {
1460*333d2b36SAndroid Build Coastguard Worker			// this might be true e.g. when building with `WITH_DEXPREOPT=false`
1461*333d2b36SAndroid Build Coastguard Worker			// create an empty file so that the `art_boot_images` is known to the packaging system.
1462*333d2b36SAndroid Build Coastguard Worker			d.outputFile = android.OptionalPathForPath(android.PathForModuleOut(ctx, "undefined_art_boot_images"))
1463*333d2b36SAndroid Build Coastguard Worker		}
1464*333d2b36SAndroid Build Coastguard Worker	})
1465*333d2b36SAndroid Build Coastguard Worker}
1466*333d2b36SAndroid Build Coastguard Worker
1467*333d2b36SAndroid Build Coastguard Worker// Creates an installation rule for host variant of ART boot image files.
1468*333d2b36SAndroid Build Coastguard Worker// Returns the list of install locations (out/host/linux-x86/...)
1469*333d2b36SAndroid Build Coastguard Workerfunc (d *artBootImages) installFile(ctx android.ModuleContext, ruleBuilderInstalls android.RuleBuilderInstalls) android.Paths {
1470*333d2b36SAndroid Build Coastguard Worker	var ret android.Paths
1471*333d2b36SAndroid Build Coastguard Worker	for _, ruleBuilderInstall := range ruleBuilderInstalls {
1472*333d2b36SAndroid Build Coastguard Worker		installDir := android.PathForModuleInstall(
1473*333d2b36SAndroid Build Coastguard Worker			ctx,
1474*333d2b36SAndroid Build Coastguard Worker			strings.TrimPrefix(filepath.Dir(ruleBuilderInstall.To), "/"),
1475*333d2b36SAndroid Build Coastguard Worker		)
1476*333d2b36SAndroid Build Coastguard Worker		filename := filepath.Base(ruleBuilderInstall.To)
1477*333d2b36SAndroid Build Coastguard Worker		ctx.InstallFile(
1478*333d2b36SAndroid Build Coastguard Worker			installDir,
1479*333d2b36SAndroid Build Coastguard Worker			filename,
1480*333d2b36SAndroid Build Coastguard Worker			ruleBuilderInstall.From,
1481*333d2b36SAndroid Build Coastguard Worker		)
1482*333d2b36SAndroid Build Coastguard Worker		ret = append(ret, installDir.Join(ctx, filename))
1483*333d2b36SAndroid Build Coastguard Worker	}
1484*333d2b36SAndroid Build Coastguard Worker	return ret
1485*333d2b36SAndroid Build Coastguard Worker}
1486*333d2b36SAndroid Build Coastguard Worker
1487*333d2b36SAndroid Build Coastguard Worker// Set `OutputFile` expclitly so that this module does not get elided when generating out/soong/Android-*.mk
1488*333d2b36SAndroid Build Coastguard Workerfunc (d *artBootImages) AndroidMkEntries() []android.AndroidMkEntries {
1489*333d2b36SAndroid Build Coastguard Worker	return []android.AndroidMkEntries{{
1490*333d2b36SAndroid Build Coastguard Worker		Class:      "ETC",
1491*333d2b36SAndroid Build Coastguard Worker		OutputFile: d.outputFile,
1492*333d2b36SAndroid Build Coastguard Worker	}}
1493*333d2b36SAndroid Build Coastguard Worker}
1494