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 { |
Colin Cross | e07f231 | 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 | 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 | |
Jooyung Han | 698dd9f | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 68 | // Marks that this module should be built for the specified APEX. |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 69 | // Call this before apex.apexMutator is run. |
Jooyung Han | 698dd9f | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 70 | BuildForApex(apex 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 | |
Colin Cross | e07f231 | 2020-08-13 11:24:56 -0700 | [diff] [blame^] | 75 | // Returns the name of APEX variation that this module will be built for. |
| 76 | //Empty string is returned when 'IsForPlatform() == true'. Note that a |
| 77 | // module can be included in multiple APEXes, in which case, the module |
| 78 | // is mutated into multiple modules each of which for an APEX. This method |
| 79 | // returns the 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. |
Colin Cross | e07f231 | 2020-08-13 11:24:56 -0700 | [diff] [blame^] | 81 | ApexVariationName() string |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 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. |
Colin Cross | e07f231 | 2020-08-13 11:24:56 -0700 | [diff] [blame^] | 84 | // This is a shortcut for ApexVariationName() == "" |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 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 |
Jooyung Han | 698dd9f | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 99 | // for an APEX marked via BuildForApex(). |
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 |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 127 | |
| 128 | // Returns nil if this module supports sdkVersion |
| 129 | // Otherwise, returns error with reason |
| 130 | ShouldSupportSdkVersion(ctx BaseModuleContext, sdkVersion int) error |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | type ApexProperties struct { |
Martin Stjernholm | 06ca82d | 2020-01-17 13:02:56 +0000 | [diff] [blame] | 134 | // Availability of this module in APEXes. Only the listed APEXes can contain |
| 135 | // this module. If the module has stubs then other APEXes and the platform may |
| 136 | // access it through them (subject to visibility). |
| 137 | // |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 138 | // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX. |
| 139 | // "//apex_available:platform" refers to non-APEX partitions like "system.img". |
Yifan Hong | d22a84a | 2020-07-28 17:37:46 -0700 | [diff] [blame] | 140 | // "com.android.gki.*" matches any APEX module name with the prefix "com.android.gki.". |
Jiyong Park | 9a1e14e | 2020-02-13 02:30:45 +0900 | [diff] [blame] | 141 | // Default is ["//apex_available:platform"]. |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 142 | Apex_available []string |
| 143 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 144 | Info ApexInfo `blueprint:"mutated"` |
Jiyong Park | 89e850a | 2020-04-07 16:37:39 +0900 | [diff] [blame] | 145 | |
| 146 | NotAvailableForPlatform bool `blueprint:"mutated"` |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 147 | } |
| 148 | |
Paul Duffin | dddd546 | 2020-04-07 15:25:44 +0100 | [diff] [blame] | 149 | // Marker interface that identifies dependencies that are excluded from APEX |
| 150 | // contents. |
| 151 | type ExcludeFromApexContentsTag interface { |
| 152 | blueprint.DependencyTag |
| 153 | |
| 154 | // Method that differentiates this interface from others. |
| 155 | ExcludeFromApexContents() |
| 156 | } |
| 157 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 158 | // Provides default implementation for the ApexModule interface. APEX-aware |
| 159 | // modules are expected to include this struct and call InitApexModule(). |
| 160 | type ApexModuleBase struct { |
| 161 | ApexProperties ApexProperties |
| 162 | |
| 163 | canHaveApexVariants bool |
Colin Cross | cefa94bd | 2019-06-03 15:07:03 -0700 | [diff] [blame] | 164 | |
| 165 | apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 166 | apexVariations []ApexInfo |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase { |
| 170 | return m |
| 171 | } |
| 172 | |
Paul Duffin | befa4b9 | 2020-03-04 14:22:45 +0000 | [diff] [blame] | 173 | func (m *ApexModuleBase) ApexAvailable() []string { |
| 174 | return m.ApexProperties.Apex_available |
| 175 | } |
| 176 | |
Jiyong Park | 62304bb | 2020-04-13 16:19:48 +0900 | [diff] [blame] | 177 | func (m *ApexModuleBase) TestFor() []string { |
| 178 | // To be implemented by concrete types inheriting ApexModuleBase |
| 179 | return nil |
| 180 | } |
| 181 | |
Jooyung Han | 698dd9f | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 182 | func (m *ApexModuleBase) BuildForApex(apex ApexInfo) { |
Colin Cross | cefa94bd | 2019-06-03 15:07:03 -0700 | [diff] [blame] | 183 | m.apexVariationsLock.Lock() |
| 184 | defer m.apexVariationsLock.Unlock() |
Jooyung Han | 698dd9f | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 185 | for _, v := range m.apexVariations { |
Colin Cross | e07f231 | 2020-08-13 11:24:56 -0700 | [diff] [blame^] | 186 | if v.ApexVariationName == apex.ApexVariationName { |
Jooyung Han | 698dd9f | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 187 | return |
Jiyong Park | f760cae | 2020-02-12 07:53:12 +0900 | [diff] [blame] | 188 | } |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 189 | } |
Jooyung Han | 698dd9f | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 190 | m.apexVariations = append(m.apexVariations, apex) |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 191 | } |
| 192 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 193 | func (m *ApexModuleBase) ApexVariations() []ApexInfo { |
Jiyong Park | f760cae | 2020-02-12 07:53:12 +0900 | [diff] [blame] | 194 | return m.apexVariations |
| 195 | } |
| 196 | |
Colin Cross | e07f231 | 2020-08-13 11:24:56 -0700 | [diff] [blame^] | 197 | func (m *ApexModuleBase) ApexVariationName() string { |
| 198 | return m.ApexProperties.Info.ApexVariationName |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | func (m *ApexModuleBase) IsForPlatform() bool { |
Colin Cross | e07f231 | 2020-08-13 11:24:56 -0700 | [diff] [blame^] | 202 | return m.ApexProperties.Info.ApexVariationName == "" |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | func (m *ApexModuleBase) CanHaveApexVariants() bool { |
| 206 | return m.canHaveApexVariants |
| 207 | } |
| 208 | |
| 209 | func (m *ApexModuleBase) IsInstallableToApex() bool { |
| 210 | // should be overriden if needed |
| 211 | return false |
| 212 | } |
| 213 | |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 214 | const ( |
Jiyong Park | b02bb40 | 2019-12-03 00:43:57 +0900 | [diff] [blame] | 215 | AvailableToPlatform = "//apex_available:platform" |
Paul Duffin | 7d74e7b | 2020-03-06 12:30:13 +0000 | [diff] [blame] | 216 | AvailableToAnyApex = "//apex_available:anyapex" |
Yifan Hong | d22a84a | 2020-07-28 17:37:46 -0700 | [diff] [blame] | 217 | AvailableToGkiApex = "com.android.gki.*" |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 218 | ) |
| 219 | |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 220 | func CheckAvailableForApex(what string, apex_available []string) bool { |
| 221 | if len(apex_available) == 0 { |
Anton Hansson | eec79eb | 2020-01-10 15:12:39 +0000 | [diff] [blame] | 222 | // apex_available defaults to ["//apex_available:platform"], |
| 223 | // which means 'available to the platform but no apexes'. |
| 224 | return what == AvailableToPlatform |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 225 | } |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 226 | return InList(what, apex_available) || |
Yifan Hong | d22a84a | 2020-07-28 17:37:46 -0700 | [diff] [blame] | 227 | (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) || |
| 228 | (strings.HasPrefix(what, "com.android.gki.") && InList(AvailableToGkiApex, apex_available)) |
Jiyong Park | a90ca00 | 2019-10-07 15:47:24 +0900 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | func (m *ApexModuleBase) AvailableFor(what string) bool { |
| 232 | return CheckAvailableForApex(what, m.ApexProperties.Apex_available) |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 233 | } |
| 234 | |
Jiyong Park | 89e850a | 2020-04-07 16:37:39 +0900 | [diff] [blame] | 235 | func (m *ApexModuleBase) NotAvailableForPlatform() bool { |
| 236 | return m.ApexProperties.NotAvailableForPlatform |
| 237 | } |
| 238 | |
| 239 | func (m *ApexModuleBase) SetNotAvailableForPlatform() { |
| 240 | m.ApexProperties.NotAvailableForPlatform = true |
| 241 | } |
| 242 | |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 243 | func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool { |
| 244 | // By default, if there is a dependency from A to B, we try to include both in the same APEX, |
| 245 | // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true. |
| 246 | // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc. |
| 247 | return true |
| 248 | } |
| 249 | |
Jooyung Han | 7556839 | 2020-03-20 04:29:24 +0900 | [diff] [blame] | 250 | func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) { |
Jooyung Han | 03b5185 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 251 | for i := range versionList { |
| 252 | ver, _ := strconv.Atoi(versionList[len(versionList)-i-1]) |
Jooyung Han | 7556839 | 2020-03-20 04:29:24 +0900 | [diff] [blame] | 253 | if ver <= maxSdkVersion { |
Jooyung Han | 03b5185 | 2020-02-26 22:45:42 +0900 | [diff] [blame] | 254 | return versionList[len(versionList)-i-1], nil |
| 255 | } |
| 256 | } |
Jooyung Han | 7556839 | 2020-03-20 04:29:24 +0900 | [diff] [blame] | 257 | 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] | 258 | } |
| 259 | |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 260 | func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) { |
| 261 | for _, n := range m.ApexProperties.Apex_available { |
Yifan Hong | d22a84a | 2020-07-28 17:37:46 -0700 | [diff] [blame] | 262 | if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex { |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 263 | continue |
| 264 | } |
Orion Hodson | 4b5438a | 2019-10-08 10:40:51 +0100 | [diff] [blame] | 265 | if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() { |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 266 | mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n) |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
Ulya Trafimovich | 7c140d8 | 2020-04-22 18:05:58 +0100 | [diff] [blame] | 271 | func (m *ApexModuleBase) Updatable() bool { |
| 272 | return m.ApexProperties.Info.Updatable |
| 273 | } |
| 274 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 275 | type byApexName []ApexInfo |
| 276 | |
| 277 | func (a byApexName) Len() int { return len(a) } |
| 278 | func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
Colin Cross | e07f231 | 2020-08-13 11:24:56 -0700 | [diff] [blame^] | 279 | 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] | 280 | |
Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 281 | func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module { |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 282 | if len(m.apexVariations) > 0 { |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 283 | m.checkApexAvailableProperty(mctx) |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 284 | |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 285 | sort.Sort(byApexName(m.apexVariations)) |
Jiyong Park | 127b40b | 2019-09-30 16:04:35 +0900 | [diff] [blame] | 286 | variations := []string{} |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 287 | variations = append(variations, "") // Original variation for platform |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 288 | for _, apex := range m.apexVariations { |
Colin Cross | e07f231 | 2020-08-13 11:24:56 -0700 | [diff] [blame^] | 289 | variations = append(variations, apex.ApexVariationName) |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 290 | } |
Logan Chien | 3aeedc9 | 2018-12-26 15:32:21 +0800 | [diff] [blame] | 291 | |
Jiyong Park | 3ff1699 | 2019-12-27 14:11:47 +0900 | [diff] [blame] | 292 | defaultVariation := "" |
| 293 | mctx.SetDefaultDependencyVariation(&defaultVariation) |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 294 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 295 | modules := mctx.CreateVariations(variations...) |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 296 | for i, mod := range modules { |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 297 | platformVariation := i == 0 |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 298 | if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) { |
Martin Stjernholm | 9e9bb7f | 2020-08-06 22:34:42 +0100 | [diff] [blame] | 299 | // Do not install the module for platform, but still allow it to output |
| 300 | // uninstallable AndroidMk entries in certain cases when they have |
| 301 | // side effects. |
| 302 | mod.MakeUninstallable() |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 303 | } |
Peter Collingbourne | dc4f986 | 2020-02-12 17:13:25 -0800 | [diff] [blame] | 304 | if !platformVariation { |
| 305 | mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1] |
| 306 | } |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 307 | } |
| 308 | return modules |
| 309 | } |
| 310 | return nil |
| 311 | } |
| 312 | |
| 313 | var apexData OncePer |
| 314 | var apexNamesMapMutex sync.Mutex |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 315 | var apexNamesKey = NewOnceKey("apexNames") |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 316 | |
| 317 | // This structure maintains the global mapping in between modules and APEXes. |
| 318 | // Examples: |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 319 | // |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 320 | // apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar |
| 321 | // apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar |
| 322 | // apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar |
| 323 | func apexNamesMap() map[string]map[string]bool { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 324 | return apexData.Once(apexNamesKey, func() interface{} { |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 325 | return make(map[string]map[string]bool) |
| 326 | }).(map[string]map[string]bool) |
| 327 | } |
| 328 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 329 | // 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] | 330 | // depended on by the specified APEXes. Directly depending means that a module |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 331 | // is explicitly listed in the build definition of the APEX via properties like |
| 332 | // native_shared_libs, java_libs, etc. |
Jooyung Han | 698dd9f | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 333 | func UpdateApexDependency(apex ApexInfo, moduleName string, directDep bool) { |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 334 | apexNamesMapMutex.Lock() |
| 335 | defer apexNamesMapMutex.Unlock() |
Jooyung Han | 698dd9f | 2020-07-22 15:17:19 +0900 | [diff] [blame] | 336 | apexesForModule, ok := apexNamesMap()[moduleName] |
| 337 | if !ok { |
| 338 | apexesForModule = make(map[string]bool) |
| 339 | apexNamesMap()[moduleName] = apexesForModule |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 340 | } |
Colin Cross | e07f231 | 2020-08-13 11:24:56 -0700 | [diff] [blame^] | 341 | apexesForModule[apex.ApexVariationName] = apexesForModule[apex.ApexVariationName] || directDep |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 342 | } |
| 343 | |
Jooyung Han | 671f1ce | 2019-12-17 12:47:13 +0900 | [diff] [blame] | 344 | // TODO(b/146393795): remove this when b/146393795 is fixed |
| 345 | func ClearApexDependency() { |
| 346 | m := apexNamesMap() |
| 347 | for k := range m { |
| 348 | delete(m, k) |
| 349 | } |
| 350 | } |
| 351 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 352 | // Tests whether a module named moduleName is directly depended on by an APEX |
| 353 | // named apexName. |
| 354 | func DirectlyInApex(apexName string, moduleName string) bool { |
| 355 | apexNamesMapMutex.Lock() |
| 356 | defer apexNamesMapMutex.Unlock() |
| 357 | if apexNames, ok := apexNamesMap()[moduleName]; ok { |
| 358 | return apexNames[apexName] |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 359 | } |
| 360 | return false |
| 361 | } |
| 362 | |
Nicolas Geoffray | c22c1bf | 2019-01-15 19:53:23 +0000 | [diff] [blame] | 363 | type hostContext interface { |
| 364 | Host() bool |
| 365 | } |
| 366 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 367 | // 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] | 368 | func DirectlyInAnyApex(ctx hostContext, moduleName string) bool { |
| 369 | if ctx.Host() { |
| 370 | // Host has no APEX. |
| 371 | return false |
| 372 | } |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 373 | apexNamesMapMutex.Lock() |
| 374 | defer apexNamesMapMutex.Unlock() |
| 375 | if apexNames, ok := apexNamesMap()[moduleName]; ok { |
| 376 | for an := range apexNames { |
| 377 | if apexNames[an] { |
Jiyong Park | 25fc6a9 | 2018-11-18 18:02:45 +0900 | [diff] [blame] | 378 | return true |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | return false |
| 383 | } |
| 384 | |
Jiyong Park | 0ddfcd1 | 2018-12-11 01:35:25 +0900 | [diff] [blame] | 385 | // Tests whether a module named module is depended on (including both |
| 386 | // direct and indirect dependencies) by any APEX. |
| 387 | func InAnyApex(moduleName string) bool { |
| 388 | apexNamesMapMutex.Lock() |
| 389 | defer apexNamesMapMutex.Unlock() |
| 390 | apexNames, ok := apexNamesMap()[moduleName] |
| 391 | return ok && len(apexNames) > 0 |
| 392 | } |
| 393 | |
| 394 | func GetApexesForModule(moduleName string) []string { |
| 395 | ret := []string{} |
| 396 | apexNamesMapMutex.Lock() |
| 397 | defer apexNamesMapMutex.Unlock() |
| 398 | if apexNames, ok := apexNamesMap()[moduleName]; ok { |
| 399 | for an := range apexNames { |
| 400 | ret = append(ret, an) |
| 401 | } |
| 402 | } |
| 403 | return ret |
Jiyong Park | de866cb | 2018-12-07 23:08:36 +0900 | [diff] [blame] | 404 | } |
| 405 | |
Jiyong Park | 9d45299 | 2018-10-03 00:38:19 +0900 | [diff] [blame] | 406 | func InitApexModule(m ApexModule) { |
| 407 | base := m.apexModuleBase() |
| 408 | base.canHaveApexVariants = true |
| 409 | |
| 410 | m.AddProperties(&base.ApexProperties) |
| 411 | } |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 412 | |
| 413 | // A dependency info for a single ApexModule, either direct or transitive. |
| 414 | type ApexModuleDepInfo struct { |
| 415 | // Name of the dependency |
| 416 | To string |
| 417 | // List of dependencies To belongs to. Includes APEX itself, if a direct dependency. |
| 418 | From []string |
| 419 | // Whether the dependency belongs to the final compiled APEX. |
| 420 | IsExternal bool |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 421 | // min_sdk_version of the ApexModule |
| 422 | MinSdkVersion string |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | // A map of a dependency name to its ApexModuleDepInfo |
| 426 | type DepNameToDepInfoMap map[string]ApexModuleDepInfo |
| 427 | |
| 428 | type ApexBundleDepsInfo struct { |
Jooyung Han | 98d63e1 | 2020-05-14 07:44:03 +0900 | [diff] [blame] | 429 | flatListPath OutputPath |
| 430 | fullListPath OutputPath |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Artur Satayev | 849f844 | 2020-04-28 14:57:42 +0100 | [diff] [blame] | 433 | type ApexBundleDepsInfoIntf interface { |
| 434 | Updatable() bool |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 435 | FlatListPath() Path |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 436 | FullListPath() Path |
| 437 | } |
| 438 | |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 439 | func (d *ApexBundleDepsInfo) FlatListPath() Path { |
| 440 | return d.flatListPath |
| 441 | } |
| 442 | |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 443 | func (d *ApexBundleDepsInfo) FullListPath() Path { |
| 444 | return d.fullListPath |
| 445 | } |
| 446 | |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 447 | // Generate two module out files: |
| 448 | // 1. FullList with transitive deps and their parents in the dep graph |
| 449 | // 2. FlatList with a flat list of transitive deps |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 450 | func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) { |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 451 | var fullContent strings.Builder |
| 452 | var flatContent strings.Builder |
| 453 | |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 454 | fmt.Fprintf(&flatContent, "%s(minSdkVersion:%s):\\n", ctx.ModuleName(), minSdkVersion) |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 455 | for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) { |
| 456 | info := depInfos[key] |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 457 | toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion) |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 458 | if info.IsExternal { |
| 459 | toName = toName + " (external)" |
| 460 | } |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 461 | fmt.Fprintf(&fullContent, "%s <- %s\\n", toName, strings.Join(SortedUniqueStrings(info.From), ", ")) |
| 462 | fmt.Fprintf(&flatContent, " %s\\n", toName) |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath |
| 466 | ctx.Build(pctx, BuildParams{ |
| 467 | Rule: WriteFile, |
| 468 | Description: "Full Dependency Info", |
| 469 | Output: d.fullListPath, |
| 470 | Args: map[string]string{ |
Artur Satayev | a8bd113 | 2020-04-27 18:07:06 +0100 | [diff] [blame] | 471 | "content": fullContent.String(), |
| 472 | }, |
| 473 | }) |
| 474 | |
| 475 | d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath |
| 476 | ctx.Build(pctx, BuildParams{ |
| 477 | Rule: WriteFile, |
| 478 | Description: "Flat Dependency Info", |
| 479 | Output: d.flatListPath, |
| 480 | Args: map[string]string{ |
| 481 | "content": flatContent.String(), |
Artur Satayev | 872a144 | 2020-04-27 17:08:37 +0100 | [diff] [blame] | 482 | }, |
| 483 | }) |
| 484 | } |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 485 | |
| 486 | // TODO(b/158059172): remove minSdkVersion allowlist |
| 487 | var minSdkVersionAllowlist = map[string]int{ |
| 488 | "adbd": 30, |
| 489 | "android.net.ipsec.ike": 30, |
| 490 | "androidx-constraintlayout_constraintlayout-solver": 30, |
| 491 | "androidx.annotation_annotation": 28, |
| 492 | "androidx.arch.core_core-common": 28, |
| 493 | "androidx.collection_collection": 28, |
| 494 | "androidx.lifecycle_lifecycle-common": 28, |
| 495 | "apache-commons-compress": 29, |
| 496 | "bouncycastle_ike_digests": 30, |
| 497 | "brotli-java": 29, |
| 498 | "captiveportal-lib": 28, |
| 499 | "flatbuffer_headers": 30, |
| 500 | "framework-permission": 30, |
| 501 | "framework-statsd": 30, |
| 502 | "gemmlowp_headers": 30, |
| 503 | "ike-internals": 30, |
| 504 | "kotlinx-coroutines-android": 28, |
| 505 | "kotlinx-coroutines-core": 28, |
| 506 | "libadb_crypto": 30, |
| 507 | "libadb_pairing_auth": 30, |
| 508 | "libadb_pairing_connection": 30, |
| 509 | "libadb_pairing_server": 30, |
| 510 | "libadb_protos": 30, |
| 511 | "libadb_tls_connection": 30, |
| 512 | "libadbconnection_client": 30, |
| 513 | "libadbconnection_server": 30, |
| 514 | "libadbd_core": 30, |
| 515 | "libadbd_services": 30, |
| 516 | "libadbd": 30, |
| 517 | "libapp_processes_protos_lite": 30, |
| 518 | "libasyncio": 30, |
| 519 | "libbrotli": 30, |
| 520 | "libbuildversion": 30, |
| 521 | "libcrypto_static": 30, |
| 522 | "libcrypto_utils": 30, |
| 523 | "libdiagnose_usb": 30, |
| 524 | "libeigen": 30, |
| 525 | "liblz4": 30, |
| 526 | "libmdnssd": 30, |
| 527 | "libneuralnetworks_common": 30, |
| 528 | "libneuralnetworks_headers": 30, |
| 529 | "libneuralnetworks": 30, |
| 530 | "libprocpartition": 30, |
| 531 | "libprotobuf-java-lite": 30, |
| 532 | "libprotoutil": 30, |
| 533 | "libqemu_pipe": 30, |
| 534 | "libstats_jni": 30, |
| 535 | "libstatslog_statsd": 30, |
| 536 | "libstatsmetadata": 30, |
| 537 | "libstatspull": 30, |
| 538 | "libstatssocket": 30, |
| 539 | "libsync": 30, |
| 540 | "libtextclassifier_hash_headers": 30, |
| 541 | "libtextclassifier_hash_static": 30, |
| 542 | "libtflite_kernel_utils": 30, |
| 543 | "libwatchdog": 29, |
| 544 | "libzstd": 30, |
| 545 | "metrics-constants-protos": 28, |
| 546 | "net-utils-framework-common": 29, |
| 547 | "permissioncontroller-statsd": 28, |
| 548 | "philox_random_headers": 30, |
| 549 | "philox_random": 30, |
| 550 | "service-permission": 30, |
| 551 | "service-statsd": 30, |
| 552 | "statsd-aidl-ndk_platform": 30, |
| 553 | "statsd": 30, |
| 554 | "tensorflow_headers": 30, |
| 555 | "xz-java": 29, |
| 556 | } |
| 557 | |
| 558 | // Function called while walking an APEX's payload dependencies. |
| 559 | // |
| 560 | // Return true if the `to` module should be visited, false otherwise. |
| 561 | type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool |
| 562 | |
| 563 | // UpdatableModule represents updatable APEX/APK |
| 564 | type UpdatableModule interface { |
| 565 | Module |
| 566 | WalkPayloadDeps(ctx ModuleContext, do PayloadDepsCallback) |
| 567 | } |
| 568 | |
| 569 | // CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version accordingly |
| 570 | func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion int) { |
| 571 | // do not enforce min_sdk_version for host |
| 572 | if ctx.Host() { |
| 573 | return |
| 574 | } |
| 575 | |
| 576 | // do not enforce for coverage build |
| 577 | if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() { |
| 578 | return |
| 579 | } |
| 580 | |
| 581 | // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version or |
| 582 | // min_sdk_version is not finalized (e.g. current or codenames) |
| 583 | if minSdkVersion == FutureApiLevel { |
| 584 | return |
| 585 | } |
| 586 | |
| 587 | m.WalkPayloadDeps(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool { |
| 588 | if externalDep { |
| 589 | // external deps are outside the payload boundary, which is "stable" interface. |
| 590 | // We don't have to check min_sdk_version for external dependencies. |
| 591 | return false |
| 592 | } |
| 593 | if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) { |
| 594 | return false |
| 595 | } |
| 596 | if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil { |
| 597 | toName := ctx.OtherModuleName(to) |
| 598 | if ver, ok := minSdkVersionAllowlist[toName]; !ok || ver > minSdkVersion { |
| 599 | ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v. Dependency path: %s", |
| 600 | minSdkVersion, ctx.ModuleName(), err.Error(), ctx.GetPathString(false)) |
| 601 | return false |
| 602 | } |
| 603 | } |
| 604 | return true |
| 605 | }) |
| 606 | } |