blob: 634959a7e0831419d3f7355a64ecbe938d926088 [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 Duffin4994d262021-04-22 12:08:59 +010026func init() {
27 registerBootclasspathBuildComponents(android.InitRegistrationContext)
28}
29
30func registerBootclasspathBuildComponents(ctx android.RegistrationContext) {
31 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
Paul Duffin7487a7a2021-05-19 09:36:09 +010032 ctx.BottomUp("bootclasspath_deps", bootclasspathDepsMutator).Parallel()
Paul Duffin4994d262021-04-22 12:08:59 +010033 })
34}
35
36// BootclasspathDepsMutator is the interface that a module must implement if it wants to add
37// dependencies onto APEX specific variants of bootclasspath fragments or bootclasspath contents.
38type BootclasspathDepsMutator interface {
39 // BootclasspathDepsMutator implementations should add dependencies using
40 // addDependencyOntoApexModulePair and addDependencyOntoApexVariants.
41 BootclasspathDepsMutator(ctx android.BottomUpMutatorContext)
42}
43
44// bootclasspathDepsMutator is called during the final deps phase after all APEX variants have
45// been created so can add dependencies onto specific APEX variants of modules.
46func bootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
47 m := ctx.Module()
48 if p, ok := m.(BootclasspathDepsMutator); ok {
49 p.BootclasspathDepsMutator(ctx)
50 }
51}
52
Paul Duffinb67d8782021-04-22 11:49:41 +010053// addDependencyOntoApexVariants adds dependencies onto the appropriate apex specific variants of
54// the module as specified in the ApexVariantReference list.
55func addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tag blueprint.DependencyTag) {
56 for i, ref := range refs {
57 apex := proptools.StringDefault(ref.Apex, "platform")
58
59 if ref.Module == nil {
60 ctx.PropertyErrorf(propertyName, "missing module name at position %d", i)
61 continue
62 }
63 name := proptools.String(ref.Module)
64
65 addDependencyOntoApexModulePair(ctx, apex, name, tag)
66 }
67}
68
69// addDependencyOntoApexModulePair adds a dependency onto the specified APEX specific variant or the
70// specified module.
71//
Paul Duffin110b0ad2021-04-27 14:36:08 +010072// If apex="platform" or "system_ext" then this adds a dependency onto the platform variant of the
73// module. This adds dependencies onto the prebuilt and source modules with the specified name,
74// depending on which ones are available. Visiting must use isActiveModule to select the preferred
75// module when both source and prebuilt modules are available.
Paul Duffin9bacf562021-04-28 21:16:02 +010076//
77// Use gatherApexModulePairDepsWithTag to retrieve the dependencies.
Paul Duffinb67d8782021-04-22 11:49:41 +010078func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tag blueprint.DependencyTag) {
79 var variations []blueprint.Variation
Paul Duffin110b0ad2021-04-27 14:36:08 +010080 if apex != "platform" && apex != "system_ext" {
Paul Duffinb67d8782021-04-22 11:49:41 +010081 // Pick the correct apex variant.
82 variations = []blueprint.Variation{
83 {Mutator: "apex", Variation: apex},
84 }
85 }
86
87 addedDep := false
88 if ctx.OtherModuleDependencyVariantExists(variations, name) {
89 ctx.AddFarVariationDependencies(variations, tag, name)
90 addedDep = true
91 }
92
93 // Add a dependency on the prebuilt module if it exists.
94 prebuiltName := android.PrebuiltNameFromSource(name)
95 if ctx.OtherModuleDependencyVariantExists(variations, prebuiltName) {
96 ctx.AddVariationDependencies(variations, tag, prebuiltName)
97 addedDep = true
Paul Duffin7487a7a2021-05-19 09:36:09 +010098 } else if ctx.Config().AlwaysUsePrebuiltSdks() && len(variations) > 0 {
99 // TODO(b/179354495): Remove this code path once the Android build has been fully migrated to
100 // use bootclasspath_fragment properly.
101 // Some prebuilt java_sdk_library modules do not yet have an APEX variations so try and add a
102 // dependency on the non-APEX variant.
103 if ctx.OtherModuleDependencyVariantExists(nil, prebuiltName) {
104 ctx.AddVariationDependencies(nil, tag, prebuiltName)
105 addedDep = true
106 }
Paul Duffinb67d8782021-04-22 11:49:41 +0100107 }
108
109 // If no appropriate variant existing for this, so no dependency could be added, then it is an
110 // error, unless missing dependencies are allowed. The simplest way to handle that is to add a
111 // dependency that will not be satisfied and the default behavior will handle it.
112 if !addedDep {
113 // Add dependency on the unprefixed (i.e. source or renamed prebuilt) module which we know does
114 // not exist. The resulting error message will contain useful information about the available
115 // variants.
116 reportMissingVariationDependency(ctx, variations, name)
117
118 // Add dependency on the missing prefixed prebuilt variant too if a module with that name exists
119 // so that information about its available variants will be reported too.
120 if ctx.OtherModuleExists(prebuiltName) {
121 reportMissingVariationDependency(ctx, variations, prebuiltName)
122 }
123 }
124}
125
126// reportMissingVariationDependency intentionally adds a dependency on a missing variation in order
127// to generate an appropriate error message with information about the available variations.
128func reportMissingVariationDependency(ctx android.BottomUpMutatorContext, variations []blueprint.Variation, name string) {
Paul Duffin81667c82021-04-26 13:55:36 +0100129 ctx.AddFarVariationDependencies(variations, nil, name)
Paul Duffinb67d8782021-04-22 11:49:41 +0100130}
131
Paul Duffin9bacf562021-04-28 21:16:02 +0100132// gatherApexModulePairDepsWithTag returns the list of dependencies with the supplied tag that was
133// added by addDependencyOntoApexModulePair.
134func gatherApexModulePairDepsWithTag(ctx android.BaseModuleContext, tag blueprint.DependencyTag) []android.Module {
135 var modules []android.Module
136 ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
137 t := ctx.OtherModuleDependencyTag(module)
138 if t == tag {
139 modules = append(modules, module)
140 }
141 })
142 return modules
143}
144
Paul Duffinb67d8782021-04-22 11:49:41 +0100145// ApexVariantReference specifies a particular apex variant of a module.
146type ApexVariantReference struct {
147 // The name of the module apex variant, i.e. the apex containing the module variant.
148 //
149 // If this is not specified then it defaults to "platform" which will cause a dependency to be
150 // added to the module's platform variant.
Paul Duffin110b0ad2021-04-27 14:36:08 +0100151 //
152 // A value of system_ext should be used for any module that will be part of the system_ext
153 // partition.
Paul Duffinb67d8782021-04-22 11:49:41 +0100154 Apex *string
155
156 // The name of the module.
157 Module *string
158}
159
160// BootclasspathFragmentsDepsProperties contains properties related to dependencies onto fragments.
161type BootclasspathFragmentsDepsProperties struct {
162 // The names of the bootclasspath_fragment modules that form part of this module.
163 Fragments []ApexVariantReference
164}
165
166// addDependenciesOntoFragments adds dependencies to the fragments specified in this properties
167// structure.
168func (p *BootclasspathFragmentsDepsProperties) addDependenciesOntoFragments(ctx android.BottomUpMutatorContext) {
169 addDependencyOntoApexVariants(ctx, "fragments", p.Fragments, bootclasspathFragmentDepTag)
170}
171
172// bootclasspathDependencyTag defines dependencies from/to bootclasspath_fragment,
173// prebuilt_bootclasspath_fragment and platform_bootclasspath onto either source or prebuilt
174// modules.
175type bootclasspathDependencyTag struct {
176 blueprint.BaseDependencyTag
177
178 name string
179}
180
181func (t bootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() {
182}
183
184// Dependencies that use the bootclasspathDependencyTag instances are only added after all the
185// visibility checking has been done so this has no functional effect. However, it does make it
186// clear that visibility is not being enforced on these tags.
187var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathDependencyTag{}
188
189// The tag used for dependencies onto bootclasspath_fragments.
190var bootclasspathFragmentDepTag = bootclasspathDependencyTag{name: "fragment"}
Paul Duffin10931582021-04-25 10:13:54 +0100191
192// BootclasspathNestedAPIProperties defines properties related to the API provided by parts of the
193// bootclasspath that are nested within the main BootclasspathAPIProperties.
194type BootclasspathNestedAPIProperties struct {
195 // java_library or preferably, java_sdk_library modules providing stub classes that define the
196 // APIs provided by this bootclasspath_fragment.
197 Stub_libs []string
198}
199
200// BootclasspathAPIProperties defines properties for defining the API provided by parts of the
201// bootclasspath.
202type BootclasspathAPIProperties struct {
203 // Api properties provide information about the APIs provided by the bootclasspath_fragment.
204 // Properties in this section apply to public, system and test api scopes. They DO NOT apply to
205 // core_platform as that is a special, ART specific scope, that does not follow the pattern and so
206 // has its own section. It is in the process of being deprecated and replaced by the system scope
207 // but this will remain for the foreseeable future to maintain backwards compatibility.
208 //
209 // Every bootclasspath_fragment must specify at least one stubs_lib in this section and must
210 // specify stubs for all the APIs provided by its contents. Failure to do so will lead to those
211 // methods being inaccessible to other parts of Android, including but not limited to
212 // applications.
213 Api BootclasspathNestedAPIProperties
214
215 // Properties related to the core platform API surface.
216 //
217 // This must only be used by the following modules:
218 // * ART
219 // * Conscrypt
220 // * I18N
221 //
222 // The bootclasspath_fragments for each of the above modules must specify at least one stubs_lib
223 // and must specify stubs for all the APIs provided by its contents. Failure to do so will lead to
224 // those methods being inaccessible to the other modules in the list.
225 Core_platform_api BootclasspathNestedAPIProperties
226}
227
228// sdkKindToStubLibs calculates the stub library modules for each relevant android.SdkKind from the
229// Stub_libs properties.
230func (p BootclasspathAPIProperties) sdkKindToStubLibs() map[android.SdkKind][]string {
231 m := map[android.SdkKind][]string{}
232 for _, kind := range []android.SdkKind{android.SdkPublic, android.SdkSystem, android.SdkTest} {
233 m[kind] = p.Api.Stub_libs
234 }
235 m[android.SdkCorePlatform] = p.Core_platform_api.Stub_libs
236 return m
237}
238
239// bootclasspathApiInfo contains paths resolved from BootclasspathAPIProperties
240type bootclasspathApiInfo struct {
241 // stubJarsByKind maps from the android.SdkKind to the paths containing dex stub jars for each
242 // kind.
243 stubJarsByKind map[android.SdkKind]android.Paths
244}
245
246var bootclasspathApiInfoProvider = blueprint.NewProvider(bootclasspathApiInfo{})