blob: d8c5453d5ec02f7d2015d7ba4a2b31064a63f694 [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) {
Paul Duffinf4600f62021-05-13 22:34:45 +0100375 if sdkLibrary, ok := module.(SdkLibraryDependency); ok && sdkLibrary.sharedLibrary() {
376 ctx.PropertyErrorf("contents", "invalid module: %s, shared libraries cannot be on the bootclasspath", ctx.OtherModuleName(module))
377 } else {
378 contents = append(contents, module)
379 }
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100380 }
381 })
382
Paul Duffin9b381ef2021-04-08 23:01:37 +0100383 // Perform hidden API processing.
384 b.generateHiddenAPIBuildActions(ctx)
385
Paul Duffin3451e162021-01-20 15:16:56 +0000386 // Construct the boot image info from the config.
satayev3db35472021-05-06 23:59:58 +0100387 info := BootclasspathFragmentApexContentInfo{
388 ClasspathFragmentProtoInstallDir: b.classpathFragmentBase().installDirPath,
389 ClasspathFragmentProtoOutput: b.classpathFragmentBase().outputFilepath,
390 imageConfig: nil,
391 }
392
393 if !SkipDexpreoptBootJars(ctx) {
394 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
395 // GenerateSingletonBuildActions method as it cannot create it for itself.
396 dexpreopt.GetGlobalSoongConfig(ctx)
397 info.imageConfig = b.getImageConfig(ctx)
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100398
399 // Only generate the boot image if the configuration does not skip it.
400 b.generateBootImageBuildActions(ctx, contents)
satayev3db35472021-05-06 23:59:58 +0100401 }
Paul Duffin3451e162021-01-20 15:16:56 +0000402
403 // Make it available for other modules.
Paul Duffine946b322021-04-25 23:04:00 +0100404 ctx.SetProvider(BootclasspathFragmentApexContentInfoProvider, info)
Paul Duffin3451e162021-01-20 15:16:56 +0000405}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000406
satayev3db35472021-05-06 23:59:58 +0100407// generateClasspathProtoBuildActions generates all required build actions for classpath.proto config
408func (b *BootclasspathFragmentModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
409 var classpathJars []classpathJar
410 if "art" == proptools.String(b.properties.Image_name) {
411 // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
412 classpathJars = configuredJarListToClasspathJars(ctx, b.ClasspathFragmentToConfiguredJarList(ctx), BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
413 } else {
414 classpathJars = configuredJarListToClasspathJars(ctx, b.ClasspathFragmentToConfiguredJarList(ctx), b.classpathType)
415 }
416 b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, classpathJars)
417}
418
419func (b *BootclasspathFragmentModule) ClasspathFragmentToConfiguredJarList(ctx android.ModuleContext) android.ConfiguredJarList {
420 // TODO(satayev): populate with actual content
421 return android.EmptyConfiguredJarList()
422}
423
Paul Duffin7771eba2021-04-23 14:25:28 +0100424func (b *BootclasspathFragmentModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
Paul Duffin64be7bb2021-03-23 23:06:38 +0000425 // Get a map of the image configs that are supported.
426 imageConfigs := genBootImageConfigs(ctx)
427
428 // Retrieve the config for this image.
429 imageNamePtr := b.properties.Image_name
430 if imageNamePtr == nil {
431 return nil
432 }
433
434 imageName := *imageNamePtr
435 imageConfig := imageConfigs[imageName]
436 if imageConfig == nil {
437 ctx.PropertyErrorf("image_name", "Unknown image name %q, expected one of %s", imageName, strings.Join(android.SortedStringKeys(imageConfigs), ", "))
438 return nil
439 }
440 return imageConfig
441}
442
Paul Duffin9b381ef2021-04-08 23:01:37 +0100443// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffin7771eba2021-04-23 14:25:28 +0100444func (b *BootclasspathFragmentModule) generateHiddenAPIBuildActions(ctx android.ModuleContext) {
Paul Duffin9b381ef2021-04-08 23:01:37 +0100445 // Resolve the properties to paths.
446 flagFileInfo := b.properties.Hidden_api.hiddenAPIFlagFileInfo(ctx)
447
448 // Store the information for use by platform_bootclasspath.
449 ctx.SetProvider(hiddenAPIFlagFileInfoProvider, flagFileInfo)
Paul Duffin10931582021-04-25 10:13:54 +0100450
451 // Convert the kind specific lists of modules into kind specific lists of jars.
452 stubJarsByKind := hiddenAPIGatherStubLibDexJarPaths(ctx)
453
454 // Store the information for use by other modules.
455 bootclasspathApiInfo := bootclasspathApiInfo{stubJarsByKind: stubJarsByKind}
456 ctx.SetProvider(bootclasspathApiInfoProvider, bootclasspathApiInfo)
Paul Duffin9b381ef2021-04-08 23:01:37 +0100457}
458
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100459// generateBootImageBuildActions generates ninja rules to create the boot image if required for this
460// module.
461func (b *BootclasspathFragmentModule) generateBootImageBuildActions(ctx android.ModuleContext, contents []android.Module) {
462 global := dexpreopt.GetGlobalConfig(ctx)
463 if !shouldBuildBootImages(ctx.Config(), global) {
464 return
465 }
466
467 // Bootclasspath fragment modules that are not preferred do not produce a boot image.
468 if !isActiveModule(ctx.Module()) {
469 return
470 }
471
472 // Bootclasspath fragment modules that have no image_name property do not produce a boot image.
473 imageConfig := b.getImageConfig(ctx)
474 if imageConfig == nil {
475 return
476 }
477
478 // Bootclasspath fragment modules that are for the platform do not produce a boot image.
479 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
480 if apexInfo.IsForPlatform() {
481 return
482 }
483
484 // Bootclasspath fragment modules that are versioned do not produce a boot image.
485 if android.IsModuleInVersionedSdk(ctx.Module()) {
486 return
487 }
488
489 // Copy the dex jars of this fragment's content modules to their predefined locations.
490 copyBootJarsToPredefinedLocations(ctx, contents, imageConfig.modules, imageConfig.dexPaths)
Paul Duffin2fc82ad2021-04-29 23:36:12 +0100491
492 // Build a profile for the image config and then use that to build the boot image.
493 profile := bootImageProfileRule(ctx, imageConfig)
494 buildBootImage(ctx, imageConfig, profile)
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100495}
496
Paul Duffin7771eba2021-04-23 14:25:28 +0100497type bootclasspathFragmentMemberType struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000498 android.SdkMemberTypeBase
499}
500
Paul Duffin7771eba2021-04-23 14:25:28 +0100501func (b *bootclasspathFragmentMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000502 mctx.AddVariationDependencies(nil, dependencyTag, names...)
503}
504
Paul Duffin7771eba2021-04-23 14:25:28 +0100505func (b *bootclasspathFragmentMemberType) IsInstance(module android.Module) bool {
506 _, ok := module.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000507 return ok
508}
509
Paul Duffin7771eba2021-04-23 14:25:28 +0100510func (b *bootclasspathFragmentMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
Paul Duffin4b64ba02021-03-29 11:02:53 +0100511 if b.PropertyName == "boot_images" {
512 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_boot_image")
513 } else {
514 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_bootclasspath_fragment")
515 }
Paul Duffinf7f65da2021-03-10 15:00:46 +0000516}
517
Paul Duffin7771eba2021-04-23 14:25:28 +0100518func (b *bootclasspathFragmentMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
519 return &bootclasspathFragmentSdkMemberProperties{}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000520}
521
Paul Duffin7771eba2021-04-23 14:25:28 +0100522type bootclasspathFragmentSdkMemberProperties struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000523 android.SdkMemberPropertiesBase
524
Paul Duffina57835e2021-04-19 13:23:06 +0100525 // The image name
Paul Duffin64be7bb2021-03-23 23:06:38 +0000526 Image_name *string
Paul Duffina57835e2021-04-19 13:23:06 +0100527
528 // Contents of the bootclasspath fragment
529 Contents []string
Paul Duffin7c955552021-04-19 13:23:53 +0100530
Paul Duffin895c7142021-04-25 13:40:15 +0100531 // Stub_libs properties.
532 Stub_libs []string
533 Core_platform_stub_libs []string
534
Paul Duffin7c955552021-04-19 13:23:53 +0100535 // Flag files by *hiddenAPIFlagFileCategory
536 Flag_files_by_category map[*hiddenAPIFlagFileCategory]android.Paths
Paul Duffinf7f65da2021-03-10 15:00:46 +0000537}
538
Paul Duffin7771eba2021-04-23 14:25:28 +0100539func (b *bootclasspathFragmentSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
540 module := variant.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000541
542 b.Image_name = module.properties.Image_name
Paul Duffin2dc665b2021-04-23 16:58:51 +0100543 b.Contents = module.properties.Contents
Paul Duffin7c955552021-04-19 13:23:53 +0100544
545 // Get the flag file information from the module.
546 mctx := ctx.SdkModuleContext()
547 flagFileInfo := mctx.OtherModuleProvider(module, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo)
548 b.Flag_files_by_category = flagFileInfo.categoryToPaths
Paul Duffin895c7142021-04-25 13:40:15 +0100549
550 // Copy stub_libs properties.
551 b.Stub_libs = module.properties.Api.Stub_libs
552 b.Core_platform_stub_libs = module.properties.Core_platform_api.Stub_libs
Paul Duffinf7f65da2021-03-10 15:00:46 +0000553}
554
Paul Duffin7771eba2021-04-23 14:25:28 +0100555func (b *bootclasspathFragmentSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffin64be7bb2021-03-23 23:06:38 +0000556 if b.Image_name != nil {
557 propertySet.AddProperty("image_name", *b.Image_name)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000558 }
Paul Duffina57835e2021-04-19 13:23:06 +0100559
Paul Duffin895c7142021-04-25 13:40:15 +0100560 builder := ctx.SnapshotBuilder()
561 requiredMemberDependency := builder.SdkMemberReferencePropertyTag(true)
562
Paul Duffina57835e2021-04-19 13:23:06 +0100563 if len(b.Contents) > 0 {
Paul Duffin895c7142021-04-25 13:40:15 +0100564 propertySet.AddPropertyWithTag("contents", b.Contents, requiredMemberDependency)
Paul Duffina57835e2021-04-19 13:23:06 +0100565 }
Paul Duffin7c955552021-04-19 13:23:53 +0100566
Paul Duffin895c7142021-04-25 13:40:15 +0100567 if len(b.Stub_libs) > 0 {
568 apiPropertySet := propertySet.AddPropertySet("api")
569 apiPropertySet.AddPropertyWithTag("stub_libs", b.Stub_libs, requiredMemberDependency)
570 }
571 if len(b.Core_platform_stub_libs) > 0 {
572 corePlatformApiPropertySet := propertySet.AddPropertySet("core_platform_api")
573 corePlatformApiPropertySet.AddPropertyWithTag("stub_libs", b.Core_platform_stub_libs, requiredMemberDependency)
574 }
575
Paul Duffin7c955552021-04-19 13:23:53 +0100576 if b.Flag_files_by_category != nil {
577 hiddenAPISet := propertySet.AddPropertySet("hidden_api")
578 for _, category := range hiddenAPIFlagFileCategories {
579 paths := b.Flag_files_by_category[category]
580 if len(paths) > 0 {
581 dests := []string{}
582 for _, p := range paths {
583 dest := filepath.Join("hiddenapi", p.Base())
584 builder.CopyToSnapshot(p, dest)
585 dests = append(dests, dest)
586 }
587 hiddenAPISet.AddProperty(category.propertyName, dests)
588 }
589 }
590 }
Paul Duffinf7f65da2021-03-10 15:00:46 +0000591}
592
Paul Duffin7771eba2021-04-23 14:25:28 +0100593var _ android.SdkMemberType = (*bootclasspathFragmentMemberType)(nil)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000594
Paul Duffin7771eba2021-04-23 14:25:28 +0100595// A prebuilt version of the bootclasspath_fragment module.
Paul Duffinf7f65da2021-03-10 15:00:46 +0000596//
Paul Duffin7771eba2021-04-23 14:25:28 +0100597// At the moment this is basically just a bootclasspath_fragment module that can be used as a
598// prebuilt. Eventually as more functionality is migrated into the bootclasspath_fragment module
599// type from the various singletons then this will diverge.
600type prebuiltBootclasspathFragmentModule struct {
601 BootclasspathFragmentModule
Paul Duffinf7f65da2021-03-10 15:00:46 +0000602 prebuilt android.Prebuilt
603}
604
Paul Duffin7771eba2021-04-23 14:25:28 +0100605func (module *prebuiltBootclasspathFragmentModule) Prebuilt() *android.Prebuilt {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000606 return &module.prebuilt
607}
608
Paul Duffin7771eba2021-04-23 14:25:28 +0100609func (module *prebuiltBootclasspathFragmentModule) Name() string {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000610 return module.prebuilt.Name(module.ModuleBase.Name())
611}
612
Paul Duffin7771eba2021-04-23 14:25:28 +0100613func prebuiltBootclasspathFragmentFactory() android.Module {
614 m := &prebuiltBootclasspathFragmentModule{}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000615 m.AddProperties(&m.properties)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000616 // This doesn't actually have any prebuilt files of its own so pass a placeholder for the srcs
617 // array.
618 android.InitPrebuiltModule(m, &[]string{"placeholder"})
619 android.InitApexModule(m)
620 android.InitSdkAwareModule(m)
Martin Stjernholmb79c7f12021-03-17 00:26:25 +0000621 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000622
Paul Duffin7771eba2021-04-23 14:25:28 +0100623 // Initialize the contents property from the image_name.
Paul Duffinc7ef9892021-03-23 23:21:59 +0000624 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
Paul Duffin7771eba2021-04-23 14:25:28 +0100625 bootclasspathFragmentInitContentsFromImage(ctx, &m.BootclasspathFragmentModule)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000626 })
Paul Duffinf7f65da2021-03-10 15:00:46 +0000627 return m
628}