blob: 5d985067a94c6bba2f6ddd129db6ee139e46405a [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 Duffin7771eba2021-04-23 14:25:28 +0100123func bootclasspathFragmentFactory() android.Module {
124 m := &BootclasspathFragmentModule{}
Paul Duffin3451e162021-01-20 15:16:56 +0000125 m.AddProperties(&m.properties)
Paul Duffina1d60252021-01-21 18:13:43 +0000126 android.InitApexModule(m)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000127 android.InitSdkAwareModule(m)
satayev3db35472021-05-06 23:59:58 +0100128 initClasspathFragment(m, BOOTCLASSPATH)
Martin Stjernholmb79c7f12021-03-17 00:26:25 +0000129 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000130
Paul Duffinc7ef9892021-03-23 23:21:59 +0000131 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
Paul Duffinc7d16442021-04-23 13:55:49 +0100132 // If code coverage has been enabled for the framework then append the properties with
133 // coverage specific properties.
134 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
135 err := proptools.AppendProperties(&m.properties.BootclasspathFragmentCoverageAffectedProperties, &m.properties.Coverage, nil)
136 if err != nil {
137 ctx.PropertyErrorf("coverage", "error trying to append coverage specific properties: %s", err)
138 return
139 }
140 }
141
142 // Initialize the contents property from the image_name.
Paul Duffin7771eba2021-04-23 14:25:28 +0100143 bootclasspathFragmentInitContentsFromImage(ctx, m)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000144 })
Paul Duffin3451e162021-01-20 15:16:56 +0000145 return m
146}
147
Paul Duffin7771eba2021-04-23 14:25:28 +0100148// bootclasspathFragmentInitContentsFromImage will initialize the contents property from the image_name if
149// necessary.
150func bootclasspathFragmentInitContentsFromImage(ctx android.EarlyModuleContext, m *BootclasspathFragmentModule) {
Paul Duffin82886d62021-03-24 01:34:57 +0000151 contents := m.properties.Contents
152 if m.properties.Image_name == nil && len(contents) == 0 {
153 ctx.ModuleErrorf(`neither of the "image_name" and "contents" properties have been supplied, please supply exactly one`)
154 }
Paul Duffinba6afd02019-11-19 19:44:10 +0000155
Paul Duffinc7ef9892021-03-23 23:21:59 +0000156 imageName := proptools.String(m.properties.Image_name)
157 if imageName == "art" {
Paul Duffin023dba02021-04-22 01:45:29 +0100158 // TODO(b/177892522): Prebuilts (versioned or not) should not use the image_name property.
Paul Duffin0c2e0832021-04-28 00:39:52 +0100159 if android.IsModuleInVersionedSdk(m) {
Paul Duffin023dba02021-04-22 01:45:29 +0100160 // The module is a versioned prebuilt so ignore it. This is done for a couple of reasons:
161 // 1. There is no way to use this at the moment so ignoring it is safe.
162 // 2. Attempting to initialize the contents property from the configuration will end up having
163 // the versioned prebuilt depending on the unversioned prebuilt. That will cause problems
164 // as the unversioned prebuilt could end up with an APEX variant created for the source
165 // APEX which will prevent it from having an APEX variant for the prebuilt APEX which in
166 // turn will prevent it from accessing the dex implementation jar from that which will
167 // break hidden API processing, amongst others.
168 return
169 }
170
Paul Duffinc7ef9892021-03-23 23:21:59 +0000171 // Get the configuration for the art apex jars. Do not use getImageConfig(ctx) here as this is
172 // too early in the Soong processing for that to work.
173 global := dexpreopt.GetGlobalConfig(ctx)
174 modules := global.ArtApexJars
175
176 // Make sure that the apex specified in the configuration is consistent and is one for which
177 // this boot image is available.
Paul Duffinc7ef9892021-03-23 23:21:59 +0000178 commonApex := ""
179 for i := 0; i < modules.Len(); i++ {
180 apex := modules.Apex(i)
181 jar := modules.Jar(i)
182 if apex == "platform" {
183 ctx.ModuleErrorf("ArtApexJars is invalid as it requests a platform variant of %q", jar)
184 continue
185 }
186 if !m.AvailableFor(apex) {
Paul Duffinf23bc472021-04-27 12:42:20 +0100187 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 +0000188 apex, m.ApexAvailable())
189 continue
190 }
191 if commonApex == "" {
192 commonApex = apex
193 } else if commonApex != apex {
194 ctx.ModuleErrorf("ArtApexJars configuration is inconsistent, expected all jars to be in the same apex but it specifies apex %q and %q",
195 commonApex, apex)
196 }
Paul Duffinc7ef9892021-03-23 23:21:59 +0000197 }
198
Paul Duffinf23bc472021-04-27 12:42:20 +0100199 if len(contents) != 0 {
200 // Nothing to do.
201 return
202 }
203
Paul Duffinc7ef9892021-03-23 23:21:59 +0000204 // Store the jars in the Contents property so that they can be used to add dependencies.
Paul Duffinba6afd02019-11-19 19:44:10 +0000205 m.properties.Contents = modules.CopyOfJars()
206 }
207}
208
209// bootclasspathImageNameContentsConsistencyCheck checks that the configuration that applies to this
210// module (if any) matches the contents.
211//
212// This should be a noop as if image_name="art" then the contents will be set from the ArtApexJars
213// config by bootclasspathFragmentInitContentsFromImage so it will be guaranteed to match. However,
214// in future this will not be the case.
215func (b *BootclasspathFragmentModule) bootclasspathImageNameContentsConsistencyCheck(ctx android.BaseModuleContext) {
216 imageName := proptools.String(b.properties.Image_name)
217 if imageName == "art" {
218 // TODO(b/177892522): Prebuilts (versioned or not) should not use the image_name property.
Paul Duffin0c2e0832021-04-28 00:39:52 +0100219 if android.IsModuleInVersionedSdk(b) {
Paul Duffinba6afd02019-11-19 19:44:10 +0000220 // The module is a versioned prebuilt so ignore it. This is done for a couple of reasons:
221 // 1. There is no way to use this at the moment so ignoring it is safe.
222 // 2. Attempting to initialize the contents property from the configuration will end up having
223 // the versioned prebuilt depending on the unversioned prebuilt. That will cause problems
224 // as the unversioned prebuilt could end up with an APEX variant created for the source
225 // APEX which will prevent it from having an APEX variant for the prebuilt APEX which in
226 // turn will prevent it from accessing the dex implementation jar from that which will
227 // break hidden API processing, amongst others.
228 return
229 }
230
231 // Get the configuration for the art apex jars.
232 modules := b.getImageConfig(ctx).modules
233 configuredJars := modules.CopyOfJars()
234
235 // Skip the check if the configured jars list is empty as that is a common configuration when
236 // building targets that do not result in a system image.
237 if len(configuredJars) == 0 {
238 return
239 }
240
241 contents := b.properties.Contents
242 if !reflect.DeepEqual(configuredJars, contents) {
243 ctx.ModuleErrorf("inconsistency in specification of contents. ArtApexJars configuration specifies %#v, contents property specifies %#v",
244 configuredJars, contents)
245 }
Paul Duffinc7ef9892021-03-23 23:21:59 +0000246 }
247}
248
Paul Duffine946b322021-04-25 23:04:00 +0100249var BootclasspathFragmentApexContentInfoProvider = blueprint.NewProvider(BootclasspathFragmentApexContentInfo{})
Paul Duffin3451e162021-01-20 15:16:56 +0000250
Paul Duffine946b322021-04-25 23:04:00 +0100251// BootclasspathFragmentApexContentInfo contains the bootclasspath_fragments contributions to the
252// apex contents.
253type BootclasspathFragmentApexContentInfo struct {
satayev3db35472021-05-06 23:59:58 +0100254 // ClasspathFragmentProtoOutput is an output path for the generated classpaths.proto config of this module.
255 //
256 // The file should be copied to a relevant place on device, see ClasspathFragmentProtoInstallDir
257 // for more details.
258 ClasspathFragmentProtoOutput android.OutputPath
259
260 // ClasspathFragmentProtoInstallDir contains information about on device location for the generated classpaths.proto file.
261 //
262 // The path encodes expected sub-location within partitions, i.e. etc/classpaths/<proto-file>,
263 // for ClasspathFragmentProtoOutput. To get sub-location, instead of the full output / make path
264 // use android.InstallPath#Rel().
265 //
266 // This is only relevant for APEX modules as they perform their own installation; while regular
267 // system files are installed via ClasspathFragmentBase#androidMkEntries().
268 ClasspathFragmentProtoInstallDir android.InstallPath
269
Paul Duffin3451e162021-01-20 15:16:56 +0000270 // The image config, internal to this module (and the dex_bootjars singleton).
Paul Duffina1d60252021-01-21 18:13:43 +0000271 //
Paul Duffine946b322021-04-25 23:04:00 +0100272 // Will be nil if the BootclasspathFragmentApexContentInfo has not been provided for a specific module. That can occur
Paul Duffina1d60252021-01-21 18:13:43 +0000273 // when SkipDexpreoptBootJars(ctx) returns true.
Paul Duffin3451e162021-01-20 15:16:56 +0000274 imageConfig *bootImageConfig
275}
276
Paul Duffine946b322021-04-25 23:04:00 +0100277func (i BootclasspathFragmentApexContentInfo) Modules() android.ConfiguredJarList {
Paul Duffin3451e162021-01-20 15:16:56 +0000278 return i.imageConfig.modules
279}
280
Paul Duffina1d60252021-01-21 18:13:43 +0000281// Get a map from ArchType to the associated boot image's contents for Android.
282//
283// Extension boot images only return their own files, not the files of the boot images they extend.
Paul Duffine946b322021-04-25 23:04:00 +0100284func (i BootclasspathFragmentApexContentInfo) AndroidBootImageFilesByArchType() map[android.ArchType]android.OutputPaths {
Paul Duffina1d60252021-01-21 18:13:43 +0000285 files := map[android.ArchType]android.OutputPaths{}
286 if i.imageConfig != nil {
287 for _, variant := range i.imageConfig.variants {
288 // We also generate boot images for host (for testing), but we don't need those in the apex.
289 // TODO(b/177892522) - consider changing this to check Os.OsClass = android.Device
290 if variant.target.Os == android.Android {
291 files[variant.target.Arch.ArchType] = variant.imagesDeps
292 }
293 }
294 }
295 return files
296}
297
Paul Duffin190fdef2021-04-26 10:33:59 +0100298// DexBootJarPathForContentModule returns the path to the dex boot jar for specified module.
299//
300// The dex boot jar is one which has had hidden API encoding performed on it.
301func (i BootclasspathFragmentApexContentInfo) DexBootJarPathForContentModule(module android.Module) android.Path {
302 j := module.(UsesLibraryDependency)
303 dexJar := j.DexJarBuildPath()
304 return dexJar
305}
306
Paul Duffin7771eba2021-04-23 14:25:28 +0100307func (b *BootclasspathFragmentModule) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffina1d60252021-01-21 18:13:43 +0000308 tag := ctx.OtherModuleDependencyTag(dep)
Paul Duffin65898052021-04-20 22:47:03 +0100309 if IsBootclasspathFragmentContentDepTag(tag) {
Paul Duffin4d101b62021-03-24 15:42:20 +0000310 // Boot image contents are automatically added to apex.
311 return true
Paul Duffinc7ef9892021-03-23 23:21:59 +0000312 }
Bob Badour07065cd2021-02-05 19:59:11 -0800313 if android.IsMetaDependencyTag(tag) {
314 // Cross-cutting metadata dependencies are metadata.
315 return false
316 }
Paul Duffina1d60252021-01-21 18:13:43 +0000317 panic(fmt.Errorf("boot_image module %q should not have a dependency on %q via tag %s", b, dep, android.PrettyPrintTag(tag)))
318}
319
Paul Duffin7771eba2021-04-23 14:25:28 +0100320func (b *BootclasspathFragmentModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Paul Duffina1d60252021-01-21 18:13:43 +0000321 return nil
322}
323
Paul Duffin65898052021-04-20 22:47:03 +0100324// ComponentDepsMutator adds dependencies onto modules before any prebuilt modules without a
325// corresponding source module are renamed. This means that adding a dependency using a name without
326// a prebuilt_ prefix will always resolve to a source module and when using a name with that prefix
327// it will always resolve to a prebuilt module.
Paul Duffin7771eba2021-04-23 14:25:28 +0100328func (b *BootclasspathFragmentModule) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin65898052021-04-20 22:47:03 +0100329 module := ctx.Module()
Paul Duffin7771eba2021-04-23 14:25:28 +0100330 _, isSourceModule := module.(*BootclasspathFragmentModule)
Paul Duffin65898052021-04-20 22:47:03 +0100331
332 for _, name := range b.properties.Contents {
333 // A bootclasspath_fragment must depend only on other source modules, while the
334 // prebuilt_bootclasspath_fragment must only depend on other prebuilt modules.
Paul Duffina9dd6fa2021-04-22 17:25:57 +0100335 //
336 // TODO(b/177892522) - avoid special handling of jacocoagent.
337 if !isSourceModule && name != "jacocoagent" {
Paul Duffin65898052021-04-20 22:47:03 +0100338 name = android.PrebuiltNameFromSource(name)
339 }
340 ctx.AddDependency(module, bootclasspathFragmentContentDepTag, name)
341 }
342
343}
344
Paul Duffin7771eba2021-04-23 14:25:28 +0100345func (b *BootclasspathFragmentModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin10931582021-04-25 10:13:54 +0100346 // Add dependencies onto all the modules that provide the API stubs for classes on this
347 // bootclasspath fragment.
348 hiddenAPIAddStubLibDependencies(ctx, b.properties.sdkKindToStubLibs())
Paul Duffinc7ef9892021-03-23 23:21:59 +0000349
Paul Duffina1d60252021-01-21 18:13:43 +0000350 if SkipDexpreoptBootJars(ctx) {
351 return
352 }
353
354 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
355 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
356 dexpreopt.RegisterToolDeps(ctx)
357}
358
Paul Duffin7771eba2021-04-23 14:25:28 +0100359func (b *BootclasspathFragmentModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffinba6afd02019-11-19 19:44:10 +0000360 // Only perform a consistency check if this module is the active module. That will prevent an
361 // unused prebuilt that was created without instrumentation from breaking an instrumentation
362 // build.
363 if isActiveModule(ctx.Module()) {
364 b.bootclasspathImageNameContentsConsistencyCheck(ctx)
365 }
366
satayev3db35472021-05-06 23:59:58 +0100367 // Generate classpaths.proto config
368 b.generateClasspathProtoBuildActions(ctx)
369
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100370 // Gather the bootclasspath fragment's contents.
371 var contents []android.Module
372 ctx.VisitDirectDeps(func(module android.Module) {
373 tag := ctx.OtherModuleDependencyTag(module)
374 if IsBootclasspathFragmentContentDepTag(tag) {
375 contents = append(contents, module)
376 }
377 })
378
Paul Duffin9b381ef2021-04-08 23:01:37 +0100379 // Perform hidden API processing.
380 b.generateHiddenAPIBuildActions(ctx)
381
Paul Duffin3451e162021-01-20 15:16:56 +0000382 // Construct the boot image info from the config.
satayev3db35472021-05-06 23:59:58 +0100383 info := BootclasspathFragmentApexContentInfo{
384 ClasspathFragmentProtoInstallDir: b.classpathFragmentBase().installDirPath,
385 ClasspathFragmentProtoOutput: b.classpathFragmentBase().outputFilepath,
386 imageConfig: nil,
387 }
388
389 if !SkipDexpreoptBootJars(ctx) {
390 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
391 // GenerateSingletonBuildActions method as it cannot create it for itself.
392 dexpreopt.GetGlobalSoongConfig(ctx)
393 info.imageConfig = b.getImageConfig(ctx)
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100394
395 // Only generate the boot image if the configuration does not skip it.
396 b.generateBootImageBuildActions(ctx, contents)
satayev3db35472021-05-06 23:59:58 +0100397 }
Paul Duffin3451e162021-01-20 15:16:56 +0000398
399 // Make it available for other modules.
Paul Duffine946b322021-04-25 23:04:00 +0100400 ctx.SetProvider(BootclasspathFragmentApexContentInfoProvider, info)
Paul Duffin3451e162021-01-20 15:16:56 +0000401}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000402
satayev3db35472021-05-06 23:59:58 +0100403// generateClasspathProtoBuildActions generates all required build actions for classpath.proto config
404func (b *BootclasspathFragmentModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
405 var classpathJars []classpathJar
406 if "art" == proptools.String(b.properties.Image_name) {
407 // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
408 classpathJars = configuredJarListToClasspathJars(ctx, b.ClasspathFragmentToConfiguredJarList(ctx), BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
409 } else {
410 classpathJars = configuredJarListToClasspathJars(ctx, b.ClasspathFragmentToConfiguredJarList(ctx), b.classpathType)
411 }
412 b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, classpathJars)
413}
414
415func (b *BootclasspathFragmentModule) ClasspathFragmentToConfiguredJarList(ctx android.ModuleContext) android.ConfiguredJarList {
416 // TODO(satayev): populate with actual content
417 return android.EmptyConfiguredJarList()
418}
419
Paul Duffin7771eba2021-04-23 14:25:28 +0100420func (b *BootclasspathFragmentModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
Paul Duffin64be7bb2021-03-23 23:06:38 +0000421 // Get a map of the image configs that are supported.
422 imageConfigs := genBootImageConfigs(ctx)
423
424 // Retrieve the config for this image.
425 imageNamePtr := b.properties.Image_name
426 if imageNamePtr == nil {
427 return nil
428 }
429
430 imageName := *imageNamePtr
431 imageConfig := imageConfigs[imageName]
432 if imageConfig == nil {
433 ctx.PropertyErrorf("image_name", "Unknown image name %q, expected one of %s", imageName, strings.Join(android.SortedStringKeys(imageConfigs), ", "))
434 return nil
435 }
436 return imageConfig
437}
438
Paul Duffin9b381ef2021-04-08 23:01:37 +0100439// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffin7771eba2021-04-23 14:25:28 +0100440func (b *BootclasspathFragmentModule) generateHiddenAPIBuildActions(ctx android.ModuleContext) {
Paul Duffin9b381ef2021-04-08 23:01:37 +0100441 // Resolve the properties to paths.
442 flagFileInfo := b.properties.Hidden_api.hiddenAPIFlagFileInfo(ctx)
443
444 // Store the information for use by platform_bootclasspath.
445 ctx.SetProvider(hiddenAPIFlagFileInfoProvider, flagFileInfo)
Paul Duffin10931582021-04-25 10:13:54 +0100446
447 // Convert the kind specific lists of modules into kind specific lists of jars.
448 stubJarsByKind := hiddenAPIGatherStubLibDexJarPaths(ctx)
449
450 // Store the information for use by other modules.
451 bootclasspathApiInfo := bootclasspathApiInfo{stubJarsByKind: stubJarsByKind}
452 ctx.SetProvider(bootclasspathApiInfoProvider, bootclasspathApiInfo)
Paul Duffin9b381ef2021-04-08 23:01:37 +0100453}
454
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100455// generateBootImageBuildActions generates ninja rules to create the boot image if required for this
456// module.
457func (b *BootclasspathFragmentModule) generateBootImageBuildActions(ctx android.ModuleContext, contents []android.Module) {
458 global := dexpreopt.GetGlobalConfig(ctx)
459 if !shouldBuildBootImages(ctx.Config(), global) {
460 return
461 }
462
463 // Bootclasspath fragment modules that are not preferred do not produce a boot image.
464 if !isActiveModule(ctx.Module()) {
465 return
466 }
467
468 // Bootclasspath fragment modules that have no image_name property do not produce a boot image.
469 imageConfig := b.getImageConfig(ctx)
470 if imageConfig == nil {
471 return
472 }
473
474 // Bootclasspath fragment modules that are for the platform do not produce a boot image.
475 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
476 if apexInfo.IsForPlatform() {
477 return
478 }
479
480 // Bootclasspath fragment modules that are versioned do not produce a boot image.
481 if android.IsModuleInVersionedSdk(ctx.Module()) {
482 return
483 }
484
485 // Copy the dex jars of this fragment's content modules to their predefined locations.
486 copyBootJarsToPredefinedLocations(ctx, contents, imageConfig.modules, imageConfig.dexPaths)
Paul Duffin2fc82ad2021-04-29 23:36:12 +0100487
488 // Build a profile for the image config and then use that to build the boot image.
489 profile := bootImageProfileRule(ctx, imageConfig)
490 buildBootImage(ctx, imageConfig, profile)
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100491}
492
Paul Duffin7771eba2021-04-23 14:25:28 +0100493type bootclasspathFragmentMemberType struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000494 android.SdkMemberTypeBase
495}
496
Paul Duffin7771eba2021-04-23 14:25:28 +0100497func (b *bootclasspathFragmentMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000498 mctx.AddVariationDependencies(nil, dependencyTag, names...)
499}
500
Paul Duffin7771eba2021-04-23 14:25:28 +0100501func (b *bootclasspathFragmentMemberType) IsInstance(module android.Module) bool {
502 _, ok := module.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000503 return ok
504}
505
Paul Duffin7771eba2021-04-23 14:25:28 +0100506func (b *bootclasspathFragmentMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
Paul Duffin4b64ba02021-03-29 11:02:53 +0100507 if b.PropertyName == "boot_images" {
508 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_boot_image")
509 } else {
510 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_bootclasspath_fragment")
511 }
Paul Duffinf7f65da2021-03-10 15:00:46 +0000512}
513
Paul Duffin7771eba2021-04-23 14:25:28 +0100514func (b *bootclasspathFragmentMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
515 return &bootclasspathFragmentSdkMemberProperties{}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000516}
517
Paul Duffin7771eba2021-04-23 14:25:28 +0100518type bootclasspathFragmentSdkMemberProperties struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000519 android.SdkMemberPropertiesBase
520
Paul Duffina57835e2021-04-19 13:23:06 +0100521 // The image name
Paul Duffin64be7bb2021-03-23 23:06:38 +0000522 Image_name *string
Paul Duffina57835e2021-04-19 13:23:06 +0100523
524 // Contents of the bootclasspath fragment
525 Contents []string
Paul Duffin7c955552021-04-19 13:23:53 +0100526
Paul Duffin895c7142021-04-25 13:40:15 +0100527 // Stub_libs properties.
528 Stub_libs []string
529 Core_platform_stub_libs []string
530
Paul Duffin7c955552021-04-19 13:23:53 +0100531 // Flag files by *hiddenAPIFlagFileCategory
532 Flag_files_by_category map[*hiddenAPIFlagFileCategory]android.Paths
Paul Duffinf7f65da2021-03-10 15:00:46 +0000533}
534
Paul Duffin7771eba2021-04-23 14:25:28 +0100535func (b *bootclasspathFragmentSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
536 module := variant.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000537
538 b.Image_name = module.properties.Image_name
Paul Duffin2dc665b2021-04-23 16:58:51 +0100539 b.Contents = module.properties.Contents
Paul Duffin7c955552021-04-19 13:23:53 +0100540
541 // Get the flag file information from the module.
542 mctx := ctx.SdkModuleContext()
543 flagFileInfo := mctx.OtherModuleProvider(module, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo)
544 b.Flag_files_by_category = flagFileInfo.categoryToPaths
Paul Duffin895c7142021-04-25 13:40:15 +0100545
546 // Copy stub_libs properties.
547 b.Stub_libs = module.properties.Api.Stub_libs
548 b.Core_platform_stub_libs = module.properties.Core_platform_api.Stub_libs
Paul Duffinf7f65da2021-03-10 15:00:46 +0000549}
550
Paul Duffin7771eba2021-04-23 14:25:28 +0100551func (b *bootclasspathFragmentSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffin64be7bb2021-03-23 23:06:38 +0000552 if b.Image_name != nil {
553 propertySet.AddProperty("image_name", *b.Image_name)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000554 }
Paul Duffina57835e2021-04-19 13:23:06 +0100555
Paul Duffin895c7142021-04-25 13:40:15 +0100556 builder := ctx.SnapshotBuilder()
557 requiredMemberDependency := builder.SdkMemberReferencePropertyTag(true)
558
Paul Duffina57835e2021-04-19 13:23:06 +0100559 if len(b.Contents) > 0 {
Paul Duffin895c7142021-04-25 13:40:15 +0100560 propertySet.AddPropertyWithTag("contents", b.Contents, requiredMemberDependency)
Paul Duffina57835e2021-04-19 13:23:06 +0100561 }
Paul Duffin7c955552021-04-19 13:23:53 +0100562
Paul Duffin895c7142021-04-25 13:40:15 +0100563 if len(b.Stub_libs) > 0 {
564 apiPropertySet := propertySet.AddPropertySet("api")
565 apiPropertySet.AddPropertyWithTag("stub_libs", b.Stub_libs, requiredMemberDependency)
566 }
567 if len(b.Core_platform_stub_libs) > 0 {
568 corePlatformApiPropertySet := propertySet.AddPropertySet("core_platform_api")
569 corePlatformApiPropertySet.AddPropertyWithTag("stub_libs", b.Core_platform_stub_libs, requiredMemberDependency)
570 }
571
Paul Duffin7c955552021-04-19 13:23:53 +0100572 if b.Flag_files_by_category != nil {
573 hiddenAPISet := propertySet.AddPropertySet("hidden_api")
574 for _, category := range hiddenAPIFlagFileCategories {
575 paths := b.Flag_files_by_category[category]
576 if len(paths) > 0 {
577 dests := []string{}
578 for _, p := range paths {
579 dest := filepath.Join("hiddenapi", p.Base())
580 builder.CopyToSnapshot(p, dest)
581 dests = append(dests, dest)
582 }
583 hiddenAPISet.AddProperty(category.propertyName, dests)
584 }
585 }
586 }
Paul Duffinf7f65da2021-03-10 15:00:46 +0000587}
588
Paul Duffin7771eba2021-04-23 14:25:28 +0100589var _ android.SdkMemberType = (*bootclasspathFragmentMemberType)(nil)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000590
Paul Duffin7771eba2021-04-23 14:25:28 +0100591// A prebuilt version of the bootclasspath_fragment module.
Paul Duffinf7f65da2021-03-10 15:00:46 +0000592//
Paul Duffin7771eba2021-04-23 14:25:28 +0100593// At the moment this is basically just a bootclasspath_fragment module that can be used as a
594// prebuilt. Eventually as more functionality is migrated into the bootclasspath_fragment module
595// type from the various singletons then this will diverge.
596type prebuiltBootclasspathFragmentModule struct {
597 BootclasspathFragmentModule
Paul Duffinf7f65da2021-03-10 15:00:46 +0000598 prebuilt android.Prebuilt
599}
600
Paul Duffin7771eba2021-04-23 14:25:28 +0100601func (module *prebuiltBootclasspathFragmentModule) Prebuilt() *android.Prebuilt {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000602 return &module.prebuilt
603}
604
Paul Duffin7771eba2021-04-23 14:25:28 +0100605func (module *prebuiltBootclasspathFragmentModule) Name() string {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000606 return module.prebuilt.Name(module.ModuleBase.Name())
607}
608
Paul Duffin7771eba2021-04-23 14:25:28 +0100609func prebuiltBootclasspathFragmentFactory() android.Module {
610 m := &prebuiltBootclasspathFragmentModule{}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000611 m.AddProperties(&m.properties)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000612 // This doesn't actually have any prebuilt files of its own so pass a placeholder for the srcs
613 // array.
614 android.InitPrebuiltModule(m, &[]string{"placeholder"})
615 android.InitApexModule(m)
616 android.InitSdkAwareModule(m)
Martin Stjernholmb79c7f12021-03-17 00:26:25 +0000617 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000618
Paul Duffin7771eba2021-04-23 14:25:28 +0100619 // Initialize the contents property from the image_name.
Paul Duffinc7ef9892021-03-23 23:21:59 +0000620 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
Paul Duffin7771eba2021-04-23 14:25:28 +0100621 bootclasspathFragmentInitContentsFromImage(ctx, &m.BootclasspathFragmentModule)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000622 })
Paul Duffinf7f65da2021-03-10 15:00:46 +0000623 return m
624}