Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Colin Cross | d649580 | 2025-01-14 15:50:48 -0800 | [diff] [blame^] | 18 | "fmt" |
| 19 | |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 20 | "android/soong/android" |
Paul Duffin | 81667c8 | 2021-04-26 13:55:36 +0100 | [diff] [blame] | 21 | |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 22 | "github.com/google/blueprint" |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
| 26 | // Contains code that is common to both platform_bootclasspath and bootclasspath_fragment. |
| 27 | |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 28 | // addDependencyOntoApexVariants adds dependencies onto the appropriate apex specific variants of |
| 29 | // the module as specified in the ApexVariantReference list. |
Colin Cross | d8d8b85 | 2024-12-20 16:32:37 -0800 | [diff] [blame] | 30 | func addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tagType bootclasspathDependencyTagType) { |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 31 | for i, ref := range refs { |
| 32 | apex := proptools.StringDefault(ref.Apex, "platform") |
| 33 | |
| 34 | if ref.Module == nil { |
| 35 | ctx.PropertyErrorf(propertyName, "missing module name at position %d", i) |
| 36 | continue |
| 37 | } |
| 38 | name := proptools.String(ref.Module) |
| 39 | |
Colin Cross | d8d8b85 | 2024-12-20 16:32:37 -0800 | [diff] [blame] | 40 | addDependencyOntoApexModulePair(ctx, apex, name, tagType) |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | // addDependencyOntoApexModulePair adds a dependency onto the specified APEX specific variant or the |
| 45 | // specified module. |
| 46 | // |
Paul Duffin | 110b0ad | 2021-04-27 14:36:08 +0100 | [diff] [blame] | 47 | // If apex="platform" or "system_ext" then this adds a dependency onto the platform variant of the |
| 48 | // module. This adds dependencies onto the prebuilt and source modules with the specified name, |
| 49 | // depending on which ones are available. Visiting must use isActiveModule to select the preferred |
| 50 | // module when both source and prebuilt modules are available. |
Paul Duffin | 9bacf56 | 2021-04-28 21:16:02 +0100 | [diff] [blame] | 51 | // |
| 52 | // Use gatherApexModulePairDepsWithTag to retrieve the dependencies. |
Colin Cross | d8d8b85 | 2024-12-20 16:32:37 -0800 | [diff] [blame] | 53 | func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tagType bootclasspathDependencyTagType) { |
| 54 | tag := bootclasspathDependencyTag{ |
| 55 | typ: tagType, |
| 56 | } |
Colin Cross | d649580 | 2025-01-14 15:50:48 -0800 | [diff] [blame^] | 57 | target := ctx.Module().Target() |
| 58 | if android.IsConfiguredJarForPlatform(apex) { |
| 59 | // Platform variant, add a direct dependency. |
| 60 | ctx.AddFarVariationDependencies(target.Variations(), tag, name) |
| 61 | } else { |
| 62 | // A module in an apex. Dependencies can't be added directly onto an apex variation, as that would |
| 63 | // require constructing a full ApexInfo configuration, which can't be predicted here. Add a dependency |
| 64 | // on the apex instead, and annotate the dependency tag with the desired module in the apex. |
| 65 | tag.moduleInApex = name |
| 66 | ctx.AddFarVariationDependencies(target.Variations(), tag, apex) |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Paul Duffin | 9bacf56 | 2021-04-28 21:16:02 +0100 | [diff] [blame] | 71 | // gatherApexModulePairDepsWithTag returns the list of dependencies with the supplied tag that was |
| 72 | // added by addDependencyOntoApexModulePair. |
Colin Cross | d8d8b85 | 2024-12-20 16:32:37 -0800 | [diff] [blame] | 73 | func gatherApexModulePairDepsWithTag(ctx android.BaseModuleContext, tagType bootclasspathDependencyTagType) []android.Module { |
Paul Duffin | 9bacf56 | 2021-04-28 21:16:02 +0100 | [diff] [blame] | 74 | var modules []android.Module |
Colin Cross | d649580 | 2025-01-14 15:50:48 -0800 | [diff] [blame^] | 75 | |
| 76 | type moduleInApex struct { |
| 77 | module string |
| 78 | apex string |
| 79 | } |
| 80 | |
| 81 | var modulesInApexes []moduleInApex |
| 82 | |
Colin Cross | d8d8b85 | 2024-12-20 16:32:37 -0800 | [diff] [blame] | 83 | ctx.VisitDirectDeps(func(module android.Module) { |
Paul Duffin | 9bacf56 | 2021-04-28 21:16:02 +0100 | [diff] [blame] | 84 | t := ctx.OtherModuleDependencyTag(module) |
Colin Cross | d8d8b85 | 2024-12-20 16:32:37 -0800 | [diff] [blame] | 85 | if bcpTag, ok := t.(bootclasspathDependencyTag); ok && bcpTag.typ == tagType { |
Colin Cross | d649580 | 2025-01-14 15:50:48 -0800 | [diff] [blame^] | 86 | if bcpTag.moduleInApex != "" { |
| 87 | modulesInApexes = append(modulesInApexes, moduleInApex{bcpTag.moduleInApex, ctx.OtherModuleName(module)}) |
| 88 | } else { |
| 89 | modules = append(modules, module) |
| 90 | } |
Paul Duffin | 9bacf56 | 2021-04-28 21:16:02 +0100 | [diff] [blame] | 91 | } |
| 92 | }) |
Colin Cross | d649580 | 2025-01-14 15:50:48 -0800 | [diff] [blame^] | 93 | |
| 94 | for _, moduleInApex := range modulesInApexes { |
| 95 | var found android.Module |
| 96 | ctx.WalkDeps(func(child, parent android.Module) bool { |
| 97 | t := ctx.OtherModuleDependencyTag(child) |
| 98 | if parent == ctx.Module() { |
| 99 | if bcpTag, ok := t.(bootclasspathDependencyTag); ok && bcpTag.typ == tagType && ctx.OtherModuleName(child) == moduleInApex.apex { |
| 100 | // recurse into the apex |
| 101 | return true |
| 102 | } |
| 103 | } else if tagType != fragment && android.IsFragmentInApexTag(t) { |
| 104 | return true |
| 105 | } else if android.IsDontReplaceSourceWithPrebuiltTag(t) { |
| 106 | return false |
| 107 | } else if t == android.PrebuiltDepTag { |
| 108 | return false |
| 109 | } else if IsBootclasspathFragmentContentDepTag(t) { |
| 110 | return false |
| 111 | } else if android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child)) == moduleInApex.module { |
| 112 | if found != nil && child != found { |
| 113 | panic(fmt.Errorf("found two conflicting modules %q in apex %q: %s and %s", |
| 114 | moduleInApex.module, moduleInApex.apex, found, child)) |
| 115 | } |
| 116 | found = child |
| 117 | } |
| 118 | return false |
| 119 | }) |
| 120 | if found != nil { |
| 121 | modules = append(modules, found) |
| 122 | } else if !ctx.Config().AllowMissingDependencies() { |
| 123 | ctx.ModuleErrorf("failed to find module %q in apex %q\n", |
| 124 | moduleInApex.module, moduleInApex.apex) |
| 125 | } |
| 126 | } |
Paul Duffin | 9bacf56 | 2021-04-28 21:16:02 +0100 | [diff] [blame] | 127 | return modules |
| 128 | } |
| 129 | |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 130 | // ApexVariantReference specifies a particular apex variant of a module. |
| 131 | type ApexVariantReference struct { |
Paul Duffin | 51227d8 | 2021-05-18 12:54:27 +0100 | [diff] [blame] | 132 | android.BpPrintableBase |
| 133 | |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 134 | // The name of the module apex variant, i.e. the apex containing the module variant. |
| 135 | // |
| 136 | // If this is not specified then it defaults to "platform" which will cause a dependency to be |
| 137 | // added to the module's platform variant. |
Paul Duffin | 110b0ad | 2021-04-27 14:36:08 +0100 | [diff] [blame] | 138 | // |
| 139 | // A value of system_ext should be used for any module that will be part of the system_ext |
| 140 | // partition. |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 141 | Apex *string |
| 142 | |
| 143 | // The name of the module. |
| 144 | Module *string |
| 145 | } |
| 146 | |
| 147 | // BootclasspathFragmentsDepsProperties contains properties related to dependencies onto fragments. |
| 148 | type BootclasspathFragmentsDepsProperties struct { |
| 149 | // The names of the bootclasspath_fragment modules that form part of this module. |
| 150 | Fragments []ApexVariantReference |
| 151 | } |
| 152 | |
| 153 | // addDependenciesOntoFragments adds dependencies to the fragments specified in this properties |
| 154 | // structure. |
| 155 | func (p *BootclasspathFragmentsDepsProperties) addDependenciesOntoFragments(ctx android.BottomUpMutatorContext) { |
Colin Cross | d8d8b85 | 2024-12-20 16:32:37 -0800 | [diff] [blame] | 156 | addDependencyOntoApexVariants(ctx, "fragments", p.Fragments, fragment) |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // bootclasspathDependencyTag defines dependencies from/to bootclasspath_fragment, |
| 160 | // prebuilt_bootclasspath_fragment and platform_bootclasspath onto either source or prebuilt |
| 161 | // modules. |
| 162 | type bootclasspathDependencyTag struct { |
| 163 | blueprint.BaseDependencyTag |
| 164 | |
Colin Cross | d8d8b85 | 2024-12-20 16:32:37 -0800 | [diff] [blame] | 165 | typ bootclasspathDependencyTagType |
| 166 | |
Colin Cross | d649580 | 2025-01-14 15:50:48 -0800 | [diff] [blame^] | 167 | // moduleInApex is set to the name of the desired module when this dependency points |
| 168 | // to the apex that the modules is contained in. |
| 169 | moduleInApex string |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Colin Cross | d8d8b85 | 2024-12-20 16:32:37 -0800 | [diff] [blame] | 172 | type bootclasspathDependencyTagType int |
| 173 | |
| 174 | const ( |
| 175 | // The tag used for dependencies onto bootclasspath_fragments. |
| 176 | fragment bootclasspathDependencyTagType = iota |
| 177 | // The tag used for dependencies onto platform_bootclasspath. |
| 178 | platform |
| 179 | dexpreoptBootJar |
| 180 | artBootJar |
| 181 | platformBootJar |
| 182 | apexBootJar |
| 183 | ) |
| 184 | |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 185 | func (t bootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() { |
| 186 | } |
| 187 | |
Colin Cross | d649580 | 2025-01-14 15:50:48 -0800 | [diff] [blame^] | 188 | func (t bootclasspathDependencyTag) LicenseAnnotations() []android.LicenseAnnotation { |
| 189 | return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency} |
Colin Cross | d8d8b85 | 2024-12-20 16:32:37 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Paul Duffin | b67d878 | 2021-04-22 11:49:41 +0100 | [diff] [blame] | 192 | // Dependencies that use the bootclasspathDependencyTag instances are only added after all the |
| 193 | // visibility checking has been done so this has no functional effect. However, it does make it |
| 194 | // clear that visibility is not being enforced on these tags. |
| 195 | var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathDependencyTag{} |
| 196 | |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 197 | // BootclasspathNestedAPIProperties defines properties related to the API provided by parts of the |
| 198 | // bootclasspath that are nested within the main BootclasspathAPIProperties. |
| 199 | type BootclasspathNestedAPIProperties struct { |
| 200 | // java_library or preferably, java_sdk_library modules providing stub classes that define the |
| 201 | // APIs provided by this bootclasspath_fragment. |
Cole Faust | aaff782 | 2024-07-30 12:59:44 -0700 | [diff] [blame] | 202 | Stub_libs proptools.Configurable[[]string] |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // BootclasspathAPIProperties defines properties for defining the API provided by parts of the |
| 206 | // bootclasspath. |
| 207 | type BootclasspathAPIProperties struct { |
| 208 | // Api properties provide information about the APIs provided by the bootclasspath_fragment. |
| 209 | // Properties in this section apply to public, system and test api scopes. They DO NOT apply to |
| 210 | // core_platform as that is a special, ART specific scope, that does not follow the pattern and so |
| 211 | // has its own section. It is in the process of being deprecated and replaced by the system scope |
| 212 | // but this will remain for the foreseeable future to maintain backwards compatibility. |
| 213 | // |
| 214 | // Every bootclasspath_fragment must specify at least one stubs_lib in this section and must |
| 215 | // specify stubs for all the APIs provided by its contents. Failure to do so will lead to those |
| 216 | // methods being inaccessible to other parts of Android, including but not limited to |
| 217 | // applications. |
| 218 | Api BootclasspathNestedAPIProperties |
| 219 | |
| 220 | // Properties related to the core platform API surface. |
| 221 | // |
| 222 | // This must only be used by the following modules: |
| 223 | // * ART |
| 224 | // * Conscrypt |
| 225 | // * I18N |
| 226 | // |
| 227 | // The bootclasspath_fragments for each of the above modules must specify at least one stubs_lib |
| 228 | // and must specify stubs for all the APIs provided by its contents. Failure to do so will lead to |
| 229 | // those methods being inaccessible to the other modules in the list. |
| 230 | Core_platform_api BootclasspathNestedAPIProperties |
| 231 | } |
| 232 | |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 233 | // apiScopeToStubLibs calculates the stub library modules for each relevant *HiddenAPIScope from the |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 234 | // Stub_libs properties. |
Cole Faust | aaff782 | 2024-07-30 12:59:44 -0700 | [diff] [blame] | 235 | func (p BootclasspathAPIProperties) apiScopeToStubLibs(ctx android.BaseModuleContext) map[*HiddenAPIScope][]string { |
Paul Duffin | 31fad80 | 2021-06-18 18:14:25 +0100 | [diff] [blame] | 236 | m := map[*HiddenAPIScope][]string{} |
| 237 | for _, apiScope := range hiddenAPISdkLibrarySupportedScopes { |
Cole Faust | aaff782 | 2024-07-30 12:59:44 -0700 | [diff] [blame] | 238 | m[apiScope] = p.Api.Stub_libs.GetOrDefault(ctx, nil) |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 239 | } |
Cole Faust | aaff782 | 2024-07-30 12:59:44 -0700 | [diff] [blame] | 240 | m[CorePlatformHiddenAPIScope] = p.Core_platform_api.Stub_libs.GetOrDefault(ctx, nil) |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 241 | return m |
| 242 | } |