blob: 68a7ad21d5adddc1e4eff72af891bf4ec7703875 [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 //
Paul Duffin34827d42021-05-13 21:25:05 +010094 // A java_sdk_library specified here will also be treated as if it was specified on the stub_libs
95 // property.
96 //
Paul Duffinc7d16442021-04-23 13:55:49 +010097 // The order of this list matters as it is the order that is used in the bootclasspath.
98 Contents []string
Paul Duffin10931582021-04-25 10:13:54 +010099
100 // The properties for specifying the API stubs provided by this fragment.
101 BootclasspathAPIProperties
Paul Duffinc7d16442021-04-23 13:55:49 +0100102}
103
Paul Duffin7771eba2021-04-23 14:25:28 +0100104type bootclasspathFragmentProperties struct {
Paul Duffin3451e162021-01-20 15:16:56 +0000105 // The name of the image this represents.
106 //
Paul Duffin82886d62021-03-24 01:34:57 +0000107 // If specified then it must be one of "art" or "boot".
Paul Duffin64be7bb2021-03-23 23:06:38 +0000108 Image_name *string
Paul Duffinc7ef9892021-03-23 23:21:59 +0000109
Paul Duffinc7d16442021-04-23 13:55:49 +0100110 // Properties whose values need to differ with and without coverage.
111 BootclasspathFragmentCoverageAffectedProperties
112 Coverage BootclasspathFragmentCoverageAffectedProperties
Paul Duffin9b381ef2021-04-08 23:01:37 +0100113
114 Hidden_api HiddenAPIFlagFileProperties
Paul Duffin3451e162021-01-20 15:16:56 +0000115}
116
Paul Duffin7771eba2021-04-23 14:25:28 +0100117type BootclasspathFragmentModule struct {
Paul Duffin3451e162021-01-20 15:16:56 +0000118 android.ModuleBase
Paul Duffina1d60252021-01-21 18:13:43 +0000119 android.ApexModuleBase
Paul Duffinf7f65da2021-03-10 15:00:46 +0000120 android.SdkBase
satayev3db35472021-05-06 23:59:58 +0100121 ClasspathFragmentBase
122
Paul Duffin7771eba2021-04-23 14:25:28 +0100123 properties bootclasspathFragmentProperties
Paul Duffin3451e162021-01-20 15:16:56 +0000124}
125
Paul Duffin2fef1362021-04-15 13:32:00 +0100126// commonBootclasspathFragment defines the methods that are implemented by both source and prebuilt
127// bootclasspath fragment modules.
128type commonBootclasspathFragment interface {
129 // produceHiddenAPIAllFlagsFile produces the all-flags.csv and intermediate files.
130 //
131 // Updates the supplied flagFileInfo with the paths to the generated files set.
132 produceHiddenAPIAllFlagsFile(ctx android.ModuleContext, contents []android.Module, stubJarsByKind map[android.SdkKind]android.Paths, flagFileInfo *hiddenAPIFlagFileInfo)
133}
134
Paul Duffin7771eba2021-04-23 14:25:28 +0100135func bootclasspathFragmentFactory() android.Module {
136 m := &BootclasspathFragmentModule{}
Paul Duffin3451e162021-01-20 15:16:56 +0000137 m.AddProperties(&m.properties)
Paul Duffina1d60252021-01-21 18:13:43 +0000138 android.InitApexModule(m)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000139 android.InitSdkAwareModule(m)
satayev3db35472021-05-06 23:59:58 +0100140 initClasspathFragment(m, BOOTCLASSPATH)
Martin Stjernholmb79c7f12021-03-17 00:26:25 +0000141 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000142
Paul Duffinc7ef9892021-03-23 23:21:59 +0000143 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
Paul Duffinc7d16442021-04-23 13:55:49 +0100144 // If code coverage has been enabled for the framework then append the properties with
145 // coverage specific properties.
146 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
147 err := proptools.AppendProperties(&m.properties.BootclasspathFragmentCoverageAffectedProperties, &m.properties.Coverage, nil)
148 if err != nil {
149 ctx.PropertyErrorf("coverage", "error trying to append coverage specific properties: %s", err)
150 return
151 }
152 }
153
154 // Initialize the contents property from the image_name.
Paul Duffin7771eba2021-04-23 14:25:28 +0100155 bootclasspathFragmentInitContentsFromImage(ctx, m)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000156 })
Paul Duffin3451e162021-01-20 15:16:56 +0000157 return m
158}
159
Paul Duffin7771eba2021-04-23 14:25:28 +0100160// bootclasspathFragmentInitContentsFromImage will initialize the contents property from the image_name if
161// necessary.
162func bootclasspathFragmentInitContentsFromImage(ctx android.EarlyModuleContext, m *BootclasspathFragmentModule) {
Paul Duffin82886d62021-03-24 01:34:57 +0000163 contents := m.properties.Contents
164 if m.properties.Image_name == nil && len(contents) == 0 {
165 ctx.ModuleErrorf(`neither of the "image_name" and "contents" properties have been supplied, please supply exactly one`)
166 }
Paul Duffinba6afd02019-11-19 19:44:10 +0000167
Paul Duffinc7ef9892021-03-23 23:21:59 +0000168 imageName := proptools.String(m.properties.Image_name)
169 if imageName == "art" {
Paul Duffin023dba02021-04-22 01:45:29 +0100170 // TODO(b/177892522): Prebuilts (versioned or not) should not use the image_name property.
Paul Duffin0c2e0832021-04-28 00:39:52 +0100171 if android.IsModuleInVersionedSdk(m) {
Paul Duffin023dba02021-04-22 01:45:29 +0100172 // The module is a versioned prebuilt so ignore it. This is done for a couple of reasons:
173 // 1. There is no way to use this at the moment so ignoring it is safe.
174 // 2. Attempting to initialize the contents property from the configuration will end up having
175 // the versioned prebuilt depending on the unversioned prebuilt. That will cause problems
176 // as the unversioned prebuilt could end up with an APEX variant created for the source
177 // APEX which will prevent it from having an APEX variant for the prebuilt APEX which in
178 // turn will prevent it from accessing the dex implementation jar from that which will
179 // break hidden API processing, amongst others.
180 return
181 }
182
Paul Duffinc7ef9892021-03-23 23:21:59 +0000183 // Get the configuration for the art apex jars. Do not use getImageConfig(ctx) here as this is
184 // too early in the Soong processing for that to work.
185 global := dexpreopt.GetGlobalConfig(ctx)
186 modules := global.ArtApexJars
187
188 // Make sure that the apex specified in the configuration is consistent and is one for which
189 // this boot image is available.
Paul Duffinc7ef9892021-03-23 23:21:59 +0000190 commonApex := ""
191 for i := 0; i < modules.Len(); i++ {
192 apex := modules.Apex(i)
193 jar := modules.Jar(i)
194 if apex == "platform" {
195 ctx.ModuleErrorf("ArtApexJars is invalid as it requests a platform variant of %q", jar)
196 continue
197 }
198 if !m.AvailableFor(apex) {
Paul Duffinf23bc472021-04-27 12:42:20 +0100199 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 +0000200 apex, m.ApexAvailable())
201 continue
202 }
203 if commonApex == "" {
204 commonApex = apex
205 } else if commonApex != apex {
206 ctx.ModuleErrorf("ArtApexJars configuration is inconsistent, expected all jars to be in the same apex but it specifies apex %q and %q",
207 commonApex, apex)
208 }
Paul Duffinc7ef9892021-03-23 23:21:59 +0000209 }
210
Paul Duffinf23bc472021-04-27 12:42:20 +0100211 if len(contents) != 0 {
212 // Nothing to do.
213 return
214 }
215
Paul Duffinc7ef9892021-03-23 23:21:59 +0000216 // Store the jars in the Contents property so that they can be used to add dependencies.
Paul Duffinba6afd02019-11-19 19:44:10 +0000217 m.properties.Contents = modules.CopyOfJars()
218 }
219}
220
221// bootclasspathImageNameContentsConsistencyCheck checks that the configuration that applies to this
222// module (if any) matches the contents.
223//
224// This should be a noop as if image_name="art" then the contents will be set from the ArtApexJars
225// config by bootclasspathFragmentInitContentsFromImage so it will be guaranteed to match. However,
226// in future this will not be the case.
227func (b *BootclasspathFragmentModule) bootclasspathImageNameContentsConsistencyCheck(ctx android.BaseModuleContext) {
228 imageName := proptools.String(b.properties.Image_name)
229 if imageName == "art" {
230 // TODO(b/177892522): Prebuilts (versioned or not) should not use the image_name property.
Paul Duffin0c2e0832021-04-28 00:39:52 +0100231 if android.IsModuleInVersionedSdk(b) {
Paul Duffinba6afd02019-11-19 19:44:10 +0000232 // The module is a versioned prebuilt so ignore it. This is done for a couple of reasons:
233 // 1. There is no way to use this at the moment so ignoring it is safe.
234 // 2. Attempting to initialize the contents property from the configuration will end up having
235 // the versioned prebuilt depending on the unversioned prebuilt. That will cause problems
236 // as the unversioned prebuilt could end up with an APEX variant created for the source
237 // APEX which will prevent it from having an APEX variant for the prebuilt APEX which in
238 // turn will prevent it from accessing the dex implementation jar from that which will
239 // break hidden API processing, amongst others.
240 return
241 }
242
243 // Get the configuration for the art apex jars.
244 modules := b.getImageConfig(ctx).modules
245 configuredJars := modules.CopyOfJars()
246
247 // Skip the check if the configured jars list is empty as that is a common configuration when
248 // building targets that do not result in a system image.
249 if len(configuredJars) == 0 {
250 return
251 }
252
253 contents := b.properties.Contents
254 if !reflect.DeepEqual(configuredJars, contents) {
255 ctx.ModuleErrorf("inconsistency in specification of contents. ArtApexJars configuration specifies %#v, contents property specifies %#v",
256 configuredJars, contents)
257 }
Paul Duffinc7ef9892021-03-23 23:21:59 +0000258 }
259}
260
Paul Duffine946b322021-04-25 23:04:00 +0100261var BootclasspathFragmentApexContentInfoProvider = blueprint.NewProvider(BootclasspathFragmentApexContentInfo{})
Paul Duffin3451e162021-01-20 15:16:56 +0000262
Paul Duffine946b322021-04-25 23:04:00 +0100263// BootclasspathFragmentApexContentInfo contains the bootclasspath_fragments contributions to the
264// apex contents.
265type BootclasspathFragmentApexContentInfo struct {
satayev3db35472021-05-06 23:59:58 +0100266 // ClasspathFragmentProtoOutput is an output path for the generated classpaths.proto config of this module.
267 //
268 // The file should be copied to a relevant place on device, see ClasspathFragmentProtoInstallDir
269 // for more details.
270 ClasspathFragmentProtoOutput android.OutputPath
271
272 // ClasspathFragmentProtoInstallDir contains information about on device location for the generated classpaths.proto file.
273 //
274 // The path encodes expected sub-location within partitions, i.e. etc/classpaths/<proto-file>,
275 // for ClasspathFragmentProtoOutput. To get sub-location, instead of the full output / make path
276 // use android.InstallPath#Rel().
277 //
278 // This is only relevant for APEX modules as they perform their own installation; while regular
279 // system files are installed via ClasspathFragmentBase#androidMkEntries().
280 ClasspathFragmentProtoInstallDir android.InstallPath
281
Paul Duffin3451e162021-01-20 15:16:56 +0000282 // The image config, internal to this module (and the dex_bootjars singleton).
Paul Duffina1d60252021-01-21 18:13:43 +0000283 //
Paul Duffine946b322021-04-25 23:04:00 +0100284 // Will be nil if the BootclasspathFragmentApexContentInfo has not been provided for a specific module. That can occur
Paul Duffina1d60252021-01-21 18:13:43 +0000285 // when SkipDexpreoptBootJars(ctx) returns true.
Paul Duffin3451e162021-01-20 15:16:56 +0000286 imageConfig *bootImageConfig
287}
288
Paul Duffine946b322021-04-25 23:04:00 +0100289func (i BootclasspathFragmentApexContentInfo) Modules() android.ConfiguredJarList {
Paul Duffin3451e162021-01-20 15:16:56 +0000290 return i.imageConfig.modules
291}
292
Paul Duffina1d60252021-01-21 18:13:43 +0000293// Get a map from ArchType to the associated boot image's contents for Android.
294//
295// Extension boot images only return their own files, not the files of the boot images they extend.
Paul Duffine946b322021-04-25 23:04:00 +0100296func (i BootclasspathFragmentApexContentInfo) AndroidBootImageFilesByArchType() map[android.ArchType]android.OutputPaths {
Paul Duffina1d60252021-01-21 18:13:43 +0000297 files := map[android.ArchType]android.OutputPaths{}
298 if i.imageConfig != nil {
299 for _, variant := range i.imageConfig.variants {
300 // We also generate boot images for host (for testing), but we don't need those in the apex.
301 // TODO(b/177892522) - consider changing this to check Os.OsClass = android.Device
302 if variant.target.Os == android.Android {
303 files[variant.target.Arch.ArchType] = variant.imagesDeps
304 }
305 }
306 }
307 return files
308}
309
Paul Duffin190fdef2021-04-26 10:33:59 +0100310// DexBootJarPathForContentModule returns the path to the dex boot jar for specified module.
311//
312// The dex boot jar is one which has had hidden API encoding performed on it.
313func (i BootclasspathFragmentApexContentInfo) DexBootJarPathForContentModule(module android.Module) android.Path {
314 j := module.(UsesLibraryDependency)
315 dexJar := j.DexJarBuildPath()
316 return dexJar
317}
318
Paul Duffin7771eba2021-04-23 14:25:28 +0100319func (b *BootclasspathFragmentModule) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffina1d60252021-01-21 18:13:43 +0000320 tag := ctx.OtherModuleDependencyTag(dep)
Paul Duffin65898052021-04-20 22:47:03 +0100321 if IsBootclasspathFragmentContentDepTag(tag) {
Paul Duffin4d101b62021-03-24 15:42:20 +0000322 // Boot image contents are automatically added to apex.
323 return true
Paul Duffinc7ef9892021-03-23 23:21:59 +0000324 }
Bob Badour07065cd2021-02-05 19:59:11 -0800325 if android.IsMetaDependencyTag(tag) {
326 // Cross-cutting metadata dependencies are metadata.
327 return false
328 }
Paul Duffina1d60252021-01-21 18:13:43 +0000329 panic(fmt.Errorf("boot_image module %q should not have a dependency on %q via tag %s", b, dep, android.PrettyPrintTag(tag)))
330}
331
Paul Duffin7771eba2021-04-23 14:25:28 +0100332func (b *BootclasspathFragmentModule) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Paul Duffina1d60252021-01-21 18:13:43 +0000333 return nil
334}
335
Paul Duffin65898052021-04-20 22:47:03 +0100336// ComponentDepsMutator adds dependencies onto modules before any prebuilt modules without a
337// corresponding source module are renamed. This means that adding a dependency using a name without
338// a prebuilt_ prefix will always resolve to a source module and when using a name with that prefix
339// it will always resolve to a prebuilt module.
Paul Duffin7771eba2021-04-23 14:25:28 +0100340func (b *BootclasspathFragmentModule) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin65898052021-04-20 22:47:03 +0100341 module := ctx.Module()
Paul Duffin7771eba2021-04-23 14:25:28 +0100342 _, isSourceModule := module.(*BootclasspathFragmentModule)
Paul Duffin65898052021-04-20 22:47:03 +0100343
344 for _, name := range b.properties.Contents {
345 // A bootclasspath_fragment must depend only on other source modules, while the
346 // prebuilt_bootclasspath_fragment must only depend on other prebuilt modules.
Paul Duffina9dd6fa2021-04-22 17:25:57 +0100347 //
348 // TODO(b/177892522) - avoid special handling of jacocoagent.
349 if !isSourceModule && name != "jacocoagent" {
Paul Duffin65898052021-04-20 22:47:03 +0100350 name = android.PrebuiltNameFromSource(name)
351 }
352 ctx.AddDependency(module, bootclasspathFragmentContentDepTag, name)
353 }
354
355}
356
Paul Duffin7771eba2021-04-23 14:25:28 +0100357func (b *BootclasspathFragmentModule) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin10931582021-04-25 10:13:54 +0100358 // Add dependencies onto all the modules that provide the API stubs for classes on this
359 // bootclasspath fragment.
360 hiddenAPIAddStubLibDependencies(ctx, b.properties.sdkKindToStubLibs())
Paul Duffinc7ef9892021-03-23 23:21:59 +0000361
Paul Duffina1d60252021-01-21 18:13:43 +0000362 if SkipDexpreoptBootJars(ctx) {
363 return
364 }
365
366 // Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
367 // path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
368 dexpreopt.RegisterToolDeps(ctx)
369}
370
Paul Duffin7771eba2021-04-23 14:25:28 +0100371func (b *BootclasspathFragmentModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffinba6afd02019-11-19 19:44:10 +0000372 // Only perform a consistency check if this module is the active module. That will prevent an
373 // unused prebuilt that was created without instrumentation from breaking an instrumentation
374 // build.
375 if isActiveModule(ctx.Module()) {
376 b.bootclasspathImageNameContentsConsistencyCheck(ctx)
377 }
378
satayev3db35472021-05-06 23:59:58 +0100379 // Generate classpaths.proto config
380 b.generateClasspathProtoBuildActions(ctx)
381
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100382 // Gather the bootclasspath fragment's contents.
383 var contents []android.Module
384 ctx.VisitDirectDeps(func(module android.Module) {
385 tag := ctx.OtherModuleDependencyTag(module)
386 if IsBootclasspathFragmentContentDepTag(tag) {
Paul Duffin79fd3d72021-05-14 16:14:17 +0100387 contents = append(contents, module)
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100388 }
389 })
390
Paul Duffin9b381ef2021-04-08 23:01:37 +0100391 // Perform hidden API processing.
Paul Duffin2fef1362021-04-15 13:32:00 +0100392 b.generateHiddenAPIBuildActions(ctx, contents)
Paul Duffin9b381ef2021-04-08 23:01:37 +0100393
Paul Duffin3451e162021-01-20 15:16:56 +0000394 // Construct the boot image info from the config.
satayev3db35472021-05-06 23:59:58 +0100395 info := BootclasspathFragmentApexContentInfo{
396 ClasspathFragmentProtoInstallDir: b.classpathFragmentBase().installDirPath,
397 ClasspathFragmentProtoOutput: b.classpathFragmentBase().outputFilepath,
398 imageConfig: nil,
399 }
400
401 if !SkipDexpreoptBootJars(ctx) {
402 // Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
403 // GenerateSingletonBuildActions method as it cannot create it for itself.
404 dexpreopt.GetGlobalSoongConfig(ctx)
405 info.imageConfig = b.getImageConfig(ctx)
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100406
407 // Only generate the boot image if the configuration does not skip it.
408 b.generateBootImageBuildActions(ctx, contents)
satayev3db35472021-05-06 23:59:58 +0100409 }
Paul Duffin3451e162021-01-20 15:16:56 +0000410
411 // Make it available for other modules.
Paul Duffine946b322021-04-25 23:04:00 +0100412 ctx.SetProvider(BootclasspathFragmentApexContentInfoProvider, info)
Paul Duffin3451e162021-01-20 15:16:56 +0000413}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000414
satayev3db35472021-05-06 23:59:58 +0100415// generateClasspathProtoBuildActions generates all required build actions for classpath.proto config
416func (b *BootclasspathFragmentModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
417 var classpathJars []classpathJar
418 if "art" == proptools.String(b.properties.Image_name) {
419 // ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
420 classpathJars = configuredJarListToClasspathJars(ctx, b.ClasspathFragmentToConfiguredJarList(ctx), BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
421 } else {
422 classpathJars = configuredJarListToClasspathJars(ctx, b.ClasspathFragmentToConfiguredJarList(ctx), b.classpathType)
423 }
424 b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, classpathJars)
425}
426
427func (b *BootclasspathFragmentModule) ClasspathFragmentToConfiguredJarList(ctx android.ModuleContext) android.ConfiguredJarList {
428 // TODO(satayev): populate with actual content
429 return android.EmptyConfiguredJarList()
430}
431
Paul Duffin7771eba2021-04-23 14:25:28 +0100432func (b *BootclasspathFragmentModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
Paul Duffin64be7bb2021-03-23 23:06:38 +0000433 // Get a map of the image configs that are supported.
434 imageConfigs := genBootImageConfigs(ctx)
435
436 // Retrieve the config for this image.
437 imageNamePtr := b.properties.Image_name
438 if imageNamePtr == nil {
439 return nil
440 }
441
442 imageName := *imageNamePtr
443 imageConfig := imageConfigs[imageName]
444 if imageConfig == nil {
445 ctx.PropertyErrorf("image_name", "Unknown image name %q, expected one of %s", imageName, strings.Join(android.SortedStringKeys(imageConfigs), ", "))
446 return nil
447 }
448 return imageConfig
449}
450
Paul Duffin9b381ef2021-04-08 23:01:37 +0100451// generateHiddenAPIBuildActions generates all the hidden API related build rules.
Paul Duffin2fef1362021-04-15 13:32:00 +0100452func (b *BootclasspathFragmentModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, contents []android.Module) {
Paul Duffin10931582021-04-25 10:13:54 +0100453
454 // Convert the kind specific lists of modules into kind specific lists of jars.
Paul Duffin34827d42021-05-13 21:25:05 +0100455 stubJarsByKind := hiddenAPIGatherStubLibDexJarPaths(ctx, contents)
Paul Duffin10931582021-04-25 10:13:54 +0100456
457 // Store the information for use by other modules.
458 bootclasspathApiInfo := bootclasspathApiInfo{stubJarsByKind: stubJarsByKind}
459 ctx.SetProvider(bootclasspathApiInfoProvider, bootclasspathApiInfo)
Paul Duffin2fef1362021-04-15 13:32:00 +0100460
461 // Resolve the properties to paths.
462 flagFileInfo := b.properties.Hidden_api.hiddenAPIFlagFileInfo(ctx)
463
464 // Delegate the production of the hidden API all flags file to a module type specific method.
465 common := ctx.Module().(commonBootclasspathFragment)
466 common.produceHiddenAPIAllFlagsFile(ctx, contents, stubJarsByKind, &flagFileInfo)
467
468 // Store the information for use by platform_bootclasspath.
469 ctx.SetProvider(hiddenAPIFlagFileInfoProvider, flagFileInfo)
470}
471
472// produceHiddenAPIAllFlagsFile produces the hidden API all-flags.csv file (and supporting files)
473// for the fragment.
474func (b *BootclasspathFragmentModule) produceHiddenAPIAllFlagsFile(ctx android.ModuleContext, contents []android.Module, stubJarsByKind map[android.SdkKind]android.Paths, flagFileInfo *hiddenAPIFlagFileInfo) {
475 // If no stubs have been provided then don't perform hidden API processing. This is a temporary
476 // workaround to avoid existing bootclasspath_fragments that do not provide stubs breaking the
477 // build.
478 // TODO(b/179354495): Remove this workaround.
479 if len(stubJarsByKind) == 0 {
480 // Nothing to do.
481 return
482 }
483
484 // Generate the rules to create the hidden API flags and update the supplied flagFileInfo with the
485 // paths to the created files.
486 hiddenAPIGenerateAllFlagsForBootclasspathFragment(ctx, contents, stubJarsByKind, flagFileInfo)
Paul Duffin9b381ef2021-04-08 23:01:37 +0100487}
488
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100489// generateBootImageBuildActions generates ninja rules to create the boot image if required for this
490// module.
491func (b *BootclasspathFragmentModule) generateBootImageBuildActions(ctx android.ModuleContext, contents []android.Module) {
492 global := dexpreopt.GetGlobalConfig(ctx)
493 if !shouldBuildBootImages(ctx.Config(), global) {
494 return
495 }
496
497 // Bootclasspath fragment modules that are not preferred do not produce a boot image.
498 if !isActiveModule(ctx.Module()) {
499 return
500 }
501
502 // Bootclasspath fragment modules that have no image_name property do not produce a boot image.
503 imageConfig := b.getImageConfig(ctx)
504 if imageConfig == nil {
505 return
506 }
507
508 // Bootclasspath fragment modules that are for the platform do not produce a boot image.
509 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
510 if apexInfo.IsForPlatform() {
511 return
512 }
513
514 // Bootclasspath fragment modules that are versioned do not produce a boot image.
515 if android.IsModuleInVersionedSdk(ctx.Module()) {
516 return
517 }
518
519 // Copy the dex jars of this fragment's content modules to their predefined locations.
520 copyBootJarsToPredefinedLocations(ctx, contents, imageConfig.modules, imageConfig.dexPaths)
Paul Duffin2fc82ad2021-04-29 23:36:12 +0100521
522 // Build a profile for the image config and then use that to build the boot image.
523 profile := bootImageProfileRule(ctx, imageConfig)
524 buildBootImage(ctx, imageConfig, profile)
Paul Duffin7ebebfd2021-04-27 19:36:57 +0100525}
526
Paul Duffin7771eba2021-04-23 14:25:28 +0100527type bootclasspathFragmentMemberType struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000528 android.SdkMemberTypeBase
529}
530
Paul Duffin7771eba2021-04-23 14:25:28 +0100531func (b *bootclasspathFragmentMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000532 mctx.AddVariationDependencies(nil, dependencyTag, names...)
533}
534
Paul Duffin7771eba2021-04-23 14:25:28 +0100535func (b *bootclasspathFragmentMemberType) IsInstance(module android.Module) bool {
536 _, ok := module.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000537 return ok
538}
539
Paul Duffin7771eba2021-04-23 14:25:28 +0100540func (b *bootclasspathFragmentMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
Paul Duffin4b64ba02021-03-29 11:02:53 +0100541 if b.PropertyName == "boot_images" {
542 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_boot_image")
543 } else {
544 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_bootclasspath_fragment")
545 }
Paul Duffinf7f65da2021-03-10 15:00:46 +0000546}
547
Paul Duffin7771eba2021-04-23 14:25:28 +0100548func (b *bootclasspathFragmentMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
549 return &bootclasspathFragmentSdkMemberProperties{}
Paul Duffinf7f65da2021-03-10 15:00:46 +0000550}
551
Paul Duffin7771eba2021-04-23 14:25:28 +0100552type bootclasspathFragmentSdkMemberProperties struct {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000553 android.SdkMemberPropertiesBase
554
Paul Duffina57835e2021-04-19 13:23:06 +0100555 // The image name
Paul Duffin64be7bb2021-03-23 23:06:38 +0000556 Image_name *string
Paul Duffina57835e2021-04-19 13:23:06 +0100557
558 // Contents of the bootclasspath fragment
559 Contents []string
Paul Duffin7c955552021-04-19 13:23:53 +0100560
Paul Duffin895c7142021-04-25 13:40:15 +0100561 // Stub_libs properties.
562 Stub_libs []string
563 Core_platform_stub_libs []string
564
Paul Duffin7c955552021-04-19 13:23:53 +0100565 // Flag files by *hiddenAPIFlagFileCategory
566 Flag_files_by_category map[*hiddenAPIFlagFileCategory]android.Paths
Paul Duffin2fef1362021-04-15 13:32:00 +0100567
568 // The path to the generated stub-flags.csv file.
569 Stub_flags_path android.OptionalPath
570
571 // The path to the generated annotation-flags.csv file.
572 Annotation_flags_path android.OptionalPath
573
574 // The path to the generated metadata.csv file.
575 Metadata_path android.OptionalPath
576
577 // The path to the generated index.csv file.
578 Index_path android.OptionalPath
579
580 // The path to the generated all-flags.csv file.
581 All_flags_path android.OptionalPath
582}
583
584func pathsToOptionalPath(paths android.Paths) android.OptionalPath {
585 switch len(paths) {
586 case 0:
587 return android.OptionalPath{}
588 case 1:
589 return android.OptionalPathForPath(paths[0])
590 default:
591 panic(fmt.Errorf("expected 0 or 1 paths, found %q", paths))
592 }
Paul Duffinf7f65da2021-03-10 15:00:46 +0000593}
594
Paul Duffin7771eba2021-04-23 14:25:28 +0100595func (b *bootclasspathFragmentSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
596 module := variant.(*BootclasspathFragmentModule)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000597
598 b.Image_name = module.properties.Image_name
Paul Duffin2dc665b2021-04-23 16:58:51 +0100599 b.Contents = module.properties.Contents
Paul Duffin7c955552021-04-19 13:23:53 +0100600
601 // Get the flag file information from the module.
602 mctx := ctx.SdkModuleContext()
603 flagFileInfo := mctx.OtherModuleProvider(module, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo)
604 b.Flag_files_by_category = flagFileInfo.categoryToPaths
Paul Duffin895c7142021-04-25 13:40:15 +0100605
Paul Duffin2fef1362021-04-15 13:32:00 +0100606 // Copy all the generated file paths.
607 b.Stub_flags_path = pathsToOptionalPath(flagFileInfo.StubFlagsPaths)
608 b.Annotation_flags_path = pathsToOptionalPath(flagFileInfo.AnnotationFlagsPaths)
609 b.Metadata_path = pathsToOptionalPath(flagFileInfo.MetadataPaths)
610 b.Index_path = pathsToOptionalPath(flagFileInfo.IndexPaths)
611 b.All_flags_path = pathsToOptionalPath(flagFileInfo.AllFlagsPaths)
612
Paul Duffin895c7142021-04-25 13:40:15 +0100613 // Copy stub_libs properties.
614 b.Stub_libs = module.properties.Api.Stub_libs
615 b.Core_platform_stub_libs = module.properties.Core_platform_api.Stub_libs
Paul Duffinf7f65da2021-03-10 15:00:46 +0000616}
617
Paul Duffin7771eba2021-04-23 14:25:28 +0100618func (b *bootclasspathFragmentSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffin64be7bb2021-03-23 23:06:38 +0000619 if b.Image_name != nil {
620 propertySet.AddProperty("image_name", *b.Image_name)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000621 }
Paul Duffina57835e2021-04-19 13:23:06 +0100622
Paul Duffin895c7142021-04-25 13:40:15 +0100623 builder := ctx.SnapshotBuilder()
624 requiredMemberDependency := builder.SdkMemberReferencePropertyTag(true)
625
Paul Duffina57835e2021-04-19 13:23:06 +0100626 if len(b.Contents) > 0 {
Paul Duffin895c7142021-04-25 13:40:15 +0100627 propertySet.AddPropertyWithTag("contents", b.Contents, requiredMemberDependency)
Paul Duffina57835e2021-04-19 13:23:06 +0100628 }
Paul Duffin7c955552021-04-19 13:23:53 +0100629
Paul Duffin895c7142021-04-25 13:40:15 +0100630 if len(b.Stub_libs) > 0 {
631 apiPropertySet := propertySet.AddPropertySet("api")
632 apiPropertySet.AddPropertyWithTag("stub_libs", b.Stub_libs, requiredMemberDependency)
633 }
634 if len(b.Core_platform_stub_libs) > 0 {
635 corePlatformApiPropertySet := propertySet.AddPropertySet("core_platform_api")
636 corePlatformApiPropertySet.AddPropertyWithTag("stub_libs", b.Core_platform_stub_libs, requiredMemberDependency)
637 }
638
Paul Duffin2fef1362021-04-15 13:32:00 +0100639 hiddenAPISet := propertySet.AddPropertySet("hidden_api")
640 hiddenAPIDir := "hiddenapi"
641
642 // Copy manually curated flag files specified on the bootclasspath_fragment.
Paul Duffin7c955552021-04-19 13:23:53 +0100643 if b.Flag_files_by_category != nil {
Paul Duffin7c955552021-04-19 13:23:53 +0100644 for _, category := range hiddenAPIFlagFileCategories {
645 paths := b.Flag_files_by_category[category]
646 if len(paths) > 0 {
647 dests := []string{}
648 for _, p := range paths {
Paul Duffin2fef1362021-04-15 13:32:00 +0100649 dest := filepath.Join(hiddenAPIDir, p.Base())
Paul Duffin7c955552021-04-19 13:23:53 +0100650 builder.CopyToSnapshot(p, dest)
651 dests = append(dests, dest)
652 }
653 hiddenAPISet.AddProperty(category.propertyName, dests)
654 }
655 }
656 }
Paul Duffin2fef1362021-04-15 13:32:00 +0100657
658 copyOptionalPath := func(path android.OptionalPath, property string) {
659 if path.Valid() {
660 p := path.Path()
661 dest := filepath.Join(hiddenAPIDir, p.Base())
662 builder.CopyToSnapshot(p, dest)
663 hiddenAPISet.AddProperty(property, dest)
664 }
665 }
666
667 // Copy all the generated files, if available.
668 copyOptionalPath(b.Stub_flags_path, "stub_flags")
669 copyOptionalPath(b.Annotation_flags_path, "annotation_flags")
670 copyOptionalPath(b.Metadata_path, "metadata")
671 copyOptionalPath(b.Index_path, "index")
672 copyOptionalPath(b.All_flags_path, "all_flags")
Paul Duffinf7f65da2021-03-10 15:00:46 +0000673}
674
Paul Duffin7771eba2021-04-23 14:25:28 +0100675var _ android.SdkMemberType = (*bootclasspathFragmentMemberType)(nil)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000676
Paul Duffin2fef1362021-04-15 13:32:00 +0100677// prebuiltBootclasspathFragmentProperties contains additional prebuilt_bootclasspath_fragment
678// specific properties.
679type prebuiltBootclasspathFragmentProperties struct {
680 Hidden_api struct {
681 // The path to the stub-flags.csv file created by the bootclasspath_fragment.
682 Stub_flags *string `android:"path"`
683
684 // The path to the annotation-flags.csv file created by the bootclasspath_fragment.
685 Annotation_flags *string `android:"path"`
686
687 // The path to the metadata.csv file created by the bootclasspath_fragment.
688 Metadata *string `android:"path"`
689
690 // The path to the index.csv file created by the bootclasspath_fragment.
691 Index *string `android:"path"`
692
693 // The path to the all-flags.csv file created by the bootclasspath_fragment.
694 All_flags *string `android:"path"`
695 }
696}
697
Paul Duffin7771eba2021-04-23 14:25:28 +0100698// A prebuilt version of the bootclasspath_fragment module.
Paul Duffinf7f65da2021-03-10 15:00:46 +0000699//
Paul Duffin7771eba2021-04-23 14:25:28 +0100700// At the moment this is basically just a bootclasspath_fragment module that can be used as a
701// prebuilt. Eventually as more functionality is migrated into the bootclasspath_fragment module
702// type from the various singletons then this will diverge.
703type prebuiltBootclasspathFragmentModule struct {
704 BootclasspathFragmentModule
Paul Duffinf7f65da2021-03-10 15:00:46 +0000705 prebuilt android.Prebuilt
Paul Duffin2fef1362021-04-15 13:32:00 +0100706
707 // Additional prebuilt specific properties.
708 prebuiltProperties prebuiltBootclasspathFragmentProperties
Paul Duffinf7f65da2021-03-10 15:00:46 +0000709}
710
Paul Duffin7771eba2021-04-23 14:25:28 +0100711func (module *prebuiltBootclasspathFragmentModule) Prebuilt() *android.Prebuilt {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000712 return &module.prebuilt
713}
714
Paul Duffin7771eba2021-04-23 14:25:28 +0100715func (module *prebuiltBootclasspathFragmentModule) Name() string {
Paul Duffinf7f65da2021-03-10 15:00:46 +0000716 return module.prebuilt.Name(module.ModuleBase.Name())
717}
718
Paul Duffin2fef1362021-04-15 13:32:00 +0100719// produceHiddenAPIAllFlagsFile returns a path to the prebuilt all-flags.csv or nil if none is
720// specified.
721func (module *prebuiltBootclasspathFragmentModule) produceHiddenAPIAllFlagsFile(ctx android.ModuleContext, _ []android.Module, _ map[android.SdkKind]android.Paths, flagFileInfo *hiddenAPIFlagFileInfo) {
722 pathsForOptionalSrc := func(src *string) android.Paths {
723 if src == nil {
724 // TODO(b/179354495): Fail if this is not provided once prebuilts have been updated.
725 return nil
726 }
727 return android.Paths{android.PathForModuleSrc(ctx, *src)}
728 }
729
730 flagFileInfo.StubFlagsPaths = pathsForOptionalSrc(module.prebuiltProperties.Hidden_api.Stub_flags)
731 flagFileInfo.AnnotationFlagsPaths = pathsForOptionalSrc(module.prebuiltProperties.Hidden_api.Annotation_flags)
732 flagFileInfo.MetadataPaths = pathsForOptionalSrc(module.prebuiltProperties.Hidden_api.Metadata)
733 flagFileInfo.IndexPaths = pathsForOptionalSrc(module.prebuiltProperties.Hidden_api.Index)
734 flagFileInfo.AllFlagsPaths = pathsForOptionalSrc(module.prebuiltProperties.Hidden_api.All_flags)
735}
736
737var _ commonBootclasspathFragment = (*prebuiltBootclasspathFragmentModule)(nil)
738
Paul Duffin7771eba2021-04-23 14:25:28 +0100739func prebuiltBootclasspathFragmentFactory() android.Module {
740 m := &prebuiltBootclasspathFragmentModule{}
Paul Duffin2fef1362021-04-15 13:32:00 +0100741 m.AddProperties(&m.properties, &m.prebuiltProperties)
Paul Duffinf7f65da2021-03-10 15:00:46 +0000742 // This doesn't actually have any prebuilt files of its own so pass a placeholder for the srcs
743 // array.
744 android.InitPrebuiltModule(m, &[]string{"placeholder"})
745 android.InitApexModule(m)
746 android.InitSdkAwareModule(m)
Martin Stjernholmb79c7f12021-03-17 00:26:25 +0000747 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibCommon)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000748
Paul Duffin7771eba2021-04-23 14:25:28 +0100749 // Initialize the contents property from the image_name.
Paul Duffinc7ef9892021-03-23 23:21:59 +0000750 android.AddLoadHook(m, func(ctx android.LoadHookContext) {
Paul Duffin7771eba2021-04-23 14:25:28 +0100751 bootclasspathFragmentInitContentsFromImage(ctx, &m.BootclasspathFragmentModule)
Paul Duffinc7ef9892021-03-23 23:21:59 +0000752 })
Paul Duffinf7f65da2021-03-10 15:00:46 +0000753 return m
754}