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