blob: f03a67302ac4a906b89af4d8c92fa7a6f08c097b [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) {
32 ctx.BottomUp("bootclasspath_deps", bootclasspathDepsMutator)
33 })
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//
72// If apex="platform" then this adds a dependency onto the platform variant of the module. This adds
73// dependencies onto the prebuilt and source modules with the specified name, depending on which
74// ones are available. Visiting must use isActiveModule to select the preferred module when both
75// source and prebuilt modules are available.
76func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tag blueprint.DependencyTag) {
77 var variations []blueprint.Variation
78 if apex != "platform" {
79 // Pick the correct apex variant.
80 variations = []blueprint.Variation{
81 {Mutator: "apex", Variation: apex},
82 }
83 }
84
85 addedDep := false
86 if ctx.OtherModuleDependencyVariantExists(variations, name) {
87 ctx.AddFarVariationDependencies(variations, tag, name)
88 addedDep = true
89 }
90
91 // Add a dependency on the prebuilt module if it exists.
92 prebuiltName := android.PrebuiltNameFromSource(name)
93 if ctx.OtherModuleDependencyVariantExists(variations, prebuiltName) {
94 ctx.AddVariationDependencies(variations, tag, prebuiltName)
95 addedDep = true
96 }
97
98 // If no appropriate variant existing for this, so no dependency could be added, then it is an
99 // error, unless missing dependencies are allowed. The simplest way to handle that is to add a
100 // dependency that will not be satisfied and the default behavior will handle it.
101 if !addedDep {
102 // Add dependency on the unprefixed (i.e. source or renamed prebuilt) module which we know does
103 // not exist. The resulting error message will contain useful information about the available
104 // variants.
105 reportMissingVariationDependency(ctx, variations, name)
106
107 // Add dependency on the missing prefixed prebuilt variant too if a module with that name exists
108 // so that information about its available variants will be reported too.
109 if ctx.OtherModuleExists(prebuiltName) {
110 reportMissingVariationDependency(ctx, variations, prebuiltName)
111 }
112 }
113}
114
115// reportMissingVariationDependency intentionally adds a dependency on a missing variation in order
116// to generate an appropriate error message with information about the available variations.
117func reportMissingVariationDependency(ctx android.BottomUpMutatorContext, variations []blueprint.Variation, name string) {
Paul Duffin81667c82021-04-26 13:55:36 +0100118 ctx.AddFarVariationDependencies(variations, nil, name)
Paul Duffinb67d8782021-04-22 11:49:41 +0100119}
120
121// ApexVariantReference specifies a particular apex variant of a module.
122type ApexVariantReference struct {
123 // The name of the module apex variant, i.e. the apex containing the module variant.
124 //
125 // If this is not specified then it defaults to "platform" which will cause a dependency to be
126 // added to the module's platform variant.
127 Apex *string
128
129 // The name of the module.
130 Module *string
131}
132
133// BootclasspathFragmentsDepsProperties contains properties related to dependencies onto fragments.
134type BootclasspathFragmentsDepsProperties struct {
135 // The names of the bootclasspath_fragment modules that form part of this module.
136 Fragments []ApexVariantReference
137}
138
139// addDependenciesOntoFragments adds dependencies to the fragments specified in this properties
140// structure.
141func (p *BootclasspathFragmentsDepsProperties) addDependenciesOntoFragments(ctx android.BottomUpMutatorContext) {
142 addDependencyOntoApexVariants(ctx, "fragments", p.Fragments, bootclasspathFragmentDepTag)
143}
144
145// bootclasspathDependencyTag defines dependencies from/to bootclasspath_fragment,
146// prebuilt_bootclasspath_fragment and platform_bootclasspath onto either source or prebuilt
147// modules.
148type bootclasspathDependencyTag struct {
149 blueprint.BaseDependencyTag
150
151 name string
152}
153
154func (t bootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() {
155}
156
157// Dependencies that use the bootclasspathDependencyTag instances are only added after all the
158// visibility checking has been done so this has no functional effect. However, it does make it
159// clear that visibility is not being enforced on these tags.
160var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathDependencyTag{}
161
162// The tag used for dependencies onto bootclasspath_fragments.
163var bootclasspathFragmentDepTag = bootclasspathDependencyTag{name: "fragment"}
Paul Duffin10931582021-04-25 10:13:54 +0100164
165// BootclasspathNestedAPIProperties defines properties related to the API provided by parts of the
166// bootclasspath that are nested within the main BootclasspathAPIProperties.
167type BootclasspathNestedAPIProperties struct {
168 // java_library or preferably, java_sdk_library modules providing stub classes that define the
169 // APIs provided by this bootclasspath_fragment.
170 Stub_libs []string
171}
172
173// BootclasspathAPIProperties defines properties for defining the API provided by parts of the
174// bootclasspath.
175type BootclasspathAPIProperties struct {
176 // Api properties provide information about the APIs provided by the bootclasspath_fragment.
177 // Properties in this section apply to public, system and test api scopes. They DO NOT apply to
178 // core_platform as that is a special, ART specific scope, that does not follow the pattern and so
179 // has its own section. It is in the process of being deprecated and replaced by the system scope
180 // but this will remain for the foreseeable future to maintain backwards compatibility.
181 //
182 // Every bootclasspath_fragment must specify at least one stubs_lib in this section and must
183 // specify stubs for all the APIs provided by its contents. Failure to do so will lead to those
184 // methods being inaccessible to other parts of Android, including but not limited to
185 // applications.
186 Api BootclasspathNestedAPIProperties
187
188 // Properties related to the core platform API surface.
189 //
190 // This must only be used by the following modules:
191 // * ART
192 // * Conscrypt
193 // * I18N
194 //
195 // The bootclasspath_fragments for each of the above modules must specify at least one stubs_lib
196 // and must specify stubs for all the APIs provided by its contents. Failure to do so will lead to
197 // those methods being inaccessible to the other modules in the list.
198 Core_platform_api BootclasspathNestedAPIProperties
199}
200
201// sdkKindToStubLibs calculates the stub library modules for each relevant android.SdkKind from the
202// Stub_libs properties.
203func (p BootclasspathAPIProperties) sdkKindToStubLibs() map[android.SdkKind][]string {
204 m := map[android.SdkKind][]string{}
205 for _, kind := range []android.SdkKind{android.SdkPublic, android.SdkSystem, android.SdkTest} {
206 m[kind] = p.Api.Stub_libs
207 }
208 m[android.SdkCorePlatform] = p.Core_platform_api.Stub_libs
209 return m
210}
211
212// bootclasspathApiInfo contains paths resolved from BootclasspathAPIProperties
213type bootclasspathApiInfo struct {
214 // stubJarsByKind maps from the android.SdkKind to the paths containing dex stub jars for each
215 // kind.
216 stubJarsByKind map[android.SdkKind]android.Paths
217}
218
219var bootclasspathApiInfoProvider = blueprint.NewProvider(bootclasspathApiInfo{})