Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 1 | // Copyright 2018 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 android |
| 16 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 17 | import ( |
Jooyung Han | 03b5185 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 18 | "fmt" |
Colin Cross | 9132ced | 2024-04-15 15:32:38 -0700 | [diff] [blame] | 19 | "slices" |
Jooyung Han | 03b5185 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 20 | "strconv" |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 21 | "strings" |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 22 | "sync" |
Paul Duffin | dddd546 | 2020-04-07 15:25:44 +0100 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint" |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 25 | ) |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 26 | |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 27 | var ( |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 28 | // This is the sdk version when APEX was first introduced |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 29 | SdkVersion_Android10 = uncheckedFinalApiLevel(29) |
Jooyung Han | 5417f77 | 2020-03-12 18:37:20 +0900 | [diff] [blame] | 30 | ) |
| 31 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 32 | // ApexInfo describes the metadata about one or more apexBundles that an apex variant of a module is |
| 33 | // part of. When an apex variant is created, the variant is associated with one apexBundle. But |
| 34 | // when multiple apex variants are merged for deduping (see mergeApexVariations), this holds the |
| 35 | // information about the apexBundles that are merged together. |
| 36 | // Accessible via `ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)` |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 37 | type ApexInfo struct { |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 38 | // Name of the apex variation that this module (i.e. the apex variant of the module) is |
Spandan Das | 50801e2 | 2024-05-13 18:29:45 +0000 | [diff] [blame] | 39 | // mutated into, or "" for a platform (i.e. non-APEX) variant. |
Jiyong Park | ab50b07 | 2021-05-12 17:13:56 +0900 | [diff] [blame] | 40 | // |
| 41 | // Also note that a module can be included in multiple APEXes, in which case, the module is |
| 42 | // mutated into one or more variants, each of which is for an APEX. The variants then can |
| 43 | // later be deduped if they don't need to be compiled differently. This is an optimization |
| 44 | // done in mergeApexVariations. |
Colin Cross | e07f231 | 2020-08-13 11:24:56 -0700 | [diff] [blame] | 45 | ApexVariationName string |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 46 | |
Jiyong Park | 4eab21d | 2021-04-15 15:17:54 +0900 | [diff] [blame] | 47 | // ApiLevel that this module has to support at minimum. |
| 48 | MinSdkVersion ApiLevel |
Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 49 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 50 | // True if this module comes from an updatable apexBundle. |
| 51 | Updatable bool |
| 52 | |
Jiyong Park | 9477c26 | 2021-06-22 20:23:05 +0900 | [diff] [blame] | 53 | // True if this module can use private platform APIs. Only non-updatable APEX can set this |
| 54 | // to true. |
| 55 | UsePlatformApis bool |
| 56 | |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 57 | // True if this is for a prebuilt_apex. |
| 58 | // |
| 59 | // If true then this will customize the apex processing to make it suitable for handling |
| 60 | // prebuilt_apex, e.g. it will prevent ApexInfos from being merged together. |
| 61 | // |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 62 | // Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants |
| 63 | // across prebuilt_apex modules. That is because there is no way to determine whether two |
| 64 | // prebuilt_apex modules that export files for the same module are compatible. e.g. they could have |
| 65 | // been built from different source at different times or they could have been built with different |
| 66 | // build options that affect the libraries. |
| 67 | // |
| 68 | // While it may be possible to provide sufficient information to determine whether two prebuilt_apex |
| 69 | // modules were compatible it would be a lot of work and would not provide much benefit for a couple |
| 70 | // of reasons: |
| 71 | // - The number of prebuilt_apex modules that will be exporting files for the same module will be |
| 72 | // low as the prebuilt_apex only exports files for the direct dependencies that require it and |
| 73 | // very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a |
| 74 | // few com.android.art* apex files that contain the same contents and could export files for the |
| 75 | // same modules but only one of them needs to do so. Contrast that with source apex modules which |
| 76 | // need apex specific variants for every module that contributes code to the apex, whether direct |
| 77 | // or indirect. |
| 78 | // - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some |
| 79 | // extra copying of files. Contrast that with source apex modules that has to build each variant |
| 80 | // from source. |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 81 | ForPrebuiltApex bool |
Spandan Das | e8173a8 | 2023-04-12 17:14:11 +0000 | [diff] [blame] | 82 | |
Spandan Das | 33bbeb2 | 2024-06-18 23:28:25 +0000 | [diff] [blame] | 83 | // Returns the name of the overridden apex (com.android.foo) |
| 84 | BaseApexName string |
Spandan Das | 003452f | 2024-09-06 00:56:25 +0000 | [diff] [blame] | 85 | |
| 86 | // Returns the value of `apex_available_name` |
| 87 | ApexAvailableName string |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 88 | } |
Yu Liu | b73c3a6 | 2024-12-10 00:58:06 +0000 | [diff] [blame] | 89 | |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 90 | func (a ApexInfo) Variation() string { |
| 91 | return a.ApexVariationName |
| 92 | } |
| 93 | |
| 94 | // Minimize is called during a transition from a module with a unique variation per apex to a module that should |
| 95 | // share variations between apexes. It returns a minimized ApexInfo that removes any apex names and replaces |
| 96 | // the variation name with one computed from the remaining properties. |
| 97 | func (a ApexInfo) Minimize() ApexInfo { |
| 98 | info := ApexInfo{ |
| 99 | MinSdkVersion: a.MinSdkVersion, |
| 100 | UsePlatformApis: a.UsePlatformApis, |
| 101 | } |
| 102 | info.ApexVariationName = info.mergedName() |
| 103 | return info |
| 104 | } |
| 105 | |
| 106 | type ApexAvailableInfo struct { |
Yu Liu | b73c3a6 | 2024-12-10 00:58:06 +0000 | [diff] [blame] | 107 | // Returns the apex names that this module is available for |
| 108 | ApexAvailableFor []string |
Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Colin Cross | 7c03506 | 2024-03-28 12:18:42 -0700 | [diff] [blame] | 111 | var ApexInfoProvider = blueprint.NewMutatorProvider[ApexInfo]("apex_mutate") |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 112 | var ApexAvailableInfoProvider = blueprint.NewMutatorProvider[ApexAvailableInfo]("apex_mutate") |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 113 | |
Lukacs T. Berki | d18d8ca | 2021-06-25 09:11:22 +0200 | [diff] [blame] | 114 | func (i ApexInfo) AddJSONData(d *map[string]interface{}) { |
| 115 | (*d)["Apex"] = map[string]interface{}{ |
| 116 | "ApexVariationName": i.ApexVariationName, |
| 117 | "MinSdkVersion": i.MinSdkVersion, |
Lukacs T. Berki | d18d8ca | 2021-06-25 09:11:22 +0200 | [diff] [blame] | 118 | "ForPrebuiltApex": i.ForPrebuiltApex, |
| 119 | } |
| 120 | } |
| 121 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 122 | // mergedName gives the name of the alias variation that will be used when multiple apex variations |
| 123 | // of a module can be deduped into one variation. For example, if libfoo is included in both apex.a |
| 124 | // and apex.b, and if the two APEXes have the same min_sdk_version (say 29), then libfoo doesn't |
| 125 | // have to be built twice, but only once. In that case, the two apex variations apex.a and apex.b |
Jiyong Park | 9477c26 | 2021-06-22 20:23:05 +0900 | [diff] [blame] | 126 | // are configured to have the same alias variation named apex29. Whether platform APIs is allowed |
| 127 | // or not also matters; if two APEXes don't have the same allowance, they get different names and |
| 128 | // thus wouldn't be merged. |
Colin Cross | 9132ced | 2024-04-15 15:32:38 -0700 | [diff] [blame] | 129 | func (i ApexInfo) mergedName() string { |
Jiyong Park | 4eab21d | 2021-04-15 15:17:54 +0900 | [diff] [blame] | 130 | name := "apex" + strconv.Itoa(i.MinSdkVersion.FinalOrFutureInt()) |
Colin Cross | 1115b4b | 2025-01-21 15:38:58 -0800 | [diff] [blame] | 131 | if i.UsePlatformApis { |
| 132 | name += "_p" |
| 133 | } |
Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 134 | return name |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 137 | // IsForPlatform tells whether this module is for the platform or not. If false is returned, it |
| 138 | // means that this apex variant of the module is built for an APEX. |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 139 | func (i ApexInfo) IsForPlatform() bool { |
| 140 | return i.ApexVariationName == "" |
| 141 | } |
| 142 | |
Jihoon Kang | 85bc193 | 2024-07-01 17:04:46 +0000 | [diff] [blame] | 143 | // To satisfy the comparable interface |
| 144 | func (i ApexInfo) Equal(other any) bool { |
| 145 | otherApexInfo, ok := other.(ApexInfo) |
| 146 | return ok && i.ApexVariationName == otherApexInfo.ApexVariationName && |
| 147 | i.MinSdkVersion == otherApexInfo.MinSdkVersion && |
| 148 | i.Updatable == otherApexInfo.Updatable && |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 149 | i.UsePlatformApis == otherApexInfo.UsePlatformApis |
Jihoon Kang | 85bc193 | 2024-07-01 17:04:46 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Spandan Das | f5e03f1 | 2024-01-25 19:25:42 +0000 | [diff] [blame] | 152 | // ApexBundleInfo contains information about the dependencies of an apex |
| 153 | type ApexBundleInfo struct { |
Spandan Das | f5e03f1 | 2024-01-25 19:25:42 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 156 | var ApexBundleInfoProvider = blueprint.NewMutatorProvider[ApexBundleInfo]("apex_mutate") |
Spandan Das | f5e03f1 | 2024-01-25 19:25:42 +0000 | [diff] [blame] | 157 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 158 | // DepIsInSameApex defines an interface that should be used to determine whether a given dependency |
| 159 | // should be considered as part of the same APEX as the current module or not. Note: this was |
| 160 | // extracted from ApexModule to make it easier to define custom subsets of the ApexModule interface |
| 161 | // and improve code navigation within the IDE. |
Paul Duffin | 923e8a5 | 2020-03-30 15:33:32 +0100 | [diff] [blame] | 162 | type DepIsInSameApex interface { |
Colin Cross | f7bbd2f | 2024-12-05 13:57:10 -0800 | [diff] [blame] | 163 | // OutgoingDepIsInSameApex tests if the module depended on via 'tag' is considered as part of |
| 164 | // the same APEX as this module. For example, a static lib dependency usually returns true here, while a |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 165 | // shared lib dependency to a stub library returns false. |
Paul Duffin | 4c3e8e2 | 2021-03-18 15:41:29 +0000 | [diff] [blame] | 166 | // |
| 167 | // This method must not be called directly without first ignoring dependencies whose tags |
| 168 | // implement ExcludeFromApexContentsTag. Calls from within the func passed to WalkPayloadDeps() |
| 169 | // are fine as WalkPayloadDeps() will ignore those dependencies automatically. Otherwise, use |
| 170 | // IsDepInSameApex instead. |
Colin Cross | f7bbd2f | 2024-12-05 13:57:10 -0800 | [diff] [blame] | 171 | OutgoingDepIsInSameApex(tag blueprint.DependencyTag) bool |
| 172 | |
| 173 | // IncomingDepIsInSameApex tests if this module depended on via 'tag' is considered as part of |
| 174 | // the same APEX as the depending module module. For example, a static lib dependency usually |
| 175 | // returns true here, while a shared lib dependency to a stub library returns false. |
| 176 | // |
| 177 | // This method must not be called directly without first ignoring dependencies whose tags |
| 178 | // implement ExcludeFromApexContentsTag. Calls from within the func passed to WalkPayloadDeps() |
| 179 | // are fine as WalkPayloadDeps() will ignore those dependencies automatically. Otherwise, use |
| 180 | // IsDepInSameApex instead. |
| 181 | IncomingDepIsInSameApex(tag blueprint.DependencyTag) bool |
Paul Duffin | 923e8a5 | 2020-03-30 15:33:32 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Paul Duffin | 4c3e8e2 | 2021-03-18 15:41:29 +0000 | [diff] [blame] | 184 | func IsDepInSameApex(ctx BaseModuleContext, module, dep Module) bool { |
| 185 | depTag := ctx.OtherModuleDependencyTag(dep) |
| 186 | if _, ok := depTag.(ExcludeFromApexContentsTag); ok { |
| 187 | // The tag defines a dependency that never requires the child module to be part of the same |
| 188 | // apex as the parent. |
| 189 | return false |
| 190 | } |
Colin Cross | f7bbd2f | 2024-12-05 13:57:10 -0800 | [diff] [blame] | 191 | |
| 192 | if m, ok := module.(DepIsInSameApex); ok && !m.OutgoingDepIsInSameApex(depTag) { |
| 193 | return false |
| 194 | } |
| 195 | if d, ok := dep.(DepIsInSameApex); ok && !d.IncomingDepIsInSameApex(depTag) { |
| 196 | return false |
| 197 | } |
| 198 | return true |
Paul Duffin | 4c3e8e2 | 2021-03-18 15:41:29 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 201 | // ApexModule is the interface that a module type is expected to implement if the module has to be |
| 202 | // built differently depending on whether the module is destined for an APEX or not (i.e., installed |
| 203 | // to one of the regular partitions). |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 204 | // |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 205 | // Native shared libraries are one such module type; when it is built for an APEX, it should depend |
| 206 | // only on stable interfaces such as NDK, stable AIDL, or C APIs from other APEXes. |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 207 | // |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 208 | // A module implementing this interface will be mutated into multiple variations by apex.apexMutator |
| 209 | // if it is directly or indirectly included in one or more APEXes. Specifically, if a module is |
| 210 | // included in apex.foo and apex.bar then three apex variants are created: platform, apex.foo and |
| 211 | // apex.bar. The platform variant is for the regular partitions (e.g., /system or /vendor, etc.) |
| 212 | // while the other two are for the APEXs, respectively. The latter two variations can be merged (see |
| 213 | // mergedName) when the two APEXes have the same min_sdk_version requirement. |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 214 | type ApexModule interface { |
| 215 | Module |
Paul Duffin | 923e8a5 | 2020-03-30 15:33:32 +0100 | [diff] [blame] | 216 | DepIsInSameApex |
| 217 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 218 | apexModuleBase() *ApexModuleBase |
| 219 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 220 | // Marks that this module should be built for the specified APEX. Call this BEFORE |
| 221 | // apex.apexMutator is run. |
Jooyung Han | 698dd9f | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 222 | BuildForApex(apex ApexInfo) |
Jiyong Park | f760cae | 2020-02-12 07:53:12 +0900 | [diff] [blame] | 223 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 224 | // Returns true if this module is present in any APEX either directly or indirectly. Call |
| 225 | // this after apex.apexMutator is run. |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 226 | InAnyApex() bool |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 227 | |
Colin Cross | 0c19b69 | 2024-11-06 13:50:21 -0800 | [diff] [blame] | 228 | // NotInPlatform returns true if the module is not available to the platform due to |
| 229 | // apex_available being set and not containing "//apex_available:platform". |
Jiyong Park | f7c3bbe | 2020-12-09 21:18:56 +0900 | [diff] [blame] | 230 | NotInPlatform() bool |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 231 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 232 | // Tests if this module could have APEX variants. Even when a module type implements |
| 233 | // ApexModule interface, APEX variants are created only for the module instances that return |
| 234 | // true here. This is useful for not creating APEX variants for certain types of shared |
| 235 | // libraries such as NDK stubs. |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 236 | CanHaveApexVariants() bool |
| 237 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 238 | // Tests if this module can be installed to APEX as a file. For example, this would return |
| 239 | // true for shared libs while return false for static libs because static libs are not |
| 240 | // installable module (but it can still be mutated for APEX) |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 241 | IsInstallableToApex() bool |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 242 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 243 | // Tests if this module is available for the specified APEX or ":platform". This is from the |
| 244 | // apex_available property of the module. |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 245 | AvailableFor(what string) bool |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 246 | |
Yu Liu | b73c3a6 | 2024-12-10 00:58:06 +0000 | [diff] [blame] | 247 | // Returns the apexes that are available for this module, valid values include |
| 248 | // "//apex_available:platform", "//apex_available:anyapex" and specific apexes. |
| 249 | // There are some differences between this one and the ApexAvailable on |
| 250 | // ApexModuleBase for cc, java library and sdkLibraryXml. |
| 251 | ApexAvailableFor() []string |
| 252 | |
Paul Duffin | b5769c1 | 2021-05-12 16:16:51 +0100 | [diff] [blame] | 253 | // AlwaysRequiresPlatformApexVariant allows the implementing module to determine whether an |
| 254 | // APEX mutator should always be created for it. |
| 255 | // |
| 256 | // Returns false by default. |
| 257 | AlwaysRequiresPlatformApexVariant() bool |
| 258 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 259 | // Returns true if this module is not available to platform (i.e. apex_available property |
| 260 | // doesn't have "//apex_available:platform"), or shouldn't be available to platform, which |
| 261 | // is the case when this module depends on other module that isn't available to platform. |
Jiyong Park | 89e850a | 2020-04-07 16:37:39 +0900 | [diff] [blame] | 262 | NotAvailableForPlatform() bool |
| 263 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 264 | // Marks that this module is not available to platform. Set by the |
Jiyong Park | 89e850a | 2020-04-07 16:37:39 +0900 | [diff] [blame] | 265 | // check-platform-availability mutator in the apex package. |
| 266 | SetNotAvailableForPlatform() |
| 267 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 268 | // Returns nil (success) if this module should support the given sdk version. Returns an |
| 269 | // error if not. No default implementation is provided for this method. A module type |
| 270 | // implementing this interface should provide an implementation. A module supports an sdk |
| 271 | // version when the module's min_sdk_version is equal to or less than the given sdk version. |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 272 | ShouldSupportSdkVersion(ctx BaseModuleContext, sdkVersion ApiLevel) error |
Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 273 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 274 | // Returns true if this module needs a unique variation per apex, effectively disabling the |
| 275 | // deduping. This is turned on when, for example if use_apex_name_macro is set so that each |
| 276 | // apex variant should be built with different macro definitions. |
Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 277 | UniqueApexVariations() bool |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 278 | } |
| 279 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 280 | // Properties that are common to all module types implementing ApexModule interface. |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 281 | type ApexProperties struct { |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 282 | // Availability of this module in APEXes. Only the listed APEXes can contain this module. If |
| 283 | // the module has stubs then other APEXes and the platform may access it through them |
| 284 | // (subject to visibility). |
Martin Stjernholm | 06ca82d | 2020-01-17 13:02:56 +0000 | [diff] [blame] | 285 | // |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 286 | // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX. |
| 287 | // "//apex_available:platform" refers to non-APEX partitions like "system.img". |
Jooyung Han | 9a419e2 | 2024-08-16 17:14:21 +0900 | [diff] [blame] | 288 | // Prefix pattern (com.foo.*) can be used to match with any APEX name with the prefix(com.foo.). |
Jiyong Park | 9a1e14e | 2020-02-13 02:30:45 +0900 | [diff] [blame] | 289 | // Default is ["//apex_available:platform"]. |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 290 | Apex_available []string |
| 291 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 292 | // See ApexModule.NotAvailableForPlatform() |
Jiyong Park | 89e850a | 2020-04-07 16:37:39 +0900 | [diff] [blame] | 293 | NotAvailableForPlatform bool `blueprint:"mutated"` |
Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 294 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 295 | // See ApexModule.UniqueApexVariants() |
Colin Cross | aede88c | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 296 | UniqueApexVariationsForDeps bool `blueprint:"mutated"` |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 297 | } |
| 298 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 299 | // Marker interface that identifies dependencies that are excluded from APEX contents. |
Paul Duffin | 573989d | 2021-03-17 13:25:29 +0000 | [diff] [blame] | 300 | // |
Paul Duffin | 4c3e8e2 | 2021-03-18 15:41:29 +0000 | [diff] [blame] | 301 | // At the moment the sdk.sdkRequirementsMutator relies on the fact that the existing tags which |
| 302 | // implement this interface do not define dependencies onto members of an sdk_snapshot. If that |
| 303 | // changes then sdk.sdkRequirementsMutator will need fixing. |
Paul Duffin | dddd546 | 2020-04-07 15:25:44 +0100 | [diff] [blame] | 304 | type ExcludeFromApexContentsTag interface { |
| 305 | blueprint.DependencyTag |
| 306 | |
| 307 | // Method that differentiates this interface from others. |
| 308 | ExcludeFromApexContents() |
| 309 | } |
| 310 | |
Cindy Zhou | 18417cb | 2020-12-10 07:12:38 -0800 | [diff] [blame] | 311 | // Interface that identifies dependencies to skip Apex dependency check |
| 312 | type SkipApexAllowedDependenciesCheck interface { |
| 313 | // Returns true to skip the Apex dependency check, which limits the allowed dependency in build. |
| 314 | SkipApexAllowedDependenciesCheck() bool |
| 315 | } |
| 316 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 317 | // ApexModuleBase provides the default implementation for the ApexModule interface. APEX-aware |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 318 | // modules are expected to include this struct and call InitApexModule(). |
| 319 | type ApexModuleBase struct { |
Jihoon Kang | 46d66de | 2024-05-22 22:42:39 +0000 | [diff] [blame] | 320 | ApexProperties ApexProperties |
| 321 | apexPropertiesLock sync.Mutex // protects ApexProperties during parallel apexDirectlyInAnyMutator |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 322 | |
| 323 | canHaveApexVariants bool |
Colin Cross | cefa94bd | 2019-06-03 15:07:03 -0700 | [diff] [blame] | 324 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 325 | apexInfos []ApexInfo |
Paul Duffin | 949abc0 | 2020-12-08 10:34:30 +0000 | [diff] [blame] | 326 | apexInfosLock sync.Mutex // protects apexInfos during parallel apexInfoMutator |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 327 | } |
| 328 | |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 329 | func (m *ApexModuleBase) ApexTransitionMutatorSplit(ctx BaseModuleContext) []ApexInfo { |
| 330 | return []ApexInfo{{}} |
| 331 | } |
| 332 | |
| 333 | func (m *ApexModuleBase) ApexTransitionMutatorOutgoing(ctx OutgoingTransitionContext, info ApexInfo) ApexInfo { |
| 334 | if !ctx.Module().(DepIsInSameApex).OutgoingDepIsInSameApex(ctx.DepTag()) { |
| 335 | return ApexInfo{} |
| 336 | } |
| 337 | return info |
| 338 | } |
| 339 | |
| 340 | func (m *ApexModuleBase) ApexTransitionMutatorIncoming(ctx IncomingTransitionContext, info ApexInfo) ApexInfo { |
| 341 | module := ctx.Module().(ApexModule) |
| 342 | if !module.CanHaveApexVariants() { |
| 343 | return ApexInfo{} |
| 344 | } |
| 345 | |
| 346 | if !ctx.Module().(DepIsInSameApex).IncomingDepIsInSameApex(ctx.DepTag()) { |
| 347 | return ApexInfo{} |
| 348 | } |
| 349 | |
| 350 | if info.ApexVariationName == "" { |
| 351 | return ApexInfo{} |
| 352 | } |
| 353 | |
| 354 | if !ctx.Module().(ApexModule).UniqueApexVariations() && !m.ApexProperties.UniqueApexVariationsForDeps && !info.ForPrebuiltApex { |
| 355 | return info.Minimize() |
| 356 | } |
| 357 | return info |
| 358 | } |
| 359 | |
| 360 | func (m *ApexModuleBase) ApexTransitionMutatorMutate(ctx BottomUpMutatorContext, info ApexInfo) { |
| 361 | SetProvider(ctx, ApexInfoProvider, info) |
| 362 | |
| 363 | module := ctx.Module().(ApexModule) |
| 364 | base := module.apexModuleBase() |
| 365 | |
| 366 | platformVariation := info.ApexVariationName == "" |
| 367 | if !platformVariation { |
| 368 | // Do some validity checks. |
| 369 | // TODO(jiyong): is this the right place? |
| 370 | base.checkApexAvailableProperty(ctx) |
| 371 | |
| 372 | SetProvider(ctx, ApexAvailableInfoProvider, ApexAvailableInfo{ |
| 373 | ApexAvailableFor: module.ApexAvailableFor(), |
| 374 | }) |
| 375 | } |
| 376 | if platformVariation && !ctx.Host() && !module.AvailableFor(AvailableToPlatform) && module.NotAvailableForPlatform() { |
| 377 | // Do not install the module for platform, but still allow it to output |
| 378 | // uninstallable AndroidMk entries in certain cases when they have side |
| 379 | // effects. TODO(jiyong): move this routine to somewhere else |
| 380 | module.MakeUninstallable() |
| 381 | } |
| 382 | } |
| 383 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 384 | // Initializes ApexModuleBase struct. Not calling this (even when inheriting from ApexModuleBase) |
| 385 | // prevents the module from being mutated for apexBundle. |
| 386 | func InitApexModule(m ApexModule) { |
| 387 | base := m.apexModuleBase() |
| 388 | base.canHaveApexVariants = true |
| 389 | |
| 390 | m.AddProperties(&base.ApexProperties) |
| 391 | } |
| 392 | |
| 393 | // Implements ApexModule |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 394 | func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase { |
| 395 | return m |
| 396 | } |
| 397 | |
Spandan Das | 8e6386e | 2023-04-11 19:08:18 +0000 | [diff] [blame] | 398 | var ( |
| 399 | availableToPlatformList = []string{AvailableToPlatform} |
| 400 | ) |
| 401 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 402 | // Implements ApexModule |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 403 | func (m *ApexModuleBase) ApexAvailable() []string { |
Spandan Das | 8e6386e | 2023-04-11 19:08:18 +0000 | [diff] [blame] | 404 | aa := m.ApexProperties.Apex_available |
| 405 | if len(aa) > 0 { |
| 406 | return aa |
| 407 | } |
| 408 | // Default is availability to platform |
| 409 | return CopyOf(availableToPlatformList) |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Yu Liu | b73c3a6 | 2024-12-10 00:58:06 +0000 | [diff] [blame] | 412 | func (m *ApexModuleBase) ApexAvailableFor() []string { |
| 413 | return m.ApexAvailable() |
| 414 | } |
| 415 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 416 | // Implements ApexModule |
Jooyung Han | 698dd9f | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 417 | func (m *ApexModuleBase) BuildForApex(apex ApexInfo) { |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 418 | m.apexInfosLock.Lock() |
| 419 | defer m.apexInfosLock.Unlock() |
Colin Cross | f697817 | 2024-11-19 11:29:46 -0800 | [diff] [blame] | 420 | if slices.ContainsFunc(m.apexInfos, func(existing ApexInfo) bool { |
| 421 | return existing.ApexVariationName == apex.ApexVariationName |
| 422 | }) { |
| 423 | return |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 424 | } |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 425 | m.apexInfos = append(m.apexInfos, apex) |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 426 | } |
| 427 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 428 | // Implements ApexModule |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 429 | func (m *ApexModuleBase) InAnyApex() bool { |
Colin Cross | 2dcbca6 | 2024-11-20 14:55:14 -0800 | [diff] [blame] | 430 | for _, apex_name := range m.ApexProperties.Apex_available { |
| 431 | if apex_name != AvailableToPlatform { |
| 432 | return true |
| 433 | } |
| 434 | } |
| 435 | return false |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 436 | } |
| 437 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 438 | // Implements ApexModule |
Jiyong Park | f7c3bbe | 2020-12-09 21:18:56 +0900 | [diff] [blame] | 439 | func (m *ApexModuleBase) NotInPlatform() bool { |
Colin Cross | 0c19b69 | 2024-11-06 13:50:21 -0800 | [diff] [blame] | 440 | return !m.AvailableFor(AvailableToPlatform) |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // Implements ApexModule |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 444 | func (m *ApexModuleBase) CanHaveApexVariants() bool { |
| 445 | return m.canHaveApexVariants |
| 446 | } |
| 447 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 448 | // Implements ApexModule |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 449 | func (m *ApexModuleBase) IsInstallableToApex() bool { |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 450 | // If needed, this will bel overridden by concrete types inheriting |
| 451 | // ApexModuleBase |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 452 | return false |
| 453 | } |
| 454 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 455 | // Implements ApexModule |
| 456 | func (m *ApexModuleBase) UniqueApexVariations() bool { |
| 457 | // If needed, this will bel overridden by concrete types inheriting |
| 458 | // ApexModuleBase |
| 459 | return false |
| 460 | } |
| 461 | |
| 462 | // Implements ApexModule |
Colin Cross | f7bbd2f | 2024-12-05 13:57:10 -0800 | [diff] [blame] | 463 | func (m *ApexModuleBase) OutgoingDepIsInSameApex(tag blueprint.DependencyTag) bool { |
| 464 | // By default, if there is a dependency from A to B, we try to include both in the same |
| 465 | // APEX, unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning |
| 466 | // true. This is overridden by some module types like apex.ApexBundle, cc.Module, |
| 467 | // java.Module, etc. |
| 468 | return true |
| 469 | } |
| 470 | |
| 471 | func (m *ApexModuleBase) IncomingDepIsInSameApex(tag blueprint.DependencyTag) bool { |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 472 | // By default, if there is a dependency from A to B, we try to include both in the same |
| 473 | // APEX, unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning |
| 474 | // true. This is overridden by some module types like apex.ApexBundle, cc.Module, |
| 475 | // java.Module, etc. |
| 476 | return true |
| 477 | } |
| 478 | |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 479 | const ( |
Jiyong Park | b02bb40 | 2019-12-03 00:43:57 +0900 | [diff] [blame] | 480 | AvailableToPlatform = "//apex_available:platform" |
Paul Duffin | 7d74e7b | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 481 | AvailableToAnyApex = "//apex_available:anyapex" |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 482 | ) |
| 483 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 484 | // CheckAvailableForApex provides the default algorithm for checking the apex availability. When the |
| 485 | // availability is empty, it defaults to ["//apex_available:platform"] which means "available to the |
| 486 | // platform but not available to any APEX". When the list is not empty, `what` is matched against |
| 487 | // the list. If there is any matching element in the list, thus function returns true. The special |
| 488 | // availability "//apex_available:anyapex" matches with anything except for |
| 489 | // "//apex_available:platform". |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 490 | func CheckAvailableForApex(what string, apex_available []string) bool { |
| 491 | if len(apex_available) == 0 { |
Anton Hansson | eec79eb | 2020-01-10 15:12:39 +0000 | [diff] [blame] | 492 | return what == AvailableToPlatform |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 493 | } |
Jooyung Han | 9a419e2 | 2024-08-16 17:14:21 +0900 | [diff] [blame] | 494 | |
| 495 | // TODO b/248601389 |
| 496 | if what == "com.google.mainline.primary.libs" || what == "com.google.mainline.go.primary.libs" { |
| 497 | return true |
| 498 | } |
| 499 | |
| 500 | for _, apex_name := range apex_available { |
| 501 | // exact match. |
| 502 | if apex_name == what { |
| 503 | return true |
| 504 | } |
| 505 | // //apex_available:anyapex matches with any apex name, but not //apex_available:platform |
| 506 | if apex_name == AvailableToAnyApex && what != AvailableToPlatform { |
| 507 | return true |
| 508 | } |
| 509 | // prefix match. |
| 510 | if strings.HasSuffix(apex_name, ".*") && strings.HasPrefix(what, strings.TrimSuffix(apex_name, "*")) { |
| 511 | return true |
| 512 | } |
William Escande | a345bba | 2024-12-12 14:00:01 -0800 | [diff] [blame] | 513 | // TODO b/383863941: Remove once legacy name is no longer used |
| 514 | if (apex_name == "com.android.btservices" && what == "com.android.bt") || (apex_name == "com.android.bt" && what == "com.android.btservices") { |
| 515 | return true |
| 516 | } |
Jooyung Han | 9a419e2 | 2024-08-16 17:14:21 +0900 | [diff] [blame] | 517 | } |
| 518 | return false |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 519 | } |
| 520 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 521 | // Implements ApexModule |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 522 | func (m *ApexModuleBase) AvailableFor(what string) bool { |
Yu Liu | b73c3a6 | 2024-12-10 00:58:06 +0000 | [diff] [blame] | 523 | return CheckAvailableForApex(what, m.ApexAvailableFor()) |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 524 | } |
| 525 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 526 | // Implements ApexModule |
Paul Duffin | b5769c1 | 2021-05-12 16:16:51 +0100 | [diff] [blame] | 527 | func (m *ApexModuleBase) AlwaysRequiresPlatformApexVariant() bool { |
| 528 | return false |
| 529 | } |
| 530 | |
| 531 | // Implements ApexModule |
Jiyong Park | 89e850a | 2020-04-07 16:37:39 +0900 | [diff] [blame] | 532 | func (m *ApexModuleBase) NotAvailableForPlatform() bool { |
| 533 | return m.ApexProperties.NotAvailableForPlatform |
| 534 | } |
| 535 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 536 | // Implements ApexModule |
Jiyong Park | 89e850a | 2020-04-07 16:37:39 +0900 | [diff] [blame] | 537 | func (m *ApexModuleBase) SetNotAvailableForPlatform() { |
| 538 | m.ApexProperties.NotAvailableForPlatform = true |
| 539 | } |
| 540 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 541 | // This function makes sure that the apex_available property is valid |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 542 | func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) { |
| 543 | for _, n := range m.ApexProperties.Apex_available { |
Jooyung Han | 40b7917 | 2024-08-16 16:00:33 +0900 | [diff] [blame] | 544 | if n == AvailableToPlatform || n == AvailableToAnyApex { |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 545 | continue |
| 546 | } |
Jooyung Han | 9a419e2 | 2024-08-16 17:14:21 +0900 | [diff] [blame] | 547 | // Prefix pattern should end with .* and has at least two components. |
| 548 | if strings.Contains(n, "*") { |
| 549 | if !strings.HasSuffix(n, ".*") { |
| 550 | mctx.PropertyErrorf("apex_available", "Wildcard should end with .* like com.foo.*") |
| 551 | } |
| 552 | if strings.Count(n, ".") < 2 { |
| 553 | mctx.PropertyErrorf("apex_available", "Wildcard requires two or more components like com.foo.*") |
| 554 | } |
| 555 | if strings.Count(n, "*") != 1 { |
| 556 | mctx.PropertyErrorf("apex_available", "Wildcard is not allowed in the middle.") |
| 557 | } |
| 558 | continue |
| 559 | } |
Orion Hodson | 4b5438a | 2019-10-08 10:40:51 +0100 | [diff] [blame] | 560 | if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() { |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 561 | mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n) |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
Martin Stjernholm | 4e6c269 | 2021-03-25 01:25:06 +0000 | [diff] [blame] | 566 | // AvailableToSameApexes returns true if the two modules are apex_available to |
| 567 | // exactly the same set of APEXes (and platform), i.e. if their apex_available |
| 568 | // properties have the same elements. |
| 569 | func AvailableToSameApexes(mod1, mod2 ApexModule) bool { |
Spandan Das | 8a8714c | 2023-04-25 18:03:54 +0000 | [diff] [blame] | 570 | mod1ApexAvail := SortedUniqueStrings(mod1.apexModuleBase().ApexProperties.Apex_available) |
| 571 | mod2ApexAvail := SortedUniqueStrings(mod2.apexModuleBase().ApexProperties.Apex_available) |
Martin Stjernholm | 4e6c269 | 2021-03-25 01:25:06 +0000 | [diff] [blame] | 572 | if len(mod1ApexAvail) != len(mod2ApexAvail) { |
| 573 | return false |
| 574 | } |
| 575 | for i, v := range mod1ApexAvail { |
| 576 | if v != mod2ApexAvail[i] { |
| 577 | return false |
| 578 | } |
| 579 | } |
| 580 | return true |
| 581 | } |
| 582 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 583 | // UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies that are |
| 584 | // in the same APEX have unique APEX variations so that the module can link against the right |
| 585 | // variant. |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 586 | func UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext, am ApexModule) { |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 587 | // If any of the dependencies requires unique apex variations, so does this module. |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 588 | mctx.VisitDirectDeps(func(dep Module) { |
| 589 | if depApexModule, ok := dep.(ApexModule); ok { |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 590 | if IsDepInSameApex(mctx, am, depApexModule) && |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 591 | (depApexModule.UniqueApexVariations() || |
| 592 | depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) { |
| 593 | am.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps = true |
| 594 | } |
| 595 | } |
| 596 | }) |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 597 | } |
| 598 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 599 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 600 | //Below are routines for extra safety checks. |
| 601 | // |
| 602 | // BuildDepsInfoLists is to flatten the dependency graph for an apexBundle into a text file |
| 603 | // (actually two in slightly different formats). The files are mostly for debugging, for example to |
| 604 | // see why a certain module is included in an APEX via which dependency path. |
| 605 | // |
| 606 | // CheckMinSdkVersion is to make sure that all modules in an apexBundle satisfy the min_sdk_version |
| 607 | // requirement of the apexBundle. |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 608 | |
| 609 | // A dependency info for a single ApexModule, either direct or transitive. |
| 610 | type ApexModuleDepInfo struct { |
| 611 | // Name of the dependency |
| 612 | To string |
| 613 | // List of dependencies To belongs to. Includes APEX itself, if a direct dependency. |
| 614 | From []string |
| 615 | // Whether the dependency belongs to the final compiled APEX. |
| 616 | IsExternal bool |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 617 | // min_sdk_version of the ApexModule |
| 618 | MinSdkVersion string |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | // A map of a dependency name to its ApexModuleDepInfo |
| 622 | type DepNameToDepInfoMap map[string]ApexModuleDepInfo |
| 623 | |
| 624 | type ApexBundleDepsInfo struct { |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 625 | flatListPath Path |
| 626 | fullListPath Path |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 627 | } |
| 628 | |
Artur Satayev | 849f844 | 2020-04-28 14:57:42 +0100 | [diff] [blame] | 629 | type ApexBundleDepsInfoIntf interface { |
| 630 | Updatable() bool |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 631 | FlatListPath() Path |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 632 | FullListPath() Path |
| 633 | } |
| 634 | |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 635 | func (d *ApexBundleDepsInfo) FlatListPath() Path { |
| 636 | return d.flatListPath |
| 637 | } |
| 638 | |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 639 | func (d *ApexBundleDepsInfo) FullListPath() Path { |
| 640 | return d.fullListPath |
| 641 | } |
| 642 | |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 643 | // Generate two module out files: |
| 644 | // 1. FullList with transitive deps and their parents in the dep graph |
| 645 | // 2. FlatList with a flat list of transitive deps |
Artur Satayev | 533b98c | 2021-03-11 18:03:42 +0000 | [diff] [blame] | 646 | // In both cases transitive deps of external deps are not included. Neither are deps that are only |
| 647 | // available to APEXes; they are developed with updatability in mind and don't need manual approval. |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 648 | func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) { |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 649 | var fullContent strings.Builder |
| 650 | var flatContent strings.Builder |
| 651 | |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 652 | fmt.Fprintf(&fullContent, "%s(minSdkVersion:%s):\n", ctx.ModuleName(), minSdkVersion) |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 653 | for _, key := range FirstUniqueStrings(SortedKeys(depInfos)) { |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 654 | info := depInfos[key] |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 655 | toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion) |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 656 | if info.IsExternal { |
| 657 | toName = toName + " (external)" |
| 658 | } |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 659 | fmt.Fprintf(&fullContent, " %s <- %s\n", toName, strings.Join(SortedUniqueStrings(info.From), ", ")) |
| 660 | fmt.Fprintf(&flatContent, "%s\n", toName) |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 661 | } |
| 662 | |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 663 | fullListPath := PathForModuleOut(ctx, "depsinfo", "fulllist.txt") |
| 664 | WriteFileRule(ctx, fullListPath, fullContent.String()) |
| 665 | d.fullListPath = fullListPath |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 666 | |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 667 | flatListPath := PathForModuleOut(ctx, "depsinfo", "flatlist.txt") |
| 668 | WriteFileRule(ctx, flatListPath, flatContent.String()) |
| 669 | d.flatListPath = flatListPath |
Mathew Inwood | 29bb2fd | 2021-02-18 11:36:40 +0000 | [diff] [blame] | 670 | |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 671 | ctx.Phony(fmt.Sprintf("%s-depsinfo", ctx.ModuleName()), fullListPath, flatListPath) |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 672 | } |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 673 | |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 674 | // Function called while walking an APEX's payload dependencies. |
| 675 | // |
| 676 | // Return true if the `to` module should be visited, false otherwise. |
Colin Cross | 9cf55a5 | 2024-12-05 14:26:03 -0800 | [diff] [blame] | 677 | type PayloadDepsCallback func(ctx BaseModuleContext, from Module, to ApexModule, externalDep bool) bool |
Colin Cross | 8bf14fc | 2024-09-25 16:41:31 -0700 | [diff] [blame] | 678 | type WalkPayloadDepsFunc func(ctx BaseModuleContext, do PayloadDepsCallback) |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 679 | |
satayev | b3fd411 | 2021-12-02 13:59:35 +0000 | [diff] [blame] | 680 | // ModuleWithMinSdkVersionCheck represents a module that implements min_sdk_version checks |
| 681 | type ModuleWithMinSdkVersionCheck interface { |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 682 | Module |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 683 | MinSdkVersion(ctx EarlyModuleContext) ApiLevel |
satayev | b3fd411 | 2021-12-02 13:59:35 +0000 | [diff] [blame] | 684 | CheckMinSdkVersion(ctx ModuleContext) |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 685 | } |
| 686 | |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 687 | // CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version |
| 688 | // accordingly |
satayev | b3fd411 | 2021-12-02 13:59:35 +0000 | [diff] [blame] | 689 | func CheckMinSdkVersion(ctx ModuleContext, minSdkVersion ApiLevel, walk WalkPayloadDepsFunc) { |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 690 | // do not enforce min_sdk_version for host |
| 691 | if ctx.Host() { |
| 692 | return |
| 693 | } |
| 694 | |
| 695 | // do not enforce for coverage build |
| 696 | if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() { |
| 697 | return |
| 698 | } |
| 699 | |
Jooyung Han | ed124c3 | 2021-01-26 11:43:46 +0900 | [diff] [blame] | 700 | // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version |
| 701 | if minSdkVersion.IsNone() { |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 702 | return |
| 703 | } |
| 704 | |
Colin Cross | 9cf55a5 | 2024-12-05 14:26:03 -0800 | [diff] [blame] | 705 | walk(ctx, func(ctx BaseModuleContext, from Module, to ApexModule, externalDep bool) bool { |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 706 | if externalDep { |
Jiyong Park | e4758ed | 2020-11-18 01:34:22 +0900 | [diff] [blame] | 707 | // external deps are outside the payload boundary, which is "stable" |
| 708 | // interface. We don't have to check min_sdk_version for external |
| 709 | // dependencies. |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 710 | return false |
| 711 | } |
Colin Cross | f7bbd2f | 2024-12-05 13:57:10 -0800 | [diff] [blame] | 712 | if !IsDepInSameApex(ctx, from, to) { |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 713 | return false |
| 714 | } |
satayev | 758968a | 2021-12-06 11:42:40 +0000 | [diff] [blame] | 715 | if m, ok := to.(ModuleWithMinSdkVersionCheck); ok { |
| 716 | // This dependency performs its own min_sdk_version check, just make sure it sets min_sdk_version |
| 717 | // to trigger the check. |
| 718 | if !m.MinSdkVersion(ctx).Specified() { |
| 719 | ctx.OtherModuleErrorf(m, "must set min_sdk_version") |
| 720 | } |
| 721 | return false |
| 722 | } |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 723 | if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil { |
| 724 | toName := ctx.OtherModuleName(to) |
Jooyung Han | 2c65d2c | 2022-06-24 14:56:43 +0900 | [diff] [blame] | 725 | ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v."+ |
| 726 | "\n\nDependency path: %s\n\n"+ |
| 727 | "Consider adding 'min_sdk_version: %q' to %q", |
| 728 | minSdkVersion, ctx.ModuleName(), err.Error(), |
| 729 | ctx.GetPathString(false), |
| 730 | minSdkVersion, toName) |
| 731 | return false |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 732 | } |
| 733 | return true |
| 734 | }) |
| 735 | } |
Spandan Das | f57a966 | 2023-04-12 19:05:49 +0000 | [diff] [blame] | 736 | |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 737 | type MinSdkVersionFromValueContext interface { |
| 738 | Config() Config |
| 739 | DeviceConfig() DeviceConfig |
| 740 | ModuleErrorContext |
| 741 | } |
| 742 | |
Sam Delmerico | 0e0d96e | 2023-08-18 22:43:28 +0000 | [diff] [blame] | 743 | // Construct ApiLevel object from min_sdk_version string value |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 744 | func MinSdkVersionFromValue(ctx MinSdkVersionFromValueContext, value string) ApiLevel { |
Sam Delmerico | 0e0d96e | 2023-08-18 22:43:28 +0000 | [diff] [blame] | 745 | if value == "" { |
| 746 | return NoneApiLevel |
| 747 | } |
| 748 | apiLevel, err := ApiLevelFromUser(ctx, value) |
| 749 | if err != nil { |
| 750 | ctx.PropertyErrorf("min_sdk_version", "%s", err.Error()) |
| 751 | return NoneApiLevel |
| 752 | } |
| 753 | return apiLevel |
| 754 | } |
| 755 | |
Spandan Das | da739a3 | 2023-12-13 00:06:32 +0000 | [diff] [blame] | 756 | var ApexExportsInfoProvider = blueprint.NewProvider[ApexExportsInfo]() |
| 757 | |
| 758 | // ApexExportsInfo contains information about the artifacts provided by apexes to dexpreopt and hiddenapi |
| 759 | type ApexExportsInfo struct { |
| 760 | // Canonical name of this APEX. Used to determine the path to the activated APEX on |
| 761 | // device (/apex/<apex_name>) |
| 762 | ApexName string |
| 763 | |
| 764 | // Path to the image profile file on host (or empty, if profile is not generated). |
| 765 | ProfilePathOnHost Path |
Spandan Das | 5be6333 | 2023-12-13 00:06:32 +0000 | [diff] [blame] | 766 | |
| 767 | // Map from the apex library name (without prebuilt_ prefix) to the dex file path on host |
| 768 | LibraryNameToDexJarPathOnHost map[string]Path |
Spandan Das | da739a3 | 2023-12-13 00:06:32 +0000 | [diff] [blame] | 769 | } |
Spandan Das | 3490dfd | 2024-03-11 21:37:25 +0000 | [diff] [blame] | 770 | |
| 771 | var PrebuiltInfoProvider = blueprint.NewProvider[PrebuiltInfo]() |
| 772 | |
| 773 | // contents of prebuilt_info.json |
| 774 | type PrebuiltInfo struct { |
| 775 | // Name of the apex, without the prebuilt_ prefix |
| 776 | Name string |
| 777 | |
| 778 | Is_prebuilt bool |
| 779 | |
| 780 | // This is relative to root of the workspace. |
| 781 | // In case of mainline modules, this file contains the build_id that was used |
| 782 | // to generate the mainline module prebuilt. |
| 783 | Prebuilt_info_file_path string `json:",omitempty"` |
| 784 | } |
Colin Cross | bdd344b | 2025-01-14 16:01:03 -0800 | [diff] [blame] | 785 | |
| 786 | // FragmentInApexTag is embedded into a dependency tag to allow apex modules to annotate |
| 787 | // their fragments in a way that allows the java bootclasspath modules to traverse from |
| 788 | // the apex to the fragment. |
| 789 | type FragmentInApexTag struct{} |
| 790 | |
| 791 | func (FragmentInApexTag) isFragmentInApexTag() {} |
| 792 | |
| 793 | type isFragmentInApexTagIntf interface { |
| 794 | isFragmentInApexTag() |
| 795 | } |
| 796 | |
| 797 | // IsFragmentInApexTag returns true if the dependency tag embeds FragmentInApexTag, |
| 798 | // signifying that it is a dependency from an apex module to its fragment. |
| 799 | func IsFragmentInApexTag(tag blueprint.DependencyTag) bool { |
| 800 | _, ok := tag.(isFragmentInApexTagIntf) |
| 801 | return ok |
| 802 | } |