blob: f857ec692b0b7fd3dda5e94f4cc6b40833439e79 [file] [log] [blame]
Jiyong Park9d452992018-10-03 00:38:19 +09001// 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
15package android
16
Jiyong Park0ddfcd12018-12-11 01:35:25 +090017import (
Jooyung Han03b51852020-02-26 22:45:42 +090018 "fmt"
Colin Crosscefa94bd2019-06-03 15:07:03 -070019 "sort"
Jooyung Han03b51852020-02-26 22:45:42 +090020 "strconv"
Artur Satayev872a1442020-04-27 17:08:37 +010021 "strings"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090022 "sync"
Paul Duffindddd5462020-04-07 15:25:44 +010023
24 "github.com/google/blueprint"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090025)
Jiyong Park25fc6a92018-11-18 18:02:45 +090026
Jooyung Han5417f772020-03-12 18:37:20 +090027const (
28 SdkVersion_Android10 = 29
29)
30
Peter Collingbournedc4f9862020-02-12 17:13:25 -080031type ApexInfo struct {
Colin Crosse07f2312020-08-13 11:24:56 -070032 // Name of the apex variation that this module is mutated into
33 ApexVariationName string
Peter Collingbournedc4f9862020-02-12 17:13:25 -080034
Jooyung Han03b51852020-02-26 22:45:42 +090035 MinSdkVersion int
Ulya Trafimovich7c140d82020-04-22 18:05:58 +010036 Updatable bool
Colin Crossaede88c2020-08-11 12:17:01 -070037 RequiredSdks SdkRefs
38
39 InApexes []string
40}
41
42func (i ApexInfo) mergedName() string {
43 name := "apex" + strconv.Itoa(i.MinSdkVersion)
44 for _, sdk := range i.RequiredSdks {
45 name += "_" + sdk.Name + "_" + sdk.Version
46 }
47 return name
Peter Collingbournedc4f9862020-02-12 17:13:25 -080048}
49
Paul Duffin923e8a52020-03-30 15:33:32 +010050// Extracted from ApexModule to make it easier to define custom subsets of the
51// ApexModule interface and improve code navigation within the IDE.
52type DepIsInSameApex interface {
53 // DepIsInSameApex tests if the other module 'dep' is installed to the same
54 // APEX as this module
55 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
56}
57
Jiyong Park9d452992018-10-03 00:38:19 +090058// ApexModule is the interface that a module type is expected to implement if
59// the module has to be built differently depending on whether the module
60// is destined for an apex or not (installed to one of the regular partitions).
61//
62// Native shared libraries are one such module type; when it is built for an
63// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
64// or C APIs from other APEXs.
65//
66// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090067// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090068// in one or more APEXs. Specifically, if a module is included in apex.foo and
69// apex.bar then three apex variants are created: platform, apex.foo and
70// apex.bar. The platform variant is for the regular partitions
71// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
72// respectively.
73type ApexModule interface {
74 Module
Paul Duffin923e8a52020-03-30 15:33:32 +010075 DepIsInSameApex
76
Jiyong Park9d452992018-10-03 00:38:19 +090077 apexModuleBase() *ApexModuleBase
78
Jooyung Han698dd9f2020-07-22 15:17:19 +090079 // Marks that this module should be built for the specified APEX.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090080 // Call this before apex.apexMutator is run.
Jooyung Han698dd9f2020-07-22 15:17:19 +090081 BuildForApex(apex ApexInfo)
Jiyong Parkf760cae2020-02-12 07:53:12 +090082
Colin Crosse07f2312020-08-13 11:24:56 -070083 // Returns the name of APEX variation that this module will be built for.
Colin Crossaede88c2020-08-11 12:17:01 -070084 // Empty string is returned when 'IsForPlatform() == true'. Note that a
85 // module can beincluded in multiple APEXes, in which case, the module
86 // is mutated into one or more variants, each of which is for one or
87 // more APEXes. This method returns the name of the APEX variation of
88 // the module.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090089 // Call this after apex.apexMutator is run.
Colin Crosse07f2312020-08-13 11:24:56 -070090 ApexVariationName() string
Jiyong Park9d452992018-10-03 00:38:19 +090091
Colin Crossaede88c2020-08-11 12:17:01 -070092 // Returns the name of the APEX modules that this variant of this module
93 // is present in.
94 // Call this after apex.apexMutator is run.
95 InApexes() []string
96
Jiyong Park0ddfcd12018-12-11 01:35:25 +090097 // Tests whether this module will be built for the platform or not.
Colin Crosse07f2312020-08-13 11:24:56 -070098 // This is a shortcut for ApexVariationName() == ""
Jiyong Park0ddfcd12018-12-11 01:35:25 +090099 IsForPlatform() bool
100
101 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +0900102 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900103 // for not creating APEX variants for certain types of shared libraries
104 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +0900105 CanHaveApexVariants() bool
106
107 // Tests if this module can be installed to APEX as a file. For example,
108 // this would return true for shared libs while return false for static
109 // libs.
110 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900111
112 // Mutate this module into one or more variants each of which is built
Jooyung Han698dd9f2020-07-22 15:17:19 +0900113 // for an APEX marked via BuildForApex().
Colin Cross43b92e02019-11-18 15:28:57 -0800114 CreateApexVariations(mctx BottomUpMutatorContext) []Module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900115
Jiyong Park127b40b2019-09-30 16:04:35 +0900116 // Tests if this module is available for the specified APEX or ":platform"
117 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900118
Jiyong Park89e850a2020-04-07 16:37:39 +0900119 // Return true if this module is not available to platform (i.e. apex_available
120 // property doesn't have "//apex_available:platform"), or shouldn't be available
121 // to platform, which is the case when this module depends on other module that
122 // isn't available to platform.
123 NotAvailableForPlatform() bool
124
125 // Mark that this module is not available to platform. Set by the
126 // check-platform-availability mutator in the apex package.
127 SetNotAvailableForPlatform()
128
Jooyung Han75568392020-03-20 04:29:24 +0900129 // Returns the highest version which is <= maxSdkVersion.
130 // For example, with maxSdkVersion is 10 and versionList is [9,11]
131 // it returns 9 as string
132 ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error)
Ulya Trafimovich7c140d82020-04-22 18:05:58 +0100133
134 // Tests if the module comes from an updatable APEX.
135 Updatable() bool
Jiyong Park62304bb2020-04-13 16:19:48 +0900136
137 // List of APEXes that this module tests. The module has access to
138 // the private part of the listed APEXes even when it is not included in the
139 // APEXes.
140 TestFor() []string
Jooyung Han749dc692020-04-15 11:03:39 +0900141
142 // Returns nil if this module supports sdkVersion
143 // Otherwise, returns error with reason
144 ShouldSupportSdkVersion(ctx BaseModuleContext, sdkVersion int) error
Colin Crossaede88c2020-08-11 12:17:01 -0700145
146 // Returns true if this module needs a unique variation per apex, for example if
147 // use_apex_name_macro is set.
148 UniqueApexVariations() bool
149
150 // UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies
151 // that are in the same APEX have unique APEX variations so that the module can link against
152 // the right variant.
153 UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext)
Jiyong Park9d452992018-10-03 00:38:19 +0900154}
155
156type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000157 // Availability of this module in APEXes. Only the listed APEXes can contain
158 // this module. If the module has stubs then other APEXes and the platform may
159 // access it through them (subject to visibility).
160 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900161 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
162 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Yifan Hongd22a84a2020-07-28 17:37:46 -0700163 // "com.android.gki.*" matches any APEX module name with the prefix "com.android.gki.".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900164 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900165 Apex_available []string
166
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800167 Info ApexInfo `blueprint:"mutated"`
Jiyong Park89e850a2020-04-07 16:37:39 +0900168
169 NotAvailableForPlatform bool `blueprint:"mutated"`
Colin Crossaede88c2020-08-11 12:17:01 -0700170
171 UniqueApexVariationsForDeps bool `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900172}
173
Paul Duffindddd5462020-04-07 15:25:44 +0100174// Marker interface that identifies dependencies that are excluded from APEX
175// contents.
176type ExcludeFromApexContentsTag interface {
177 blueprint.DependencyTag
178
179 // Method that differentiates this interface from others.
180 ExcludeFromApexContents()
181}
182
Jiyong Park9d452992018-10-03 00:38:19 +0900183// Provides default implementation for the ApexModule interface. APEX-aware
184// modules are expected to include this struct and call InitApexModule().
185type ApexModuleBase struct {
186 ApexProperties ApexProperties
187
188 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700189
190 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800191 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900192}
193
194func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
195 return m
196}
197
Paul Duffinbefa4b92020-03-04 14:22:45 +0000198func (m *ApexModuleBase) ApexAvailable() []string {
199 return m.ApexProperties.Apex_available
200}
201
Jiyong Park62304bb2020-04-13 16:19:48 +0900202func (m *ApexModuleBase) TestFor() []string {
203 // To be implemented by concrete types inheriting ApexModuleBase
204 return nil
205}
206
Colin Crossaede88c2020-08-11 12:17:01 -0700207func (m *ApexModuleBase) UniqueApexVariations() bool {
208 return false
209}
210
211func (m *ApexModuleBase) UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext) {
212 // anyInSameApex returns true if the two ApexInfo lists contain any values in an InApexes list
213 // in common. It is used instead of DepIsInSameApex because it needs to determine if the dep
214 // is in the same APEX due to being directly included, not only if it is included _because_ it
215 // is a dependency.
216 anyInSameApex := func(a, b []ApexInfo) bool {
217 collectApexes := func(infos []ApexInfo) []string {
218 var ret []string
219 for _, info := range infos {
220 ret = append(ret, info.InApexes...)
221 }
222 return ret
223 }
224
225 aApexes := collectApexes(a)
226 bApexes := collectApexes(b)
227 sort.Strings(bApexes)
228 for _, aApex := range aApexes {
229 index := sort.SearchStrings(bApexes, aApex)
230 if index < len(bApexes) && bApexes[index] == aApex {
231 return true
232 }
233 }
234 return false
235 }
236
237 mctx.VisitDirectDeps(func(dep Module) {
238 if depApexModule, ok := dep.(ApexModule); ok {
239 if anyInSameApex(depApexModule.apexModuleBase().apexVariations, m.apexVariations) &&
240 (depApexModule.UniqueApexVariations() ||
241 depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) {
242 m.ApexProperties.UniqueApexVariationsForDeps = true
243 }
244 }
245 })
246}
247
Jooyung Han698dd9f2020-07-22 15:17:19 +0900248func (m *ApexModuleBase) BuildForApex(apex ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700249 m.apexVariationsLock.Lock()
250 defer m.apexVariationsLock.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900251 for _, v := range m.apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700252 if v.ApexVariationName == apex.ApexVariationName {
Jooyung Han698dd9f2020-07-22 15:17:19 +0900253 return
Jiyong Parkf760cae2020-02-12 07:53:12 +0900254 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900255 }
Jooyung Han698dd9f2020-07-22 15:17:19 +0900256 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900257}
258
Colin Crosse07f2312020-08-13 11:24:56 -0700259func (m *ApexModuleBase) ApexVariationName() string {
260 return m.ApexProperties.Info.ApexVariationName
Jiyong Park9d452992018-10-03 00:38:19 +0900261}
262
Colin Crossaede88c2020-08-11 12:17:01 -0700263func (m *ApexModuleBase) InApexes() []string {
264 return m.ApexProperties.Info.InApexes
265}
266
Jiyong Park9d452992018-10-03 00:38:19 +0900267func (m *ApexModuleBase) IsForPlatform() bool {
Colin Crosse07f2312020-08-13 11:24:56 -0700268 return m.ApexProperties.Info.ApexVariationName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900269}
270
271func (m *ApexModuleBase) CanHaveApexVariants() bool {
272 return m.canHaveApexVariants
273}
274
275func (m *ApexModuleBase) IsInstallableToApex() bool {
276 // should be overriden if needed
277 return false
278}
279
Jiyong Park127b40b2019-09-30 16:04:35 +0900280const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900281 AvailableToPlatform = "//apex_available:platform"
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000282 AvailableToAnyApex = "//apex_available:anyapex"
Yifan Hongd22a84a2020-07-28 17:37:46 -0700283 AvailableToGkiApex = "com.android.gki.*"
Jiyong Park127b40b2019-09-30 16:04:35 +0900284)
285
Jiyong Parka90ca002019-10-07 15:47:24 +0900286func CheckAvailableForApex(what string, apex_available []string) bool {
287 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000288 // apex_available defaults to ["//apex_available:platform"],
289 // which means 'available to the platform but no apexes'.
290 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900291 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900292 return InList(what, apex_available) ||
Yifan Hongd22a84a2020-07-28 17:37:46 -0700293 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) ||
294 (strings.HasPrefix(what, "com.android.gki.") && InList(AvailableToGkiApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900295}
296
297func (m *ApexModuleBase) AvailableFor(what string) bool {
298 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900299}
300
Jiyong Park89e850a2020-04-07 16:37:39 +0900301func (m *ApexModuleBase) NotAvailableForPlatform() bool {
302 return m.ApexProperties.NotAvailableForPlatform
303}
304
305func (m *ApexModuleBase) SetNotAvailableForPlatform() {
306 m.ApexProperties.NotAvailableForPlatform = true
307}
308
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900309func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
310 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
311 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
312 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
313 return true
314}
315
Jooyung Han75568392020-03-20 04:29:24 +0900316func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) {
Jooyung Han03b51852020-02-26 22:45:42 +0900317 for i := range versionList {
318 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
Jooyung Han75568392020-03-20 04:29:24 +0900319 if ver <= maxSdkVersion {
Jooyung Han03b51852020-02-26 22:45:42 +0900320 return versionList[len(versionList)-i-1], nil
321 }
322 }
Jooyung Han75568392020-03-20 04:29:24 +0900323 return "", fmt.Errorf("not found a version(<=%d) in versionList: %v", maxSdkVersion, versionList)
Jooyung Han03b51852020-02-26 22:45:42 +0900324}
325
Jiyong Park127b40b2019-09-30 16:04:35 +0900326func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
327 for _, n := range m.ApexProperties.Apex_available {
Yifan Hongd22a84a2020-07-28 17:37:46 -0700328 if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900329 continue
330 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100331 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900332 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
333 }
334 }
335}
336
Ulya Trafimovich7c140d82020-04-22 18:05:58 +0100337func (m *ApexModuleBase) Updatable() bool {
338 return m.ApexProperties.Info.Updatable
339}
340
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800341type byApexName []ApexInfo
342
343func (a byApexName) Len() int { return len(a) }
344func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
Colin Crosse07f2312020-08-13 11:24:56 -0700345func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800346
Colin Crossaede88c2020-08-11 12:17:01 -0700347// mergeApexVariations deduplicates APEX variations that would build identically into a common
348// variation. It returns the reduced list of variations and a list of aliases from the original
349// variation names to the new variation names.
350func mergeApexVariations(apexVariations []ApexInfo) (merged []ApexInfo, aliases [][2]string) {
351 sort.Sort(byApexName(apexVariations))
352 seen := make(map[string]int)
353 for _, apexInfo := range apexVariations {
354 apexName := apexInfo.ApexVariationName
355 mergedName := apexInfo.mergedName()
356 if index, exists := seen[mergedName]; exists {
357 merged[index].InApexes = append(merged[index].InApexes, apexName)
358 merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable
359 } else {
360 seen[mergedName] = len(merged)
361 apexInfo.ApexVariationName = apexInfo.mergedName()
362 apexInfo.InApexes = CopyOf(apexInfo.InApexes)
363 merged = append(merged, apexInfo)
364 }
365 aliases = append(aliases, [2]string{apexName, mergedName})
366 }
367 return merged, aliases
368}
369
Colin Cross43b92e02019-11-18 15:28:57 -0800370func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900371 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900372 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900373
Colin Crossaede88c2020-08-11 12:17:01 -0700374 var apexVariations []ApexInfo
375 var aliases [][2]string
376 if !mctx.Module().(ApexModule).UniqueApexVariations() && !m.ApexProperties.UniqueApexVariationsForDeps {
377 apexVariations, aliases = mergeApexVariations(m.apexVariations)
378 } else {
379 apexVariations = m.apexVariations
380 }
381
382 sort.Sort(byApexName(apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900383 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900384 variations = append(variations, "") // Original variation for platform
Colin Crossaede88c2020-08-11 12:17:01 -0700385 for _, apex := range apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700386 variations = append(variations, apex.ApexVariationName)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800387 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800388
Jiyong Park3ff16992019-12-27 14:11:47 +0900389 defaultVariation := ""
390 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900391
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900392 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800393 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900394 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800395 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +0100396 // Do not install the module for platform, but still allow it to output
397 // uninstallable AndroidMk entries in certain cases when they have
398 // side effects.
399 mod.MakeUninstallable()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900400 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800401 if !platformVariation {
Colin Crossaede88c2020-08-11 12:17:01 -0700402 mod.(ApexModule).apexModuleBase().ApexProperties.Info = apexVariations[i-1]
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800403 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900404 }
Colin Crossaede88c2020-08-11 12:17:01 -0700405
406 for _, alias := range aliases {
407 mctx.CreateAliasVariation(alias[0], alias[1])
408 }
409
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900410 return modules
411 }
412 return nil
413}
414
415var apexData OncePer
416var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800417var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900418
419// This structure maintains the global mapping in between modules and APEXes.
420// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900421//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900422// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
423// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
424// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
425func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800426 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900427 return make(map[string]map[string]bool)
428 }).(map[string]map[string]bool)
429}
430
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900431// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900432// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900433// is explicitly listed in the build definition of the APEX via properties like
434// native_shared_libs, java_libs, etc.
Jooyung Han698dd9f2020-07-22 15:17:19 +0900435func UpdateApexDependency(apex ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900436 apexNamesMapMutex.Lock()
437 defer apexNamesMapMutex.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900438 apexesForModule, ok := apexNamesMap()[moduleName]
439 if !ok {
440 apexesForModule = make(map[string]bool)
441 apexNamesMap()[moduleName] = apexesForModule
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442 }
Colin Crosse07f2312020-08-13 11:24:56 -0700443 apexesForModule[apex.ApexVariationName] = apexesForModule[apex.ApexVariationName] || directDep
Colin Crossaede88c2020-08-11 12:17:01 -0700444 for _, apexName := range apex.InApexes {
445 apexesForModule[apexName] = apexesForModule[apex.ApexVariationName] || directDep
446 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900447}
448
Jooyung Han671f1ce2019-12-17 12:47:13 +0900449// TODO(b/146393795): remove this when b/146393795 is fixed
450func ClearApexDependency() {
451 m := apexNamesMap()
452 for k := range m {
453 delete(m, k)
454 }
455}
456
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900457// Tests whether a module named moduleName is directly depended on by an APEX
458// named apexName.
459func DirectlyInApex(apexName string, moduleName string) bool {
460 apexNamesMapMutex.Lock()
461 defer apexNamesMapMutex.Unlock()
Colin Crossaede88c2020-08-11 12:17:01 -0700462 if apexNamesForModule, ok := apexNamesMap()[moduleName]; ok {
463 return apexNamesForModule[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900464 }
465 return false
466}
467
Colin Crossaede88c2020-08-11 12:17:01 -0700468// Tests whether a module named moduleName is directly depended on by all APEXes
469// in a list of apexNames.
470func DirectlyInAllApexes(apexNames []string, moduleName string) bool {
471 apexNamesMapMutex.Lock()
472 defer apexNamesMapMutex.Unlock()
473 for _, apexName := range apexNames {
474 apexNamesForModule := apexNamesMap()[moduleName]
475 if !apexNamesForModule[apexName] {
476 return false
477 }
478 }
479 return true
480}
481
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000482type hostContext interface {
483 Host() bool
484}
485
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900486// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000487func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
488 if ctx.Host() {
489 // Host has no APEX.
490 return false
491 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900492 apexNamesMapMutex.Lock()
493 defer apexNamesMapMutex.Unlock()
494 if apexNames, ok := apexNamesMap()[moduleName]; ok {
495 for an := range apexNames {
496 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900497 return true
498 }
499 }
500 }
501 return false
502}
503
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900504// Tests whether a module named module is depended on (including both
505// direct and indirect dependencies) by any APEX.
506func InAnyApex(moduleName string) bool {
507 apexNamesMapMutex.Lock()
508 defer apexNamesMapMutex.Unlock()
509 apexNames, ok := apexNamesMap()[moduleName]
510 return ok && len(apexNames) > 0
511}
512
513func GetApexesForModule(moduleName string) []string {
514 ret := []string{}
515 apexNamesMapMutex.Lock()
516 defer apexNamesMapMutex.Unlock()
517 if apexNames, ok := apexNamesMap()[moduleName]; ok {
518 for an := range apexNames {
519 ret = append(ret, an)
520 }
521 }
522 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900523}
524
Jiyong Park9d452992018-10-03 00:38:19 +0900525func InitApexModule(m ApexModule) {
526 base := m.apexModuleBase()
527 base.canHaveApexVariants = true
528
529 m.AddProperties(&base.ApexProperties)
530}
Artur Satayev872a1442020-04-27 17:08:37 +0100531
532// A dependency info for a single ApexModule, either direct or transitive.
533type ApexModuleDepInfo struct {
534 // Name of the dependency
535 To string
536 // List of dependencies To belongs to. Includes APEX itself, if a direct dependency.
537 From []string
538 // Whether the dependency belongs to the final compiled APEX.
539 IsExternal bool
Artur Satayev480e25b2020-04-27 18:53:18 +0100540 // min_sdk_version of the ApexModule
541 MinSdkVersion string
Artur Satayev872a1442020-04-27 17:08:37 +0100542}
543
544// A map of a dependency name to its ApexModuleDepInfo
545type DepNameToDepInfoMap map[string]ApexModuleDepInfo
546
547type ApexBundleDepsInfo struct {
Jooyung Han98d63e12020-05-14 07:44:03 +0900548 flatListPath OutputPath
549 fullListPath OutputPath
Artur Satayev872a1442020-04-27 17:08:37 +0100550}
551
Artur Satayev849f8442020-04-28 14:57:42 +0100552type ApexBundleDepsInfoIntf interface {
553 Updatable() bool
Artur Satayeva8bd1132020-04-27 18:07:06 +0100554 FlatListPath() Path
Artur Satayev872a1442020-04-27 17:08:37 +0100555 FullListPath() Path
556}
557
Artur Satayeva8bd1132020-04-27 18:07:06 +0100558func (d *ApexBundleDepsInfo) FlatListPath() Path {
559 return d.flatListPath
560}
561
Artur Satayev872a1442020-04-27 17:08:37 +0100562func (d *ApexBundleDepsInfo) FullListPath() Path {
563 return d.fullListPath
564}
565
Artur Satayeva8bd1132020-04-27 18:07:06 +0100566// Generate two module out files:
567// 1. FullList with transitive deps and their parents in the dep graph
568// 2. FlatList with a flat list of transitive deps
Artur Satayev480e25b2020-04-27 18:53:18 +0100569func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) {
Artur Satayeva8bd1132020-04-27 18:07:06 +0100570 var fullContent strings.Builder
571 var flatContent strings.Builder
572
Artur Satayev480e25b2020-04-27 18:53:18 +0100573 fmt.Fprintf(&flatContent, "%s(minSdkVersion:%s):\\n", ctx.ModuleName(), minSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100574 for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
575 info := depInfos[key]
Artur Satayev480e25b2020-04-27 18:53:18 +0100576 toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100577 if info.IsExternal {
578 toName = toName + " (external)"
579 }
Artur Satayeva8bd1132020-04-27 18:07:06 +0100580 fmt.Fprintf(&fullContent, "%s <- %s\\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
581 fmt.Fprintf(&flatContent, " %s\\n", toName)
Artur Satayev872a1442020-04-27 17:08:37 +0100582 }
583
584 d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
585 ctx.Build(pctx, BuildParams{
586 Rule: WriteFile,
587 Description: "Full Dependency Info",
588 Output: d.fullListPath,
589 Args: map[string]string{
Artur Satayeva8bd1132020-04-27 18:07:06 +0100590 "content": fullContent.String(),
591 },
592 })
593
594 d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
595 ctx.Build(pctx, BuildParams{
596 Rule: WriteFile,
597 Description: "Flat Dependency Info",
598 Output: d.flatListPath,
599 Args: map[string]string{
600 "content": flatContent.String(),
Artur Satayev872a1442020-04-27 17:08:37 +0100601 },
602 })
603}
Jooyung Han749dc692020-04-15 11:03:39 +0900604
605// TODO(b/158059172): remove minSdkVersion allowlist
606var minSdkVersionAllowlist = map[string]int{
607 "adbd": 30,
608 "android.net.ipsec.ike": 30,
609 "androidx-constraintlayout_constraintlayout-solver": 30,
610 "androidx.annotation_annotation": 28,
611 "androidx.arch.core_core-common": 28,
612 "androidx.collection_collection": 28,
613 "androidx.lifecycle_lifecycle-common": 28,
614 "apache-commons-compress": 29,
615 "bouncycastle_ike_digests": 30,
616 "brotli-java": 29,
617 "captiveportal-lib": 28,
618 "flatbuffer_headers": 30,
619 "framework-permission": 30,
620 "framework-statsd": 30,
621 "gemmlowp_headers": 30,
622 "ike-internals": 30,
623 "kotlinx-coroutines-android": 28,
624 "kotlinx-coroutines-core": 28,
625 "libadb_crypto": 30,
626 "libadb_pairing_auth": 30,
627 "libadb_pairing_connection": 30,
628 "libadb_pairing_server": 30,
629 "libadb_protos": 30,
630 "libadb_tls_connection": 30,
631 "libadbconnection_client": 30,
632 "libadbconnection_server": 30,
633 "libadbd_core": 30,
634 "libadbd_services": 30,
635 "libadbd": 30,
636 "libapp_processes_protos_lite": 30,
637 "libasyncio": 30,
638 "libbrotli": 30,
639 "libbuildversion": 30,
640 "libcrypto_static": 30,
641 "libcrypto_utils": 30,
642 "libdiagnose_usb": 30,
643 "libeigen": 30,
644 "liblz4": 30,
645 "libmdnssd": 30,
646 "libneuralnetworks_common": 30,
647 "libneuralnetworks_headers": 30,
648 "libneuralnetworks": 30,
649 "libprocpartition": 30,
650 "libprotobuf-java-lite": 30,
651 "libprotoutil": 30,
652 "libqemu_pipe": 30,
653 "libstats_jni": 30,
654 "libstatslog_statsd": 30,
655 "libstatsmetadata": 30,
656 "libstatspull": 30,
657 "libstatssocket": 30,
658 "libsync": 30,
659 "libtextclassifier_hash_headers": 30,
660 "libtextclassifier_hash_static": 30,
661 "libtflite_kernel_utils": 30,
662 "libwatchdog": 29,
663 "libzstd": 30,
664 "metrics-constants-protos": 28,
665 "net-utils-framework-common": 29,
666 "permissioncontroller-statsd": 28,
667 "philox_random_headers": 30,
668 "philox_random": 30,
669 "service-permission": 30,
670 "service-statsd": 30,
671 "statsd-aidl-ndk_platform": 30,
672 "statsd": 30,
673 "tensorflow_headers": 30,
674 "xz-java": 29,
675}
676
677// Function called while walking an APEX's payload dependencies.
678//
679// Return true if the `to` module should be visited, false otherwise.
680type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool
681
682// UpdatableModule represents updatable APEX/APK
683type UpdatableModule interface {
684 Module
685 WalkPayloadDeps(ctx ModuleContext, do PayloadDepsCallback)
686}
687
688// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version accordingly
689func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion int) {
690 // do not enforce min_sdk_version for host
691 if ctx.Host() {
692 return
693 }
694
695 // do not enforce for coverage build
696 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() {
697 return
698 }
699
700 // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version or
701 // min_sdk_version is not finalized (e.g. current or codenames)
702 if minSdkVersion == FutureApiLevel {
703 return
704 }
705
706 m.WalkPayloadDeps(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool {
707 if externalDep {
708 // external deps are outside the payload boundary, which is "stable" interface.
709 // We don't have to check min_sdk_version for external dependencies.
710 return false
711 }
712 if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
713 return false
714 }
715 if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil {
716 toName := ctx.OtherModuleName(to)
717 if ver, ok := minSdkVersionAllowlist[toName]; !ok || ver > minSdkVersion {
718 ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v. Dependency path: %s",
719 minSdkVersion, ctx.ModuleName(), err.Error(), ctx.GetPathString(false))
720 return false
721 }
722 }
723 return true
724 })
725}