blob: 856f43919f665bf0a92986ad2882400ce126b18d [file] [log] [blame]
Paul Duffinb67d8782021-04-22 11:49:41 +01001// Copyright 2021 Google Inc. All rights reserved.
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 Duffinb67d8782021-04-22 11:49:41 +010018 "android/soong/android"
Paul Duffin81667c82021-04-26 13:55:36 +010019
Paul Duffinb67d8782021-04-22 11:49:41 +010020 "github.com/google/blueprint"
21 "github.com/google/blueprint/proptools"
22)
23
24// Contains code that is common to both platform_bootclasspath and bootclasspath_fragment.
25
Paul Duffinb67d8782021-04-22 11:49:41 +010026// addDependencyOntoApexVariants adds dependencies onto the appropriate apex specific variants of
27// the module as specified in the ApexVariantReference list.
Colin Crossd8d8b852024-12-20 16:32:37 -080028func addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tagType bootclasspathDependencyTagType) {
Paul Duffinb67d8782021-04-22 11:49:41 +010029 for i, ref := range refs {
30 apex := proptools.StringDefault(ref.Apex, "platform")
31
32 if ref.Module == nil {
33 ctx.PropertyErrorf(propertyName, "missing module name at position %d", i)
34 continue
35 }
36 name := proptools.String(ref.Module)
37
Colin Crossd8d8b852024-12-20 16:32:37 -080038 addDependencyOntoApexModulePair(ctx, apex, name, tagType)
Paul Duffinb67d8782021-04-22 11:49:41 +010039 }
40}
41
42// addDependencyOntoApexModulePair adds a dependency onto the specified APEX specific variant or the
43// specified module.
44//
Paul Duffin110b0ad2021-04-27 14:36:08 +010045// If apex="platform" or "system_ext" then this adds a dependency onto the platform variant of the
46// module. This adds dependencies onto the prebuilt and source modules with the specified name,
47// depending on which ones are available. Visiting must use isActiveModule to select the preferred
48// module when both source and prebuilt modules are available.
Paul Duffin9bacf562021-04-28 21:16:02 +010049//
50// Use gatherApexModulePairDepsWithTag to retrieve the dependencies.
Colin Crossd8d8b852024-12-20 16:32:37 -080051func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tagType bootclasspathDependencyTagType) {
52 tag := bootclasspathDependencyTag{
53 typ: tagType,
54 }
Jiakai Zhangb1639db2023-07-11 15:03:13 +010055 if !android.IsConfiguredJarForPlatform(apex) {
Colin Crossd8d8b852024-12-20 16:32:37 -080056 tag.apex = apex
Paul Duffinb67d8782021-04-22 11:49:41 +010057 }
58
Paul Duffinb2c21732022-05-11 14:29:53 +000059 target := ctx.Module().Target()
Paul Duffinb2c21732022-05-11 14:29:53 +000060
Colin Crossd8d8b852024-12-20 16:32:37 -080061 ctx.AddFarVariationDependencies(target.Variations(), tag, name)
Paul Duffinb67d8782021-04-22 11:49:41 +010062}
63
Paul Duffin9bacf562021-04-28 21:16:02 +010064// gatherApexModulePairDepsWithTag returns the list of dependencies with the supplied tag that was
65// added by addDependencyOntoApexModulePair.
Colin Crossd8d8b852024-12-20 16:32:37 -080066func gatherApexModulePairDepsWithTag(ctx android.BaseModuleContext, tagType bootclasspathDependencyTagType) []android.Module {
Paul Duffin9bacf562021-04-28 21:16:02 +010067 var modules []android.Module
Colin Crossd8d8b852024-12-20 16:32:37 -080068 ctx.VisitDirectDeps(func(module android.Module) {
Paul Duffin9bacf562021-04-28 21:16:02 +010069 t := ctx.OtherModuleDependencyTag(module)
Colin Crossd8d8b852024-12-20 16:32:37 -080070 if bcpTag, ok := t.(bootclasspathDependencyTag); ok && bcpTag.typ == tagType {
Paul Duffin9bacf562021-04-28 21:16:02 +010071 modules = append(modules, module)
72 }
73 })
74 return modules
75}
76
Paul Duffinb67d8782021-04-22 11:49:41 +010077// ApexVariantReference specifies a particular apex variant of a module.
78type ApexVariantReference struct {
Paul Duffin51227d82021-05-18 12:54:27 +010079 android.BpPrintableBase
80
Paul Duffinb67d8782021-04-22 11:49:41 +010081 // The name of the module apex variant, i.e. the apex containing the module variant.
82 //
83 // If this is not specified then it defaults to "platform" which will cause a dependency to be
84 // added to the module's platform variant.
Paul Duffin110b0ad2021-04-27 14:36:08 +010085 //
86 // A value of system_ext should be used for any module that will be part of the system_ext
87 // partition.
Paul Duffinb67d8782021-04-22 11:49:41 +010088 Apex *string
89
90 // The name of the module.
91 Module *string
92}
93
94// BootclasspathFragmentsDepsProperties contains properties related to dependencies onto fragments.
95type BootclasspathFragmentsDepsProperties struct {
96 // The names of the bootclasspath_fragment modules that form part of this module.
97 Fragments []ApexVariantReference
98}
99
100// addDependenciesOntoFragments adds dependencies to the fragments specified in this properties
101// structure.
102func (p *BootclasspathFragmentsDepsProperties) addDependenciesOntoFragments(ctx android.BottomUpMutatorContext) {
Colin Crossd8d8b852024-12-20 16:32:37 -0800103 addDependencyOntoApexVariants(ctx, "fragments", p.Fragments, fragment)
Paul Duffinb67d8782021-04-22 11:49:41 +0100104}
105
106// bootclasspathDependencyTag defines dependencies from/to bootclasspath_fragment,
107// prebuilt_bootclasspath_fragment and platform_bootclasspath onto either source or prebuilt
108// modules.
109type bootclasspathDependencyTag struct {
110 blueprint.BaseDependencyTag
111
Colin Crossd8d8b852024-12-20 16:32:37 -0800112 typ bootclasspathDependencyTagType
113
114 apex string
Paul Duffinb67d8782021-04-22 11:49:41 +0100115}
116
Colin Crossd8d8b852024-12-20 16:32:37 -0800117type bootclasspathDependencyTagType int
118
119const (
120 // The tag used for dependencies onto bootclasspath_fragments.
121 fragment bootclasspathDependencyTagType = iota
122 // The tag used for dependencies onto platform_bootclasspath.
123 platform
124 dexpreoptBootJar
125 artBootJar
126 platformBootJar
127 apexBootJar
128)
129
Paul Duffinb67d8782021-04-22 11:49:41 +0100130func (t bootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() {
131}
132
Colin Crossd8d8b852024-12-20 16:32:37 -0800133func (t bootclasspathDependencyTag) ApexTransition() string {
134 return t.apex
135}
136
Paul Duffinb67d8782021-04-22 11:49:41 +0100137// Dependencies that use the bootclasspathDependencyTag instances are only added after all the
138// visibility checking has been done so this has no functional effect. However, it does make it
139// clear that visibility is not being enforced on these tags.
140var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathDependencyTag{}
141
Paul Duffin10931582021-04-25 10:13:54 +0100142// BootclasspathNestedAPIProperties defines properties related to the API provided by parts of the
143// bootclasspath that are nested within the main BootclasspathAPIProperties.
144type BootclasspathNestedAPIProperties struct {
145 // java_library or preferably, java_sdk_library modules providing stub classes that define the
146 // APIs provided by this bootclasspath_fragment.
Cole Faustaaff7822024-07-30 12:59:44 -0700147 Stub_libs proptools.Configurable[[]string]
Paul Duffin10931582021-04-25 10:13:54 +0100148}
149
150// BootclasspathAPIProperties defines properties for defining the API provided by parts of the
151// bootclasspath.
152type BootclasspathAPIProperties struct {
153 // Api properties provide information about the APIs provided by the bootclasspath_fragment.
154 // Properties in this section apply to public, system and test api scopes. They DO NOT apply to
155 // core_platform as that is a special, ART specific scope, that does not follow the pattern and so
156 // has its own section. It is in the process of being deprecated and replaced by the system scope
157 // but this will remain for the foreseeable future to maintain backwards compatibility.
158 //
159 // Every bootclasspath_fragment must specify at least one stubs_lib in this section and must
160 // specify stubs for all the APIs provided by its contents. Failure to do so will lead to those
161 // methods being inaccessible to other parts of Android, including but not limited to
162 // applications.
163 Api BootclasspathNestedAPIProperties
164
165 // Properties related to the core platform API surface.
166 //
167 // This must only be used by the following modules:
168 // * ART
169 // * Conscrypt
170 // * I18N
171 //
172 // The bootclasspath_fragments for each of the above modules must specify at least one stubs_lib
173 // and must specify stubs for all the APIs provided by its contents. Failure to do so will lead to
174 // those methods being inaccessible to the other modules in the list.
175 Core_platform_api BootclasspathNestedAPIProperties
176}
177
Paul Duffin31fad802021-06-18 18:14:25 +0100178// apiScopeToStubLibs calculates the stub library modules for each relevant *HiddenAPIScope from the
Paul Duffin10931582021-04-25 10:13:54 +0100179// Stub_libs properties.
Cole Faustaaff7822024-07-30 12:59:44 -0700180func (p BootclasspathAPIProperties) apiScopeToStubLibs(ctx android.BaseModuleContext) map[*HiddenAPIScope][]string {
Paul Duffin31fad802021-06-18 18:14:25 +0100181 m := map[*HiddenAPIScope][]string{}
182 for _, apiScope := range hiddenAPISdkLibrarySupportedScopes {
Cole Faustaaff7822024-07-30 12:59:44 -0700183 m[apiScope] = p.Api.Stub_libs.GetOrDefault(ctx, nil)
Paul Duffin10931582021-04-25 10:13:54 +0100184 }
Cole Faustaaff7822024-07-30 12:59:44 -0700185 m[CorePlatformHiddenAPIScope] = p.Core_platform_api.Stub_libs.GetOrDefault(ctx, nil)
Paul Duffin10931582021-04-25 10:13:54 +0100186 return m
187}