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 | cefa94bd | 2019-06-03 15:07:03 -0700 | [diff] [blame] | 19 | "sort" |
Jooyung Han | 03b5185 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 20 | "strconv" |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 21 | "sync" |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 22 | ) |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 23 | |
Jooyung Han | 5417f77 | 2020-03-12 18:37:20 +0900 | [diff] [blame] | 24 | const ( |
| 25 | SdkVersion_Android10 = 29 |
| 26 | ) |
| 27 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 28 | type ApexInfo struct { |
| 29 | // Name of the apex variant that this module is mutated into |
| 30 | ApexName string |
| 31 | |
Jooyung Han | 03b5185 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 32 | MinSdkVersion int |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Paul Duffin | 923e8a5 | 2020-03-30 15:33:32 +0100 | [diff] [blame^] | 35 | // Extracted from ApexModule to make it easier to define custom subsets of the |
| 36 | // ApexModule interface and improve code navigation within the IDE. |
| 37 | type DepIsInSameApex interface { |
| 38 | // DepIsInSameApex tests if the other module 'dep' is installed to the same |
| 39 | // APEX as this module |
| 40 | DepIsInSameApex(ctx BaseModuleContext, dep Module) bool |
| 41 | } |
| 42 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 43 | // ApexModule is the interface that a module type is expected to implement if |
| 44 | // the module has to be built differently depending on whether the module |
| 45 | // is destined for an apex or not (installed to one of the regular partitions). |
| 46 | // |
| 47 | // Native shared libraries are one such module type; when it is built for an |
| 48 | // APEX, it should depend only on stable interfaces such as NDK, stable AIDL, |
| 49 | // or C APIs from other APEXs. |
| 50 | // |
| 51 | // A module implementing this interface will be mutated into multiple |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 52 | // variations by apex.apexMutator if it is directly or indirectly included |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 53 | // in one or more APEXs. Specifically, if a module is included in apex.foo and |
| 54 | // apex.bar then three apex variants are created: platform, apex.foo and |
| 55 | // apex.bar. The platform variant is for the regular partitions |
| 56 | // (e.g., /system or /vendor, etc.) while the other two are for the APEXs, |
| 57 | // respectively. |
| 58 | type ApexModule interface { |
| 59 | Module |
Paul Duffin | 923e8a5 | 2020-03-30 15:33:32 +0100 | [diff] [blame^] | 60 | DepIsInSameApex |
| 61 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 62 | apexModuleBase() *ApexModuleBase |
| 63 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 64 | // Marks that this module should be built for the specified APEXes. |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 65 | // Call this before apex.apexMutator is run. |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 66 | BuildForApexes(apexes []ApexInfo) |
Jiyong Park | f760cae | 2020-02-12 07:53:12 +0900 | [diff] [blame] | 67 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 68 | // Returns the APEXes that this module will be built for |
| 69 | ApexVariations() []ApexInfo |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 70 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 71 | // Returns the name of APEX that this module will be built for. Empty string |
| 72 | // is returned when 'IsForPlatform() == true'. Note that a module can be |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 73 | // included in multiple APEXes, in which case, the module is mutated into |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 74 | // multiple modules each of which for an APEX. This method returns the |
| 75 | // name of the APEX that a variant module is for. |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 76 | // Call this after apex.apexMutator is run. |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 77 | ApexName() string |
| 78 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 79 | // Tests whether this module will be built for the platform or not. |
| 80 | // This is a shortcut for ApexName() == "" |
| 81 | IsForPlatform() bool |
| 82 | |
| 83 | // Tests if this module could have APEX variants. APEX variants are |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 84 | // created only for the modules that returns true here. This is useful |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 85 | // for not creating APEX variants for certain types of shared libraries |
| 86 | // such as NDK stubs. |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 87 | CanHaveApexVariants() bool |
| 88 | |
| 89 | // Tests if this module can be installed to APEX as a file. For example, |
| 90 | // this would return true for shared libs while return false for static |
| 91 | // libs. |
| 92 | IsInstallableToApex() bool |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 93 | |
| 94 | // Mutate this module into one or more variants each of which is built |
Jiyong Park | f760cae | 2020-02-12 07:53:12 +0900 | [diff] [blame] | 95 | // for an APEX marked via BuildForApexes(). |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 96 | CreateApexVariations(mctx BottomUpMutatorContext) []Module |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 97 | |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 98 | // Tests if this module is available for the specified APEX or ":platform" |
| 99 | AvailableFor(what string) bool |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 100 | |
Jooyung Han | 7556839 | 2020-03-20 04:29:24 +0900 | [diff] [blame] | 101 | // Returns the highest version which is <= maxSdkVersion. |
| 102 | // For example, with maxSdkVersion is 10 and versionList is [9,11] |
| 103 | // it returns 9 as string |
| 104 | ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | type ApexProperties struct { |
Martin Stjernholm | 06ca82d | 2020-01-17 13:02:56 +0000 | [diff] [blame] | 108 | // Availability of this module in APEXes. Only the listed APEXes can contain |
| 109 | // this module. If the module has stubs then other APEXes and the platform may |
| 110 | // access it through them (subject to visibility). |
| 111 | // |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 112 | // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX. |
| 113 | // "//apex_available:platform" refers to non-APEX partitions like "system.img". |
Jiyong Park | 9a1e14e | 2020-02-13 02:30:45 +0900 | [diff] [blame] | 114 | // Default is ["//apex_available:platform"]. |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 115 | Apex_available []string |
| 116 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 117 | Info ApexInfo `blueprint:"mutated"` |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Provides default implementation for the ApexModule interface. APEX-aware |
| 121 | // modules are expected to include this struct and call InitApexModule(). |
| 122 | type ApexModuleBase struct { |
| 123 | ApexProperties ApexProperties |
| 124 | |
| 125 | canHaveApexVariants bool |
Colin Cross | cefa94bd | 2019-06-03 15:07:03 -0700 | [diff] [blame] | 126 | |
| 127 | apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 128 | apexVariations []ApexInfo |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase { |
| 132 | return m |
| 133 | } |
| 134 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 135 | func (m *ApexModuleBase) ApexAvailable() []string { |
| 136 | return m.ApexProperties.Apex_available |
| 137 | } |
| 138 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 139 | func (m *ApexModuleBase) BuildForApexes(apexes []ApexInfo) { |
Colin Cross | cefa94bd | 2019-06-03 15:07:03 -0700 | [diff] [blame] | 140 | m.apexVariationsLock.Lock() |
| 141 | defer m.apexVariationsLock.Unlock() |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 142 | nextApex: |
| 143 | for _, apex := range apexes { |
| 144 | for _, v := range m.apexVariations { |
| 145 | if v.ApexName == apex.ApexName { |
| 146 | continue nextApex |
| 147 | } |
Jiyong Park | f760cae | 2020-02-12 07:53:12 +0900 | [diff] [blame] | 148 | } |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 149 | m.apexVariations = append(m.apexVariations, apex) |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 153 | func (m *ApexModuleBase) ApexVariations() []ApexInfo { |
Jiyong Park | f760cae | 2020-02-12 07:53:12 +0900 | [diff] [blame] | 154 | return m.apexVariations |
| 155 | } |
| 156 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 157 | func (m *ApexModuleBase) ApexName() string { |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 158 | return m.ApexProperties.Info.ApexName |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | func (m *ApexModuleBase) IsForPlatform() bool { |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 162 | return m.ApexProperties.Info.ApexName == "" |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | func (m *ApexModuleBase) CanHaveApexVariants() bool { |
| 166 | return m.canHaveApexVariants |
| 167 | } |
| 168 | |
| 169 | func (m *ApexModuleBase) IsInstallableToApex() bool { |
| 170 | // should be overriden if needed |
| 171 | return false |
| 172 | } |
| 173 | |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 174 | const ( |
Jiyong Park | b02bb40 | 2019-12-03 00:43:57 +0900 | [diff] [blame] | 175 | AvailableToPlatform = "//apex_available:platform" |
Paul Duffin | 7d74e7b | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 176 | AvailableToAnyApex = "//apex_available:anyapex" |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 177 | ) |
| 178 | |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 179 | func CheckAvailableForApex(what string, apex_available []string) bool { |
| 180 | if len(apex_available) == 0 { |
Anton Hansson | eec79eb | 2020-01-10 15:12:39 +0000 | [diff] [blame] | 181 | // apex_available defaults to ["//apex_available:platform"], |
| 182 | // which means 'available to the platform but no apexes'. |
| 183 | return what == AvailableToPlatform |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 184 | } |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 185 | return InList(what, apex_available) || |
Paul Duffin | 7d74e7b | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 186 | (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | func (m *ApexModuleBase) AvailableFor(what string) bool { |
| 190 | return CheckAvailableForApex(what, m.ApexProperties.Apex_available) |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 191 | } |
| 192 | |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 193 | func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool { |
| 194 | // By default, if there is a dependency from A to B, we try to include both in the same APEX, |
| 195 | // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true. |
| 196 | // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc. |
| 197 | return true |
| 198 | } |
| 199 | |
Jooyung Han | 7556839 | 2020-03-20 04:29:24 +0900 | [diff] [blame] | 200 | func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) { |
Jooyung Han | 03b5185 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 201 | for i := range versionList { |
| 202 | ver, _ := strconv.Atoi(versionList[len(versionList)-i-1]) |
Jooyung Han | 7556839 | 2020-03-20 04:29:24 +0900 | [diff] [blame] | 203 | if ver <= maxSdkVersion { |
Jooyung Han | 03b5185 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 204 | return versionList[len(versionList)-i-1], nil |
| 205 | } |
| 206 | } |
Jooyung Han | 7556839 | 2020-03-20 04:29:24 +0900 | [diff] [blame] | 207 | return "", fmt.Errorf("not found a version(<=%d) in versionList: %v", maxSdkVersion, versionList) |
Jooyung Han | 03b5185 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 208 | } |
| 209 | |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 210 | func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) { |
| 211 | for _, n := range m.ApexProperties.Apex_available { |
Paul Duffin | 7d74e7b | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 212 | if n == AvailableToPlatform || n == AvailableToAnyApex { |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 213 | continue |
| 214 | } |
Orion Hodson | 4b5438a | 2019-10-08 10:40:51 +0100 | [diff] [blame] | 215 | if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() { |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 216 | mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n) |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 221 | type byApexName []ApexInfo |
| 222 | |
| 223 | func (a byApexName) Len() int { return len(a) } |
| 224 | func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 225 | func (a byApexName) Less(i, j int) bool { return a[i].ApexName < a[j].ApexName } |
| 226 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 227 | func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module { |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 228 | if len(m.apexVariations) > 0 { |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 229 | m.checkApexAvailableProperty(mctx) |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 230 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 231 | sort.Sort(byApexName(m.apexVariations)) |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 232 | variations := []string{} |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 233 | variations = append(variations, "") // Original variation for platform |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 234 | for _, apex := range m.apexVariations { |
| 235 | variations = append(variations, apex.ApexName) |
| 236 | } |
Logan Chien | 3aeedc9 | 2018-12-26 15:32:21 +0800 | [diff] [blame] | 237 | |
Jiyong Park | 3ff1699 | 2019-12-27 14:11:47 +0900 | [diff] [blame] | 238 | defaultVariation := "" |
| 239 | mctx.SetDefaultDependencyVariation(&defaultVariation) |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 240 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 241 | modules := mctx.CreateVariations(variations...) |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 242 | for i, mod := range modules { |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 243 | platformVariation := i == 0 |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 244 | if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) { |
| 245 | mod.SkipInstall() |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 246 | } |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 247 | if !platformVariation { |
| 248 | mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1] |
| 249 | } |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 250 | } |
| 251 | return modules |
| 252 | } |
| 253 | return nil |
| 254 | } |
| 255 | |
| 256 | var apexData OncePer |
| 257 | var apexNamesMapMutex sync.Mutex |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 258 | var apexNamesKey = NewOnceKey("apexNames") |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 259 | |
| 260 | // This structure maintains the global mapping in between modules and APEXes. |
| 261 | // Examples: |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 262 | // |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 263 | // apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar |
| 264 | // apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar |
| 265 | // apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar |
| 266 | func apexNamesMap() map[string]map[string]bool { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 267 | return apexData.Once(apexNamesKey, func() interface{} { |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 268 | return make(map[string]map[string]bool) |
| 269 | }).(map[string]map[string]bool) |
| 270 | } |
| 271 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 272 | // 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] | 273 | // depended on by the specified APEXes. Directly depending means that a module |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 274 | // is explicitly listed in the build definition of the APEX via properties like |
| 275 | // native_shared_libs, java_libs, etc. |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 276 | func UpdateApexDependency(apexes []ApexInfo, moduleName string, directDep bool) { |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 277 | apexNamesMapMutex.Lock() |
| 278 | defer apexNamesMapMutex.Unlock() |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 279 | for _, apex := range apexes { |
Jiyong Park | f760cae | 2020-02-12 07:53:12 +0900 | [diff] [blame] | 280 | apexesForModule, ok := apexNamesMap()[moduleName] |
| 281 | if !ok { |
| 282 | apexesForModule = make(map[string]bool) |
| 283 | apexNamesMap()[moduleName] = apexesForModule |
| 284 | } |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 285 | apexesForModule[apex.ApexName] = apexesForModule[apex.ApexName] || directDep |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 286 | } |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 287 | } |
| 288 | |
Jooyung Han | 671f1ce | 2019-12-17 12:47:13 +0900 | [diff] [blame] | 289 | // TODO(b/146393795): remove this when b/146393795 is fixed |
| 290 | func ClearApexDependency() { |
| 291 | m := apexNamesMap() |
| 292 | for k := range m { |
| 293 | delete(m, k) |
| 294 | } |
| 295 | } |
| 296 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 297 | // Tests whether a module named moduleName is directly depended on by an APEX |
| 298 | // named apexName. |
| 299 | func DirectlyInApex(apexName string, moduleName string) bool { |
| 300 | apexNamesMapMutex.Lock() |
| 301 | defer apexNamesMapMutex.Unlock() |
| 302 | if apexNames, ok := apexNamesMap()[moduleName]; ok { |
| 303 | return apexNames[apexName] |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 304 | } |
| 305 | return false |
| 306 | } |
| 307 | |
Nicolas Geoffray | c22c1bf | 2019-01-15 19:53:23 +0000 | [diff] [blame] | 308 | type hostContext interface { |
| 309 | Host() bool |
| 310 | } |
| 311 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 312 | // 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] | 313 | func DirectlyInAnyApex(ctx hostContext, moduleName string) bool { |
| 314 | if ctx.Host() { |
| 315 | // Host has no APEX. |
| 316 | return false |
| 317 | } |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 318 | apexNamesMapMutex.Lock() |
| 319 | defer apexNamesMapMutex.Unlock() |
| 320 | if apexNames, ok := apexNamesMap()[moduleName]; ok { |
| 321 | for an := range apexNames { |
| 322 | if apexNames[an] { |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 323 | return true |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | return false |
| 328 | } |
| 329 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 330 | // Tests whether a module named module is depended on (including both |
| 331 | // direct and indirect dependencies) by any APEX. |
| 332 | func InAnyApex(moduleName string) bool { |
| 333 | apexNamesMapMutex.Lock() |
| 334 | defer apexNamesMapMutex.Unlock() |
| 335 | apexNames, ok := apexNamesMap()[moduleName] |
| 336 | return ok && len(apexNames) > 0 |
| 337 | } |
| 338 | |
| 339 | func GetApexesForModule(moduleName string) []string { |
| 340 | ret := []string{} |
| 341 | apexNamesMapMutex.Lock() |
| 342 | defer apexNamesMapMutex.Unlock() |
| 343 | if apexNames, ok := apexNamesMap()[moduleName]; ok { |
| 344 | for an := range apexNames { |
| 345 | ret = append(ret, an) |
| 346 | } |
| 347 | } |
| 348 | return ret |
Jiyong Park | de866cb | 2018-12-07 23:08:36 +0900 | [diff] [blame] | 349 | } |
| 350 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 351 | func InitApexModule(m ApexModule) { |
| 352 | base := m.apexModuleBase() |
| 353 | base.canHaveApexVariants = true |
| 354 | |
| 355 | m.AddProperties(&base.ApexProperties) |
| 356 | } |