blob: ec61e70942dde02728458cba9474db20e0445c1c [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 Duffin7c955552021-04-19 13:23:53 +010019 "path/filepath"
Paul Duffinba6afd02019-11-19 19:44:10 +000020 "reflect"
Paul Duffin3451e162021-01-20 15:16:56 +000021 "strings"
22
23 "android/soong/android"
Paul Duffina1d60252021-01-21 18:13:43 +000024 "android/soong/dexpreopt"
Paul Duffinc7ef9892021-03-23 23:21:59 +000025 "github.com/google/blueprint/proptools"
Martin Stjernholmb79c7f12021-03-17 00:26:25 +000026
Paul Duffin3451e162021-01-20 15:16:56 +000027 "github.com/google/blueprint"
28)
29
30func init() {
Paul Duffin7771eba2021-04-23 14:25:28 +010031 registerBootclasspathFragmentBuildComponents(android.InitRegistrationContext)
Paul Duffinf7f65da2021-03-10 15:00:46 +000032
Paul Duffin7771eba2021-04-23 14:25:28 +010033 android.RegisterSdkMemberType(&bootclasspathFragmentMemberType{
Paul Duffin4b64ba02021-03-29 11:02:53 +010034 SdkMemberTypeBase: android.SdkMemberTypeBase{
Paul Duffin2d3da312021-05-06 12:02:27 +010035 PropertyName: "bootclasspath_fragments",
36 SupportsSdk: true,
Paul Duffin4b64ba02021-03-29 11:02:53 +010037 },
38 })
Paul Duffin3451e162021-01-20 15:16:56 +000039}
40
Paul Duffin7771eba2021-04-23 14:25:28 +010041func registerBootclasspathFragmentBuildComponents(ctx android.RegistrationContext) {
Paul Duffin7771eba2021-04-23 14:25:28 +010042 ctx.RegisterModuleType("bootclasspath_fragment", bootclasspathFragmentFactory)
43 ctx.RegisterModuleType("prebuilt_bootclasspath_fragment", prebuiltBootclasspathFragmentFactory)
Paul Duffin3451e162021-01-20 15:16:56 +000044}
45
Paul Duffin65898052021-04-20 22:47:03 +010046type bootclasspathFragmentContentDependencyTag struct {
Paul Duffinc7ef9892021-03-23 23:21:59 +000047 blueprint.BaseDependencyTag
48}
49
Paul Duffin7771eba2021-04-23 14:25:28 +010050// Avoid having to make bootclasspath_fragment content visible to the bootclasspath_fragment.
Paul Duffinc7ef9892021-03-23 23:21:59 +000051//
Paul Duffin7771eba2021-04-23 14:25:28 +010052// This is a temporary workaround to make it easier to migrate to bootclasspath_fragment modules
53// with proper dependencies.
Paul Duffinc7ef9892021-03-23 23:21:59 +000054// TODO(b/177892522): Remove this and add needed visibility.
Paul Duffin65898052021-04-20 22:47:03 +010055func (b bootclasspathFragmentContentDependencyTag) ExcludeFromVisibilityEnforcement() {
56}
57
58// The bootclasspath_fragment contents must never depend on prebuilts.
59func (b bootclasspathFragmentContentDependencyTag) ReplaceSourceWithPrebuilt() bool {
60 return false
Paul Duffinc7ef9892021-03-23 23:21:59 +000061}
62
Paul Duffine95b53a2021-04-23 20:41:23 +010063// SdkMemberType causes dependencies added with this tag to be automatically added to the sdk as if
Paul Duffina10bd3c2021-05-12 13:46:54 +010064// they were specified using java_boot_libs or java_sdk_libs.
65func (b bootclasspathFragmentContentDependencyTag) SdkMemberType(child android.Module) android.SdkMemberType {
66 // If the module is a java_sdk_library then treat it as if it was specified in the java_sdk_libs
67 // property, otherwise treat if it was specified in the java_boot_libs property.
68 if javaSdkLibrarySdkMemberType.IsInstance(child) {
69 return javaSdkLibrarySdkMemberType
70 }
71
Paul Duffine95b53a2021-04-23 20:41:23 +010072 return javaBootLibsSdkMemberType
73}
74
75func (b bootclasspathFragmentContentDependencyTag) ExportMember() bool {
76 return true
77}
78
Paul Duffin7771eba2021-04-23 14:25:28 +010079// The tag used for the dependency between the bootclasspath_fragment module and its contents.
Paul Duffin65898052021-04-20 22:47:03 +010080var bootclasspathFragmentContentDepTag = bootclasspathFragmentContentDependencyTag{}
Paul Duffinc7ef9892021-03-23 23:21:59 +000081
Paul Duffin65898052021-04-20 22:47:03 +010082var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathFragmentContentDepTag
83var _ android.ReplaceSourceWithPrebuilt = bootclasspathFragmentContentDepTag
Paul Duffine95b53a2021-04-23 20:41:23 +010084var _ android.SdkMemberTypeDependencyTag = bootclasspathFragmentContentDepTag
Paul Duffinc7ef9892021-03-23 23:21:59 +000085
Paul Duffin65898052021-04-20 22:47:03 +010086func IsBootclasspathFragmentContentDepTag(tag blueprint.DependencyTag) bool {
87 return tag == bootclasspathFragmentContentDepTag
Paul Duffin4d101b62021-03-24 15:42:20 +000088}
89
Paul Duffinc7d16442021-04-23 13:55:49 +010090// Properties that can be different when coverage is enabled.
91type BootclasspathFragmentCoverageAffectedProperties struct {
92 // The contents of this bootclasspath_fragment, could be either java_library, or java_sdk_library.
93 //
94 // The order of this list matters as it is the order that is used in the bootclasspath.
95 Contents []string
Paul Duffin10931582021-04-25 10:13:54 +010096
97 // The properties for specifying the API stubs provided by this fragment.
98 BootclasspathAPIProperties
Paul Duffinc7d16442021-04-23 13:55:49 +010099}
100
Paul Duffin7771eba2021-04-23 14:25:28 +0100101type bootclasspathFragmentProperties struct {
Paul Duffin3451e162021-01-20 15:16:56 +0000102 // The name of the image this represents.
103 //
Paul Duffin82886d62021-03-24 01:34:57 +0000104 // If specified then it must be one of "art" or "boot".
Paul Duffin64be7bb2021-03-23 23:06:38 +0000105 Image_name *string
Paul Duffinc7ef9892021-03-23 23:21:59 +0000106
Paul Duffinc7d16442021-04-23 13:55:49 +0100107 // Properties whose values need to differ with and without coverage.
108 BootclasspathFragmentCoverageAffectedProperties
109 Coverage BootclasspathFragmentCoverageAffectedProperties
Paul Duffin9b381ef2021-04-08 23:01:37 +0100110
111 Hidden_api HiddenAPIFlagFileProperties
Paul Duffin3451e162021-01-20 15:16:56 +0000112}
113
Paul Duffin7771eba2021-04-23 14:25:28 +0100114type BootclasspathFragmentModule struct {
Paul Duffin3451e162021-01-20 15:16:56 +0000115 android.ModuleBase
Paul Duffina1d60252021-01-21 18:13:43 +0000116 android.ApexModuleBase
Paul Duffinf7f65da2021-03-10 15:00:46 +0000117 android.SdkBase
satayev3db35472021-05-06 23:59:58 +0100118 ClasspathFragmentBase
119
Paul Duffin7771eba2021-04-23 14:25:28 +0100120 properties bootclasspathFragmentProperties
Paul Duffin3451e162021-01-20 15:16:56 +0000121}
122
Paul Duffin2fef1362021-04-15 13:32:00 +0100123// commonBootclasspathFragment defines the methods that are implemented by both source and prebuilt
124// bootclasspath fragment modules.
125type commonBootclasspathFragment interface {
126 // produceHiddenAPIAllFlagsFile produces the all-flags.csv and intermediate files.
127 //
128 // Updates the supplied flagFileInfo with the paths to the generated files set.
129 produceHiddenAPIAllFlagsFile(ctx android.ModuleContext, contents []android.Module, stubJarsByKind map[android.SdkKind]android.Paths, flagFileInfo *hiddenAPIFlagFileInfo)
130}
131
Paul Duffin7771eba2021-04-23 14:25:28 +0100132func bootclasspathFragmentFactory() android.Module {
133 m := &BootclasspathFragmentModule{}
Paul Duffin3451e162021-01-20 15:16:56 +0000134 m.AddProperties(&m.properties)
Paul Duffina1d60252021-01-21 18:13:43 +0000135 android.InitApexModule(m)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000136 android.InitSdkAwareModule(m)
satayev3db35472021-05-06 23:59:58 +0100137 initClasspathFragment(m, BOOTCLASSPATH)
Martin Stjernholmb79c7f12021-03-17 00:26:25 +0000138 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000139
Paul Duffinc7ef9892021-03-23 23:21:59 +0000140 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
Paul Duffinc7d16442021-04-23 13:55:49 +0100141 // If code coverage has been enabled for the framework then append the properties with
142 // coverage specific properties.
143 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
144 err := proptools.AppendProperties(&m.properties.BootclasspathFragmentCoverageAffectedProperties, &m.properties.Coverage, nil)
145 if err != nil {
146 ctx.PropertyErrorf("coverage", "error trying to append coverage specific properties: %s", err)
147 return
148 }
149 }
150
151 // Initialize the contents property from the image_name.
Paul Duffin7771eba2021-04-23 14:25:28 +0100152 bootclasspathFragmentInitContentsFromImage(ctx, m)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000153 })
Paul Duffin3451e162021-01-20 15:16:56 +0000154 return m
155}
156
Paul Duffin7771eba2021-04-23 14:25:28 +0100157// bootclasspathFragmentInitContentsFromImage will initialize the contents property from the image_name if
158// necessary.
159func bootclasspathFragmentInitContentsFromImage(ctx android.EarlyModuleContext, m *BootclasspathFragmentModule) {
Paul Duffin82886d62021-03-24 01:34:57 +0000160 contents := m.properties.Contents
161 if m.properties.Image_name == nil && len(contents) == 0 {
162 ctx.ModuleErrorf(`neither of the "image_name" and "contents" properties have been supplied, please supply exactly one`)
163 }
Paul Duffinba6afd02019-11-19 19:44:10 +0000164
Paul Duffinc7ef9892021-03-23 23:21:59 +0000165 imageName := proptools.String(m.properties.Image_name)
166 if imageName == "art" {
Paul Duffin023dba02021-04-22 01:45:29 +0100167 // TODO(b/177892522): Prebuilts (versioned or not) should not use the image_name property.
Paul Duffin0c2e0832021-04-28 00:39:52 +0100168 if android.IsModuleInVersionedSdk(m) {
Paul Duffin023dba02021-04-22 01:45:29 +0100169 // The module is a versioned prebuilt so ignore it. This is done for a couple of reasons:
170 // 1. There is no way to use this at the moment so ignoring it is safe.
171 // 2. Attempting to initialize the contents property from the configuration will end up having
172 // the versioned prebuilt depending on the unversioned prebuilt. That will cause problems
173 // as the unversioned prebuilt could end up with an APEX variant created for the source
174 // APEX which will prevent it from having an APEX variant for the prebuilt APEX which in
175 // turn will prevent it from accessing the dex implementation jar from that which will
176 // break hidden API processing, amongst others.
177 return
178 }
179
Paul Duffinc7ef9892021-03-23 23:21:59 +0000180 // Get the configuration for the art apex jars. Do not use getImageConfig(ctx) here as this is
181 // too early in the Soong processing for that to work.
182 global := dexpreopt.GetGlobalConfig(ctx)
183 modules := global.ArtApexJars
184
185 // Make sure that the apex specified in the configuration is consistent and is one for which
186 // this boot image is available.
Paul Duffinc7ef9892021-03-23 23:21:59 +0000187 commonApex := ""
188 for i := 0; i < modules.Len(); i++ {
189 apex := modules.Apex(i)
190 jar := modules.Jar(i)
191 if apex == "platform" {
192 ctx.ModuleErrorf("ArtApexJars is invalid as it requests a platform variant of %q", jar)
193 continue
194 }
195 if !m.AvailableFor(apex) {
Paul Duffinf23bc472021-04-27 12:42:20 +0100196 ctx.ModuleErrorf("ArtApexJars configuration incompatible with this module, ArtApexJars expects this to be in apex %q but this is only in apexes %q",
Paul Duffinc7ef9892021-03-23 23:21:59 +0000197 apex, m.ApexAvailable())
198 continue
199 }
200 if commonApex == "" {
201 commonApex = apex
202 } else if commonApex != apex {
203 ctx.ModuleErrorf("ArtApexJars configuration is inconsistent, expected all jars to be in the same apex but it specifies apex %q and %q",
204 commonApex, apex)
205 }
Paul Duffinc7ef9892021-03-23 23:21:59 +0000206 }
207
Paul Duffinf23bc472021-04-27 12:42:20 +0100208 if len(contents) != 0 {
209 // Nothing to do.
210 return
211 }
212
Paul Duffinc7ef9892021-03-23 23:21:59 +0000213 // Store the jars in the Contents property so that they can be used to add dependencies.
Paul Duffinba6afd02019-11-19 19:44:10 +0000214 m.properties.Contents = modules.CopyOfJars()
215 }
216}
217
218// bootclasspathImageNameContentsConsistencyCheck checks that the configuration that applies to this
219// module (if any) matches the contents.
220//
221// This should be a noop as if image_name="art" then the contents will be set from the ArtApexJars
222// config by bootclasspathFragmentInitContentsFromImage so it will be guaranteed to match. However,
223// in future this will not be the case.
224func (b *BootclasspathFragmentModule) bootclasspathImageNameContentsConsistencyCheck(ctx android.BaseModuleContext) {
225 imageName := proptools.String(b.properties.Image_name)
226 if imageName == "art" {
227 // TODO(b/177892522): Prebuilts (versioned or not) should not use the image_name property.
Paul Duffin0c2e0832021-04-28 00:39:52 +0100228 if android.IsModuleInVersionedSdk(b) {
Paul Duffinba6afd02019-11-19 19:44:10 +0000229 // The module is a versioned prebuilt so ignore it. This is done for a couple of reasons:
230 // 1. There is no way to use this at the moment so ignoring it is safe.
231 // 2. Attempting to initialize the contents property from the configuration will end up having
232 // the versioned prebuilt depending on the unversioned prebuilt. That will cause problems
233 // as the unversioned prebuilt could end up with an APEX variant created for the source
234 // APEX which will prevent it from having an APEX variant for the prebuilt APEX which in
235 // turn will prevent it from accessing the dex implementation jar from that which will
236 // break hidden API processing, amongst others.
237 return
238 }
239
240 // Get the configuration for the art apex jars.
241 modules := b.getImageConfig(ctx).modules
242 configuredJars := modules.CopyOfJars()
243
244 // Skip the check if the configured jars list is empty as that is a common configuration when
245 // building targets that do not result in a system image.
246 if len(configuredJars) == 0 {
247 return
248 }
249
250 contents := b.properties.Contents
251 if !reflect.DeepEqual(configuredJars, contents) {
252 ctx.ModuleErrorf("inconsistency in specification of contents. ArtApexJars configuration specifies %#v, contents property specifies %#v",
253 configuredJars, contents)
254 }
Paul Duffinc7ef9892021-03-23 23:21:59 +0000255 }
256}
257
Paul Duffine946b322021-04-25 23:04:00 +0100258var BootclasspathFragmentApexContentInfoProvider = blueprint.NewProvider(BootclasspathFragmentApexContentInfo{})
Paul Duffin3451e162021-01-20 15:16:56 +0000259
Paul Duffine946b322021-04-25 23:04:00 +0100260// BootclasspathFragmentApexContentInfo contains the bootclasspath_fragments contributions to the
261// apex contents.
262type BootclasspathFragmentApexContentInfo struct {
satayev3db35472021-05-06 23:59:58 +0100263 // ClasspathFragmentProtoOutput is an output path for the generated classpaths.proto config of this module.
264 //
265 // The file should be copied to a relevant place on device, see ClasspathFragmentProtoInstallDir
266 // for more details.
267 ClasspathFragmentProtoOutput android.OutputPath
268
269 // ClasspathFragmentProtoInstallDir contains information about on device location for the generated classpaths.proto file.
270 //
271 // The path encodes expected sub-location within partitions, i.e. etc/classpaths/<proto-file>,
272 // for ClasspathFragmentProtoOutput. To get sub-location, instead of the full output / make path
273 // use android.InstallPath#Rel().
274 //
275 // This is only relevant for APEX modules as they perform their own installation; while regular
276 // system files are installed via ClasspathFragmentBase#androidMkEntries().
277 ClasspathFragmentProtoInstallDir android.InstallPath
278
Paul Duffin3451e162021-01-20 15:16:56 +0000279 // The image config, internal to this module (and the dex_bootjars singleton).
Paul Duffina1d60252021-01-21 18:13:43 +0000280 //
Paul Duffine946b322021-04-25 23:04:00 +0100281 // Will be nil if the BootclasspathFragmentApexContentInfo has not been provided for a specific module. That can occur
Paul Duffina1d60252021-01-21 18:13:43 +0000282 // when SkipDexpreoptBootJars(ctx) returns true.
Paul Duffin3451e162021-01-20 15:16:56 +0000283 imageConfig *bootImageConfig
284}
285
Paul Duffine946b322021-04-25 23:04:00 +0100286func (i BootclasspathFragmentApexContentInfo) Modules() android.ConfiguredJarList {
Paul Duffin3451e162021-01-20 15:16:56 +0000287 return i.imageConfig.modules
288}
289
Paul Duffina1d60252021-01-21 18:13:43 +0000290// Get a map from ArchType to the associated boot image's contents for Android.
291//
292// Extension boot images only return their own files, not the files of the boot images they extend.
Paul Duffine946b322021-04-25 23:04:00 +0100293func (i BootclasspathFragmentApexContentInfo) AndroidBootImageFilesByArchType() map[android.ArchType]android.OutputPaths {
Paul Duffina1d60252021-01-21 18:13:43 +0000294 files := map[android.ArchType]android.OutputPaths{}
295 if i.imageConfig != nil {
296 for _, variant := range i.imageConfig.variants {
297 // We also generate boot images for host (for testing), but we don't need those in the apex.
298 // TODO(b/177892522) - consider changing this to check Os.OsClass = android.Device
299 if variant.target.Os == android.Android {
300 files[variant.target.Arch.ArchType] = variant.imagesDeps
301 }
302 }
303 }
304 return files
305}
306
Paul Duffin190fdef2021-04-26 10:33:59 +0100307// DexBootJarPathForContentModule returns the path to the dex boot jar for specified module.
308//
309// The dex boot jar is one which has had hidden API encoding performed on it.
310func (i BootclasspathFragmentApexContentInfo) DexBootJarPathForContentModule(module android.Module) android.Path {
311 j := module.(UsesLibraryDependency)
312 dexJar := j.DexJarBuildPath()
313 return dexJar
314}
315
Paul Duffin7771eba2021-04-23 14:25:28 +0100316func (b *BootclasspathFragmentModule) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffina1d60252021-01-21 18:13:43 +0000317 tag := ctx.OtherModuleDependencyTag(dep)
Paul Duffin65898052021-04-20 22:47:03 +0100318 if IsBootclasspathFragmentContentDepTag(tag) {
Paul Duffin4d101b62021-03-24 15:42:20 +0000319 // Boot image contents are automatically added to apex.
320 return true
Paul Duffinc7ef9892021-03-23 23:21:59 +0000321 }
Bob Badour07065cd2021-02-05 19:59:11 -0800322 if android.IsMetaDependencyTag(tag) {
323 // Cross-cutting metadata dependencies are metadata.
324 return false
325 }
Paul Duffina1d60252021-01-21 18:13:43 +0000326 panic(fmt.Errorf("boot_image module %q should not have a dependency on %q via tag %s", b, dep, android.PrettyPrintTag(tag)))
327}
328
Paul Duffin7771eba2021-04-23 14:25:28 +0100329func (b *BootclasspathFragmentModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Paul Duffina1d60252021-01-21 18:13:43 +0000330 return nil
331}
332
Paul Duffin65898052021-04-20 22:47:03 +0100333// ComponentDepsMutator adds dependencies onto modules before any prebuilt modules without a
334// corresponding source module are renamed. This means that adding a dependency using a name without
335// a prebuilt_ prefix will always resolve to a source module and when using a name with that prefix
336// it will always resolve to a prebuilt module.
Paul Duffin7771eba2021-04-23 14:25:28 +0100337func (b *BootclasspathFragmentModule) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin65898052021-04-20 22:47:03 +0100338 module := ctx.Module()
Paul Duffin7771eba2021-04-23 14:25:28 +0100339 _, isSourceModule := module.(*BootclasspathFragmentModule)
Paul Duffin65898052021-04-20 22:47:03 +0100340
341 for _, name := range b.properties.Contents {
342 // A bootclasspath_fragment must depend only on other source modules, while the
343 // prebuilt_bootclasspath_fragment must only depend on other prebuilt modules.
Paul Duffina9dd6fa2021-04-22 17:25:57 +0100344 //
345 // TODO(b/177892522) - avoid special handling of jacocoagent.
346 if !isSourceModule && name != "jacocoagent" {
Paul Duffin65898052021-04-20 22:47:03 +0100347 name = android.PrebuiltNameFromSource(name)
348 }
349 ctx.AddDependency(module, bootclasspathFragmentContentDepTag, name)
350 }
351
352}
353
Paul Duffin7771eba2021-04-23 14:25:28 +0100354func (b *BootclasspathFragmentModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin10931582021-04-25 10:13:54 +0100355 // Add dependencies onto all the modules that provide the API stubs for classes on this
356 // bootclasspath fragment.
357 hiddenAPIAddStubLibDependencies(ctx, b.properties.sdkKindToStubLibs())
Paul Duffinc7ef9892021-03-23 23:21:59 +0000358
Paul Duffina1d60252021-01-21 18:13:43 +0000359 if SkipDexpreoptBootJars(ctx) {
360 return
361 }
362
363 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
364 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
365 dexpreopt.RegisterToolDeps(ctx)
366}
367
Paul Duffin7771eba2021-04-23 14:25:28 +0100368func (b *BootclasspathFragmentModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffinba6afd02019-11-19 19:44:10 +0000369 // Only perform a consistency check if this module is the active module. That will prevent an
370 // unused prebuilt that was created without instrumentation from breaking an instrumentation
371 // build.
372 if isActiveModule(ctx.Module()) {
373 b.bootclasspathImageNameContentsConsistencyCheck(ctx)
374 }
375
satayev3db35472021-05-06 23:59:58 +0100376 // Generate classpaths.proto config
377 b.generateClasspathProtoBuildActions(ctx)
378
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100379 // Gather the bootclasspath fragment's contents.
380 var contents []android.Module
381 ctx.VisitDirectDeps(func(module android.Module) {
382 tag := ctx.OtherModuleDependencyTag(module)
383 if IsBootclasspathFragmentContentDepTag(tag) {
Paul Duffinf4600f62021-05-13 22:34:45 +0100384 if sdkLibrary, ok := module.(SdkLibraryDependency); ok && sdkLibrary.sharedLibrary() {
385 ctx.PropertyErrorf("contents", "invalid module: %s, shared libraries cannot be on the bootclasspath", ctx.OtherModuleName(module))
386 } else {
387 contents = append(contents, module)
388 }
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100389 }
390 })
391
Paul Duffin9b381ef2021-04-08 23:01:37 +0100392 // Perform hidden API processing.
Paul Duffin2fef1362021-04-15 13:32:00 +0100393 b.generateHiddenAPIBuildActions(ctx, contents)
Paul Duffin9b381ef2021-04-08 23:01:37 +0100394
Paul Duffin3451e162021-01-20 15:16:56 +0000395 // Construct the boot image info from the config.
satayev3db35472021-05-06 23:59:58 +0100396 info := BootclasspathFragmentApexContentInfo{
397 ClasspathFragmentProtoInstallDir: b.classpathFragmentBase().installDirPath,
398 ClasspathFragmentProtoOutput: b.classpathFragmentBase().outputFilepath,
399 imageConfig: nil,
400 }
401
402 if !SkipDexpreoptBootJars(ctx) {
403 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
404 // GenerateSingletonBuildActions method as it cannot create it for itself.
405 dexpreopt.GetGlobalSoongConfig(ctx)
406 info.imageConfig = b.getImageConfig(ctx)
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100407
408 // Only generate the boot image if the configuration does not skip it.
409 b.generateBootImageBuildActions(ctx, contents)
satayev3db35472021-05-06 23:59:58 +0100410 }
Paul Duffin3451e162021-01-20 15:16:56 +0000411
412 // Make it available for other modules.
Paul Duffine946b322021-04-25 23:04:00 +0100413 ctx.SetProvider(BootclasspathFragmentApexContentInfoProvider, info)
Paul Duffin3451e162021-01-20 15:16:56 +0000414}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000415
satayev3db35472021-05-06 23:59:58 +0100416// generateClasspathProtoBuildActions generates all required build actions for classpath.proto config
417func (b *BootclasspathFragmentModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
418 var classpathJars []classpathJar
419 if "art" == proptools.String(b.properties.Image_name) {
420 // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
421 classpathJars = configuredJarListToClasspathJars(ctx, b.ClasspathFragmentToConfiguredJarList(ctx), BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
422 } else {
423 classpathJars = configuredJarListToClasspathJars(ctx, b.ClasspathFragmentToConfiguredJarList(ctx), b.classpathType)
424 }
425 b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, classpathJars)
426}
427
428func (b *BootclasspathFragmentModule) ClasspathFragmentToConfiguredJarList(ctx android.ModuleContext) android.ConfiguredJarList {
429 // TODO(satayev): populate with actual content
430 return android.EmptyConfiguredJarList()
431}
432
Paul Duffin7771eba2021-04-23 14:25:28 +0100433func (b *BootclasspathFragmentModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
Paul Duffin64be7bb2021-03-23 23:06:38 +0000434 // Get a map of the image configs that are supported.
435 imageConfigs := genBootImageConfigs(ctx)
436
437 // Retrieve the config for this image.
438 imageNamePtr := b.properties.Image_name
439 if imageNamePtr == nil {
440 return nil
441 }
442
443 imageName := *imageNamePtr
444 imageConfig := imageConfigs[imageName]
445 if imageConfig == nil {
446 ctx.PropertyErrorf("image_name", "Unknown image name %q, expected one of %s", imageName, strings.Join(android.SortedStringKeys(imageConfigs), ", "))
447 return nil
448 }
449 return imageConfig
450}
451
Paul Duffin9b381ef2021-04-08 23:01:37 +0100452// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffin2fef1362021-04-15 13:32:00 +0100453func (b *BootclasspathFragmentModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, contents []android.Module) {
Paul Duffin10931582021-04-25 10:13:54 +0100454
455 // Convert the kind specific lists of modules into kind specific lists of jars.
456 stubJarsByKind := hiddenAPIGatherStubLibDexJarPaths(ctx)
457
458 // Store the information for use by other modules.
459 bootclasspathApiInfo := bootclasspathApiInfo{stubJarsByKind: stubJarsByKind}
460 ctx.SetProvider(bootclasspathApiInfoProvider, bootclasspathApiInfo)
Paul Duffin2fef1362021-04-15 13:32:00 +0100461
462 // Resolve the properties to paths.
463 flagFileInfo := b.properties.Hidden_api.hiddenAPIFlagFileInfo(ctx)
464
465 // Delegate the production of the hidden API all flags file to a module type specific method.
466 common := ctx.Module().(commonBootclasspathFragment)
467 common.produceHiddenAPIAllFlagsFile(ctx, contents, stubJarsByKind, &flagFileInfo)
468
469 // Store the information for use by platform_bootclasspath.
470 ctx.SetProvider(hiddenAPIFlagFileInfoProvider, flagFileInfo)
471}
472
473// produceHiddenAPIAllFlagsFile produces the hidden API all-flags.csv file (and supporting files)
474// for the fragment.
475func (b *BootclasspathFragmentModule) produceHiddenAPIAllFlagsFile(ctx android.ModuleContext, contents []android.Module, stubJarsByKind map[android.SdkKind]android.Paths, flagFileInfo *hiddenAPIFlagFileInfo) {
476 // If no stubs have been provided then don't perform hidden API processing. This is a temporary
477 // workaround to avoid existing bootclasspath_fragments that do not provide stubs breaking the
478 // build.
479 // TODO(b/179354495): Remove this workaround.
480 if len(stubJarsByKind) == 0 {
481 // Nothing to do.
482 return
483 }
484
485 // Generate the rules to create the hidden API flags and update the supplied flagFileInfo with the
486 // paths to the created files.
487 hiddenAPIGenerateAllFlagsForBootclasspathFragment(ctx, contents, stubJarsByKind, flagFileInfo)
Paul Duffin9b381ef2021-04-08 23:01:37 +0100488}
489
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100490// generateBootImageBuildActions generates ninja rules to create the boot image if required for this
491// module.
492func (b *BootclasspathFragmentModule) generateBootImageBuildActions(ctx android.ModuleContext, contents []android.Module) {
493 global := dexpreopt.GetGlobalConfig(ctx)
494 if !shouldBuildBootImages(ctx.Config(), global) {
495 return
496 }
497
498 // Bootclasspath fragment modules that are not preferred do not produce a boot image.
499 if !isActiveModule(ctx.Module()) {
500 return
501 }
502
503 // Bootclasspath fragment modules that have no image_name property do not produce a boot image.
504 imageConfig := b.getImageConfig(ctx)
505 if imageConfig == nil {
506 return
507 }
508
509 // Bootclasspath fragment modules that are for the platform do not produce a boot image.
510 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
511 if apexInfo.IsForPlatform() {
512 return
513 }
514
515 // Bootclasspath fragment modules that are versioned do not produce a boot image.
516 if android.IsModuleInVersionedSdk(ctx.Module()) {
517 return
518 }
519
520 // Copy the dex jars of this fragment's content modules to their predefined locations.
521 copyBootJarsToPredefinedLocations(ctx, contents, imageConfig.modules, imageConfig.dexPaths)
Paul Duffin2fc82ad2021-04-29 23:36:12 +0100522
523 // Build a profile for the image config and then use that to build the boot image.
524 profile := bootImageProfileRule(ctx, imageConfig)
525 buildBootImage(ctx, imageConfig, profile)
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100526}
527
Paul Duffin7771eba2021-04-23 14:25:28 +0100528type bootclasspathFragmentMemberType struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000529 android.SdkMemberTypeBase
530}
531
Paul Duffin7771eba2021-04-23 14:25:28 +0100532func (b *bootclasspathFragmentMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000533 mctx.AddVariationDependencies(nil, dependencyTag, names...)
534}
535
Paul Duffin7771eba2021-04-23 14:25:28 +0100536func (b *bootclasspathFragmentMemberType) IsInstance(module android.Module) bool {
537 _, ok := module.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000538 return ok
539}
540
Paul Duffin7771eba2021-04-23 14:25:28 +0100541func (b *bootclasspathFragmentMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
Paul Duffin4b64ba02021-03-29 11:02:53 +0100542 if b.PropertyName == "boot_images" {
543 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_boot_image")
544 } else {
545 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_bootclasspath_fragment")
546 }
Paul Duffinf7f65da2021-03-10 15:00:46 +0000547}
548
Paul Duffin7771eba2021-04-23 14:25:28 +0100549func (b *bootclasspathFragmentMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
550 return &bootclasspathFragmentSdkMemberProperties{}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000551}
552
Paul Duffin7771eba2021-04-23 14:25:28 +0100553type bootclasspathFragmentSdkMemberProperties struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000554 android.SdkMemberPropertiesBase
555
Paul Duffina57835e2021-04-19 13:23:06 +0100556 // The image name
Paul Duffin64be7bb2021-03-23 23:06:38 +0000557 Image_name *string
Paul Duffina57835e2021-04-19 13:23:06 +0100558
559 // Contents of the bootclasspath fragment
560 Contents []string
Paul Duffin7c955552021-04-19 13:23:53 +0100561
Paul Duffin895c7142021-04-25 13:40:15 +0100562 // Stub_libs properties.
563 Stub_libs []string
564 Core_platform_stub_libs []string
565
Paul Duffin7c955552021-04-19 13:23:53 +0100566 // Flag files by *hiddenAPIFlagFileCategory
567 Flag_files_by_category map[*hiddenAPIFlagFileCategory]android.Paths
Paul Duffin2fef1362021-04-15 13:32:00 +0100568
569 // The path to the generated stub-flags.csv file.
570 Stub_flags_path android.OptionalPath
571
572 // The path to the generated annotation-flags.csv file.
573 Annotation_flags_path android.OptionalPath
574
575 // The path to the generated metadata.csv file.
576 Metadata_path android.OptionalPath
577
578 // The path to the generated index.csv file.
579 Index_path android.OptionalPath
580
581 // The path to the generated all-flags.csv file.
582 All_flags_path android.OptionalPath
583}
584
585func pathsToOptionalPath(paths android.Paths) android.OptionalPath {
586 switch len(paths) {
587 case 0:
588 return android.OptionalPath{}
589 case 1:
590 return android.OptionalPathForPath(paths[0])
591 default:
592 panic(fmt.Errorf("expected 0 or 1 paths, found %q", paths))
593 }
Paul Duffinf7f65da2021-03-10 15:00:46 +0000594}
595
Paul Duffin7771eba2021-04-23 14:25:28 +0100596func (b *bootclasspathFragmentSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
597 module := variant.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000598
599 b.Image_name = module.properties.Image_name
Paul Duffin2dc665b2021-04-23 16:58:51 +0100600 b.Contents = module.properties.Contents
Paul Duffin7c955552021-04-19 13:23:53 +0100601
602 // Get the flag file information from the module.
603 mctx := ctx.SdkModuleContext()
604 flagFileInfo := mctx.OtherModuleProvider(module, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo)
605 b.Flag_files_by_category = flagFileInfo.categoryToPaths
Paul Duffin895c7142021-04-25 13:40:15 +0100606
Paul Duffin2fef1362021-04-15 13:32:00 +0100607 // Copy all the generated file paths.
608 b.Stub_flags_path = pathsToOptionalPath(flagFileInfo.StubFlagsPaths)
609 b.Annotation_flags_path = pathsToOptionalPath(flagFileInfo.AnnotationFlagsPaths)
610 b.Metadata_path = pathsToOptionalPath(flagFileInfo.MetadataPaths)
611 b.Index_path = pathsToOptionalPath(flagFileInfo.IndexPaths)
612 b.All_flags_path = pathsToOptionalPath(flagFileInfo.AllFlagsPaths)
613
Paul Duffin895c7142021-04-25 13:40:15 +0100614 // Copy stub_libs properties.
615 b.Stub_libs = module.properties.Api.Stub_libs
616 b.Core_platform_stub_libs = module.properties.Core_platform_api.Stub_libs
Paul Duffinf7f65da2021-03-10 15:00:46 +0000617}
618
Paul Duffin7771eba2021-04-23 14:25:28 +0100619func (b *bootclasspathFragmentSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffin64be7bb2021-03-23 23:06:38 +0000620 if b.Image_name != nil {
621 propertySet.AddProperty("image_name", *b.Image_name)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000622 }
Paul Duffina57835e2021-04-19 13:23:06 +0100623
Paul Duffin895c7142021-04-25 13:40:15 +0100624 builder := ctx.SnapshotBuilder()
625 requiredMemberDependency := builder.SdkMemberReferencePropertyTag(true)
626
Paul Duffina57835e2021-04-19 13:23:06 +0100627 if len(b.Contents) > 0 {
Paul Duffin895c7142021-04-25 13:40:15 +0100628 propertySet.AddPropertyWithTag("contents", b.Contents, requiredMemberDependency)
Paul Duffina57835e2021-04-19 13:23:06 +0100629 }
Paul Duffin7c955552021-04-19 13:23:53 +0100630
Paul Duffin895c7142021-04-25 13:40:15 +0100631 if len(b.Stub_libs) > 0 {
632 apiPropertySet := propertySet.AddPropertySet("api")
633 apiPropertySet.AddPropertyWithTag("stub_libs", b.Stub_libs, requiredMemberDependency)
634 }
635 if len(b.Core_platform_stub_libs) > 0 {
636 corePlatformApiPropertySet := propertySet.AddPropertySet("core_platform_api")
637 corePlatformApiPropertySet.AddPropertyWithTag("stub_libs", b.Core_platform_stub_libs, requiredMemberDependency)
638 }
639
Paul Duffin2fef1362021-04-15 13:32:00 +0100640 hiddenAPISet := propertySet.AddPropertySet("hidden_api")
641 hiddenAPIDir := "hiddenapi"
642
643 // Copy manually curated flag files specified on the bootclasspath_fragment.
Paul Duffin7c955552021-04-19 13:23:53 +0100644 if b.Flag_files_by_category != nil {
Paul Duffin7c955552021-04-19 13:23:53 +0100645 for _, category := range hiddenAPIFlagFileCategories {
646 paths := b.Flag_files_by_category[category]
647 if len(paths) > 0 {
648 dests := []string{}
649 for _, p := range paths {
Paul Duffin2fef1362021-04-15 13:32:00 +0100650 dest := filepath.Join(hiddenAPIDir, p.Base())
Paul Duffin7c955552021-04-19 13:23:53 +0100651 builder.CopyToSnapshot(p, dest)
652 dests = append(dests, dest)
653 }
654 hiddenAPISet.AddProperty(category.propertyName, dests)
655 }
656 }
657 }
Paul Duffin2fef1362021-04-15 13:32:00 +0100658
659 copyOptionalPath := func(path android.OptionalPath, property string) {
660 if path.Valid() {
661 p := path.Path()
662 dest := filepath.Join(hiddenAPIDir, p.Base())
663 builder.CopyToSnapshot(p, dest)
664 hiddenAPISet.AddProperty(property, dest)
665 }
666 }
667
668 // Copy all the generated files, if available.
669 copyOptionalPath(b.Stub_flags_path, "stub_flags")
670 copyOptionalPath(b.Annotation_flags_path, "annotation_flags")
671 copyOptionalPath(b.Metadata_path, "metadata")
672 copyOptionalPath(b.Index_path, "index")
673 copyOptionalPath(b.All_flags_path, "all_flags")
Paul Duffinf7f65da2021-03-10 15:00:46 +0000674}
675
Paul Duffin7771eba2021-04-23 14:25:28 +0100676var _ android.SdkMemberType = (*bootclasspathFragmentMemberType)(nil)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000677
Paul Duffin2fef1362021-04-15 13:32:00 +0100678// prebuiltBootclasspathFragmentProperties contains additional prebuilt_bootclasspath_fragment
679// specific properties.
680type prebuiltBootclasspathFragmentProperties struct {
681 Hidden_api struct {
682 // The path to the stub-flags.csv file created by the bootclasspath_fragment.
683 Stub_flags *string `android:"path"`
684
685 // The path to the annotation-flags.csv file created by the bootclasspath_fragment.
686 Annotation_flags *string `android:"path"`
687
688 // The path to the metadata.csv file created by the bootclasspath_fragment.
689 Metadata *string `android:"path"`
690
691 // The path to the index.csv file created by the bootclasspath_fragment.
692 Index *string `android:"path"`
693
694 // The path to the all-flags.csv file created by the bootclasspath_fragment.
695 All_flags *string `android:"path"`
696 }
697}
698
Paul Duffin7771eba2021-04-23 14:25:28 +0100699// A prebuilt version of the bootclasspath_fragment module.
Paul Duffinf7f65da2021-03-10 15:00:46 +0000700//
Paul Duffin7771eba2021-04-23 14:25:28 +0100701// At the moment this is basically just a bootclasspath_fragment module that can be used as a
702// prebuilt. Eventually as more functionality is migrated into the bootclasspath_fragment module
703// type from the various singletons then this will diverge.
704type prebuiltBootclasspathFragmentModule struct {
705 BootclasspathFragmentModule
Paul Duffinf7f65da2021-03-10 15:00:46 +0000706 prebuilt android.Prebuilt
Paul Duffin2fef1362021-04-15 13:32:00 +0100707
708 // Additional prebuilt specific properties.
709 prebuiltProperties prebuiltBootclasspathFragmentProperties
Paul Duffinf7f65da2021-03-10 15:00:46 +0000710}
711
Paul Duffin7771eba2021-04-23 14:25:28 +0100712func (module *prebuiltBootclasspathFragmentModule) Prebuilt() *android.Prebuilt {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000713 return &module.prebuilt
714}
715
Paul Duffin7771eba2021-04-23 14:25:28 +0100716func (module *prebuiltBootclasspathFragmentModule) Name() string {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000717 return module.prebuilt.Name(module.ModuleBase.Name())
718}
719
Paul Duffin2fef1362021-04-15 13:32:00 +0100720// produceHiddenAPIAllFlagsFile returns a path to the prebuilt all-flags.csv or nil if none is
721// specified.
722func (module *prebuiltBootclasspathFragmentModule) produceHiddenAPIAllFlagsFile(ctx android.ModuleContext, _ []android.Module, _ map[android.SdkKind]android.Paths, flagFileInfo *hiddenAPIFlagFileInfo) {
723 pathsForOptionalSrc := func(src *string) android.Paths {
724 if src == nil {
725 // TODO(b/179354495): Fail if this is not provided once prebuilts have been updated.
726 return nil
727 }
728 return android.Paths{android.PathForModuleSrc(ctx, *src)}
729 }
730
731 flagFileInfo.StubFlagsPaths = pathsForOptionalSrc(module.prebuiltProperties.Hidden_api.Stub_flags)
732 flagFileInfo.AnnotationFlagsPaths = pathsForOptionalSrc(module.prebuiltProperties.Hidden_api.Annotation_flags)
733 flagFileInfo.MetadataPaths = pathsForOptionalSrc(module.prebuiltProperties.Hidden_api.Metadata)
734 flagFileInfo.IndexPaths = pathsForOptionalSrc(module.prebuiltProperties.Hidden_api.Index)
735 flagFileInfo.AllFlagsPaths = pathsForOptionalSrc(module.prebuiltProperties.Hidden_api.All_flags)
736}
737
738var _ commonBootclasspathFragment = (*prebuiltBootclasspathFragmentModule)(nil)
739
Paul Duffin7771eba2021-04-23 14:25:28 +0100740func prebuiltBootclasspathFragmentFactory() android.Module {
741 m := &prebuiltBootclasspathFragmentModule{}
Paul Duffin2fef1362021-04-15 13:32:00 +0100742 m.AddProperties(&m.properties, &m.prebuiltProperties)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000743 // This doesn't actually have any prebuilt files of its own so pass a placeholder for the srcs
744 // array.
745 android.InitPrebuiltModule(m, &[]string{"placeholder"})
746 android.InitApexModule(m)
747 android.InitSdkAwareModule(m)
Martin Stjernholmb79c7f12021-03-17 00:26:25 +0000748 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000749
Paul Duffin7771eba2021-04-23 14:25:28 +0100750 // Initialize the contents property from the image_name.
Paul Duffinc7ef9892021-03-23 23:21:59 +0000751 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
Paul Duffin7771eba2021-04-23 14:25:28 +0100752 bootclasspathFragmentInitContentsFromImage(ctx, &m.BootclasspathFragmentModule)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000753 })
Paul Duffinf7f65da2021-03-10 15:00:46 +0000754 return m
755}