blob: 7580352d0abb50080686a5d379806318afe5c0dc [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.
260 produceHiddenAPIOutput(ctx android.ModuleContext, contents []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.
267 produceBootImageFiles(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageFilesByArch
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 Duffin5466a362021-06-07 10:25:31 +0100586 var bootImageFilesByArch bootImageFilesByArch
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 Duffin56afb272021-07-01 22:04:22 +0100590 bootImageFilesByArch = 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.
595 buildBootImageZipInPredefinedLocation(ctx, imageConfig, bootImageFilesByArch)
596
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
601 for _, variant := range imageConfig.apexVariants() {
602 arch := variant.target.Arch.ArchType.String()
603 for _, install := range variant.deviceInstalls {
604 // Remove the "/" prefix because the path should be relative to $ANDROID_PRODUCT_OUT.
605 installDir := strings.TrimPrefix(filepath.Dir(install.To), "/")
606 installBase := filepath.Base(install.To)
607 installPath := android.PathForModuleInPartitionInstall(ctx, "", installDir)
608
609 b.bootImageDeviceInstalls = append(b.bootImageDeviceInstalls, dexpreopterInstall{
610 name: arch + "-" + installBase,
611 moduleName: b.Name(),
612 outputPathOnHost: install.From,
613 installDirOnDevice: installPath,
614 installFileOnDevice: installBase,
615 })
616 }
617 }
Paul Duffince918b02021-06-07 14:33:47 +0100618 }
619
Paul Duffine5218812021-06-07 13:28:19 +0100620 // A prebuilt fragment cannot contribute to an apex.
621 if !android.IsModulePrebuilt(ctx.Module()) {
622 // Provide the apex content info.
Paul Duffin5466a362021-06-07 10:25:31 +0100623 b.provideApexContentInfo(ctx, imageConfig, hiddenAPIOutput, bootImageFilesByArch)
Paul Duffine5218812021-06-07 13:28:19 +0100624 }
Paul Duffinea465fb2022-03-04 18:39:29 +0000625 } else {
626 // Versioned fragments are not needed by make.
627 b.HideFromMake()
628 }
629
630 // In order for information about bootclasspath_fragment modules to be added to module-info.json
631 // it is necessary to output an entry to Make. As bootclasspath_fragment modules are part of an
632 // APEX there can be multiple variants, including the default/platform variant and only one can
633 // be output to Make but it does not really matter which variant is output. The default/platform
634 // variant is the first (ctx.PrimaryModule()) and is usually hidden from make so this just picks
635 // the last variant (ctx.FinalModule()).
636 if ctx.Module() != ctx.FinalModule() {
637 b.HideFromMake()
Paul Duffin1a8010a2021-05-15 12:39:23 +0100638 }
639}
640
Paul Duffince918b02021-06-07 14:33:47 +0100641// shouldCopyBootFilesToPredefinedLocations determines whether the current module should copy boot
642// files, e.g. boot dex jars or boot image files, to the predefined location expected by the rest
643// of the build.
644//
645// This ensures that only a single module will copy its files to the image configuration.
646func shouldCopyBootFilesToPredefinedLocations(ctx android.ModuleContext, imageConfig *bootImageConfig) bool {
647 // Bootclasspath fragment modules that are for the platform do not produce boot related files.
648 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
649 if apexInfo.IsForPlatform() {
650 return false
651 }
652
653 // If the image configuration has no modules specified then it means that the build has been
654 // configured to build something other than a boot image, e.g. an sdk, so do not try and copy the
655 // files.
656 if imageConfig.modules.Len() == 0 {
657 return false
658 }
659
660 // Only copy files from the module that is preferred.
661 return isActiveModule(ctx.Module())
662}
663
Paul Duffin1a8010a2021-05-15 12:39:23 +0100664// provideApexContentInfo creates, initializes and stores the apex content info for use by other
665// modules.
Paul Duffin5466a362021-06-07 10:25:31 +0100666func (b *BootclasspathFragmentModule) provideApexContentInfo(ctx android.ModuleContext, imageConfig *bootImageConfig, hiddenAPIOutput *HiddenAPIOutput, bootImageFilesByArch bootImageFilesByArch) {
Paul Duffin1a8010a2021-05-15 12:39:23 +0100667 // Construct the apex content info from the config.
Paul Duffine5218812021-06-07 13:28:19 +0100668 info := BootclasspathFragmentApexContentInfo{
669 // Populate the apex content info with paths to the dex jars.
670 contentModuleDexJarPaths: hiddenAPIOutput.EncodedBootDexFilesByModule,
671 }
Paul Duffin1a8010a2021-05-15 12:39:23 +0100672
Paul Duffin58e0e762021-05-21 19:27:58 +0100673 if imageConfig != nil {
674 info.modules = imageConfig.modules
Jiakai Zhang29e35e12021-12-08 10:48:35 +0000675 global := dexpreopt.GetGlobalConfig(ctx)
676 if !global.DisableGenerateProfile {
677 info.profilePathOnHost = imageConfig.profilePathOnHost
678 info.profileInstallPathInApex = imageConfig.profileInstallPathInApex
679 }
Jiakai Zhang6decef92022-01-12 17:56:19 +0000680
681 info.shouldInstallBootImageInApex = imageConfig.shouldInstallInApex()
Paul Duffin1a8010a2021-05-15 12:39:23 +0100682 }
Paul Duffin3451e162021-01-20 15:16:56 +0000683
Paul Duffin5466a362021-06-07 10:25:31 +0100684 info.bootImageFilesByArch = bootImageFilesByArch
685
Paul Duffin1a8010a2021-05-15 12:39:23 +0100686 // Make the apex content info available for other modules.
687 ctx.SetProvider(BootclasspathFragmentApexContentInfoProvider, info)
688}
689
satayev3db35472021-05-06 23:59:58 +0100690// generateClasspathProtoBuildActions generates all required build actions for classpath.proto config
691func (b *BootclasspathFragmentModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
692 var classpathJars []classpathJar
satayevb3090502021-06-15 17:49:10 +0100693 configuredJars := b.configuredJars(ctx)
satayev3db35472021-05-06 23:59:58 +0100694 if "art" == proptools.String(b.properties.Image_name) {
695 // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
satayevb3090502021-06-15 17:49:10 +0100696 classpathJars = configuredJarListToClasspathJars(ctx, configuredJars, BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
satayev3db35472021-05-06 23:59:58 +0100697 } else {
satayevb3090502021-06-15 17:49:10 +0100698 classpathJars = configuredJarListToClasspathJars(ctx, configuredJars, b.classpathType)
satayev3db35472021-05-06 23:59:58 +0100699 }
satayevb3090502021-06-15 17:49:10 +0100700 b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, configuredJars, classpathJars)
satayev3db35472021-05-06 23:59:58 +0100701}
702
satayev142ed272021-06-15 16:21:17 +0100703func (b *BootclasspathFragmentModule) configuredJars(ctx android.ModuleContext) android.ConfiguredJarList {
satayev8fab6f82021-05-07 00:10:33 +0100704 if "art" == proptools.String(b.properties.Image_name) {
705 return b.getImageConfig(ctx).modules
706 }
707
708 global := dexpreopt.GetGlobalConfig(ctx)
709
satayevd604b212021-07-21 14:23:52 +0100710 possibleUpdatableModules := gatherPossibleApexModuleNamesAndStems(ctx, b.properties.Contents, bootclasspathFragmentContentDepTag)
satayevd34eb0c2021-08-06 13:20:28 +0100711 jars, unknown := global.ApexBootJars.Filter(possibleUpdatableModules)
satayev1b75a3c2021-06-04 18:09:40 +0100712
713 // TODO(satayev): for apex_test we want to include all contents unconditionally to classpaths
satayevd604b212021-07-21 14:23:52 +0100714 // config. However, any test specific jars would not be present in ApexBootJars. Instead,
satayev1b75a3c2021-06-04 18:09:40 +0100715 // we should check if we are creating a config for apex_test via ApexInfo and amend the values.
716 // This is an exception to support end-to-end test for SdkExtensions, until such support exists.
Paul Duffin56c93e82021-06-29 20:04:45 +0100717 if android.InList("test_framework-sdkextensions", possibleUpdatableModules) {
satayev1b75a3c2021-06-04 18:09:40 +0100718 jars = jars.Append("com.android.sdkext", "test_framework-sdkextensions")
Pedro Loureiro561c7762022-01-13 14:05:15 +0000719 } else if android.InList("AddNewActivity", possibleUpdatableModules) {
720 jars = jars.Append("test_com.android.cts.frameworkresapkplits", "AddNewActivity")
Samiul Islam7b385c52021-10-11 22:47:13 +0100721 } else if android.InList("test_framework-apexd", possibleUpdatableModules) {
722 jars = jars.Append("com.android.apex.test_package", "test_framework-apexd")
satayevd34eb0c2021-08-06 13:20:28 +0100723 } else if global.ApexBootJars.Len() != 0 && !android.IsModuleInVersionedSdk(ctx.Module()) {
724 unknown = android.RemoveListFromList(unknown, b.properties.Coverage.Contents)
725 _, unknown = android.RemoveFromList("core-icu4j", unknown)
Keun young Park59799962021-10-14 15:42:04 -0700726 // This module only exists in car products.
727 // So ignore it even if it is not in PRODUCT_APEX_BOOT_JARS.
728 // TODO(b/202896428): Add better way to handle this.
729 _, unknown = android.RemoveFromList("android.car-module", unknown)
satayevd34eb0c2021-08-06 13:20:28 +0100730 if len(unknown) > 0 {
731 ctx.ModuleErrorf("%s in contents must also be declared in PRODUCT_APEX_BOOT_JARS", unknown)
732 }
satayev1b75a3c2021-06-04 18:09:40 +0100733 }
734 return jars
satayev3db35472021-05-06 23:59:58 +0100735}
736
Paul Duffin7771eba2021-04-23 14:25:28 +0100737func (b *BootclasspathFragmentModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
Paul Duffin64be7bb2021-03-23 23:06:38 +0000738 // Get a map of the image configs that are supported.
739 imageConfigs := genBootImageConfigs(ctx)
740
741 // Retrieve the config for this image.
742 imageNamePtr := b.properties.Image_name
743 if imageNamePtr == nil {
744 return nil
745 }
746
747 imageName := *imageNamePtr
748 imageConfig := imageConfigs[imageName]
749 if imageConfig == nil {
750 ctx.PropertyErrorf("image_name", "Unknown image name %q, expected one of %s", imageName, strings.Join(android.SortedStringKeys(imageConfigs), ", "))
751 return nil
752 }
753 return imageConfig
754}
755
Paul Duffin9b381ef2021-04-08 23:01:37 +0100756// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffine5218812021-06-07 13:28:19 +0100757func (b *BootclasspathFragmentModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, contents []android.Module, fragments []android.Module) *HiddenAPIOutput {
Paul Duffin10931582021-04-25 10:13:54 +0100758
Paul Duffin1352f7c2021-05-21 22:18:49 +0100759 // Create hidden API input structure.
Paul Duffinf1b358c2021-05-17 07:38:47 +0100760 input := b.createHiddenAPIFlagInput(ctx, contents, fragments)
Paul Duffin10931582021-04-25 10:13:54 +0100761
Paul Duffinda286f42021-06-29 11:59:23 +0100762 // Delegate the production of the hidden API all-flags.csv file to a module type specific method.
763 common := ctx.Module().(commonBootclasspathFragment)
764 output := common.produceHiddenAPIOutput(ctx, contents, input)
Paul Duffin62370922021-05-23 16:55:37 +0100765
Paul Duffin67b9d612021-07-21 17:38:47 +0100766 // If the source or prebuilts module does not provide a signature patterns file then generate one
767 // from the flags.
768 // TODO(b/192868581): Remove once the source and prebuilts provide a signature patterns file of
769 // their own.
770 if output.SignaturePatternsPath == nil {
Paul Duffin846beb72022-03-15 17:45:57 +0000771 output.SignaturePatternsPath = buildRuleSignaturePatternsFile(
772 ctx, output.AllFlagsPath, []string{"*"}, nil, nil)
Paul Duffin67b9d612021-07-21 17:38:47 +0100773 }
774
Paul Duffin62370922021-05-23 16:55:37 +0100775 // Initialize a HiddenAPIInfo structure.
Paul Duffinaf99afa2021-05-21 22:18:56 +0100776 hiddenAPIInfo := HiddenAPIInfo{
Paul Duffin62370922021-05-23 16:55:37 +0100777 // The monolithic hidden API processing needs access to the flag files that override the default
778 // flags from all the fragments whether or not they actually perform their own hidden API flag
779 // generation. That is because the monolithic hidden API processing uses those flag files to
780 // perform its own flag generation.
Paul Duffin1352f7c2021-05-21 22:18:49 +0100781 FlagFilesByCategory: input.FlagFilesByCategory,
Paul Duffin18cf1972021-05-21 22:46:59 +0100782
Paul Duffinf1b358c2021-05-17 07:38:47 +0100783 // Other bootclasspath_fragments that depend on this need the transitive set of stub dex jars
784 // from this to resolve any references from their code to classes provided by this fragment
785 // and the fragments this depends upon.
Paul Duffin31fad802021-06-18 18:14:25 +0100786 TransitiveStubDexJarsByScope: input.transitiveStubDexJarsByScope(),
Paul Duffin62370922021-05-23 16:55:37 +0100787 }
Paul Duffin2fef1362021-04-15 13:32:00 +0100788
Paul Duffine5218812021-06-07 13:28:19 +0100789 // The monolithic hidden API processing also needs access to all the output files produced by
790 // hidden API processing of this fragment.
Paul Duffin54e41972021-07-19 13:23:40 +0100791 hiddenAPIInfo.HiddenAPIFlagOutput = output.HiddenAPIFlagOutput
Paul Duffin62370922021-05-23 16:55:37 +0100792
793 // Provide it for use by other modules.
Paul Duffinaf99afa2021-05-21 22:18:56 +0100794 ctx.SetProvider(HiddenAPIInfoProvider, hiddenAPIInfo)
Paul Duffin54c98f52021-05-15 08:54:30 +0100795
Paul Duffin1e6f5c42021-05-21 16:15:31 +0100796 return output
Paul Duffin2fef1362021-04-15 13:32:00 +0100797}
798
Paul Duffine5218812021-06-07 13:28:19 +0100799// retrieveLegacyEncodedBootDexFiles attempts to retrieve the legacy encoded boot dex jar files.
800func retrieveLegacyEncodedBootDexFiles(ctx android.ModuleContext, contents []android.Module) bootDexJarByModule {
801 // If the current bootclasspath_fragment is the active module or a source module then retrieve the
802 // encoded dex files, otherwise return an empty map.
803 //
804 // An inactive (i.e. not preferred) bootclasspath_fragment needs to retrieve the encoded dex jars
805 // as they are still needed by an apex. An inactive prebuilt_bootclasspath_fragment does not need
806 // to do so and may not yet have access to dex boot jars from a prebuilt_apex/apex_set.
807 if isActiveModule(ctx.Module()) || !android.IsModulePrebuilt(ctx.Module()) {
808 return extractEncodedDexJarsFromModules(ctx, contents)
809 } else {
810 return nil
811 }
812}
813
Paul Duffin1352f7c2021-05-21 22:18:49 +0100814// createHiddenAPIFlagInput creates a HiddenAPIFlagInput struct and initializes it with information derived
815// from the properties on this module and its dependencies.
Paul Duffinf1b358c2021-05-17 07:38:47 +0100816func (b *BootclasspathFragmentModule) createHiddenAPIFlagInput(ctx android.ModuleContext, contents []android.Module, fragments []android.Module) HiddenAPIFlagInput {
Paul Duffinf1b358c2021-05-17 07:38:47 +0100817 // Merge the HiddenAPIInfo from all the fragment dependencies.
818 dependencyHiddenApiInfo := newHiddenAPIInfo()
819 dependencyHiddenApiInfo.mergeFromFragmentDeps(ctx, fragments)
820
821 // Create hidden API flag input structure.
Paul Duffin1352f7c2021-05-21 22:18:49 +0100822 input := newHiddenAPIFlagInput()
823
824 // Update the input structure with information obtained from the stub libraries.
825 input.gatherStubLibInfo(ctx, contents)
826
827 // Populate with flag file paths from the properties.
Paul Duffin9b61abb2022-07-27 16:16:54 +0000828 input.extractFlagFilesFromProperties(ctx, &b.properties.HiddenAPIFlagFileProperties)
Paul Duffin1352f7c2021-05-21 22:18:49 +0100829
Paul Duffin1e9e9382022-07-27 15:55:06 +0000830 // Populate with package rules from the properties.
831 input.extractPackageRulesFromProperties(&b.sourceOnlyProperties.HiddenAPIPackageProperties)
832
Paul Duffin5cca7c42021-05-26 10:16:01 +0100833 // Add the stub dex jars from this module's fragment dependencies.
Paul Duffin280a31a2021-06-27 20:28:29 +0100834 input.DependencyStubDexJarsByScope.addStubDexJarsByModule(dependencyHiddenApiInfo.TransitiveStubDexJarsByScope)
Paul Duffinf1b358c2021-05-17 07:38:47 +0100835
Paul Duffin1352f7c2021-05-21 22:18:49 +0100836 return input
837}
838
Paul Duffinc15b9e92022-03-31 15:42:30 +0100839// isTestFragment returns true if the current module is a test bootclasspath_fragment.
840func (b *BootclasspathFragmentModule) isTestFragment() bool {
Paul Duffinff9b6fa2022-04-12 18:20:14 +0100841 if b.testFragment {
842 return true
843 }
844
845 // TODO(b/194063708): Once test fragments all use bootclasspath_fragment_test
846 // Some temporary exceptions until all test fragments use the
847 // bootclasspath_fragment_test module type.
848 name := b.BaseModuleName()
849 if strings.HasPrefix(name, "test_") {
850 return true
851 }
852 if name == "apex.apexd_test_bootclasspath-fragment" {
853 return true
854 }
855
856 return false
Paul Duffinc15b9e92022-03-31 15:42:30 +0100857}
858
Paul Duffine5218812021-06-07 13:28:19 +0100859// produceHiddenAPIOutput produces the hidden API all-flags.csv file (and supporting files)
860// for the fragment as well as encoding the flags in the boot dex jars.
861func (b *BootclasspathFragmentModule) produceHiddenAPIOutput(ctx android.ModuleContext, contents []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput {
Paul Duffin1352f7c2021-05-21 22:18:49 +0100862 // Generate the rules to create the hidden API flags and update the supplied hiddenAPIInfo with the
Paul Duffin2fef1362021-04-15 13:32:00 +0100863 // paths to the created files.
Paul Duffin1e18e982021-08-03 15:42:27 +0100864 output := hiddenAPIRulesForBootclasspathFragment(ctx, contents, input)
865
866 // If the module specifies split_packages or package_prefixes then use those to generate the
867 // signature patterns.
Paul Duffin1e9e9382022-07-27 15:55:06 +0000868 splitPackages := input.SplitPackages
869 packagePrefixes := input.PackagePrefixes
870 singlePackages := input.SinglePackages
Paul Duffin846beb72022-03-15 17:45:57 +0000871 if splitPackages != nil || packagePrefixes != nil || singlePackages != nil {
Paul Duffin846beb72022-03-15 17:45:57 +0000872 output.SignaturePatternsPath = buildRuleSignaturePatternsFile(
873 ctx, output.AllFlagsPath, splitPackages, packagePrefixes, singlePackages)
Paul Duffin9fd56472022-03-31 15:42:30 +0100874 } else if !b.isTestFragment() {
875 ctx.ModuleErrorf(`Must specify at least one of the split_packages, package_prefixes and single_packages properties
876 If this is a new bootclasspath_fragment or you are unsure what to do add the
877 the following to the bootclasspath_fragment:
878 hidden_api: {split_packages: ["*"]},
879 and then run the following:
880 m analyze_bcpf && analyze_bcpf --bcpf %q
881 it will analyze the bootclasspath_fragment and provide hints as to what you
882 should specify here. If you are happy with its suggestions then you can add
883 the --fix option and it will fix them for you.`, b.BaseModuleName())
Paul Duffin1e18e982021-08-03 15:42:27 +0100884 }
885
886 return output
Paul Duffin9b381ef2021-04-08 23:01:37 +0100887}
888
Paul Duffin5466a362021-06-07 10:25:31 +0100889// produceBootImageFiles builds the boot image files from the source if it is required.
Paul Duffin56afb272021-07-01 22:04:22 +0100890func (b *BootclasspathFragmentModule) produceBootImageFiles(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageFilesByArch {
Paul Duffin5466a362021-06-07 10:25:31 +0100891 if SkipDexpreoptBootJars(ctx) {
892 return nil
893 }
894
Paul Duffin5466a362021-06-07 10:25:31 +0100895 // Only generate the boot image if the configuration does not skip it.
Paul Duffin56afb272021-07-01 22:04:22 +0100896 return b.generateBootImageBuildActions(ctx, imageConfig)
Paul Duffin5466a362021-06-07 10:25:31 +0100897}
898
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100899// generateBootImageBuildActions generates ninja rules to create the boot image if required for this
900// module.
Paul Duffin58e0e762021-05-21 19:27:58 +0100901//
Paul Duffin56afb272021-07-01 22:04:22 +0100902// If it could not create the files then it will return nil. Otherwise, it will return a map from
903// android.ArchType to the predefined paths of the boot image files.
904func (b *BootclasspathFragmentModule) generateBootImageBuildActions(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageFilesByArch {
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100905 global := dexpreopt.GetGlobalConfig(ctx)
906 if !shouldBuildBootImages(ctx.Config(), global) {
Paul Duffin56afb272021-07-01 22:04:22 +0100907 return nil
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100908 }
909
910 // Bootclasspath fragment modules that are for the platform do not produce a boot image.
911 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
912 if apexInfo.IsForPlatform() {
Paul Duffin56afb272021-07-01 22:04:22 +0100913 return nil
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100914 }
915
916 // Bootclasspath fragment modules that are versioned do not produce a boot image.
917 if android.IsModuleInVersionedSdk(ctx.Module()) {
Paul Duffin56afb272021-07-01 22:04:22 +0100918 return nil
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100919 }
920
Paul Duffin2fc82ad2021-04-29 23:36:12 +0100921 // Build a profile for the image config and then use that to build the boot image.
922 profile := bootImageProfileRule(ctx, imageConfig)
Paul Duffina56be7d2021-07-02 13:00:43 +0100923
924 // Build boot image files for the host variants.
925 buildBootImageVariantsForBuildOs(ctx, imageConfig, profile)
926
927 // Build boot image files for the android variants.
928 androidBootImageFilesByArch := buildBootImageVariantsForAndroidOs(ctx, imageConfig, profile)
929
930 // Return the boot image files for the android variants for inclusion in an APEX and to be zipped
931 // up for the dist.
932 return androidBootImageFilesByArch
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100933}
934
Jiakai Zhang6decef92022-01-12 17:56:19 +0000935func (b *BootclasspathFragmentModule) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinea465fb2022-03-04 18:39:29 +0000936 // Use the generated classpath proto as the output.
937 outputFile := b.outputFilepath
938 // Create a fake entry that will cause this to be added to the module-info.json file.
939 entriesList := []android.AndroidMkEntries{{
940 Class: "FAKE",
941 OutputFile: android.OptionalPathForPath(outputFile),
942 Include: "$(BUILD_PHONY_PACKAGE)",
943 ExtraFooters: []android.AndroidMkExtraFootersFunc{
944 func(w io.Writer, name, prefix, moduleDir string) {
945 // Allow the bootclasspath_fragment to be built by simply passing its name on the command
946 // line.
947 fmt.Fprintln(w, ".PHONY:", b.Name())
948 fmt.Fprintln(w, b.Name()+":", outputFile.String())
949 },
950 },
951 }}
Jiakai Zhang6decef92022-01-12 17:56:19 +0000952 for _, install := range b.bootImageDeviceInstalls {
953 entriesList = append(entriesList, install.ToMakeEntries())
954 }
955 return entriesList
956}
957
958// Returns the names of all Make modules that handle the installation of the boot image.
959func (b *BootclasspathFragmentModule) BootImageDeviceInstallMakeModules() []string {
960 var makeModules []string
961 for _, install := range b.bootImageDeviceInstalls {
962 makeModules = append(makeModules, install.FullModuleName())
963 }
964 return makeModules
965}
966
braleeb0c1f0c2021-06-07 22:49:13 +0800967// Collect information for opening IDE project files in java/jdeps.go.
968func (b *BootclasspathFragmentModule) IDEInfo(dpInfo *android.IdeInfo) {
969 dpInfo.Deps = append(dpInfo.Deps, b.properties.Contents...)
970 dpInfo.Paths = append(dpInfo.Paths, b.modulePaths...)
971}
972
Paul Duffin7771eba2021-04-23 14:25:28 +0100973type bootclasspathFragmentMemberType struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000974 android.SdkMemberTypeBase
975}
976
Paul Duffin296701e2021-07-14 10:29:36 +0100977func (b *bootclasspathFragmentMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
978 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000979}
980
Paul Duffin7771eba2021-04-23 14:25:28 +0100981func (b *bootclasspathFragmentMemberType) IsInstance(module android.Module) bool {
982 _, ok := module.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000983 return ok
984}
985
Paul Duffin7771eba2021-04-23 14:25:28 +0100986func (b *bootclasspathFragmentMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
Paul Duffin4b64ba02021-03-29 11:02:53 +0100987 if b.PropertyName == "boot_images" {
988 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_boot_image")
989 } else {
990 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_bootclasspath_fragment")
991 }
Paul Duffinf7f65da2021-03-10 15:00:46 +0000992}
993
Paul Duffin7771eba2021-04-23 14:25:28 +0100994func (b *bootclasspathFragmentMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
995 return &bootclasspathFragmentSdkMemberProperties{}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000996}
997
Paul Duffin7771eba2021-04-23 14:25:28 +0100998type bootclasspathFragmentSdkMemberProperties struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000999 android.SdkMemberPropertiesBase
1000
Paul Duffina57835e2021-04-19 13:23:06 +01001001 // The image name
Paul Duffin64be7bb2021-03-23 23:06:38 +00001002 Image_name *string
Paul Duffina57835e2021-04-19 13:23:06 +01001003
1004 // Contents of the bootclasspath fragment
1005 Contents []string
Paul Duffin7c955552021-04-19 13:23:53 +01001006
Paul Duffin895c7142021-04-25 13:40:15 +01001007 // Stub_libs properties.
1008 Stub_libs []string
1009 Core_platform_stub_libs []string
1010
Paul Duffin51227d82021-05-18 12:54:27 +01001011 // Fragment properties
1012 Fragments []ApexVariantReference
1013
Paul Duffin7c955552021-04-19 13:23:53 +01001014 // Flag files by *hiddenAPIFlagFileCategory
Paul Duffin438eb572021-05-21 16:58:23 +01001015 Flag_files_by_category FlagFilesByCategory
Paul Duffin2fef1362021-04-15 13:32:00 +01001016
Paul Duffin2fef1362021-04-15 13:32:00 +01001017 // The path to the generated annotation-flags.csv file.
1018 Annotation_flags_path android.OptionalPath
1019
1020 // The path to the generated metadata.csv file.
1021 Metadata_path android.OptionalPath
1022
1023 // The path to the generated index.csv file.
1024 Index_path android.OptionalPath
1025
Paul Duffin67b9d612021-07-21 17:38:47 +01001026 // The path to the generated stub-flags.csv file.
Paul Duffin191be3a2021-08-10 16:14:16 +01001027 Stub_flags_path android.OptionalPath `supported_build_releases:"S"`
Paul Duffin67b9d612021-07-21 17:38:47 +01001028
Paul Duffin2fef1362021-04-15 13:32:00 +01001029 // The path to the generated all-flags.csv file.
Paul Duffin191be3a2021-08-10 16:14:16 +01001030 All_flags_path android.OptionalPath `supported_build_releases:"S"`
1031
1032 // The path to the generated signature-patterns.csv file.
Paul Duffine7babdb2022-02-10 13:06:54 +00001033 Signature_patterns_path android.OptionalPath `supported_build_releases:"Tiramisu+"`
Paul Duffin191be3a2021-08-10 16:14:16 +01001034
1035 // The path to the generated filtered-stub-flags.csv file.
Paul Duffine7babdb2022-02-10 13:06:54 +00001036 Filtered_stub_flags_path android.OptionalPath `supported_build_releases:"Tiramisu+"`
Paul Duffin191be3a2021-08-10 16:14:16 +01001037
1038 // The path to the generated filtered-flags.csv file.
Paul Duffine7babdb2022-02-10 13:06:54 +00001039 Filtered_flags_path android.OptionalPath `supported_build_releases:"Tiramisu+"`
Paul Duffin2fef1362021-04-15 13:32:00 +01001040}
1041
Paul Duffin7771eba2021-04-23 14:25:28 +01001042func (b *bootclasspathFragmentSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
1043 module := variant.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +00001044
1045 b.Image_name = module.properties.Image_name
Paul Duffin2dc665b2021-04-23 16:58:51 +01001046 b.Contents = module.properties.Contents
Paul Duffin7c955552021-04-19 13:23:53 +01001047
Paul Duffinaf99afa2021-05-21 22:18:56 +01001048 // Get the hidden API information from the module.
Paul Duffin7c955552021-04-19 13:23:53 +01001049 mctx := ctx.SdkModuleContext()
Paul Duffinaf99afa2021-05-21 22:18:56 +01001050 hiddenAPIInfo := mctx.OtherModuleProvider(module, HiddenAPIInfoProvider).(HiddenAPIInfo)
1051 b.Flag_files_by_category = hiddenAPIInfo.FlagFilesByCategory
Paul Duffin895c7142021-04-25 13:40:15 +01001052
Paul Duffin2fef1362021-04-15 13:32:00 +01001053 // Copy all the generated file paths.
Paul Duffinaf99afa2021-05-21 22:18:56 +01001054 b.Annotation_flags_path = android.OptionalPathForPath(hiddenAPIInfo.AnnotationFlagsPath)
1055 b.Metadata_path = android.OptionalPathForPath(hiddenAPIInfo.MetadataPath)
1056 b.Index_path = android.OptionalPathForPath(hiddenAPIInfo.IndexPath)
Paul Duffin67b9d612021-07-21 17:38:47 +01001057
1058 b.Stub_flags_path = android.OptionalPathForPath(hiddenAPIInfo.StubFlagsPath)
Paul Duffinaf99afa2021-05-21 22:18:56 +01001059 b.All_flags_path = android.OptionalPathForPath(hiddenAPIInfo.AllFlagsPath)
Paul Duffin2fef1362021-04-15 13:32:00 +01001060
Paul Duffin191be3a2021-08-10 16:14:16 +01001061 b.Signature_patterns_path = android.OptionalPathForPath(hiddenAPIInfo.SignaturePatternsPath)
1062 b.Filtered_stub_flags_path = android.OptionalPathForPath(hiddenAPIInfo.FilteredStubFlagsPath)
1063 b.Filtered_flags_path = android.OptionalPathForPath(hiddenAPIInfo.FilteredFlagsPath)
1064
Paul Duffin895c7142021-04-25 13:40:15 +01001065 // Copy stub_libs properties.
1066 b.Stub_libs = module.properties.Api.Stub_libs
1067 b.Core_platform_stub_libs = module.properties.Core_platform_api.Stub_libs
Paul Duffin51227d82021-05-18 12:54:27 +01001068
1069 // Copy fragment properties.
1070 b.Fragments = module.properties.Fragments
Paul Duffinf7f65da2021-03-10 15:00:46 +00001071}
1072
Paul Duffin7771eba2021-04-23 14:25:28 +01001073func (b *bootclasspathFragmentSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffin64be7bb2021-03-23 23:06:38 +00001074 if b.Image_name != nil {
1075 propertySet.AddProperty("image_name", *b.Image_name)
Paul Duffinf7f65da2021-03-10 15:00:46 +00001076 }
Paul Duffina57835e2021-04-19 13:23:06 +01001077
Paul Duffin895c7142021-04-25 13:40:15 +01001078 builder := ctx.SnapshotBuilder()
1079 requiredMemberDependency := builder.SdkMemberReferencePropertyTag(true)
1080
Paul Duffina57835e2021-04-19 13:23:06 +01001081 if len(b.Contents) > 0 {
Paul Duffin895c7142021-04-25 13:40:15 +01001082 propertySet.AddPropertyWithTag("contents", b.Contents, requiredMemberDependency)
Paul Duffina57835e2021-04-19 13:23:06 +01001083 }
Paul Duffin7c955552021-04-19 13:23:53 +01001084
Paul Duffin895c7142021-04-25 13:40:15 +01001085 if len(b.Stub_libs) > 0 {
1086 apiPropertySet := propertySet.AddPropertySet("api")
1087 apiPropertySet.AddPropertyWithTag("stub_libs", b.Stub_libs, requiredMemberDependency)
1088 }
1089 if len(b.Core_platform_stub_libs) > 0 {
1090 corePlatformApiPropertySet := propertySet.AddPropertySet("core_platform_api")
1091 corePlatformApiPropertySet.AddPropertyWithTag("stub_libs", b.Core_platform_stub_libs, requiredMemberDependency)
1092 }
Paul Duffin51227d82021-05-18 12:54:27 +01001093 if len(b.Fragments) > 0 {
1094 propertySet.AddProperty("fragments", b.Fragments)
1095 }
Paul Duffin895c7142021-04-25 13:40:15 +01001096
Paul Duffin2fef1362021-04-15 13:32:00 +01001097 hiddenAPISet := propertySet.AddPropertySet("hidden_api")
1098 hiddenAPIDir := "hiddenapi"
1099
1100 // Copy manually curated flag files specified on the bootclasspath_fragment.
Paul Duffin7c955552021-04-19 13:23:53 +01001101 if b.Flag_files_by_category != nil {
Paul Duffin524c82c2021-06-09 14:39:28 +01001102 for _, category := range HiddenAPIFlagFileCategories {
Paul Duffin7c955552021-04-19 13:23:53 +01001103 paths := b.Flag_files_by_category[category]
1104 if len(paths) > 0 {
1105 dests := []string{}
1106 for _, p := range paths {
Paul Duffin2fef1362021-04-15 13:32:00 +01001107 dest := filepath.Join(hiddenAPIDir, p.Base())
Paul Duffin7c955552021-04-19 13:23:53 +01001108 builder.CopyToSnapshot(p, dest)
1109 dests = append(dests, dest)
1110 }
Paul Duffin524c82c2021-06-09 14:39:28 +01001111 hiddenAPISet.AddProperty(category.PropertyName, dests)
Paul Duffin7c955552021-04-19 13:23:53 +01001112 }
1113 }
1114 }
Paul Duffin2fef1362021-04-15 13:32:00 +01001115
1116 copyOptionalPath := func(path android.OptionalPath, property string) {
1117 if path.Valid() {
1118 p := path.Path()
1119 dest := filepath.Join(hiddenAPIDir, p.Base())
1120 builder.CopyToSnapshot(p, dest)
1121 hiddenAPISet.AddProperty(property, dest)
1122 }
1123 }
1124
1125 // Copy all the generated files, if available.
Paul Duffin2fef1362021-04-15 13:32:00 +01001126 copyOptionalPath(b.Annotation_flags_path, "annotation_flags")
1127 copyOptionalPath(b.Metadata_path, "metadata")
1128 copyOptionalPath(b.Index_path, "index")
Paul Duffin191be3a2021-08-10 16:14:16 +01001129
Paul Duffin67b9d612021-07-21 17:38:47 +01001130 copyOptionalPath(b.Stub_flags_path, "stub_flags")
Paul Duffin2fef1362021-04-15 13:32:00 +01001131 copyOptionalPath(b.All_flags_path, "all_flags")
Paul Duffin191be3a2021-08-10 16:14:16 +01001132
1133 copyOptionalPath(b.Signature_patterns_path, "signature_patterns")
1134 copyOptionalPath(b.Filtered_stub_flags_path, "filtered_stub_flags")
1135 copyOptionalPath(b.Filtered_flags_path, "filtered_flags")
Paul Duffinf7f65da2021-03-10 15:00:46 +00001136}
1137
Paul Duffin7771eba2021-04-23 14:25:28 +01001138var _ android.SdkMemberType = (*bootclasspathFragmentMemberType)(nil)
Paul Duffinf7f65da2021-03-10 15:00:46 +00001139
Paul Duffin2fef1362021-04-15 13:32:00 +01001140// prebuiltBootclasspathFragmentProperties contains additional prebuilt_bootclasspath_fragment
1141// specific properties.
1142type prebuiltBootclasspathFragmentProperties struct {
1143 Hidden_api struct {
Paul Duffin2fef1362021-04-15 13:32:00 +01001144 // The path to the annotation-flags.csv file created by the bootclasspath_fragment.
1145 Annotation_flags *string `android:"path"`
1146
1147 // The path to the metadata.csv file created by the bootclasspath_fragment.
1148 Metadata *string `android:"path"`
1149
1150 // The path to the index.csv file created by the bootclasspath_fragment.
1151 Index *string `android:"path"`
1152
Paul Duffin8d007e92021-07-22 12:00:49 +01001153 // The path to the signature-patterns.csv file created by the bootclasspath_fragment.
1154 Signature_patterns *string `android:"path"`
1155
Paul Duffin67b9d612021-07-21 17:38:47 +01001156 // The path to the stub-flags.csv file created by the bootclasspath_fragment.
1157 Stub_flags *string `android:"path"`
1158
Paul Duffin2fef1362021-04-15 13:32:00 +01001159 // The path to the all-flags.csv file created by the bootclasspath_fragment.
1160 All_flags *string `android:"path"`
Paul Duffin191be3a2021-08-10 16:14:16 +01001161
1162 // The path to the filtered-stub-flags.csv file created by the bootclasspath_fragment.
1163 Filtered_stub_flags *string `android:"path"`
1164
1165 // The path to the filtered-flags.csv file created by the bootclasspath_fragment.
1166 Filtered_flags *string `android:"path"`
Paul Duffin2fef1362021-04-15 13:32:00 +01001167 }
1168}
1169
Paul Duffin7771eba2021-04-23 14:25:28 +01001170// A prebuilt version of the bootclasspath_fragment module.
Paul Duffinf7f65da2021-03-10 15:00:46 +00001171//
Paul Duffin7771eba2021-04-23 14:25:28 +01001172// At the moment this is basically just a bootclasspath_fragment module that can be used as a
1173// prebuilt. Eventually as more functionality is migrated into the bootclasspath_fragment module
1174// type from the various singletons then this will diverge.
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001175type PrebuiltBootclasspathFragmentModule struct {
Paul Duffin7771eba2021-04-23 14:25:28 +01001176 BootclasspathFragmentModule
Paul Duffinf7f65da2021-03-10 15:00:46 +00001177 prebuilt android.Prebuilt
Paul Duffin2fef1362021-04-15 13:32:00 +01001178
1179 // Additional prebuilt specific properties.
1180 prebuiltProperties prebuiltBootclasspathFragmentProperties
Paul Duffinf7f65da2021-03-10 15:00:46 +00001181}
1182
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001183func (module *PrebuiltBootclasspathFragmentModule) Prebuilt() *android.Prebuilt {
Paul Duffinf7f65da2021-03-10 15:00:46 +00001184 return &module.prebuilt
1185}
1186
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001187func (module *PrebuiltBootclasspathFragmentModule) Name() string {
Paul Duffinf7f65da2021-03-10 15:00:46 +00001188 return module.prebuilt.Name(module.ModuleBase.Name())
1189}
1190
Paul Duffine5218812021-06-07 13:28:19 +01001191// produceHiddenAPIOutput returns a path to the prebuilt all-flags.csv or nil if none is specified.
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001192func (module *PrebuiltBootclasspathFragmentModule) produceHiddenAPIOutput(ctx android.ModuleContext, contents []android.Module, input HiddenAPIFlagInput) *HiddenAPIOutput {
Paul Duffin191be3a2021-08-10 16:14:16 +01001193 pathForOptionalSrc := func(src *string, defaultPath android.Path) android.Path {
Paul Duffin8d007e92021-07-22 12:00:49 +01001194 if src == nil {
Paul Duffin191be3a2021-08-10 16:14:16 +01001195 return defaultPath
Paul Duffin8d007e92021-07-22 12:00:49 +01001196 }
1197 return android.PathForModuleSrc(ctx, *src)
1198 }
Paul Duffin54e41972021-07-19 13:23:40 +01001199 pathForSrc := func(property string, src *string) android.Path {
Paul Duffin2fef1362021-04-15 13:32:00 +01001200 if src == nil {
Paul Duffin54e41972021-07-19 13:23:40 +01001201 ctx.PropertyErrorf(property, "is required but was not specified")
1202 return android.PathForModuleSrc(ctx, "missing", property)
Paul Duffin2fef1362021-04-15 13:32:00 +01001203 }
Paul Duffin1e6f5c42021-05-21 16:15:31 +01001204 return android.PathForModuleSrc(ctx, *src)
Paul Duffin2fef1362021-04-15 13:32:00 +01001205 }
1206
Paul Duffine5218812021-06-07 13:28:19 +01001207 // Retrieve the dex files directly from the content modules. They in turn should retrieve the
1208 // encoded dex jars from the prebuilt .apex files.
1209 encodedBootDexJarsByModule := extractEncodedDexJarsFromModules(ctx, contents)
1210
1211 output := HiddenAPIOutput{
1212 HiddenAPIFlagOutput: HiddenAPIFlagOutput{
Paul Duffin8d007e92021-07-22 12:00:49 +01001213 AnnotationFlagsPath: pathForSrc("hidden_api.annotation_flags", module.prebuiltProperties.Hidden_api.Annotation_flags),
1214 MetadataPath: pathForSrc("hidden_api.metadata", module.prebuiltProperties.Hidden_api.Metadata),
1215 IndexPath: pathForSrc("hidden_api.index", module.prebuiltProperties.Hidden_api.Index),
Paul Duffin191be3a2021-08-10 16:14:16 +01001216 SignaturePatternsPath: pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Signature_patterns, nil),
1217 // TODO: Temporarily handle stub_flags/all_flags properties until prebuilts have been updated.
1218 StubFlagsPath: pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Stub_flags, nil),
1219 AllFlagsPath: pathForOptionalSrc(module.prebuiltProperties.Hidden_api.All_flags, nil),
Paul Duffine5218812021-06-07 13:28:19 +01001220 },
Paul Duffin191be3a2021-08-10 16:14:16 +01001221
Paul Duffine5218812021-06-07 13:28:19 +01001222 EncodedBootDexFilesByModule: encodedBootDexJarsByModule,
Paul Duffin1e6f5c42021-05-21 16:15:31 +01001223 }
1224
Paul Duffin191be3a2021-08-10 16:14:16 +01001225 // TODO: Temporarily fallback to stub_flags/all_flags properties until prebuilts have been updated.
1226 output.FilteredStubFlagsPath = pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Filtered_stub_flags, output.StubFlagsPath)
1227 output.FilteredFlagsPath = pathForOptionalSrc(module.prebuiltProperties.Hidden_api.Filtered_flags, output.AllFlagsPath)
1228
Paul Duffin1e6f5c42021-05-21 16:15:31 +01001229 return &output
Paul Duffin2fef1362021-04-15 13:32:00 +01001230}
1231
Paul Duffin5466a362021-06-07 10:25:31 +01001232// produceBootImageFiles extracts the boot image files from the APEX if available.
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001233func (module *PrebuiltBootclasspathFragmentModule) produceBootImageFiles(ctx android.ModuleContext, imageConfig *bootImageConfig) bootImageFilesByArch {
Paul Duffin5466a362021-06-07 10:25:31 +01001234 if !shouldCopyBootFilesToPredefinedLocations(ctx, imageConfig) {
1235 return nil
1236 }
1237
Martin Stjernholm44825602021-09-17 01:44:12 +01001238 di := android.FindDeapexerProviderForModule(ctx)
1239 if di == nil {
1240 return nil // An error has been reported by FindDeapexerProviderForModule.
Paul Duffin5466a362021-06-07 10:25:31 +01001241 }
1242
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001243 profile := (android.WritablePath)(nil)
1244 if imageConfig.profileInstallPathInApex != "" {
1245 profile = di.PrebuiltExportPath(imageConfig.profileInstallPathInApex)
Paul Duffin5466a362021-06-07 10:25:31 +01001246 }
1247
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001248 // Build the boot image files for the host variants. These are always built from the dex files
1249 // provided by the contents of this module as prebuilt versions of the host boot image files are
1250 // not available, i.e. there is no host specific prebuilt apex containing them. This has to be
1251 // built without a profile as the prebuilt modules do not provide a profile.
1252 buildBootImageVariantsForBuildOs(ctx, imageConfig, profile)
Paul Duffina56be7d2021-07-02 13:00:43 +01001253
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001254 if imageConfig.shouldInstallInApex() {
1255 // If the boot image files for the android variants are in the prebuilt apex, we must use those
1256 // rather than building new ones because those boot image files are going to be used on device.
1257 files := bootImageFilesByArch{}
1258 for _, variant := range imageConfig.apexVariants() {
1259 arch := variant.target.Arch.ArchType
1260 for _, toPath := range variant.imagesDeps {
1261 apexRelativePath := apexRootRelativePathToBootImageFile(arch, toPath.Base())
1262 // Get the path to the file that the deapexer extracted from the prebuilt apex file.
1263 fromPath := di.PrebuiltExportPath(apexRelativePath)
1264
1265 // Return the toPath as the calling code expects the paths in the returned map to be the
1266 // paths predefined in the bootImageConfig.
1267 files[arch] = append(files[arch], toPath)
1268
1269 // Copy the file to the predefined location.
1270 ctx.Build(pctx, android.BuildParams{
1271 Rule: android.Cp,
1272 Input: fromPath,
1273 Output: toPath,
1274 })
1275 }
1276 }
1277 return files
1278 } else {
1279 if profile == nil {
1280 ctx.ModuleErrorf("Unable to produce boot image files: neither boot image files nor profiles exists in the prebuilt apex")
1281 return nil
1282 }
1283 // Build boot image files for the android variants from the dex files provided by the contents
1284 // of this module.
1285 return buildBootImageVariantsForAndroidOs(ctx, imageConfig, profile)
1286 }
Paul Duffin5466a362021-06-07 10:25:31 +01001287}
1288
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001289var _ commonBootclasspathFragment = (*PrebuiltBootclasspathFragmentModule)(nil)
Paul Duffin2fef1362021-04-15 13:32:00 +01001290
Paul Duffin5466a362021-06-07 10:25:31 +01001291// createBootImageTag creates the tag to uniquely identify the boot image file among all of the
1292// files that a module requires from the prebuilt .apex file.
1293func createBootImageTag(arch android.ArchType, baseName string) string {
1294 tag := fmt.Sprintf(".bootimage-%s-%s", arch, baseName)
1295 return tag
1296}
1297
1298// RequiredFilesFromPrebuiltApex returns the list of all files the prebuilt_bootclasspath_fragment
1299// requires from a prebuilt .apex file.
1300//
1301// If there is no image config associated with this fragment then it returns nil. Otherwise, it
1302// returns the files that are listed in the image config.
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001303func (module *PrebuiltBootclasspathFragmentModule) RequiredFilesFromPrebuiltApex(ctx android.BaseModuleContext) []string {
Paul Duffin5466a362021-06-07 10:25:31 +01001304 imageConfig := module.getImageConfig(ctx)
1305 if imageConfig != nil {
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001306 files := []string{}
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001307 if imageConfig.profileInstallPathInApex != "" {
1308 // Add the boot image profile.
1309 files = append(files, imageConfig.profileInstallPathInApex)
1310 }
1311 if imageConfig.shouldInstallInApex() {
1312 // Add the boot image files, e.g. .art, .oat and .vdex files.
1313 for _, variant := range imageConfig.apexVariants() {
1314 arch := variant.target.Arch.ArchType
1315 for _, path := range variant.imagesDeps.Paths() {
1316 base := path.Base()
1317 files = append(files, apexRootRelativePathToBootImageFile(arch, base))
1318 }
Paul Duffin5466a362021-06-07 10:25:31 +01001319 }
1320 }
1321 return files
1322 }
1323 return nil
1324}
1325
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001326func apexRootRelativePathToBootImageFile(arch android.ArchType, base string) string {
1327 return filepath.Join("javalib", arch.String(), base)
1328}
1329
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001330var _ android.RequiredFilesFromPrebuiltApex = (*PrebuiltBootclasspathFragmentModule)(nil)
Paul Duffin5466a362021-06-07 10:25:31 +01001331
Paul Duffin7771eba2021-04-23 14:25:28 +01001332func prebuiltBootclasspathFragmentFactory() android.Module {
Jiakai Zhange6e90db2022-01-28 14:58:56 +00001333 m := &PrebuiltBootclasspathFragmentModule{}
Paul Duffin2fef1362021-04-15 13:32:00 +01001334 m.AddProperties(&m.properties, &m.prebuiltProperties)
Paul Duffinf7f65da2021-03-10 15:00:46 +00001335 // This doesn't actually have any prebuilt files of its own so pass a placeholder for the srcs
1336 // array.
1337 android.InitPrebuiltModule(m, &[]string{"placeholder"})
1338 android.InitApexModule(m)
1339 android.InitSdkAwareModule(m)
Martin Stjernholmb79c7f12021-03-17 00:26:25 +00001340 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +00001341
Paul Duffin7771eba2021-04-23 14:25:28 +01001342 // Initialize the contents property from the image_name.
Paul Duffinc7ef9892021-03-23 23:21:59 +00001343 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
Paul Duffin7771eba2021-04-23 14:25:28 +01001344 bootclasspathFragmentInitContentsFromImage(ctx, &m.BootclasspathFragmentModule)
Paul Duffinc7ef9892021-03-23 23:21:59 +00001345 })
Paul Duffinf7f65da2021-03-10 15:00:46 +00001346 return m
1347}