blob: 3a28c5910e52e2cd29d9ed9440d72b005996349a [file] [log] [blame]
Paul Duffin3451e162021-01-20 15:16:56 +00001// Copyright (C) 2021 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Paul Duffina1d60252021-01-21 18:13:43 +000018 "fmt"
Paul Duffinea465fb2022-03-04 18:39:29 +000019 "io"
Paul Duffin7c955552021-04-19 13:23:53 +010020 "path/filepath"
Paul Duffinba6afd02019-11-19 19:44:10 +000021 "reflect"
Paul Duffin3451e162021-01-20 15:16:56 +000022 "strings"
23
24 "android/soong/android"
Paul Duffina1d60252021-01-21 18:13:43 +000025 "android/soong/dexpreopt"
Colin Crossc33e5212021-05-25 18:16:02 -070026
Paul Duffinc7ef9892021-03-23 23:21:59 +000027 "github.com/google/blueprint/proptools"
Martin Stjernholmb79c7f12021-03-17 00:26:25 +000028
Paul Duffin3451e162021-01-20 15:16:56 +000029 "github.com/google/blueprint"
30)
31
32func init() {
Paul Duffin7771eba2021-04-23 14:25:28 +010033 registerBootclasspathFragmentBuildComponents(android.InitRegistrationContext)
Paul Duffinf7f65da2021-03-10 15:00:46 +000034
Paul Duffin4e7d1c42022-05-13 13:12:19 +000035 android.RegisterSdkMemberType(BootclasspathFragmentSdkMemberType)
Paul Duffin3451e162021-01-20 15:16:56 +000036}
37
Paul Duffin7771eba2021-04-23 14:25:28 +010038func registerBootclasspathFragmentBuildComponents(ctx android.RegistrationContext) {
Paul Duffin7771eba2021-04-23 14:25:28 +010039 ctx.RegisterModuleType("bootclasspath_fragment", bootclasspathFragmentFactory)
Paul Duffinc15b9e92022-03-31 15:42:30 +010040 ctx.RegisterModuleType("bootclasspath_fragment_test", testBootclasspathFragmentFactory)
Paul Duffin7771eba2021-04-23 14:25:28 +010041 ctx.RegisterModuleType("prebuilt_bootclasspath_fragment", prebuiltBootclasspathFragmentFactory)
Paul Duffin3451e162021-01-20 15:16:56 +000042}
43
Paul Duffin4e7d1c42022-05-13 13:12:19 +000044// BootclasspathFragmentSdkMemberType is the member type used to add bootclasspath_fragments to
45// the SDK snapshot. It is exported for use by apex.
46var BootclasspathFragmentSdkMemberType = &bootclasspathFragmentMemberType{
47 SdkMemberTypeBase: android.SdkMemberTypeBase{
48 PropertyName: "bootclasspath_fragments",
49 SupportsSdk: true,
50 },
51}
52
Paul Duffin65898052021-04-20 22:47:03 +010053type bootclasspathFragmentContentDependencyTag struct {
Paul Duffinc7ef9892021-03-23 23:21:59 +000054 blueprint.BaseDependencyTag
55}
56
Paul Duffin7771eba2021-04-23 14:25:28 +010057// Avoid having to make bootclasspath_fragment content visible to the bootclasspath_fragment.
Paul Duffinc7ef9892021-03-23 23:21:59 +000058//
Paul Duffin7771eba2021-04-23 14:25:28 +010059// This is a temporary workaround to make it easier to migrate to bootclasspath_fragment modules
60// with proper dependencies.
Paul Duffinc7ef9892021-03-23 23:21:59 +000061// TODO(b/177892522): Remove this and add needed visibility.
Paul Duffin65898052021-04-20 22:47:03 +010062func (b bootclasspathFragmentContentDependencyTag) ExcludeFromVisibilityEnforcement() {
63}
64
65// The bootclasspath_fragment contents must never depend on prebuilts.
66func (b bootclasspathFragmentContentDependencyTag) ReplaceSourceWithPrebuilt() bool {
67 return false
Paul Duffinc7ef9892021-03-23 23:21:59 +000068}
69
Paul Duffine95b53a2021-04-23 20:41:23 +010070// SdkMemberType causes dependencies added with this tag to be automatically added to the sdk as if
Paul Duffina10bd3c2021-05-12 13:46:54 +010071// they were specified using java_boot_libs or java_sdk_libs.
72func (b bootclasspathFragmentContentDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType {
73 // If the module is a java_sdk_library then treat it as if it was specified in the java_sdk_libs
74 // property, otherwise treat if it was specified in the java_boot_libs property.
75 if javaSdkLibrarySdkMemberType.IsInstance(child) {
76 return javaSdkLibrarySdkMemberType
77 }
78
Paul Duffine95b53a2021-04-23 20:41:23 +010079 return javaBootLibsSdkMemberType
80}
81
82func (b bootclasspathFragmentContentDependencyTag) ExportMember() bool {
83 return true
84}
85
Colin Crossc33e5212021-05-25 18:16:02 -070086// Contents of bootclasspath fragments in an apex are considered to be directly in the apex, as if
87// they were listed in java_libs.
88func (b bootclasspathFragmentContentDependencyTag) CopyDirectlyInAnyApex() {}
89
Paul Duffinfef55002021-06-17 14:56:05 +010090// Contents of bootclasspath fragments require files from prebuilt apex files.
91func (b bootclasspathFragmentContentDependencyTag) RequiresFilesFromPrebuiltApex() {}
92
Paul Duffin7771eba2021-04-23 14:25:28 +010093// The tag used for the dependency between the bootclasspath_fragment module and its contents.
Paul Duffin65898052021-04-20 22:47:03 +010094var bootclasspathFragmentContentDepTag = bootclasspathFragmentContentDependencyTag{}
Paul Duffinc7ef9892021-03-23 23:21:59 +000095
Paul Duffin65898052021-04-20 22:47:03 +010096var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathFragmentContentDepTag
97var _ android.ReplaceSourceWithPrebuilt = bootclasspathFragmentContentDepTag
Paul Duffinf7b3d0d2021-09-02 14:29:21 +010098var _ android.SdkMemberDependencyTag = bootclasspathFragmentContentDepTag
Colin Crossc33e5212021-05-25 18:16:02 -070099var _ android.CopyDirectlyInAnyApexTag = bootclasspathFragmentContentDepTag
Paul Duffinfef55002021-06-17 14:56:05 +0100100var _ android.RequiresFilesFromPrebuiltApexTag = bootclasspathFragmentContentDepTag
Paul Duffinc7ef9892021-03-23 23:21:59 +0000101
Paul Duffin65898052021-04-20 22:47:03 +0100102func IsBootclasspathFragmentContentDepTag(tag blueprint.DependencyTag) bool {
103 return tag == bootclasspathFragmentContentDepTag
Paul Duffin4d101b62021-03-24 15:42:20 +0000104}
105
Paul Duffinc7d16442021-04-23 13:55:49 +0100106// Properties that can be different when coverage is enabled.
107type BootclasspathFragmentCoverageAffectedProperties struct {
108 // The contents of this bootclasspath_fragment, could be either java_library, or java_sdk_library.
109 //
Paul Duffin34827d42021-05-13 21:25:05 +0100110 // A java_sdk_library specified here will also be treated as if it was specified on the stub_libs
111 // property.
112 //
Paul Duffinc7d16442021-04-23 13:55:49 +0100113 // The order of this list matters as it is the order that is used in the bootclasspath.
114 Contents []string
Paul Duffin10931582021-04-25 10:13:54 +0100115
116 // The properties for specifying the API stubs provided by this fragment.
117 BootclasspathAPIProperties
Paul Duffinc7d16442021-04-23 13:55:49 +0100118}
119
Paul Duffin7771eba2021-04-23 14:25:28 +0100120type bootclasspathFragmentProperties struct {
Paul Duffin3451e162021-01-20 15:16:56 +0000121 // The name of the image this represents.
122 //
Paul Duffin82886d62021-03-24 01:34:57 +0000123 // If specified then it must be one of "art" or "boot".
Paul Duffin64be7bb2021-03-23 23:06:38 +0000124 Image_name *string
Paul Duffinc7ef9892021-03-23 23:21:59 +0000125
Paul Duffinc7d16442021-04-23 13:55:49 +0100126 // Properties whose values need to differ with and without coverage.
127 BootclasspathFragmentCoverageAffectedProperties
128 Coverage BootclasspathFragmentCoverageAffectedProperties
Paul Duffin9b381ef2021-04-08 23:01:37 +0100129
Paul Duffin31fad802021-06-18 18:14:25 +0100130 // Hidden API related properties.
Paul Duffin9b61abb2022-07-27 16:16:54 +0000131 HiddenAPIFlagFileProperties
Paul Duffin70cfdff2021-05-15 09:10:42 +0100132
Paul Duffin5cca7c42021-05-26 10:16:01 +0100133 // The list of additional stub libraries which this fragment's contents use but which are not
134 // provided by another bootclasspath_fragment.
135 //
136 // Note, "android-non-updatable" is treated specially. While no such module exists it is treated
137 // as if it was a java_sdk_library. So, when public API stubs are needed then it will be replaced
138 // with "android-non-updatable.stubs", with "androidn-non-updatable.system.stubs" when the system
139 // stubs are needed and so on.
140 Additional_stubs []string
141
Paul Duffin70cfdff2021-05-15 09:10:42 +0100142 // Properties that allow a fragment to depend on other fragments. This is needed for hidden API
143 // processing as it needs access to all the classes used by a fragment including those provided
144 // by other fragments.
145 BootclasspathFragmentsDepsProperties
Paul Duffin3451e162021-01-20 15:16:56 +0000146}
147
Paul Duffin1e9e9382022-07-27 15:55:06 +0000148type HiddenAPIPackageProperties struct {
Paul Duffin1e18e982021-08-03 15:42:27 +0100149 Hidden_api struct {
150 // Contains prefixes of a package hierarchy that is provided solely by this
151 // bootclasspath_fragment.
152 //
153 // This affects the signature patterns file that is used to select the subset of monolithic
154 // hidden API flags. See split_packages property for more details.
155 Package_prefixes []string
156
Paul Duffin846beb72022-03-15 17:45:57 +0000157 // A list of individual packages that are provided solely by this
158 // bootclasspath_fragment but which cannot be listed in package_prefixes
159 // because there are sub-packages which are provided by other modules.
160 //
161 // This should only be used for legacy packages. New packages should be
162 // covered by a package prefix.
163 Single_packages []string
164
Paul Duffin1e18e982021-08-03 15:42:27 +0100165 // The list of split packages provided by this bootclasspath_fragment.
166 //
167 // A split package is one that contains classes which are provided by multiple
168 // bootclasspath_fragment modules.
169 //
170 // This defaults to "*" - which treats all packages as being split. A module that has no split
171 // packages must specify an empty list.
172 //
173 // This affects the signature patterns file that is generated by a bootclasspath_fragment and
174 // used to select the subset of monolithic hidden API flags against which the flags generated
175 // by the bootclasspath_fragment are compared.
176 //
177 // The signature patterns file selects the subset of monolithic hidden API flags using a number
178 // of patterns, i.e.:
179 // * The qualified name (including package) of an outermost class, e.g. java/lang/Character.
180 // This selects all the flags for all the members of this class and any nested classes.
181 // * A package wildcard, e.g. java/lang/*. This selects all the flags for all the members of all
182 // the classes in this package (but not in sub-packages).
183 // * A recursive package wildcard, e.g. java/**. This selects all the flags for all the members
184 // of all the classes in this package and sub-packages.
185 //
186 // The signature patterns file is constructed as follows:
187 // * All the signatures are retrieved from the all-flags.csv file.
188 // * The member and inner class names are removed.
189 // * If a class is in a split package then that is kept, otherwise the class part is removed
190 // and replaced with a wildcard, i.e. *.
191 // * If a package matches a package prefix then the package is removed.
192 // * All the package prefixes are added with a recursive wildcard appended to each, i.e. **.
193 // * The resulting patterns are sorted.
194 //
195 // So, by default (i.e. without specifying any package_prefixes or split_packages) the signature
196 // patterns is a list of class names, because there are no package packages and all packages are
197 // assumed to be split.
198 //
199 // If any split packages are specified then only those packages are treated as split and all
200 // other packages are treated as belonging solely to the bootclasspath_fragment and so they use
201 // wildcard package patterns.
202 //
203 // So, if an empty list of split packages is specified then the signature patterns file just
204 // includes a wildcard package pattern for every package provided by the bootclasspath_fragment.
205 //
206 // If split_packages are specified and a package that is split is not listed then it could lead
207 // to build failures as it will select monolithic flags that are generated by another
208 // bootclasspath_fragment to compare against the flags provided by this fragment. The latter
209 // will obviously not contain those flags and that can cause the comparison and build to fail.
210 //
211 // If any package prefixes are specified then any matching packages are removed from the
212 // signature patterns and replaced with a single recursive package pattern.
213 //
214 // It is not strictly necessary to specify either package_prefixes or split_packages as the
215 // defaults will produce a valid set of signature patterns. However, those patterns may include
216 // implementation details, e.g. names of implementation classes or packages, which will be
217 // exported to the sdk snapshot in the signature patterns file. That is something that should be
218 // avoided where possible. Specifying package_prefixes and split_packages allows those
219 // implementation details to be excluded from the snapshot.
220 Split_packages []string
221 }
222}
223
Paul Duffin846beb72022-03-15 17:45:57 +0000224type SourceOnlyBootclasspathProperties struct {
Paul Duffin1e9e9382022-07-27 15:55:06 +0000225 HiddenAPIPackageProperties
226 Coverage HiddenAPIPackageProperties
Paul Duffin846beb72022-03-15 17:45:57 +0000227}
228
Paul Duffin7771eba2021-04-23 14:25:28 +0100229type BootclasspathFragmentModule struct {
Paul Duffin3451e162021-01-20 15:16:56 +0000230 android.ModuleBase
Paul Duffina1d60252021-01-21 18:13:43 +0000231 android.ApexModuleBase
Paul Duffinf7f65da2021-03-10 15:00:46 +0000232 android.SdkBase
satayev3db35472021-05-06 23:59:58 +0100233 ClasspathFragmentBase
234
Paul Duffinc15b9e92022-03-31 15:42:30 +0100235 // True if this fragment is for testing purposes.
236 testFragment bool
237
Paul Duffin7771eba2021-04-23 14:25:28 +0100238 properties bootclasspathFragmentProperties
braleeb0c1f0c2021-06-07 22:49:13 +0800239
Paul Duffin1e18e982021-08-03 15:42:27 +0100240 sourceOnlyProperties SourceOnlyBootclasspathProperties
241
braleeb0c1f0c2021-06-07 22:49:13 +0800242 // Collect the module directory for IDE info in java/jdeps.go.
243 modulePaths []string
Jiakai Zhang6decef92022-01-12 17:56:19 +0000244
245 // Installs for on-device boot image files. This list has entries only if the installs should be
246 // handled by Make (e.g., the boot image should be installed on the system partition, rather than
247 // in the APEX).
248 bootImageDeviceInstalls []dexpreopterInstall
Paul Duffin3451e162021-01-20 15:16:56 +0000249}
250
Paul Duffin2fef1362021-04-15 13:32:00 +0100251// commonBootclasspathFragment defines the methods that are implemented by both source and prebuilt
252// bootclasspath fragment modules.
253type commonBootclasspathFragment interface {
Paul Duffine5218812021-06-07 13:28:19 +0100254 // produceHiddenAPIOutput produces the all-flags.csv and intermediate files and encodes the flags
255 // into dex files.
Paul Duffin2fef1362021-04-15 13:32:00 +0100256 //
Paul Duffine5218812021-06-07 13:28:19 +0100257 // Returns a *HiddenAPIOutput containing the paths for the generated files. Returns nil if the
258 // module cannot contribute to hidden API processing, e.g. because it is a prebuilt module in a
259 // versioned sdk.
Paul Duffin1938dba2022-07-26 23:53:00 +0000260 produceHiddenAPIOutput(ctx android.ModuleContext, contents []android.Module, fragments []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput
Paul Duffin5466a362021-06-07 10:25:31 +0100261
Paul Duffin56afb272021-07-01 22:04:22 +0100262 // produceBootImageFiles will attempt to produce rules to create the boot image files at the paths
263 // predefined in the bootImageConfig.
Paul Duffin5466a362021-06-07 10:25:31 +0100264 //
Paul Duffin56afb272021-07-01 22:04:22 +0100265 // If it could not create the files then it will return nil. Otherwise, it will return a map from
266 // android.ArchType to the predefined paths of the boot image files.
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100267 produceBootImageFiles(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageOutputs
Paul Duffin2fef1362021-04-15 13:32:00 +0100268}
269
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100270var _ commonBootclasspathFragment = (*BootclasspathFragmentModule)(nil)
271
Paul Duffin5466a362021-06-07 10:25:31 +0100272// bootImageFilesByArch is a map from android.ArchType to the paths to the boot image files.
273//
274// The paths include the .art, .oat and .vdex files, one for each of the modules from which the boot
275// image is created.
276type bootImageFilesByArch map[android.ArchType]android.Paths
277
Paul Duffin7771eba2021-04-23 14:25:28 +0100278func bootclasspathFragmentFactory() android.Module {
279 m := &BootclasspathFragmentModule{}
Paul Duffin1e18e982021-08-03 15:42:27 +0100280 m.AddProperties(&m.properties, &m.sourceOnlyProperties)
Paul Duffina1d60252021-01-21 18:13:43 +0000281 android.InitApexModule(m)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000282 android.InitSdkAwareModule(m)
satayev3db35472021-05-06 23:59:58 +0100283 initClasspathFragment(m, BOOTCLASSPATH)
Paul Duffinb2c21732022-05-11 14:29:53 +0000284 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000285
Paul Duffinc7ef9892021-03-23 23:21:59 +0000286 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
Paul Duffinc7d16442021-04-23 13:55:49 +0100287 // If code coverage has been enabled for the framework then append the properties with
288 // coverage specific properties.
289 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
290 err := proptools.AppendProperties(&m.properties.BootclasspathFragmentCoverageAffectedProperties, &m.properties.Coverage, nil)
291 if err != nil {
292 ctx.PropertyErrorf("coverage", "error trying to append coverage specific properties: %s", err)
293 return
294 }
Paul Duffin846beb72022-03-15 17:45:57 +0000295
Paul Duffin1e9e9382022-07-27 15:55:06 +0000296 err = proptools.AppendProperties(&m.sourceOnlyProperties.HiddenAPIPackageProperties, &m.sourceOnlyProperties.Coverage, nil)
Paul Duffin846beb72022-03-15 17:45:57 +0000297 if err != nil {
298 ctx.PropertyErrorf("coverage", "error trying to append hidden api coverage specific properties: %s", err)
299 return
300 }
Paul Duffinc7d16442021-04-23 13:55:49 +0100301 }
302
303 // Initialize the contents property from the image_name.
Paul Duffin7771eba2021-04-23 14:25:28 +0100304 bootclasspathFragmentInitContentsFromImage(ctx, m)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000305 })
Paul Duffin3451e162021-01-20 15:16:56 +0000306 return m
307}
308
Paul Duffinc15b9e92022-03-31 15:42:30 +0100309func testBootclasspathFragmentFactory() android.Module {
310 m := bootclasspathFragmentFactory().(*BootclasspathFragmentModule)
311 m.testFragment = true
312 return m
313}
314
Paul Duffin7771eba2021-04-23 14:25:28 +0100315// bootclasspathFragmentInitContentsFromImage will initialize the contents property from the image_name if
316// necessary.
317func bootclasspathFragmentInitContentsFromImage(ctx android.EarlyModuleContext, m *BootclasspathFragmentModule) {
Paul Duffin82886d62021-03-24 01:34:57 +0000318 contents := m.properties.Contents
Paul Duffin8018e502021-05-21 19:28:09 +0100319 if len(contents) == 0 {
320 ctx.PropertyErrorf("contents", "required property is missing")
321 return
322 }
323
324 if m.properties.Image_name == nil {
325 // Nothing to do.
326 return
Paul Duffin82886d62021-03-24 01:34:57 +0000327 }
Paul Duffinba6afd02019-11-19 19:44:10 +0000328
Paul Duffinc7ef9892021-03-23 23:21:59 +0000329 imageName := proptools.String(m.properties.Image_name)
Paul Duffin8018e502021-05-21 19:28:09 +0100330 if imageName != "art" {
331 ctx.PropertyErrorf("image_name", `unknown image name %q, expected "art"`, imageName)
332 return
Paul Duffinba6afd02019-11-19 19:44:10 +0000333 }
Paul Duffin8018e502021-05-21 19:28:09 +0100334
335 // TODO(b/177892522): Prebuilts (versioned or not) should not use the image_name property.
336 if android.IsModuleInVersionedSdk(m) {
337 // The module is a versioned prebuilt so ignore it. This is done for a couple of reasons:
338 // 1. There is no way to use this at the moment so ignoring it is safe.
339 // 2. Attempting to initialize the contents property from the configuration will end up having
340 // the versioned prebuilt depending on the unversioned prebuilt. That will cause problems
341 // as the unversioned prebuilt could end up with an APEX variant created for the source
342 // APEX which will prevent it from having an APEX variant for the prebuilt APEX which in
343 // turn will prevent it from accessing the dex implementation jar from that which will
344 // break hidden API processing, amongst others.
345 return
346 }
347
348 // Get the configuration for the art apex jars. Do not use getImageConfig(ctx) here as this is
349 // too early in the Soong processing for that to work.
350 global := dexpreopt.GetGlobalConfig(ctx)
351 modules := global.ArtApexJars
352
353 // Make sure that the apex specified in the configuration is consistent and is one for which
354 // this boot image is available.
355 commonApex := ""
356 for i := 0; i < modules.Len(); i++ {
357 apex := modules.Apex(i)
358 jar := modules.Jar(i)
359 if apex == "platform" {
360 ctx.ModuleErrorf("ArtApexJars is invalid as it requests a platform variant of %q", jar)
361 continue
362 }
363 if !m.AvailableFor(apex) {
364 ctx.ModuleErrorf("ArtApexJars configuration incompatible with this module, ArtApexJars expects this to be in apex %q but this is only in apexes %q",
365 apex, m.ApexAvailable())
366 continue
367 }
368 if commonApex == "" {
369 commonApex = apex
370 } else if commonApex != apex {
371 ctx.ModuleErrorf("ArtApexJars configuration is inconsistent, expected all jars to be in the same apex but it specifies apex %q and %q",
372 commonApex, apex)
373 }
374 }
Paul Duffinba6afd02019-11-19 19:44:10 +0000375}
376
377// bootclasspathImageNameContentsConsistencyCheck checks that the configuration that applies to this
378// module (if any) matches the contents.
379//
380// This should be a noop as if image_name="art" then the contents will be set from the ArtApexJars
381// config by bootclasspathFragmentInitContentsFromImage so it will be guaranteed to match. However,
382// in future this will not be the case.
383func (b *BootclasspathFragmentModule) bootclasspathImageNameContentsConsistencyCheck(ctx android.BaseModuleContext) {
384 imageName := proptools.String(b.properties.Image_name)
385 if imageName == "art" {
386 // TODO(b/177892522): Prebuilts (versioned or not) should not use the image_name property.
Paul Duffin0c2e0832021-04-28 00:39:52 +0100387 if android.IsModuleInVersionedSdk(b) {
Paul Duffinba6afd02019-11-19 19:44:10 +0000388 // The module is a versioned prebuilt so ignore it. This is done for a couple of reasons:
389 // 1. There is no way to use this at the moment so ignoring it is safe.
390 // 2. Attempting to initialize the contents property from the configuration will end up having
391 // the versioned prebuilt depending on the unversioned prebuilt. That will cause problems
392 // as the unversioned prebuilt could end up with an APEX variant created for the source
393 // APEX which will prevent it from having an APEX variant for the prebuilt APEX which in
394 // turn will prevent it from accessing the dex implementation jar from that which will
395 // break hidden API processing, amongst others.
396 return
397 }
398
399 // Get the configuration for the art apex jars.
400 modules := b.getImageConfig(ctx).modules
401 configuredJars := modules.CopyOfJars()
402
403 // Skip the check if the configured jars list is empty as that is a common configuration when
404 // building targets that do not result in a system image.
405 if len(configuredJars) == 0 {
406 return
407 }
408
409 contents := b.properties.Contents
410 if !reflect.DeepEqual(configuredJars, contents) {
411 ctx.ModuleErrorf("inconsistency in specification of contents. ArtApexJars configuration specifies %#v, contents property specifies %#v",
412 configuredJars, contents)
413 }
Paul Duffinc7ef9892021-03-23 23:21:59 +0000414 }
415}
416
Paul Duffine946b322021-04-25 23:04:00 +0100417var BootclasspathFragmentApexContentInfoProvider = blueprint.NewProvider(BootclasspathFragmentApexContentInfo{})
Paul Duffin3451e162021-01-20 15:16:56 +0000418
Paul Duffine946b322021-04-25 23:04:00 +0100419// BootclasspathFragmentApexContentInfo contains the bootclasspath_fragments contributions to the
420// apex contents.
421type BootclasspathFragmentApexContentInfo struct {
Paul Duffin58e0e762021-05-21 19:27:58 +0100422 // The configured modules, will be empty if this is from a bootclasspath_fragment that does not
423 // set image_name: "art".
424 modules android.ConfiguredJarList
425
426 // Map from arch type to the boot image files.
Paul Duffin5466a362021-06-07 10:25:31 +0100427 bootImageFilesByArch bootImageFilesByArch
Paul Duffin1a8010a2021-05-15 12:39:23 +0100428
Jiakai Zhang6decef92022-01-12 17:56:19 +0000429 // True if the boot image should be installed in the APEX.
430 shouldInstallBootImageInApex bool
431
Paul Duffine5218812021-06-07 13:28:19 +0100432 // Map from the base module name (without prebuilt_ prefix) of a fragment's contents module to the
433 // hidden API encoded dex jar path.
434 contentModuleDexJarPaths bootDexJarByModule
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000435
436 // Path to the image profile file on host (or empty, if profile is not generated).
437 profilePathOnHost android.Path
438
439 // Install path of the boot image profile if it needs to be installed in the APEX, or empty if not
440 // needed.
441 profileInstallPathInApex string
Paul Duffin3451e162021-01-20 15:16:56 +0000442}
443
Paul Duffine946b322021-04-25 23:04:00 +0100444func (i BootclasspathFragmentApexContentInfo) Modules() android.ConfiguredJarList {
Paul Duffin58e0e762021-05-21 19:27:58 +0100445 return i.modules
Paul Duffin3451e162021-01-20 15:16:56 +0000446}
447
Paul Duffina1d60252021-01-21 18:13:43 +0000448// Get a map from ArchType to the associated boot image's contents for Android.
449//
450// Extension boot images only return their own files, not the files of the boot images they extend.
Paul Duffin5466a362021-06-07 10:25:31 +0100451func (i BootclasspathFragmentApexContentInfo) AndroidBootImageFilesByArchType() bootImageFilesByArch {
Paul Duffin58e0e762021-05-21 19:27:58 +0100452 return i.bootImageFilesByArch
Paul Duffina1d60252021-01-21 18:13:43 +0000453}
454
Jiakai Zhang6decef92022-01-12 17:56:19 +0000455// Return true if the boot image should be installed in the APEX.
456func (i *BootclasspathFragmentApexContentInfo) ShouldInstallBootImageInApex() bool {
457 return i.shouldInstallBootImageInApex
458}
459
Paul Duffin190fdef2021-04-26 10:33:59 +0100460// DexBootJarPathForContentModule returns the path to the dex boot jar for specified module.
461//
462// The dex boot jar is one which has had hidden API encoding performed on it.
Paul Duffin1a8010a2021-05-15 12:39:23 +0100463func (i BootclasspathFragmentApexContentInfo) DexBootJarPathForContentModule(module android.Module) (android.Path, error) {
Paul Duffine5218812021-06-07 13:28:19 +0100464 // A bootclasspath_fragment cannot use a prebuilt library so Name() will return the base name
465 // without a prebuilt_ prefix so is safe to use as the key for the contentModuleDexJarPaths.
Paul Duffin1a8010a2021-05-15 12:39:23 +0100466 name := module.Name()
467 if dexJar, ok := i.contentModuleDexJarPaths[name]; ok {
468 return dexJar, nil
469 } else {
470 return nil, fmt.Errorf("unknown bootclasspath_fragment content module %s, expected one of %s",
471 name, strings.Join(android.SortedStringKeys(i.contentModuleDexJarPaths), ", "))
472 }
Paul Duffin190fdef2021-04-26 10:33:59 +0100473}
474
Jiakai Zhang49b1eb62021-11-26 18:09:27 +0000475func (i BootclasspathFragmentApexContentInfo) ProfilePathOnHost() android.Path {
476 return i.profilePathOnHost
477}
478
479func (i BootclasspathFragmentApexContentInfo) ProfileInstallPathInApex() string {
480 return i.profileInstallPathInApex
481}
482
Paul Duffin7771eba2021-04-23 14:25:28 +0100483func (b *BootclasspathFragmentModule) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffina1d60252021-01-21 18:13:43 +0000484 tag := ctx.OtherModuleDependencyTag(dep)
Paul Duffin65898052021-04-20 22:47:03 +0100485 if IsBootclasspathFragmentContentDepTag(tag) {
Paul Duffin4d101b62021-03-24 15:42:20 +0000486 // Boot image contents are automatically added to apex.
487 return true
Paul Duffinc7ef9892021-03-23 23:21:59 +0000488 }
Bob Badour07065cd2021-02-05 19:59:11 -0800489 if android.IsMetaDependencyTag(tag) {
490 // Cross-cutting metadata dependencies are metadata.
491 return false
492 }
Paul Duffina1d60252021-01-21 18:13:43 +0000493 panic(fmt.Errorf("boot_image module %q should not have a dependency on %q via tag %s", b, dep, android.PrettyPrintTag(tag)))
494}
495
Paul Duffin7771eba2021-04-23 14:25:28 +0100496func (b *BootclasspathFragmentModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Paul Duffina1d60252021-01-21 18:13:43 +0000497 return nil
498}
499
Paul Duffin65898052021-04-20 22:47:03 +0100500// ComponentDepsMutator adds dependencies onto modules before any prebuilt modules without a
501// corresponding source module are renamed. This means that adding a dependency using a name without
502// a prebuilt_ prefix will always resolve to a source module and when using a name with that prefix
503// it will always resolve to a prebuilt module.
Paul Duffin7771eba2021-04-23 14:25:28 +0100504func (b *BootclasspathFragmentModule) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin65898052021-04-20 22:47:03 +0100505 module := ctx.Module()
Paul Duffin7771eba2021-04-23 14:25:28 +0100506 _, isSourceModule := module.(*BootclasspathFragmentModule)
Paul Duffin65898052021-04-20 22:47:03 +0100507
508 for _, name := range b.properties.Contents {
509 // A bootclasspath_fragment must depend only on other source modules, while the
510 // prebuilt_bootclasspath_fragment must only depend on other prebuilt modules.
Paul Duffina9dd6fa2021-04-22 17:25:57 +0100511 //
512 // TODO(b/177892522) - avoid special handling of jacocoagent.
513 if !isSourceModule && name != "jacocoagent" {
Paul Duffin65898052021-04-20 22:47:03 +0100514 name = android.PrebuiltNameFromSource(name)
515 }
516 ctx.AddDependency(module, bootclasspathFragmentContentDepTag, name)
517 }
518
519}
520
Paul Duffin7771eba2021-04-23 14:25:28 +0100521func (b *BootclasspathFragmentModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin10931582021-04-25 10:13:54 +0100522 // Add dependencies onto all the modules that provide the API stubs for classes on this
523 // bootclasspath fragment.
Paul Duffin31fad802021-06-18 18:14:25 +0100524 hiddenAPIAddStubLibDependencies(ctx, b.properties.apiScopeToStubLibs())
Paul Duffinc7ef9892021-03-23 23:21:59 +0000525
Paul Duffin5cca7c42021-05-26 10:16:01 +0100526 for _, additionalStubModule := range b.properties.Additional_stubs {
527 for _, apiScope := range hiddenAPISdkLibrarySupportedScopes {
528 // Add a dependency onto a possibly scope specific stub library.
529 scopeSpecificDependency := apiScope.scopeSpecificStubModule(ctx, additionalStubModule)
530 tag := hiddenAPIStubsDependencyTag{apiScope: apiScope, fromAdditionalDependency: true}
531 ctx.AddVariationDependencies(nil, tag, scopeSpecificDependency)
532 }
533 }
534
Paul Duffina1d60252021-01-21 18:13:43 +0000535 if SkipDexpreoptBootJars(ctx) {
536 return
537 }
538
539 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
540 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
541 dexpreopt.RegisterToolDeps(ctx)
542}
543
Paul Duffinf1b358c2021-05-17 07:38:47 +0100544func (b *BootclasspathFragmentModule) BootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
545 // Add dependencies on all the fragments.
546 b.properties.BootclasspathFragmentsDepsProperties.addDependenciesOntoFragments(ctx)
547}
548
Paul Duffin7771eba2021-04-23 14:25:28 +0100549func (b *BootclasspathFragmentModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffinba6afd02019-11-19 19:44:10 +0000550 // Only perform a consistency check if this module is the active module. That will prevent an
551 // unused prebuilt that was created without instrumentation from breaking an instrumentation
552 // build.
553 if isActiveModule(ctx.Module()) {
554 b.bootclasspathImageNameContentsConsistencyCheck(ctx)
555 }
556
satayev3db35472021-05-06 23:59:58 +0100557 // Generate classpaths.proto config
558 b.generateClasspathProtoBuildActions(ctx)
559
braleeb0c1f0c2021-06-07 22:49:13 +0800560 // Collect the module directory for IDE info in java/jdeps.go.
561 b.modulePaths = append(b.modulePaths, ctx.ModuleDir())
562
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100563 // Gather the bootclasspath fragment's contents.
564 var contents []android.Module
565 ctx.VisitDirectDeps(func(module android.Module) {
566 tag := ctx.OtherModuleDependencyTag(module)
567 if IsBootclasspathFragmentContentDepTag(tag) {
Paul Duffin79fd3d72021-05-14 16:14:17 +0100568 contents = append(contents, module)
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100569 }
570 })
571
Paul Duffinf1b358c2021-05-17 07:38:47 +0100572 fragments := gatherApexModulePairDepsWithTag(ctx, bootclasspathFragmentDepTag)
573
Paul Duffin1a8010a2021-05-15 12:39:23 +0100574 // Verify that the image_name specified on a bootclasspath_fragment is valid even if this is a
575 // prebuilt which will not use the image config.
576 imageConfig := b.getImageConfig(ctx)
577
Paul Duffine5218812021-06-07 13:28:19 +0100578 // A versioned prebuilt_bootclasspath_fragment cannot and does not need to perform hidden API
579 // processing. It cannot do it because it is not part of a prebuilt_apex and so has no access to
580 // the correct dex implementation jar. It does not need to because the platform-bootclasspath
581 // always references the latest bootclasspath_fragments.
582 if !android.IsModuleInVersionedSdk(ctx.Module()) {
583 // Perform hidden API processing.
584 hiddenAPIOutput := b.generateHiddenAPIBuildActions(ctx, contents, fragments)
585
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100586 var bootImageFiles bootImageOutputs
Paul Duffince918b02021-06-07 14:33:47 +0100587 if imageConfig != nil {
Paul Duffin5466a362021-06-07 10:25:31 +0100588 // Delegate the production of the boot image files to a module type specific method.
589 common := ctx.Module().(commonBootclasspathFragment)
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100590 bootImageFiles = common.produceBootImageFiles(ctx, imageConfig)
Paul Duffin5466a362021-06-07 10:25:31 +0100591
Paul Duffince918b02021-06-07 14:33:47 +0100592 if shouldCopyBootFilesToPredefinedLocations(ctx, imageConfig) {
Paul Duffin56afb272021-07-01 22:04:22 +0100593 // Zip the boot image files up, if available. This will generate the zip file in a
594 // predefined location.
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100595 buildBootImageZipInPredefinedLocation(ctx, imageConfig, bootImageFiles.byArch)
Paul Duffin56afb272021-07-01 22:04:22 +0100596
Paul Duffince918b02021-06-07 14:33:47 +0100597 // Copy the dex jars of this fragment's content modules to their predefined locations.
598 copyBootJarsToPredefinedLocations(ctx, hiddenAPIOutput.EncodedBootDexFilesByModule, imageConfig.dexPathsByModule)
599 }
Jiakai Zhang6decef92022-01-12 17:56:19 +0000600
Paul Duffine10a9f22022-10-04 16:39:18 +0100601 for _, variant := range bootImageFiles.variants {
602 archType := variant.config.target.Arch.ArchType
603 arch := archType.String()
Jiakai Zhang6decef92022-01-12 17:56:19 +0000604 for _, install := range variant.deviceInstalls {
605 // Remove the "/" prefix because the path should be relative to $ANDROID_PRODUCT_OUT.
606 installDir := strings.TrimPrefix(filepath.Dir(install.To), "/")
607 installBase := filepath.Base(install.To)
608 installPath := android.PathForModuleInPartitionInstall(ctx, "", installDir)
609
610 b.bootImageDeviceInstalls = append(b.bootImageDeviceInstalls, dexpreopterInstall{
611 name: arch + "-" + installBase,
612 moduleName: b.Name(),
613 outputPathOnHost: install.From,
614 installDirOnDevice: installPath,
615 installFileOnDevice: installBase,
616 })
617 }
618 }
Paul Duffince918b02021-06-07 14:33:47 +0100619 }
620
Paul Duffine5218812021-06-07 13:28:19 +0100621 // A prebuilt fragment cannot contribute to an apex.
622 if !android.IsModulePrebuilt(ctx.Module()) {
623 // Provide the apex content info.
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100624 b.provideApexContentInfo(ctx, imageConfig, hiddenAPIOutput, bootImageFiles)
Paul Duffine5218812021-06-07 13:28:19 +0100625 }
Paul Duffinea465fb2022-03-04 18:39:29 +0000626 } else {
627 // Versioned fragments are not needed by make.
628 b.HideFromMake()
629 }
630
631 // In order for information about bootclasspath_fragment modules to be added to module-info.json
632 // it is necessary to output an entry to Make. As bootclasspath_fragment modules are part of an
633 // APEX there can be multiple variants, including the default/platform variant and only one can
634 // be output to Make but it does not really matter which variant is output. The default/platform
635 // variant is the first (ctx.PrimaryModule()) and is usually hidden from make so this just picks
636 // the last variant (ctx.FinalModule()).
637 if ctx.Module() != ctx.FinalModule() {
638 b.HideFromMake()
Paul Duffin1a8010a2021-05-15 12:39:23 +0100639 }
640}
641
Paul Duffince918b02021-06-07 14:33:47 +0100642// shouldCopyBootFilesToPredefinedLocations determines whether the current module should copy boot
643// files, e.g. boot dex jars or boot image files, to the predefined location expected by the rest
644// of the build.
645//
646// This ensures that only a single module will copy its files to the image configuration.
647func shouldCopyBootFilesToPredefinedLocations(ctx android.ModuleContext, imageConfig *bootImageConfig) bool {
648 // Bootclasspath fragment modules that are for the platform do not produce boot related files.
649 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
650 if apexInfo.IsForPlatform() {
651 return false
652 }
653
654 // If the image configuration has no modules specified then it means that the build has been
655 // configured to build something other than a boot image, e.g. an sdk, so do not try and copy the
656 // files.
657 if imageConfig.modules.Len() == 0 {
658 return false
659 }
660
661 // Only copy files from the module that is preferred.
662 return isActiveModule(ctx.Module())
663}
664
Paul Duffin1a8010a2021-05-15 12:39:23 +0100665// provideApexContentInfo creates, initializes and stores the apex content info for use by other
666// modules.
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100667func (b *BootclasspathFragmentModule) provideApexContentInfo(ctx android.ModuleContext, imageConfig *bootImageConfig, hiddenAPIOutput *HiddenAPIOutput, bootImageFiles bootImageOutputs) {
Paul Duffin1a8010a2021-05-15 12:39:23 +0100668 // Construct the apex content info from the config.
Paul Duffine5218812021-06-07 13:28:19 +0100669 info := BootclasspathFragmentApexContentInfo{
670 // Populate the apex content info with paths to the dex jars.
671 contentModuleDexJarPaths: hiddenAPIOutput.EncodedBootDexFilesByModule,
672 }
Paul Duffin1a8010a2021-05-15 12:39:23 +0100673
Paul Duffin58e0e762021-05-21 19:27:58 +0100674 if imageConfig != nil {
675 info.modules = imageConfig.modules
Jiakai Zhang29e35e12021-12-08 10:48:35 +0000676 global := dexpreopt.GetGlobalConfig(ctx)
677 if !global.DisableGenerateProfile {
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100678 info.profilePathOnHost = bootImageFiles.profile
Jiakai Zhang29e35e12021-12-08 10:48:35 +0000679 info.profileInstallPathInApex = imageConfig.profileInstallPathInApex
680 }
Jiakai Zhang6decef92022-01-12 17:56:19 +0000681
682 info.shouldInstallBootImageInApex = imageConfig.shouldInstallInApex()
Paul Duffin1a8010a2021-05-15 12:39:23 +0100683 }
Paul Duffin3451e162021-01-20 15:16:56 +0000684
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100685 info.bootImageFilesByArch = bootImageFiles.byArch
Paul Duffin5466a362021-06-07 10:25:31 +0100686
Paul Duffin1a8010a2021-05-15 12:39:23 +0100687 // Make the apex content info available for other modules.
688 ctx.SetProvider(BootclasspathFragmentApexContentInfoProvider, info)
689}
690
satayev3db35472021-05-06 23:59:58 +0100691// generateClasspathProtoBuildActions generates all required build actions for classpath.proto config
692func (b *BootclasspathFragmentModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
693 var classpathJars []classpathJar
satayevb3090502021-06-15 17:49:10 +0100694 configuredJars := b.configuredJars(ctx)
satayev3db35472021-05-06 23:59:58 +0100695 if "art" == proptools.String(b.properties.Image_name) {
696 // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
satayevb3090502021-06-15 17:49:10 +0100697 classpathJars = configuredJarListToClasspathJars(ctx, configuredJars, BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
satayev3db35472021-05-06 23:59:58 +0100698 } else {
satayevb3090502021-06-15 17:49:10 +0100699 classpathJars = configuredJarListToClasspathJars(ctx, configuredJars, b.classpathType)
satayev3db35472021-05-06 23:59:58 +0100700 }
satayevb3090502021-06-15 17:49:10 +0100701 b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, configuredJars, classpathJars)
satayev3db35472021-05-06 23:59:58 +0100702}
703
satayev142ed272021-06-15 16:21:17 +0100704func (b *BootclasspathFragmentModule) configuredJars(ctx android.ModuleContext) android.ConfiguredJarList {
satayev8fab6f82021-05-07 00:10:33 +0100705 if "art" == proptools.String(b.properties.Image_name) {
706 return b.getImageConfig(ctx).modules
707 }
708
709 global := dexpreopt.GetGlobalConfig(ctx)
710
satayevd604b212021-07-21 14:23:52 +0100711 possibleUpdatableModules := gatherPossibleApexModuleNamesAndStems(ctx, b.properties.Contents, bootclasspathFragmentContentDepTag)
satayevd34eb0c2021-08-06 13:20:28 +0100712 jars, unknown := global.ApexBootJars.Filter(possibleUpdatableModules)
satayev1b75a3c2021-06-04 18:09:40 +0100713
714 // TODO(satayev): for apex_test we want to include all contents unconditionally to classpaths
satayevd604b212021-07-21 14:23:52 +0100715 // config. However, any test specific jars would not be present in ApexBootJars. Instead,
satayev1b75a3c2021-06-04 18:09:40 +0100716 // we should check if we are creating a config for apex_test via ApexInfo and amend the values.
717 // This is an exception to support end-to-end test for SdkExtensions, until such support exists.
Paul Duffin56c93e82021-06-29 20:04:45 +0100718 if android.InList("test_framework-sdkextensions", possibleUpdatableModules) {
satayev1b75a3c2021-06-04 18:09:40 +0100719 jars = jars.Append("com.android.sdkext", "test_framework-sdkextensions")
Samiul Islam7b385c52021-10-11 22:47:13 +0100720 } else if android.InList("test_framework-apexd", possibleUpdatableModules) {
721 jars = jars.Append("com.android.apex.test_package", "test_framework-apexd")
satayevd34eb0c2021-08-06 13:20:28 +0100722 } else if global.ApexBootJars.Len() != 0 && !android.IsModuleInVersionedSdk(ctx.Module()) {
723 unknown = android.RemoveListFromList(unknown, b.properties.Coverage.Contents)
724 _, unknown = android.RemoveFromList("core-icu4j", unknown)
Keun young Park59799962021-10-14 15:42:04 -0700725 // This module only exists in car products.
726 // So ignore it even if it is not in PRODUCT_APEX_BOOT_JARS.
727 // TODO(b/202896428): Add better way to handle this.
728 _, unknown = android.RemoveFromList("android.car-module", unknown)
satayevd34eb0c2021-08-06 13:20:28 +0100729 if len(unknown) > 0 {
730 ctx.ModuleErrorf("%s in contents must also be declared in PRODUCT_APEX_BOOT_JARS", unknown)
731 }
satayev1b75a3c2021-06-04 18:09:40 +0100732 }
733 return jars
satayev3db35472021-05-06 23:59:58 +0100734}
735
Paul Duffin7771eba2021-04-23 14:25:28 +0100736func (b *BootclasspathFragmentModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
Paul Duffin64be7bb2021-03-23 23:06:38 +0000737 // Get a map of the image configs that are supported.
738 imageConfigs := genBootImageConfigs(ctx)
739
740 // Retrieve the config for this image.
741 imageNamePtr := b.properties.Image_name
742 if imageNamePtr == nil {
743 return nil
744 }
745
746 imageName := *imageNamePtr
747 imageConfig := imageConfigs[imageName]
748 if imageConfig == nil {
749 ctx.PropertyErrorf("image_name", "Unknown image name %q, expected one of %s", imageName, strings.Join(android.SortedStringKeys(imageConfigs), ", "))
750 return nil
751 }
752 return imageConfig
753}
754
Paul Duffin9b381ef2021-04-08 23:01:37 +0100755// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffine5218812021-06-07 13:28:19 +0100756func (b *BootclasspathFragmentModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, contents []android.Module, fragments []android.Module) *HiddenAPIOutput {
Paul Duffin10931582021-04-25 10:13:54 +0100757
Paul Duffin1352f7c2021-05-21 22:18:49 +0100758 // Create hidden API input structure.
Paul Duffinf1b358c2021-05-17 07:38:47 +0100759 input := b.createHiddenAPIFlagInput(ctx, contents, fragments)
Paul Duffin10931582021-04-25 10:13:54 +0100760
Paul Duffinda286f42021-06-29 11:59:23 +0100761 // Delegate the production of the hidden API all-flags.csv file to a module type specific method.
762 common := ctx.Module().(commonBootclasspathFragment)
Paul Duffin1938dba2022-07-26 23:53:00 +0000763 output := common.produceHiddenAPIOutput(ctx, contents, fragments, input)
Paul Duffin62370922021-05-23 16:55:37 +0100764
Paul Duffin67b9d612021-07-21 17:38:47 +0100765 // If the source or prebuilts module does not provide a signature patterns file then generate one
766 // from the flags.
767 // TODO(b/192868581): Remove once the source and prebuilts provide a signature patterns file of
768 // their own.
769 if output.SignaturePatternsPath == nil {
Paul Duffin846beb72022-03-15 17:45:57 +0000770 output.SignaturePatternsPath = buildRuleSignaturePatternsFile(
Paul Duffin1938dba2022-07-26 23:53:00 +0000771 ctx, output.AllFlagsPath, []string{"*"}, nil, nil, "")
Paul Duffin67b9d612021-07-21 17:38:47 +0100772 }
773
Paul Duffin62370922021-05-23 16:55:37 +0100774 // Initialize a HiddenAPIInfo structure.
Paul Duffinaf99afa2021-05-21 22:18:56 +0100775 hiddenAPIInfo := HiddenAPIInfo{
Paul Duffin62370922021-05-23 16:55:37 +0100776 // The monolithic hidden API processing needs access to the flag files that override the default
777 // flags from all the fragments whether or not they actually perform their own hidden API flag
778 // generation. That is because the monolithic hidden API processing uses those flag files to
779 // perform its own flag generation.
Paul Duffin1352f7c2021-05-21 22:18:49 +0100780 FlagFilesByCategory: input.FlagFilesByCategory,
Paul Duffin18cf1972021-05-21 22:46:59 +0100781
Paul Duffinf1b358c2021-05-17 07:38:47 +0100782 // Other bootclasspath_fragments that depend on this need the transitive set of stub dex jars
783 // from this to resolve any references from their code to classes provided by this fragment
784 // and the fragments this depends upon.
Paul Duffin31fad802021-06-18 18:14:25 +0100785 TransitiveStubDexJarsByScope: input.transitiveStubDexJarsByScope(),
Paul Duffin62370922021-05-23 16:55:37 +0100786 }
Paul Duffin2fef1362021-04-15 13:32:00 +0100787
Paul Duffine5218812021-06-07 13:28:19 +0100788 // The monolithic hidden API processing also needs access to all the output files produced by
789 // hidden API processing of this fragment.
Paul Duffin54e41972021-07-19 13:23:40 +0100790 hiddenAPIInfo.HiddenAPIFlagOutput = output.HiddenAPIFlagOutput
Paul Duffin62370922021-05-23 16:55:37 +0100791
792 // Provide it for use by other modules.
Paul Duffinaf99afa2021-05-21 22:18:56 +0100793 ctx.SetProvider(HiddenAPIInfoProvider, hiddenAPIInfo)
Paul Duffin54c98f52021-05-15 08:54:30 +0100794
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100795 return output
Paul Duffin2fef1362021-04-15 13:32:00 +0100796}
797
Paul Duffine5218812021-06-07 13:28:19 +0100798// retrieveLegacyEncodedBootDexFiles attempts to retrieve the legacy encoded boot dex jar files.
799func retrieveLegacyEncodedBootDexFiles(ctx android.ModuleContext, contents []android.Module) bootDexJarByModule {
800 // If the current bootclasspath_fragment is the active module or a source module then retrieve the
801 // encoded dex files, otherwise return an empty map.
802 //
803 // An inactive (i.e. not preferred) bootclasspath_fragment needs to retrieve the encoded dex jars
804 // as they are still needed by an apex. An inactive prebuilt_bootclasspath_fragment does not need
805 // to do so and may not yet have access to dex boot jars from a prebuilt_apex/apex_set.
806 if isActiveModule(ctx.Module()) || !android.IsModulePrebuilt(ctx.Module()) {
807 return extractEncodedDexJarsFromModules(ctx, contents)
808 } else {
809 return nil
810 }
811}
812
Paul Duffin1352f7c2021-05-21 22:18:49 +0100813// createHiddenAPIFlagInput creates a HiddenAPIFlagInput struct and initializes it with information derived
814// from the properties on this module and its dependencies.
Paul Duffinf1b358c2021-05-17 07:38:47 +0100815func (b *BootclasspathFragmentModule) createHiddenAPIFlagInput(ctx android.ModuleContext, contents []android.Module, fragments []android.Module) HiddenAPIFlagInput {
Paul Duffinf1b358c2021-05-17 07:38:47 +0100816 // Merge the HiddenAPIInfo from all the fragment dependencies.
817 dependencyHiddenApiInfo := newHiddenAPIInfo()
818 dependencyHiddenApiInfo.mergeFromFragmentDeps(ctx, fragments)
819
820 // Create hidden API flag input structure.
Paul Duffin1352f7c2021-05-21 22:18:49 +0100821 input := newHiddenAPIFlagInput()
822
823 // Update the input structure with information obtained from the stub libraries.
824 input.gatherStubLibInfo(ctx, contents)
825
826 // Populate with flag file paths from the properties.
Paul Duffin9b61abb2022-07-27 16:16:54 +0000827 input.extractFlagFilesFromProperties(ctx, &b.properties.HiddenAPIFlagFileProperties)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100828
Paul Duffin1e9e9382022-07-27 15:55:06 +0000829 // Populate with package rules from the properties.
830 input.extractPackageRulesFromProperties(&b.sourceOnlyProperties.HiddenAPIPackageProperties)
831
Paul Duffin3f1ae0b2022-07-27 16:27:42 +0000832 input.gatherPropertyInfo(ctx, contents)
833
Paul Duffin5cca7c42021-05-26 10:16:01 +0100834 // Add the stub dex jars from this module's fragment dependencies.
Paul Duffin280a31a2021-06-27 20:28:29 +0100835 input.DependencyStubDexJarsByScope.addStubDexJarsByModule(dependencyHiddenApiInfo.TransitiveStubDexJarsByScope)
Paul Duffinf1b358c2021-05-17 07:38:47 +0100836
Paul Duffin1352f7c2021-05-21 22:18:49 +0100837 return input
838}
839
Paul Duffinc15b9e92022-03-31 15:42:30 +0100840// isTestFragment returns true if the current module is a test bootclasspath_fragment.
841func (b *BootclasspathFragmentModule) isTestFragment() bool {
Paul Duffind0fe1302022-09-14 17:04:51 +0000842 return b.testFragment
Paul Duffinc15b9e92022-03-31 15:42:30 +0100843}
844
Paul Duffinaf705182022-09-14 11:47:34 +0100845// generateHiddenApiFlagRules generates rules to generate hidden API flags and compute the signature
846// patterns file.
847func (b *BootclasspathFragmentModule) generateHiddenApiFlagRules(ctx android.ModuleContext, contents []android.Module, input HiddenAPIFlagInput, bootDexInfoByModule bootDexInfoByModule, suffix string) HiddenAPIFlagOutput {
Paul Duffin1352f7c2021-05-21 22:18:49 +0100848 // Generate the rules to create the hidden API flags and update the supplied hiddenAPIInfo with the
Paul Duffin2fef1362021-04-15 13:32:00 +0100849 // paths to the created files.
Paul Duffin1938dba2022-07-26 23:53:00 +0000850 flagOutput := hiddenAPIFlagRulesForBootclasspathFragment(ctx, bootDexInfoByModule, contents, input, suffix)
Paul Duffin1e18e982021-08-03 15:42:27 +0100851
852 // If the module specifies split_packages or package_prefixes then use those to generate the
853 // signature patterns.
Paul Duffin1e9e9382022-07-27 15:55:06 +0000854 splitPackages := input.SplitPackages
855 packagePrefixes := input.PackagePrefixes
856 singlePackages := input.SinglePackages
Paul Duffin846beb72022-03-15 17:45:57 +0000857 if splitPackages != nil || packagePrefixes != nil || singlePackages != nil {
Paul Duffinaf705182022-09-14 11:47:34 +0100858 flagOutput.SignaturePatternsPath = buildRuleSignaturePatternsFile(
Paul Duffin1938dba2022-07-26 23:53:00 +0000859 ctx, flagOutput.AllFlagsPath, splitPackages, packagePrefixes, singlePackages, suffix)
Paul Duffin9fd56472022-03-31 15:42:30 +0100860 } else if !b.isTestFragment() {
861 ctx.ModuleErrorf(`Must specify at least one of the split_packages, package_prefixes and single_packages properties
862 If this is a new bootclasspath_fragment or you are unsure what to do add the
863 the following to the bootclasspath_fragment:
864 hidden_api: {split_packages: ["*"]},
865 and then run the following:
866 m analyze_bcpf && analyze_bcpf --bcpf %q
867 it will analyze the bootclasspath_fragment and provide hints as to what you
868 should specify here. If you are happy with its suggestions then you can add
869 the --fix option and it will fix them for you.`, b.BaseModuleName())
Paul Duffin1e18e982021-08-03 15:42:27 +0100870 }
Paul Duffinaf705182022-09-14 11:47:34 +0100871 return flagOutput
872}
873
874// produceHiddenAPIOutput produces the hidden API all-flags.csv file (and supporting files)
875// for the fragment as well as encoding the flags in the boot dex jars.
Paul Duffin1938dba2022-07-26 23:53:00 +0000876func (b *BootclasspathFragmentModule) produceHiddenAPIOutput(ctx android.ModuleContext, contents []android.Module, fragments []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput {
Paul Duffinaf705182022-09-14 11:47:34 +0100877 // Gather information about the boot dex files for the boot libraries provided by this fragment.
878 bootDexInfoByModule := extractBootDexInfoFromModules(ctx, contents)
879
880 // Generate the flag file needed to encode into the dex files.
881 flagOutput := b.generateHiddenApiFlagRules(ctx, contents, input, bootDexInfoByModule, "")
882
883 // Encode those flags into the dex files of the contents of this fragment.
884 encodedBootDexFilesByModule := hiddenAPIEncodeRulesForBootclasspathFragment(ctx, bootDexInfoByModule, flagOutput.AllFlagsPath)
885
886 // Store that information for return for use by other rules.
887 output := &HiddenAPIOutput{
888 HiddenAPIFlagOutput: flagOutput,
889 EncodedBootDexFilesByModule: encodedBootDexFilesByModule,
890 }
Paul Duffin1e18e982021-08-03 15:42:27 +0100891
Paul Duffin1938dba2022-07-26 23:53:00 +0000892 // Get the ApiLevel associated with SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE, defaulting to current
893 // if not set.
894 config := ctx.Config()
895 targetApiLevel := android.ApiLevelOrPanic(ctx,
896 config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE", "current"))
897
898 // Filter the contents list to remove any modules that do not support the target build release.
899 // The current build release supports all the modules.
900 contentsForSdkSnapshot := []android.Module{}
901 for _, module := range contents {
902 // If the module has a min_sdk_version that is higher than the target build release then it will
903 // not work on the target build release and so must not be included in the sdk snapshot.
904 minApiLevel := android.MinApiLevelForSdkSnapshot(ctx, module)
905 if minApiLevel.GreaterThan(targetApiLevel) {
906 continue
907 }
908
909 contentsForSdkSnapshot = append(contentsForSdkSnapshot, module)
910 }
911
912 var flagFilesByCategory FlagFilesByCategory
913 if len(contentsForSdkSnapshot) != len(contents) {
914 // The sdk snapshot has different contents to the runtime fragment so it is not possible to
915 // reuse the hidden API information generated for the fragment. So, recompute that information
916 // for the sdk snapshot.
917 filteredInput := b.createHiddenAPIFlagInput(ctx, contentsForSdkSnapshot, fragments)
918
919 // Gather information about the boot dex files for the boot libraries provided by this fragment.
920 filteredBootDexInfoByModule := extractBootDexInfoFromModules(ctx, contentsForSdkSnapshot)
921 flagOutput = b.generateHiddenApiFlagRules(ctx, contentsForSdkSnapshot, filteredInput, filteredBootDexInfoByModule, "-for-sdk-snapshot")
922 flagFilesByCategory = filteredInput.FlagFilesByCategory
923 } else {
924 // The sdk snapshot has the same contents as the runtime fragment so reuse that information.
925 flagFilesByCategory = input.FlagFilesByCategory
926 }
Paul Duffin887efdd2022-09-14 16:37:12 +0100927
928 // Make the information available for the sdk snapshot.
929 ctx.SetProvider(HiddenAPIInfoForSdkProvider, HiddenAPIInfoForSdk{
930 FlagFilesByCategory: flagFilesByCategory,
931 HiddenAPIFlagOutput: flagOutput,
932 })
933
Paul Duffin1e18e982021-08-03 15:42:27 +0100934 return output
Paul Duffin9b381ef2021-04-08 23:01:37 +0100935}
936
Paul Duffin5466a362021-06-07 10:25:31 +0100937// produceBootImageFiles builds the boot image files from the source if it is required.
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100938func (b *BootclasspathFragmentModule) produceBootImageFiles(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageOutputs {
Paul Duffin5466a362021-06-07 10:25:31 +0100939 if SkipDexpreoptBootJars(ctx) {
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100940 return bootImageOutputs{}
Paul Duffin5466a362021-06-07 10:25:31 +0100941 }
942
Paul Duffin5466a362021-06-07 10:25:31 +0100943 // Only generate the boot image if the configuration does not skip it.
Paul Duffin56afb272021-07-01 22:04:22 +0100944 return b.generateBootImageBuildActions(ctx, imageConfig)
Paul Duffin5466a362021-06-07 10:25:31 +0100945}
946
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100947// generateBootImageBuildActions generates ninja rules to create the boot image if required for this
948// module.
Paul Duffin58e0e762021-05-21 19:27:58 +0100949//
Paul Duffin56afb272021-07-01 22:04:22 +0100950// If it could not create the files then it will return nil. Otherwise, it will return a map from
951// android.ArchType to the predefined paths of the boot image files.
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100952func (b *BootclasspathFragmentModule) generateBootImageBuildActions(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageOutputs {
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100953 global := dexpreopt.GetGlobalConfig(ctx)
954 if !shouldBuildBootImages(ctx.Config(), global) {
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100955 return bootImageOutputs{}
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100956 }
957
958 // Bootclasspath fragment modules that are for the platform do not produce a boot image.
959 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
960 if apexInfo.IsForPlatform() {
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100961 return bootImageOutputs{}
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100962 }
963
964 // Bootclasspath fragment modules that are versioned do not produce a boot image.
965 if android.IsModuleInVersionedSdk(ctx.Module()) {
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100966 return bootImageOutputs{}
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100967 }
968
Paul Duffin2fc82ad2021-04-29 23:36:12 +0100969 // Build a profile for the image config and then use that to build the boot image.
970 profile := bootImageProfileRule(ctx, imageConfig)
Paul Duffina56be7d2021-07-02 13:00:43 +0100971
972 // Build boot image files for the host variants.
973 buildBootImageVariantsForBuildOs(ctx, imageConfig, profile)
974
975 // Build boot image files for the android variants.
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100976 bootImageFiles := buildBootImageVariantsForAndroidOs(ctx, imageConfig, profile)
Paul Duffina56be7d2021-07-02 13:00:43 +0100977
978 // Return the boot image files for the android variants for inclusion in an APEX and to be zipped
979 // up for the dist.
Paul Duffin9f6ac0b2022-10-04 15:36:44 +0100980 return bootImageFiles
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100981}
982
Jiakai Zhang6decef92022-01-12 17:56:19 +0000983func (b *BootclasspathFragmentModule) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinea465fb2022-03-04 18:39:29 +0000984 // Use the generated classpath proto as the output.
985 outputFile := b.outputFilepath
986 // Create a fake entry that will cause this to be added to the module-info.json file.
987 entriesList := []android.AndroidMkEntries{{
988 Class: "FAKE",
989 OutputFile: android.OptionalPathForPath(outputFile),
990 Include: "$(BUILD_PHONY_PACKAGE)",
991 ExtraFooters: []android.AndroidMkExtraFootersFunc{
992 func(w io.Writer, name, prefix, moduleDir string) {
993 // Allow the bootclasspath_fragment to be built by simply passing its name on the command
994 // line.
995 fmt.Fprintln(w, ".PHONY:", b.Name())
996 fmt.Fprintln(w, b.Name()+":", outputFile.String())
997 },
998 },
999 }}
Jiakai Zhang6decef92022-01-12 17:56:19 +00001000 for _, install := range b.bootImageDeviceInstalls {
1001 entriesList = append(entriesList, install.ToMakeEntries())
1002 }
1003 return entriesList
1004}
1005
1006// Returns the names of all Make modules that handle the installation of the boot image.
1007func (b *BootclasspathFragmentModule) BootImageDeviceInstallMakeModules() []string {
1008 var makeModules []string
1009 for _, install := range b.bootImageDeviceInstalls {
1010 makeModules = append(makeModules, install.FullModuleName())
1011 }
1012 return makeModules
1013}
1014
braleeb0c1f0c2021-06-07 22:49:13 +08001015// Collect information for opening IDE project files in java/jdeps.go.
1016func (b *BootclasspathFragmentModule) IDEInfo(dpInfo *android.IdeInfo) {
1017 dpInfo.Deps = append(dpInfo.Deps, b.properties.Contents...)
1018 dpInfo.Paths = append(dpInfo.Paths, b.modulePaths...)
1019}
1020
Paul Duffin7771eba2021-04-23 14:25:28 +01001021type bootclasspathFragmentMemberType struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +00001022 android.SdkMemberTypeBase
1023}
1024
Paul Duffin296701e2021-07-14 10:29:36 +01001025func (b *bootclasspathFragmentMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
1026 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffinf7f65da2021-03-10 15:00:46 +00001027}
1028
Paul Duffin7771eba2021-04-23 14:25:28 +01001029func (b *bootclasspathFragmentMemberType) IsInstance(module android.Module) bool {
1030 _, ok := module.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +00001031 return ok
1032}
1033
Paul Duffin7771eba2021-04-23 14:25:28 +01001034func (b *bootclasspathFragmentMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
Paul Duffin4b64ba02021-03-29 11:02:53 +01001035 if b.PropertyName == "boot_images" {
1036 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_boot_image")
1037 } else {
1038 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_bootclasspath_fragment")
1039 }
Paul Duffinf7f65da2021-03-10 15:00:46 +00001040}
1041
Paul Duffin7771eba2021-04-23 14:25:28 +01001042func (b *bootclasspathFragmentMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1043 return &bootclasspathFragmentSdkMemberProperties{}
Paul Duffinf7f65da2021-03-10 15:00:46 +00001044}
1045
Paul Duffin7771eba2021-04-23 14:25:28 +01001046type bootclasspathFragmentSdkMemberProperties struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +00001047 android.SdkMemberPropertiesBase
1048
Paul Duffina57835e2021-04-19 13:23:06 +01001049 // The image name
Paul Duffin64be7bb2021-03-23 23:06:38 +00001050 Image_name *string
Paul Duffina57835e2021-04-19 13:23:06 +01001051
1052 // Contents of the bootclasspath fragment
1053 Contents []string
Paul Duffin7c955552021-04-19 13:23:53 +01001054
Paul Duffin895c7142021-04-25 13:40:15 +01001055 // Stub_libs properties.
1056 Stub_libs []string
1057 Core_platform_stub_libs []string
1058
Paul Duffin51227d82021-05-18 12:54:27 +01001059 // Fragment properties
1060 Fragments []ApexVariantReference
1061
Paul Duffin7c955552021-04-19 13:23:53 +01001062 // Flag files by *hiddenAPIFlagFileCategory
Paul Duffin438eb572021-05-21 16:58:23 +01001063 Flag_files_by_category FlagFilesByCategory
Paul Duffin2fef1362021-04-15 13:32:00 +01001064
Paul Duffin2fef1362021-04-15 13:32:00 +01001065 // The path to the generated annotation-flags.csv file.
1066 Annotation_flags_path android.OptionalPath
1067
1068 // The path to the generated metadata.csv file.
1069 Metadata_path android.OptionalPath
1070
1071 // The path to the generated index.csv file.
1072 Index_path android.OptionalPath
1073
Paul Duffin67b9d612021-07-21 17:38:47 +01001074 // The path to the generated stub-flags.csv file.
Paul Duffin191be3a2021-08-10 16:14:16 +01001075 Stub_flags_path android.OptionalPath `supported_build_releases:"S"`
Paul Duffin67b9d612021-07-21 17:38:47 +01001076
Paul Duffin2fef1362021-04-15 13:32:00 +01001077 // The path to the generated all-flags.csv file.
Paul Duffin191be3a2021-08-10 16:14:16 +01001078 All_flags_path android.OptionalPath `supported_build_releases:"S"`
1079
1080 // The path to the generated signature-patterns.csv file.
Paul Duffine7babdb2022-02-10 13:06:54 +00001081 Signature_patterns_path android.OptionalPath `supported_build_releases:"Tiramisu+"`
Paul Duffin191be3a2021-08-10 16:14:16 +01001082
1083 // The path to the generated filtered-stub-flags.csv file.
Paul Duffine7babdb2022-02-10 13:06:54 +00001084 Filtered_stub_flags_path android.OptionalPath `supported_build_releases:"Tiramisu+"`
Paul Duffin191be3a2021-08-10 16:14:16 +01001085
1086 // The path to the generated filtered-flags.csv file.
Paul Duffine7babdb2022-02-10 13:06:54 +00001087 Filtered_flags_path android.OptionalPath `supported_build_releases:"Tiramisu+"`
Paul Duffin2fef1362021-04-15 13:32:00 +01001088}
1089
Paul Duffin7771eba2021-04-23 14:25:28 +01001090func (b *bootclasspathFragmentSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1091 module := variant.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +00001092
1093 b.Image_name = module.properties.Image_name
Paul Duffin2dc665b2021-04-23 16:58:51 +01001094 b.Contents = module.properties.Contents
Paul Duffin7c955552021-04-19 13:23:53 +01001095
Paul Duffinaf99afa2021-05-21 22:18:56 +01001096 // Get the hidden API information from the module.
Paul Duffin7c955552021-04-19 13:23:53 +01001097 mctx := ctx.SdkModuleContext()
Paul Duffin887efdd2022-09-14 16:37:12 +01001098 hiddenAPIInfo := mctx.OtherModuleProvider(module, HiddenAPIInfoForSdkProvider).(HiddenAPIInfoForSdk)
Paul Duffinaf99afa2021-05-21 22:18:56 +01001099 b.Flag_files_by_category = hiddenAPIInfo.FlagFilesByCategory
Paul Duffin895c7142021-04-25 13:40:15 +01001100
Paul Duffin2fef1362021-04-15 13:32:00 +01001101 // Copy all the generated file paths.
Paul Duffinaf99afa2021-05-21 22:18:56 +01001102 b.Annotation_flags_path = android.OptionalPathForPath(hiddenAPIInfo.AnnotationFlagsPath)
1103 b.Metadata_path = android.OptionalPathForPath(hiddenAPIInfo.MetadataPath)
1104 b.Index_path = android.OptionalPathForPath(hiddenAPIInfo.IndexPath)
Paul Duffin67b9d612021-07-21 17:38:47 +01001105
1106 b.Stub_flags_path = android.OptionalPathForPath(hiddenAPIInfo.StubFlagsPath)
Paul Duffinaf99afa2021-05-21 22:18:56 +01001107 b.All_flags_path = android.OptionalPathForPath(hiddenAPIInfo.AllFlagsPath)
Paul Duffin2fef1362021-04-15 13:32:00 +01001108
Paul Duffin191be3a2021-08-10 16:14:16 +01001109 b.Signature_patterns_path = android.OptionalPathForPath(hiddenAPIInfo.SignaturePatternsPath)
1110 b.Filtered_stub_flags_path = android.OptionalPathForPath(hiddenAPIInfo.FilteredStubFlagsPath)
1111 b.Filtered_flags_path = android.OptionalPathForPath(hiddenAPIInfo.FilteredFlagsPath)
1112
Paul Duffin895c7142021-04-25 13:40:15 +01001113 // Copy stub_libs properties.
1114 b.Stub_libs = module.properties.Api.Stub_libs
1115 b.Core_platform_stub_libs = module.properties.Core_platform_api.Stub_libs
Paul Duffin51227d82021-05-18 12:54:27 +01001116
1117 // Copy fragment properties.
1118 b.Fragments = module.properties.Fragments
Paul Duffinf7f65da2021-03-10 15:00:46 +00001119}
1120
Paul Duffin7771eba2021-04-23 14:25:28 +01001121func (b *bootclasspathFragmentSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffin64be7bb2021-03-23 23:06:38 +00001122 if b.Image_name != nil {
1123 propertySet.AddProperty("image_name", *b.Image_name)
Paul Duffinf7f65da2021-03-10 15:00:46 +00001124 }
Paul Duffina57835e2021-04-19 13:23:06 +01001125
Paul Duffin895c7142021-04-25 13:40:15 +01001126 builder := ctx.SnapshotBuilder()
1127 requiredMemberDependency := builder.SdkMemberReferencePropertyTag(true)
1128
Paul Duffina57835e2021-04-19 13:23:06 +01001129 if len(b.Contents) > 0 {
Paul Duffin895c7142021-04-25 13:40:15 +01001130 propertySet.AddPropertyWithTag("contents", b.Contents, requiredMemberDependency)
Paul Duffina57835e2021-04-19 13:23:06 +01001131 }
Paul Duffin7c955552021-04-19 13:23:53 +01001132
Paul Duffin895c7142021-04-25 13:40:15 +01001133 if len(b.Stub_libs) > 0 {
1134 apiPropertySet := propertySet.AddPropertySet("api")
1135 apiPropertySet.AddPropertyWithTag("stub_libs", b.Stub_libs, requiredMemberDependency)
1136 }
1137 if len(b.Core_platform_stub_libs) > 0 {
1138 corePlatformApiPropertySet := propertySet.AddPropertySet("core_platform_api")
1139 corePlatformApiPropertySet.AddPropertyWithTag("stub_libs", b.Core_platform_stub_libs, requiredMemberDependency)
1140 }
Paul Duffin51227d82021-05-18 12:54:27 +01001141 if len(b.Fragments) > 0 {
1142 propertySet.AddProperty("fragments", b.Fragments)
1143 }
Paul Duffin895c7142021-04-25 13:40:15 +01001144
Paul Duffin2fef1362021-04-15 13:32:00 +01001145 hiddenAPISet := propertySet.AddPropertySet("hidden_api")
1146 hiddenAPIDir := "hiddenapi"
1147
1148 // Copy manually curated flag files specified on the bootclasspath_fragment.
Paul Duffin7c955552021-04-19 13:23:53 +01001149 if b.Flag_files_by_category != nil {
Paul Duffin524c82c2021-06-09 14:39:28 +01001150 for _, category := range HiddenAPIFlagFileCategories {
Paul Duffin7c955552021-04-19 13:23:53 +01001151 paths := b.Flag_files_by_category[category]
1152 if len(paths) > 0 {
1153 dests := []string{}
1154 for _, p := range paths {
Paul Duffin2fef1362021-04-15 13:32:00 +01001155 dest := filepath.Join(hiddenAPIDir, p.Base())
Paul Duffin7c955552021-04-19 13:23:53 +01001156 builder.CopyToSnapshot(p, dest)
1157 dests = append(dests, dest)
1158 }
Paul Duffin524c82c2021-06-09 14:39:28 +01001159 hiddenAPISet.AddProperty(category.PropertyName, dests)
Paul Duffin7c955552021-04-19 13:23:53 +01001160 }
1161 }
1162 }
Paul Duffin2fef1362021-04-15 13:32:00 +01001163
1164 copyOptionalPath := func(path android.OptionalPath, property string) {
1165 if path.Valid() {
1166 p := path.Path()
1167 dest := filepath.Join(hiddenAPIDir, p.Base())
1168 builder.CopyToSnapshot(p, dest)
1169 hiddenAPISet.AddProperty(property, dest)
1170 }
1171 }
1172
1173 // Copy all the generated files, if available.
Paul Duffin2fef1362021-04-15 13:32:00 +01001174 copyOptionalPath(b.Annotation_flags_path, "annotation_flags")
1175 copyOptionalPath(b.Metadata_path, "metadata")
1176 copyOptionalPath(b.Index_path, "index")
Paul Duffin191be3a2021-08-10 16:14:16 +01001177
Paul Duffin67b9d612021-07-21 17:38:47 +01001178 copyOptionalPath(b.Stub_flags_path, "stub_flags")
Paul Duffin2fef1362021-04-15 13:32:00 +01001179 copyOptionalPath(b.All_flags_path, "all_flags")
Paul Duffin191be3a2021-08-10 16:14:16 +01001180
1181 copyOptionalPath(b.Signature_patterns_path, "signature_patterns")
1182 copyOptionalPath(b.Filtered_stub_flags_path, "filtered_stub_flags")
1183 copyOptionalPath(b.Filtered_flags_path, "filtered_flags")
Paul Duffinf7f65da2021-03-10 15:00:46 +00001184}
1185
Paul Duffin7771eba2021-04-23 14:25:28 +01001186var _ android.SdkMemberType = (*bootclasspathFragmentMemberType)(nil)
Paul Duffinf7f65da2021-03-10 15:00:46 +00001187
Paul Duffin2fef1362021-04-15 13:32:00 +01001188// prebuiltBootclasspathFragmentProperties contains additional prebuilt_bootclasspath_fragment
1189// specific properties.
1190type prebuiltBootclasspathFragmentProperties struct {
1191 Hidden_api struct {
Paul Duffin2fef1362021-04-15 13:32:00 +01001192 // The path to the annotation-flags.csv file created by the bootclasspath_fragment.
1193 Annotation_flags *string `android:"path"`
1194
1195 // The path to the metadata.csv file created by the bootclasspath_fragment.
1196 Metadata *string `android:"path"`
1197
1198 // The path to the index.csv file created by the bootclasspath_fragment.
1199 Index *string `android:"path"`
1200
Paul Duffin8d007e92021-07-22 12:00:49 +01001201 // The path to the signature-patterns.csv file created by the bootclasspath_fragment.
1202 Signature_patterns *string `android:"path"`
1203
Paul Duffin67b9d612021-07-21 17:38:47 +01001204 // The path to the stub-flags.csv file created by the bootclasspath_fragment.
1205 Stub_flags *string `android:"path"`
1206
Paul Duffin2fef1362021-04-15 13:32:00 +01001207 // The path to the all-flags.csv file created by the bootclasspath_fragment.
1208 All_flags *string `android:"path"`
Paul Duffin191be3a2021-08-10 16:14:16 +01001209
1210 // The path to the filtered-stub-flags.csv file created by the bootclasspath_fragment.
1211 Filtered_stub_flags *string `android:"path"`
1212
1213 // The path to the filtered-flags.csv file created by the bootclasspath_fragment.
1214 Filtered_flags *string `android:"path"`
Paul Duffin2fef1362021-04-15 13:32:00 +01001215 }
1216}
1217
Paul Duffin7771eba2021-04-23 14:25:28 +01001218// A prebuilt version of the bootclasspath_fragment module.
Paul Duffinf7f65da2021-03-10 15:00:46 +00001219//
Paul Duffin7771eba2021-04-23 14:25:28 +01001220// At the moment this is basically just a bootclasspath_fragment module that can be used as a
1221// prebuilt. Eventually as more functionality is migrated into the bootclasspath_fragment module
1222// type from the various singletons then this will diverge.
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001223type PrebuiltBootclasspathFragmentModule struct {
Paul Duffin7771eba2021-04-23 14:25:28 +01001224 BootclasspathFragmentModule
Paul Duffinf7f65da2021-03-10 15:00:46 +00001225 prebuilt android.Prebuilt
Paul Duffin2fef1362021-04-15 13:32:00 +01001226
1227 // Additional prebuilt specific properties.
1228 prebuiltProperties prebuiltBootclasspathFragmentProperties
Paul Duffinf7f65da2021-03-10 15:00:46 +00001229}
1230
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001231func (module *PrebuiltBootclasspathFragmentModule) Prebuilt() *android.Prebuilt {
Paul Duffinf7f65da2021-03-10 15:00:46 +00001232 return &module.prebuilt
1233}
1234
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001235func (module *PrebuiltBootclasspathFragmentModule) Name() string {
Paul Duffinf7f65da2021-03-10 15:00:46 +00001236 return module.prebuilt.Name(module.ModuleBase.Name())
1237}
1238
Paul Duffine5218812021-06-07 13:28:19 +01001239// produceHiddenAPIOutput returns a path to the prebuilt all-flags.csv or nil if none is specified.
Paul Duffin1938dba2022-07-26 23:53:00 +00001240func (module *PrebuiltBootclasspathFragmentModule) produceHiddenAPIOutput(ctx android.ModuleContext, contents []android.Module, fragments []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput {
Paul Duffin191be3a2021-08-10 16:14:16 +01001241 pathForOptionalSrc := func(src *string, defaultPath android.Path) android.Path {
Paul Duffin8d007e92021-07-22 12:00:49 +01001242 if src == nil {
Paul Duffin191be3a2021-08-10 16:14:16 +01001243 return defaultPath
Paul Duffin8d007e92021-07-22 12:00:49 +01001244 }
1245 return android.PathForModuleSrc(ctx, *src)
1246 }
Paul Duffin54e41972021-07-19 13:23:40 +01001247 pathForSrc := func(property string, src *string) android.Path {
Paul Duffin2fef1362021-04-15 13:32:00 +01001248 if src == nil {
Paul Duffin54e41972021-07-19 13:23:40 +01001249 ctx.PropertyErrorf(property, "is required but was not specified")
1250 return android.PathForModuleSrc(ctx, "missing", property)
Paul Duffin2fef1362021-04-15 13:32:00 +01001251 }
Paul Duffin1e6f5c42021-05-21 16:15:31 +01001252 return android.PathForModuleSrc(ctx, *src)
Paul Duffin2fef1362021-04-15 13:32:00 +01001253 }
1254
Paul Duffine5218812021-06-07 13:28:19 +01001255 // Retrieve the dex files directly from the content modules. They in turn should retrieve the
1256 // encoded dex jars from the prebuilt .apex files.
1257 encodedBootDexJarsByModule := extractEncodedDexJarsFromModules(ctx, contents)
1258
1259 output := HiddenAPIOutput{
1260 HiddenAPIFlagOutput: HiddenAPIFlagOutput{
Paul Duffin8d007e92021-07-22 12:00:49 +01001261 AnnotationFlagsPath: pathForSrc("hidden_api.annotation_flags", module.prebuiltProperties.Hidden_api.Annotation_flags),
1262 MetadataPath: pathForSrc("hidden_api.metadata", module.prebuiltProperties.Hidden_api.Metadata),
1263 IndexPath: pathForSrc("hidden_api.index", module.prebuiltProperties.Hidden_api.Index),
Paul Duffin191be3a2021-08-10 16:14:16 +01001264 SignaturePatternsPath: pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Signature_patterns, nil),
1265 // TODO: Temporarily handle stub_flags/all_flags properties until prebuilts have been updated.
1266 StubFlagsPath: pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Stub_flags, nil),
1267 AllFlagsPath: pathForOptionalSrc(module.prebuiltProperties.Hidden_api.All_flags, nil),
Paul Duffine5218812021-06-07 13:28:19 +01001268 },
Paul Duffin191be3a2021-08-10 16:14:16 +01001269
Paul Duffine5218812021-06-07 13:28:19 +01001270 EncodedBootDexFilesByModule: encodedBootDexJarsByModule,
Paul Duffin1e6f5c42021-05-21 16:15:31 +01001271 }
1272
Paul Duffin191be3a2021-08-10 16:14:16 +01001273 // TODO: Temporarily fallback to stub_flags/all_flags properties until prebuilts have been updated.
1274 output.FilteredStubFlagsPath = pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Filtered_stub_flags, output.StubFlagsPath)
1275 output.FilteredFlagsPath = pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Filtered_flags, output.AllFlagsPath)
1276
Paul Duffin1e6f5c42021-05-21 16:15:31 +01001277 return &output
Paul Duffin2fef1362021-04-15 13:32:00 +01001278}
1279
Paul Duffin5466a362021-06-07 10:25:31 +01001280// produceBootImageFiles extracts the boot image files from the APEX if available.
Paul Duffin9f6ac0b2022-10-04 15:36:44 +01001281func (module *PrebuiltBootclasspathFragmentModule) produceBootImageFiles(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageOutputs {
Paul Duffin5466a362021-06-07 10:25:31 +01001282 if !shouldCopyBootFilesToPredefinedLocations(ctx, imageConfig) {
Paul Duffin9f6ac0b2022-10-04 15:36:44 +01001283 return bootImageOutputs{}
Paul Duffin5466a362021-06-07 10:25:31 +01001284 }
1285
Martin Stjernholm44825602021-09-17 01:44:12 +01001286 di := android.FindDeapexerProviderForModule(ctx)
1287 if di == nil {
Paul Duffin9f6ac0b2022-10-04 15:36:44 +01001288 return bootImageOutputs{} // An error has been reported by FindDeapexerProviderForModule.
Paul Duffin5466a362021-06-07 10:25:31 +01001289 }
1290
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001291 profile := (android.WritablePath)(nil)
1292 if imageConfig.profileInstallPathInApex != "" {
1293 profile = di.PrebuiltExportPath(imageConfig.profileInstallPathInApex)
Paul Duffin5466a362021-06-07 10:25:31 +01001294 }
1295
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001296 // Build the boot image files for the host variants. These are always built from the dex files
1297 // provided by the contents of this module as prebuilt versions of the host boot image files are
1298 // not available, i.e. there is no host specific prebuilt apex containing them. This has to be
1299 // built without a profile as the prebuilt modules do not provide a profile.
1300 buildBootImageVariantsForBuildOs(ctx, imageConfig, profile)
Paul Duffina56be7d2021-07-02 13:00:43 +01001301
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001302 if imageConfig.shouldInstallInApex() {
1303 // If the boot image files for the android variants are in the prebuilt apex, we must use those
1304 // rather than building new ones because those boot image files are going to be used on device.
1305 files := bootImageFilesByArch{}
Paul Duffine10a9f22022-10-04 16:39:18 +01001306 bootImageFiles := bootImageOutputs{
1307 byArch: files,
1308 profile: profile,
1309 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001310 for _, variant := range imageConfig.apexVariants() {
1311 arch := variant.target.Arch.ArchType
Paul Duffine10a9f22022-10-04 16:39:18 +01001312 bootImageFiles.variants = append(bootImageFiles.variants, bootImageVariantOutputs{
1313 variant,
1314 // No device installs needed when installed in APEX.
1315 nil,
1316 })
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001317 for _, toPath := range variant.imagesDeps {
1318 apexRelativePath := apexRootRelativePathToBootImageFile(arch, toPath.Base())
1319 // Get the path to the file that the deapexer extracted from the prebuilt apex file.
1320 fromPath := di.PrebuiltExportPath(apexRelativePath)
1321
1322 // Return the toPath as the calling code expects the paths in the returned map to be the
1323 // paths predefined in the bootImageConfig.
1324 files[arch] = append(files[arch], toPath)
1325
1326 // Copy the file to the predefined location.
1327 ctx.Build(pctx, android.BuildParams{
1328 Rule: android.Cp,
1329 Input: fromPath,
1330 Output: toPath,
1331 })
1332 }
1333 }
Paul Duffine10a9f22022-10-04 16:39:18 +01001334 return bootImageFiles
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001335 } else {
1336 if profile == nil {
1337 ctx.ModuleErrorf("Unable to produce boot image files: neither boot image files nor profiles exists in the prebuilt apex")
Paul Duffin9f6ac0b2022-10-04 15:36:44 +01001338 return bootImageOutputs{}
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001339 }
1340 // Build boot image files for the android variants from the dex files provided by the contents
1341 // of this module.
1342 return buildBootImageVariantsForAndroidOs(ctx, imageConfig, profile)
1343 }
Paul Duffin5466a362021-06-07 10:25:31 +01001344}
1345
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001346var _ commonBootclasspathFragment = (*PrebuiltBootclasspathFragmentModule)(nil)
Paul Duffin2fef1362021-04-15 13:32:00 +01001347
Paul Duffin5466a362021-06-07 10:25:31 +01001348// createBootImageTag creates the tag to uniquely identify the boot image file among all of the
1349// files that a module requires from the prebuilt .apex file.
1350func createBootImageTag(arch android.ArchType, baseName string) string {
1351 tag := fmt.Sprintf(".bootimage-%s-%s", arch, baseName)
1352 return tag
1353}
1354
1355// RequiredFilesFromPrebuiltApex returns the list of all files the prebuilt_bootclasspath_fragment
1356// requires from a prebuilt .apex file.
1357//
1358// If there is no image config associated with this fragment then it returns nil. Otherwise, it
1359// returns the files that are listed in the image config.
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001360func (module *PrebuiltBootclasspathFragmentModule) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffin5466a362021-06-07 10:25:31 +01001361 imageConfig := module.getImageConfig(ctx)
1362 if imageConfig != nil {
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001363 files := []string{}
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001364 if imageConfig.profileInstallPathInApex != "" {
1365 // Add the boot image profile.
1366 files = append(files, imageConfig.profileInstallPathInApex)
1367 }
1368 if imageConfig.shouldInstallInApex() {
1369 // Add the boot image files, e.g. .art, .oat and .vdex files.
1370 for _, variant := range imageConfig.apexVariants() {
1371 arch := variant.target.Arch.ArchType
1372 for _, path := range variant.imagesDeps.Paths() {
1373 base := path.Base()
1374 files = append(files, apexRootRelativePathToBootImageFile(arch, base))
1375 }
Paul Duffin5466a362021-06-07 10:25:31 +01001376 }
1377 }
1378 return files
1379 }
1380 return nil
1381}
1382
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001383func apexRootRelativePathToBootImageFile(arch android.ArchType, base string) string {
1384 return filepath.Join("javalib", arch.String(), base)
1385}
1386
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001387var _ android.RequiredFilesFromPrebuiltApex = (*PrebuiltBootclasspathFragmentModule)(nil)
Paul Duffin5466a362021-06-07 10:25:31 +01001388
Paul Duffin7771eba2021-04-23 14:25:28 +01001389func prebuiltBootclasspathFragmentFactory() android.Module {
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001390 m := &PrebuiltBootclasspathFragmentModule{}
Paul Duffin2fef1362021-04-15 13:32:00 +01001391 m.AddProperties(&m.properties, &m.prebuiltProperties)
Paul Duffinf7f65da2021-03-10 15:00:46 +00001392 // This doesn't actually have any prebuilt files of its own so pass a placeholder for the srcs
1393 // array.
1394 android.InitPrebuiltModule(m, &[]string{"placeholder"})
1395 android.InitApexModule(m)
1396 android.InitSdkAwareModule(m)
Martin Stjernholmb79c7f12021-03-17 00:26:25 +00001397 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +00001398
Paul Duffin7771eba2021-04-23 14:25:28 +01001399 // Initialize the contents property from the image_name.
Paul Duffinc7ef9892021-03-23 23:21:59 +00001400 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
Paul Duffin7771eba2021-04-23 14:25:28 +01001401 bootclasspathFragmentInitContentsFromImage(ctx, &m.BootclasspathFragmentModule)
Paul Duffinc7ef9892021-03-23 23:21:59 +00001402 })
Paul Duffinf7f65da2021-03-10 15:00:46 +00001403 return m
1404}