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 | 0c4e016 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 18 | "fmt" |
Colin Cross | cefa94bd | 2019-06-03 15:07:03 -0700 | [diff] [blame] | 19 | "sort" |
Jooyung Han | 0c4e016 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 20 | "strconv" |
Artur Satayev | 334b517 | 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 | 3766cb7 | 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 | |
Jooyung Han | 23b0adf | 2020-03-12 18:37:20 +0900 | [diff] [blame] | 27 | const ( |
| 28 | SdkVersion_Android10 = 29 |
| 29 | ) |
| 30 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 31 | type ApexInfo struct { |
Colin Cross | 7438571 | 2020-08-13 11:24:56 -0700 | [diff] [blame] | 32 | // Name of the apex variation that this module is mutated into |
| 33 | ApexVariationName string |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 34 | |
Jooyung Han | 0c4e016 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 35 | MinSdkVersion int |
Ulya Trafimovich | c0eb0b1 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 36 | Updatable bool |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 37 | RequiredSdks SdkRefs |
| 38 | |
| 39 | InApexes []string |
| 40 | } |
| 41 | |
| 42 | func (i ApexInfo) mergedName() string { |
| 43 | name := "apex" + strconv.Itoa(i.MinSdkVersion) |
| 44 | for _, sdk := range i.RequiredSdks { |
| 45 | name += "_" + sdk.Name + "_" + sdk.Version |
| 46 | } |
| 47 | return name |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Paul Duffin | 03e7d0c | 2020-03-30 15:33:32 +0100 | [diff] [blame] | 50 | // Extracted from ApexModule to make it easier to define custom subsets of the |
| 51 | // ApexModule interface and improve code navigation within the IDE. |
| 52 | type DepIsInSameApex interface { |
| 53 | // DepIsInSameApex tests if the other module 'dep' is installed to the same |
| 54 | // APEX as this module |
| 55 | DepIsInSameApex(ctx BaseModuleContext, dep Module) bool |
| 56 | } |
| 57 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 58 | // ApexModule is the interface that a module type is expected to implement if |
| 59 | // the module has to be built differently depending on whether the module |
| 60 | // is destined for an apex or not (installed to one of the regular partitions). |
| 61 | // |
| 62 | // Native shared libraries are one such module type; when it is built for an |
| 63 | // APEX, it should depend only on stable interfaces such as NDK, stable AIDL, |
| 64 | // or C APIs from other APEXs. |
| 65 | // |
| 66 | // A module implementing this interface will be mutated into multiple |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 67 | // variations by apex.apexMutator if it is directly or indirectly included |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 68 | // in one or more APEXs. Specifically, if a module is included in apex.foo and |
| 69 | // apex.bar then three apex variants are created: platform, apex.foo and |
| 70 | // apex.bar. The platform variant is for the regular partitions |
| 71 | // (e.g., /system or /vendor, etc.) while the other two are for the APEXs, |
| 72 | // respectively. |
| 73 | type ApexModule interface { |
| 74 | Module |
Paul Duffin | 03e7d0c | 2020-03-30 15:33:32 +0100 | [diff] [blame] | 75 | DepIsInSameApex |
| 76 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 77 | apexModuleBase() *ApexModuleBase |
| 78 | |
Jooyung Han | 345f0c4 | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 79 | // Marks that this module should be built for the specified APEX. |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 80 | // Call this before apex.apexMutator is run. |
Jooyung Han | 345f0c4 | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 81 | BuildForApex(apex ApexInfo) |
Jiyong Park | f760cae | 2020-02-12 07:53:12 +0900 | [diff] [blame] | 82 | |
Colin Cross | 7438571 | 2020-08-13 11:24:56 -0700 | [diff] [blame] | 83 | // Returns the name of APEX variation that this module will be built for. |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 84 | // Empty string is returned when 'IsForPlatform() == true'. Note that a |
| 85 | // module can beincluded in multiple APEXes, in which case, the module |
| 86 | // is mutated into one or more variants, each of which is for one or |
| 87 | // more APEXes. This method returns the name of the APEX variation of |
| 88 | // the module. |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 89 | // Call this after apex.apexMutator is run. |
Colin Cross | 7438571 | 2020-08-13 11:24:56 -0700 | [diff] [blame] | 90 | ApexVariationName() string |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 91 | |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 92 | // Returns the name of the APEX modules that this variant of this module |
| 93 | // is present in. |
| 94 | // Call this after apex.apexMutator is run. |
| 95 | InApexes() []string |
| 96 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 97 | // Tests whether this module will be built for the platform or not. |
Colin Cross | 7438571 | 2020-08-13 11:24:56 -0700 | [diff] [blame] | 98 | // This is a shortcut for ApexVariationName() == "" |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 99 | IsForPlatform() bool |
| 100 | |
| 101 | // Tests if this module could have APEX variants. APEX variants are |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 102 | // created only for the modules that returns true here. This is useful |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 103 | // for not creating APEX variants for certain types of shared libraries |
| 104 | // such as NDK stubs. |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 105 | CanHaveApexVariants() bool |
| 106 | |
| 107 | // Tests if this module can be installed to APEX as a file. For example, |
| 108 | // this would return true for shared libs while return false for static |
| 109 | // libs. |
| 110 | IsInstallableToApex() bool |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 111 | |
| 112 | // Mutate this module into one or more variants each of which is built |
Jooyung Han | 345f0c4 | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 113 | // for an APEX marked via BuildForApex(). |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 114 | CreateApexVariations(mctx BottomUpMutatorContext) []Module |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 115 | |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 116 | // Tests if this module is available for the specified APEX or ":platform" |
| 117 | AvailableFor(what string) bool |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 118 | |
Jiyong Park | 6a9ddc3 | 2020-04-07 16:37:39 +0900 | [diff] [blame] | 119 | // Return true if this module is not available to platform (i.e. apex_available |
| 120 | // property doesn't have "//apex_available:platform"), or shouldn't be available |
| 121 | // to platform, which is the case when this module depends on other module that |
| 122 | // isn't available to platform. |
| 123 | NotAvailableForPlatform() bool |
| 124 | |
| 125 | // Mark that this module is not available to platform. Set by the |
| 126 | // check-platform-availability mutator in the apex package. |
| 127 | SetNotAvailableForPlatform() |
| 128 | |
Jooyung Han | 7406660 | 2020-03-20 04:29:24 +0900 | [diff] [blame] | 129 | // Returns the highest version which is <= maxSdkVersion. |
| 130 | // For example, with maxSdkVersion is 10 and versionList is [9,11] |
| 131 | // it returns 9 as string |
| 132 | ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) |
Jiyong Park | f0d01b7 | 2020-04-13 16:19:48 +0900 | [diff] [blame] | 133 | |
Ulya Trafimovich | c0eb0b1 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 134 | // Tests if the module comes from an updatable APEX. |
| 135 | Updatable() bool |
| 136 | |
Jiyong Park | f0d01b7 | 2020-04-13 16:19:48 +0900 | [diff] [blame] | 137 | // List of APEXes that this module tests. The module has access to |
| 138 | // the private part of the listed APEXes even when it is not included in the |
| 139 | // APEXes. |
| 140 | TestFor() []string |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 141 | |
| 142 | // Returns true if this module needs a unique variation per apex, for example if |
| 143 | // use_apex_name_macro is set. |
| 144 | UniqueApexVariations() bool |
| 145 | |
| 146 | // UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies |
| 147 | // that are in the same APEX have unique APEX variations so that the module can link against |
| 148 | // the right variant. |
| 149 | UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext) |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | type ApexProperties struct { |
Martin Stjernholm | 06ca82d | 2020-01-17 13:02:56 +0000 | [diff] [blame] | 153 | // Availability of this module in APEXes. Only the listed APEXes can contain |
| 154 | // this module. If the module has stubs then other APEXes and the platform may |
| 155 | // access it through them (subject to visibility). |
| 156 | // |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 157 | // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX. |
| 158 | // "//apex_available:platform" refers to non-APEX partitions like "system.img". |
Jiyong Park | 9a1e14e | 2020-02-13 02:30:45 +0900 | [diff] [blame] | 159 | // Default is ["//apex_available:platform"]. |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 160 | Apex_available []string |
| 161 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 162 | Info ApexInfo `blueprint:"mutated"` |
Jiyong Park | 6a9ddc3 | 2020-04-07 16:37:39 +0900 | [diff] [blame] | 163 | |
| 164 | NotAvailableForPlatform bool `blueprint:"mutated"` |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 165 | |
| 166 | UniqueApexVariationsForDeps bool `blueprint:"mutated"` |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 167 | } |
| 168 | |
Paul Duffin | 3766cb7 | 2020-04-07 15:25:44 +0100 | [diff] [blame] | 169 | // Marker interface that identifies dependencies that are excluded from APEX |
| 170 | // contents. |
| 171 | type ExcludeFromApexContentsTag interface { |
| 172 | blueprint.DependencyTag |
| 173 | |
| 174 | // Method that differentiates this interface from others. |
| 175 | ExcludeFromApexContents() |
| 176 | } |
| 177 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 178 | // Provides default implementation for the ApexModule interface. APEX-aware |
| 179 | // modules are expected to include this struct and call InitApexModule(). |
| 180 | type ApexModuleBase struct { |
| 181 | ApexProperties ApexProperties |
| 182 | |
| 183 | canHaveApexVariants bool |
Colin Cross | cefa94bd | 2019-06-03 15:07:03 -0700 | [diff] [blame] | 184 | |
| 185 | apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 186 | apexVariations []ApexInfo |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase { |
| 190 | return m |
| 191 | } |
| 192 | |
Paul Duffin | 3a6c095 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 193 | func (m *ApexModuleBase) ApexAvailable() []string { |
| 194 | return m.ApexProperties.Apex_available |
| 195 | } |
| 196 | |
Jiyong Park | f0d01b7 | 2020-04-13 16:19:48 +0900 | [diff] [blame] | 197 | func (m *ApexModuleBase) TestFor() []string { |
| 198 | // To be implemented by concrete types inheriting ApexModuleBase |
| 199 | return nil |
| 200 | } |
| 201 | |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 202 | func (m *ApexModuleBase) UniqueApexVariations() bool { |
| 203 | return false |
| 204 | } |
| 205 | |
| 206 | func (m *ApexModuleBase) UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext) { |
| 207 | // anyInSameApex returns true if the two ApexInfo lists contain any values in an InApexes list |
| 208 | // in common. It is used instead of DepIsInSameApex because it needs to determine if the dep |
| 209 | // is in the same APEX due to being directly included, not only if it is included _because_ it |
| 210 | // is a dependency. |
| 211 | anyInSameApex := func(a, b []ApexInfo) bool { |
| 212 | collectApexes := func(infos []ApexInfo) []string { |
| 213 | var ret []string |
| 214 | for _, info := range infos { |
| 215 | ret = append(ret, info.InApexes...) |
| 216 | } |
| 217 | return ret |
| 218 | } |
| 219 | |
| 220 | aApexes := collectApexes(a) |
| 221 | bApexes := collectApexes(b) |
| 222 | sort.Strings(bApexes) |
| 223 | for _, aApex := range aApexes { |
| 224 | index := sort.SearchStrings(bApexes, aApex) |
| 225 | if index < len(bApexes) && bApexes[index] == aApex { |
| 226 | return true |
| 227 | } |
| 228 | } |
| 229 | return false |
| 230 | } |
| 231 | |
| 232 | mctx.VisitDirectDeps(func(dep Module) { |
| 233 | if depApexModule, ok := dep.(ApexModule); ok { |
| 234 | if anyInSameApex(depApexModule.apexModuleBase().apexVariations, m.apexVariations) && |
| 235 | (depApexModule.UniqueApexVariations() || |
| 236 | depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) { |
| 237 | m.ApexProperties.UniqueApexVariationsForDeps = true |
| 238 | } |
| 239 | } |
| 240 | }) |
| 241 | } |
| 242 | |
Jooyung Han | 345f0c4 | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 243 | func (m *ApexModuleBase) BuildForApex(apex ApexInfo) { |
Colin Cross | cefa94bd | 2019-06-03 15:07:03 -0700 | [diff] [blame] | 244 | m.apexVariationsLock.Lock() |
| 245 | defer m.apexVariationsLock.Unlock() |
Jooyung Han | 345f0c4 | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 246 | for _, v := range m.apexVariations { |
Colin Cross | 7438571 | 2020-08-13 11:24:56 -0700 | [diff] [blame] | 247 | if v.ApexVariationName == apex.ApexVariationName { |
Jooyung Han | 345f0c4 | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 248 | return |
Jiyong Park | f760cae | 2020-02-12 07:53:12 +0900 | [diff] [blame] | 249 | } |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 250 | } |
Jooyung Han | 345f0c4 | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 251 | m.apexVariations = append(m.apexVariations, apex) |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 252 | } |
| 253 | |
Colin Cross | 7438571 | 2020-08-13 11:24:56 -0700 | [diff] [blame] | 254 | func (m *ApexModuleBase) ApexVariationName() string { |
| 255 | return m.ApexProperties.Info.ApexVariationName |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 256 | } |
| 257 | |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 258 | func (m *ApexModuleBase) InApexes() []string { |
| 259 | return m.ApexProperties.Info.InApexes |
| 260 | } |
| 261 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 262 | func (m *ApexModuleBase) IsForPlatform() bool { |
Colin Cross | 7438571 | 2020-08-13 11:24:56 -0700 | [diff] [blame] | 263 | return m.ApexProperties.Info.ApexVariationName == "" |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | func (m *ApexModuleBase) CanHaveApexVariants() bool { |
| 267 | return m.canHaveApexVariants |
| 268 | } |
| 269 | |
| 270 | func (m *ApexModuleBase) IsInstallableToApex() bool { |
| 271 | // should be overriden if needed |
| 272 | return false |
| 273 | } |
| 274 | |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 275 | const ( |
Jiyong Park | b02bb40 | 2019-12-03 00:43:57 +0900 | [diff] [blame] | 276 | AvailableToPlatform = "//apex_available:platform" |
Paul Duffin | 404db3f | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 277 | AvailableToAnyApex = "//apex_available:anyapex" |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 278 | ) |
| 279 | |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 280 | func CheckAvailableForApex(what string, apex_available []string) bool { |
| 281 | if len(apex_available) == 0 { |
Anton Hansson | eec79eb | 2020-01-10 15:12:39 +0000 | [diff] [blame] | 282 | // apex_available defaults to ["//apex_available:platform"], |
| 283 | // which means 'available to the platform but no apexes'. |
| 284 | return what == AvailableToPlatform |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 285 | } |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 286 | return InList(what, apex_available) || |
Paul Duffin | 404db3f | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 287 | (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | func (m *ApexModuleBase) AvailableFor(what string) bool { |
| 291 | return CheckAvailableForApex(what, m.ApexProperties.Apex_available) |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 292 | } |
| 293 | |
Jiyong Park | 6a9ddc3 | 2020-04-07 16:37:39 +0900 | [diff] [blame] | 294 | func (m *ApexModuleBase) NotAvailableForPlatform() bool { |
| 295 | return m.ApexProperties.NotAvailableForPlatform |
| 296 | } |
| 297 | |
| 298 | func (m *ApexModuleBase) SetNotAvailableForPlatform() { |
| 299 | m.ApexProperties.NotAvailableForPlatform = true |
| 300 | } |
| 301 | |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 302 | func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool { |
| 303 | // By default, if there is a dependency from A to B, we try to include both in the same APEX, |
| 304 | // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true. |
| 305 | // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc. |
| 306 | return true |
| 307 | } |
| 308 | |
Jooyung Han | 7406660 | 2020-03-20 04:29:24 +0900 | [diff] [blame] | 309 | func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) { |
Jooyung Han | 0c4e016 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 310 | for i := range versionList { |
| 311 | ver, _ := strconv.Atoi(versionList[len(versionList)-i-1]) |
Jooyung Han | 7406660 | 2020-03-20 04:29:24 +0900 | [diff] [blame] | 312 | if ver <= maxSdkVersion { |
Jooyung Han | 0c4e016 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 313 | return versionList[len(versionList)-i-1], nil |
| 314 | } |
| 315 | } |
Jooyung Han | 7406660 | 2020-03-20 04:29:24 +0900 | [diff] [blame] | 316 | return "", fmt.Errorf("not found a version(<=%d) in versionList: %v", maxSdkVersion, versionList) |
Jooyung Han | 0c4e016 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 317 | } |
| 318 | |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 319 | func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) { |
| 320 | for _, n := range m.ApexProperties.Apex_available { |
Paul Duffin | 404db3f | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 321 | if n == AvailableToPlatform || n == AvailableToAnyApex { |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 322 | continue |
| 323 | } |
Orion Hodson | 4b5438a | 2019-10-08 10:40:51 +0100 | [diff] [blame] | 324 | if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() { |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 325 | mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n) |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
Ulya Trafimovich | c0eb0b1 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 330 | func (m *ApexModuleBase) Updatable() bool { |
| 331 | return m.ApexProperties.Info.Updatable |
| 332 | } |
| 333 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 334 | type byApexName []ApexInfo |
| 335 | |
| 336 | func (a byApexName) Len() int { return len(a) } |
| 337 | func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
Colin Cross | 7438571 | 2020-08-13 11:24:56 -0700 | [diff] [blame] | 338 | func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName } |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 339 | |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 340 | // mergeApexVariations deduplicates APEX variations that would build identically into a common |
| 341 | // variation. It returns the reduced list of variations and a list of aliases from the original |
| 342 | // variation names to the new variation names. |
| 343 | func mergeApexVariations(apexVariations []ApexInfo) (merged []ApexInfo, aliases [][2]string) { |
| 344 | sort.Sort(byApexName(apexVariations)) |
| 345 | seen := make(map[string]int) |
| 346 | for _, apexInfo := range apexVariations { |
| 347 | apexName := apexInfo.ApexVariationName |
| 348 | mergedName := apexInfo.mergedName() |
| 349 | if index, exists := seen[mergedName]; exists { |
| 350 | merged[index].InApexes = append(merged[index].InApexes, apexName) |
| 351 | merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable |
| 352 | } else { |
| 353 | seen[mergedName] = len(merged) |
| 354 | apexInfo.ApexVariationName = apexInfo.mergedName() |
| 355 | apexInfo.InApexes = CopyOf(apexInfo.InApexes) |
| 356 | merged = append(merged, apexInfo) |
| 357 | } |
| 358 | aliases = append(aliases, [2]string{apexName, mergedName}) |
| 359 | } |
| 360 | return merged, aliases |
| 361 | } |
| 362 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 363 | func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module { |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 364 | if len(m.apexVariations) > 0 { |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 365 | m.checkApexAvailableProperty(mctx) |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 366 | |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 367 | var apexVariations []ApexInfo |
| 368 | var aliases [][2]string |
| 369 | if !mctx.Module().(ApexModule).UniqueApexVariations() && !m.ApexProperties.UniqueApexVariationsForDeps { |
| 370 | apexVariations, aliases = mergeApexVariations(m.apexVariations) |
| 371 | } else { |
| 372 | apexVariations = m.apexVariations |
| 373 | } |
| 374 | |
| 375 | sort.Sort(byApexName(apexVariations)) |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 376 | variations := []string{} |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 377 | variations = append(variations, "") // Original variation for platform |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 378 | for _, apex := range apexVariations { |
Colin Cross | 7438571 | 2020-08-13 11:24:56 -0700 | [diff] [blame] | 379 | variations = append(variations, apex.ApexVariationName) |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 380 | } |
Logan Chien | 3aeedc9 | 2018-12-26 15:32:21 +0800 | [diff] [blame] | 381 | |
Jiyong Park | 3ff1699 | 2019-12-27 14:11:47 +0900 | [diff] [blame] | 382 | defaultVariation := "" |
| 383 | mctx.SetDefaultDependencyVariation(&defaultVariation) |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 384 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 385 | modules := mctx.CreateVariations(variations...) |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 386 | for i, mod := range modules { |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 387 | platformVariation := i == 0 |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 388 | if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) { |
| 389 | mod.SkipInstall() |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 390 | } |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 391 | if !platformVariation { |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 392 | mod.(ApexModule).apexModuleBase().ApexProperties.Info = apexVariations[i-1] |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 393 | } |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 394 | } |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 395 | |
| 396 | for _, alias := range aliases { |
| 397 | mctx.CreateAliasVariation(alias[0], alias[1]) |
| 398 | } |
| 399 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 400 | return modules |
| 401 | } |
| 402 | return nil |
| 403 | } |
| 404 | |
| 405 | var apexData OncePer |
| 406 | var apexNamesMapMutex sync.Mutex |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 407 | var apexNamesKey = NewOnceKey("apexNames") |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 408 | |
| 409 | // This structure maintains the global mapping in between modules and APEXes. |
| 410 | // Examples: |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 411 | // |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 412 | // apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar |
| 413 | // apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar |
| 414 | // apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar |
| 415 | func apexNamesMap() map[string]map[string]bool { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 416 | return apexData.Once(apexNamesKey, func() interface{} { |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 417 | return make(map[string]map[string]bool) |
| 418 | }).(map[string]map[string]bool) |
| 419 | } |
| 420 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 421 | // Update the map to mark that a module named moduleName is directly or indirectly |
Jiyong Park | f760cae | 2020-02-12 07:53:12 +0900 | [diff] [blame] | 422 | // depended on by the specified APEXes. Directly depending means that a module |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 423 | // is explicitly listed in the build definition of the APEX via properties like |
| 424 | // native_shared_libs, java_libs, etc. |
Jooyung Han | 345f0c4 | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 425 | func UpdateApexDependency(apex ApexInfo, moduleName string, directDep bool) { |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 426 | apexNamesMapMutex.Lock() |
| 427 | defer apexNamesMapMutex.Unlock() |
Jooyung Han | 345f0c4 | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 428 | apexesForModule, ok := apexNamesMap()[moduleName] |
| 429 | if !ok { |
| 430 | apexesForModule = make(map[string]bool) |
| 431 | apexNamesMap()[moduleName] = apexesForModule |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 432 | } |
Colin Cross | 7438571 | 2020-08-13 11:24:56 -0700 | [diff] [blame] | 433 | apexesForModule[apex.ApexVariationName] = apexesForModule[apex.ApexVariationName] || directDep |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 434 | for _, apexName := range apex.InApexes { |
| 435 | apexesForModule[apexName] = apexesForModule[apex.ApexVariationName] || directDep |
| 436 | } |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 437 | } |
| 438 | |
Jooyung Han | 671f1ce | 2019-12-17 12:47:13 +0900 | [diff] [blame] | 439 | // TODO(b/146393795): remove this when b/146393795 is fixed |
| 440 | func ClearApexDependency() { |
| 441 | m := apexNamesMap() |
| 442 | for k := range m { |
| 443 | delete(m, k) |
| 444 | } |
| 445 | } |
| 446 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 447 | // Tests whether a module named moduleName is directly depended on by an APEX |
| 448 | // named apexName. |
| 449 | func DirectlyInApex(apexName string, moduleName string) bool { |
| 450 | apexNamesMapMutex.Lock() |
| 451 | defer apexNamesMapMutex.Unlock() |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 452 | if apexNamesForModule, ok := apexNamesMap()[moduleName]; ok { |
| 453 | return apexNamesForModule[apexName] |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 454 | } |
| 455 | return false |
| 456 | } |
| 457 | |
Colin Cross | 274a72d | 2020-08-11 12:17:01 -0700 | [diff] [blame] | 458 | // Tests whether a module named moduleName is directly depended on by all APEXes |
| 459 | // in a list of apexNames. |
| 460 | func DirectlyInAllApexes(apexNames []string, moduleName string) bool { |
| 461 | apexNamesMapMutex.Lock() |
| 462 | defer apexNamesMapMutex.Unlock() |
| 463 | for _, apexName := range apexNames { |
| 464 | apexNamesForModule := apexNamesMap()[moduleName] |
| 465 | if !apexNamesForModule[apexName] { |
| 466 | return false |
| 467 | } |
| 468 | } |
| 469 | return true |
| 470 | } |
| 471 | |
Nicolas Geoffray | c22c1bf | 2019-01-15 19:53:23 +0000 | [diff] [blame] | 472 | type hostContext interface { |
| 473 | Host() bool |
| 474 | } |
| 475 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 476 | // Tests whether a module named moduleName is directly depended on by any APEX. |
Nicolas Geoffray | c22c1bf | 2019-01-15 19:53:23 +0000 | [diff] [blame] | 477 | func DirectlyInAnyApex(ctx hostContext, moduleName string) bool { |
| 478 | if ctx.Host() { |
| 479 | // Host has no APEX. |
| 480 | return false |
| 481 | } |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 482 | apexNamesMapMutex.Lock() |
| 483 | defer apexNamesMapMutex.Unlock() |
| 484 | if apexNames, ok := apexNamesMap()[moduleName]; ok { |
| 485 | for an := range apexNames { |
| 486 | if apexNames[an] { |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 487 | return true |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | return false |
| 492 | } |
| 493 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 494 | // Tests whether a module named module is depended on (including both |
| 495 | // direct and indirect dependencies) by any APEX. |
| 496 | func InAnyApex(moduleName string) bool { |
| 497 | apexNamesMapMutex.Lock() |
| 498 | defer apexNamesMapMutex.Unlock() |
| 499 | apexNames, ok := apexNamesMap()[moduleName] |
| 500 | return ok && len(apexNames) > 0 |
| 501 | } |
| 502 | |
| 503 | func GetApexesForModule(moduleName string) []string { |
| 504 | ret := []string{} |
| 505 | apexNamesMapMutex.Lock() |
| 506 | defer apexNamesMapMutex.Unlock() |
| 507 | if apexNames, ok := apexNamesMap()[moduleName]; ok { |
| 508 | for an := range apexNames { |
| 509 | ret = append(ret, an) |
| 510 | } |
| 511 | } |
| 512 | return ret |
Jiyong Park | de866cb | 2018-12-07 23:08:36 +0900 | [diff] [blame] | 513 | } |
| 514 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 515 | func InitApexModule(m ApexModule) { |
| 516 | base := m.apexModuleBase() |
| 517 | base.canHaveApexVariants = true |
| 518 | |
| 519 | m.AddProperties(&base.ApexProperties) |
| 520 | } |
Artur Satayev | 334b517 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 521 | |
| 522 | // A dependency info for a single ApexModule, either direct or transitive. |
| 523 | type ApexModuleDepInfo struct { |
| 524 | // Name of the dependency |
| 525 | To string |
| 526 | // List of dependencies To belongs to. Includes APEX itself, if a direct dependency. |
| 527 | From []string |
| 528 | // Whether the dependency belongs to the final compiled APEX. |
| 529 | IsExternal bool |
Artur Satayev | 388d39b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 530 | // min_sdk_version of the ApexModule |
| 531 | MinSdkVersion string |
Artur Satayev | 334b517 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | // A map of a dependency name to its ApexModuleDepInfo |
| 535 | type DepNameToDepInfoMap map[string]ApexModuleDepInfo |
| 536 | |
| 537 | type ApexBundleDepsInfo struct { |
Jooyung Han | 93a06c1 | 2020-05-14 07:44:03 +0900 | [diff] [blame] | 538 | flatListPath OutputPath |
| 539 | fullListPath OutputPath |
Artur Satayev | 334b517 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Artur Satayev | 2b4b7bb | 2020-04-28 14:57:42 +0100 | [diff] [blame] | 542 | type ApexBundleDepsInfoIntf interface { |
| 543 | Updatable() bool |
Artur Satayev | 5e7c32d | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 544 | FlatListPath() Path |
Artur Satayev | 334b517 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 545 | FullListPath() Path |
| 546 | } |
| 547 | |
Artur Satayev | 5e7c32d | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 548 | func (d *ApexBundleDepsInfo) FlatListPath() Path { |
| 549 | return d.flatListPath |
| 550 | } |
| 551 | |
Artur Satayev | 334b517 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 552 | func (d *ApexBundleDepsInfo) FullListPath() Path { |
| 553 | return d.fullListPath |
| 554 | } |
| 555 | |
Artur Satayev | 5e7c32d | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 556 | // Generate two module out files: |
| 557 | // 1. FullList with transitive deps and their parents in the dep graph |
| 558 | // 2. FlatList with a flat list of transitive deps |
Artur Satayev | 388d39b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 559 | func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) { |
Artur Satayev | 5e7c32d | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 560 | var fullContent strings.Builder |
| 561 | var flatContent strings.Builder |
| 562 | |
Artur Satayev | 388d39b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 563 | fmt.Fprintf(&flatContent, "%s(minSdkVersion:%s):\\n", ctx.ModuleName(), minSdkVersion) |
Artur Satayev | 334b517 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 564 | for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) { |
| 565 | info := depInfos[key] |
Artur Satayev | 388d39b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 566 | toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion) |
Artur Satayev | 334b517 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 567 | if info.IsExternal { |
| 568 | toName = toName + " (external)" |
| 569 | } |
Artur Satayev | 5e7c32d | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 570 | fmt.Fprintf(&fullContent, "%s <- %s\\n", toName, strings.Join(SortedUniqueStrings(info.From), ", ")) |
| 571 | fmt.Fprintf(&flatContent, " %s\\n", toName) |
Artur Satayev | 334b517 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath |
| 575 | ctx.Build(pctx, BuildParams{ |
| 576 | Rule: WriteFile, |
| 577 | Description: "Full Dependency Info", |
| 578 | Output: d.fullListPath, |
| 579 | Args: map[string]string{ |
Artur Satayev | 5e7c32d | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 580 | "content": fullContent.String(), |
| 581 | }, |
| 582 | }) |
| 583 | |
| 584 | d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath |
| 585 | ctx.Build(pctx, BuildParams{ |
| 586 | Rule: WriteFile, |
| 587 | Description: "Flat Dependency Info", |
| 588 | Output: d.flatListPath, |
| 589 | Args: map[string]string{ |
| 590 | "content": flatContent.String(), |
Artur Satayev | 334b517 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 591 | }, |
| 592 | }) |
| 593 | } |