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