blob: 83a7fe1f4b0c7bf6dda14b8f8ac49472b4ba3353 [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 Crossd6b25252020-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 Crossd6b25252020-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 Crossd6b25252020-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 Crossd6b25252020-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 m.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 Crossd6b25252020-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 Crossd6b25252020-08-11 12:17:01 -0700207func (m *ApexModuleBase) UniqueApexVariations() bool {
208 return false
209}
210
211func (m *ApexModuleBase) UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext) {
212 mctx.VisitDirectDeps(func(dep Module) {
213 if depApexModule, ok := dep.(ApexModule); ok {
214 if depApexModule.DepIsInSameApex(mctx, depApexModule) &&
215 (depApexModule.UniqueApexVariations() ||
216 depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) {
217 m.ApexProperties.UniqueApexVariationsForDeps = true
218 }
219 }
220 })
221}
222
Jooyung Han698dd9f2020-07-22 15:17:19 +0900223func (m *ApexModuleBase) BuildForApex(apex ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700224 m.apexVariationsLock.Lock()
225 defer m.apexVariationsLock.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900226 for _, v := range m.apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700227 if v.ApexVariationName == apex.ApexVariationName {
Jooyung Han698dd9f2020-07-22 15:17:19 +0900228 return
Jiyong Parkf760cae2020-02-12 07:53:12 +0900229 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900230 }
Jooyung Han698dd9f2020-07-22 15:17:19 +0900231 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900232}
233
Colin Crosse07f2312020-08-13 11:24:56 -0700234func (m *ApexModuleBase) ApexVariationName() string {
235 return m.ApexProperties.Info.ApexVariationName
Jiyong Park9d452992018-10-03 00:38:19 +0900236}
237
Colin Crossd6b25252020-08-11 12:17:01 -0700238func (m *ApexModuleBase) InApexes() []string {
239 return m.ApexProperties.Info.InApexes
240}
241
Jiyong Park9d452992018-10-03 00:38:19 +0900242func (m *ApexModuleBase) IsForPlatform() bool {
Colin Crosse07f2312020-08-13 11:24:56 -0700243 return m.ApexProperties.Info.ApexVariationName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900244}
245
246func (m *ApexModuleBase) CanHaveApexVariants() bool {
247 return m.canHaveApexVariants
248}
249
250func (m *ApexModuleBase) IsInstallableToApex() bool {
251 // should be overriden if needed
252 return false
253}
254
Jiyong Park127b40b2019-09-30 16:04:35 +0900255const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900256 AvailableToPlatform = "//apex_available:platform"
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000257 AvailableToAnyApex = "//apex_available:anyapex"
Yifan Hongd22a84a2020-07-28 17:37:46 -0700258 AvailableToGkiApex = "com.android.gki.*"
Jiyong Park127b40b2019-09-30 16:04:35 +0900259)
260
Jiyong Parka90ca002019-10-07 15:47:24 +0900261func CheckAvailableForApex(what string, apex_available []string) bool {
262 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000263 // apex_available defaults to ["//apex_available:platform"],
264 // which means 'available to the platform but no apexes'.
265 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900266 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900267 return InList(what, apex_available) ||
Yifan Hongd22a84a2020-07-28 17:37:46 -0700268 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) ||
269 (strings.HasPrefix(what, "com.android.gki.") && InList(AvailableToGkiApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900270}
271
272func (m *ApexModuleBase) AvailableFor(what string) bool {
273 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900274}
275
Jiyong Park89e850a2020-04-07 16:37:39 +0900276func (m *ApexModuleBase) NotAvailableForPlatform() bool {
277 return m.ApexProperties.NotAvailableForPlatform
278}
279
280func (m *ApexModuleBase) SetNotAvailableForPlatform() {
281 m.ApexProperties.NotAvailableForPlatform = true
282}
283
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900284func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
285 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
286 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
287 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
288 return true
289}
290
Jooyung Han75568392020-03-20 04:29:24 +0900291func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) {
Jooyung Han03b51852020-02-26 22:45:42 +0900292 for i := range versionList {
293 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
Jooyung Han75568392020-03-20 04:29:24 +0900294 if ver <= maxSdkVersion {
Jooyung Han03b51852020-02-26 22:45:42 +0900295 return versionList[len(versionList)-i-1], nil
296 }
297 }
Jooyung Han75568392020-03-20 04:29:24 +0900298 return "", fmt.Errorf("not found a version(<=%d) in versionList: %v", maxSdkVersion, versionList)
Jooyung Han03b51852020-02-26 22:45:42 +0900299}
300
Jiyong Park127b40b2019-09-30 16:04:35 +0900301func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
302 for _, n := range m.ApexProperties.Apex_available {
Yifan Hongd22a84a2020-07-28 17:37:46 -0700303 if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900304 continue
305 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100306 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900307 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
308 }
309 }
310}
311
Ulya Trafimovich7c140d82020-04-22 18:05:58 +0100312func (m *ApexModuleBase) Updatable() bool {
313 return m.ApexProperties.Info.Updatable
314}
315
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800316type byApexName []ApexInfo
317
318func (a byApexName) Len() int { return len(a) }
319func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
Colin Crosse07f2312020-08-13 11:24:56 -0700320func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800321
Colin Crossd6b25252020-08-11 12:17:01 -0700322// mergeApexVariations deduplicates APEX variations that would build identically into a common
323// variation. It returns the reduced list of variations and a list of aliases from the original
324// variation names to the new variation names.
325func mergeApexVariations(apexVariations []ApexInfo) (merged []ApexInfo, aliases [][2]string) {
326 sort.Sort(byApexName(apexVariations))
327 seen := make(map[string]int)
328 for _, apexInfo := range apexVariations {
329 apexName := apexInfo.ApexVariationName
330 mergedName := apexInfo.mergedName()
331 if index, exists := seen[mergedName]; exists {
332 merged[index].InApexes = append(merged[index].InApexes, apexName)
333 merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable
334 } else {
335 seen[mergedName] = len(merged)
336 apexInfo.ApexVariationName = apexInfo.mergedName()
337 apexInfo.InApexes = CopyOf(apexInfo.InApexes)
338 merged = append(merged, apexInfo)
339 }
340 aliases = append(aliases, [2]string{apexName, mergedName})
341 }
342 return merged, aliases
343}
344
Colin Cross43b92e02019-11-18 15:28:57 -0800345func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900346 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900347 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900348
Colin Crossd6b25252020-08-11 12:17:01 -0700349 var apexVariations []ApexInfo
350 var aliases [][2]string
351 if !mctx.Module().(ApexModule).UniqueApexVariations() && !m.ApexProperties.UniqueApexVariationsForDeps {
352 apexVariations, aliases = mergeApexVariations(m.apexVariations)
353 } else {
354 apexVariations = m.apexVariations
355 }
356
357 sort.Sort(byApexName(apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900358 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900359 variations = append(variations, "") // Original variation for platform
Colin Crossd6b25252020-08-11 12:17:01 -0700360 for _, apex := range apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700361 variations = append(variations, apex.ApexVariationName)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800362 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800363
Jiyong Park3ff16992019-12-27 14:11:47 +0900364 defaultVariation := ""
365 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900366
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900367 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800368 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900369 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800370 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +0100371 // Do not install the module for platform, but still allow it to output
372 // uninstallable AndroidMk entries in certain cases when they have
373 // side effects.
374 mod.MakeUninstallable()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900375 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800376 if !platformVariation {
Colin Crossd6b25252020-08-11 12:17:01 -0700377 mod.(ApexModule).apexModuleBase().ApexProperties.Info = apexVariations[i-1]
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800378 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900379 }
Colin Crossd6b25252020-08-11 12:17:01 -0700380
381 for _, alias := range aliases {
382 mctx.CreateAliasVariation(alias[0], alias[1])
383 }
384
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900385 return modules
386 }
387 return nil
388}
389
390var apexData OncePer
391var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800392var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900393
394// This structure maintains the global mapping in between modules and APEXes.
395// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900396//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900397// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
398// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
399// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
400func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800401 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900402 return make(map[string]map[string]bool)
403 }).(map[string]map[string]bool)
404}
405
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900406// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900407// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900408// is explicitly listed in the build definition of the APEX via properties like
409// native_shared_libs, java_libs, etc.
Jooyung Han698dd9f2020-07-22 15:17:19 +0900410func UpdateApexDependency(apex ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900411 apexNamesMapMutex.Lock()
412 defer apexNamesMapMutex.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900413 apexesForModule, ok := apexNamesMap()[moduleName]
414 if !ok {
415 apexesForModule = make(map[string]bool)
416 apexNamesMap()[moduleName] = apexesForModule
Jiyong Park25fc6a92018-11-18 18:02:45 +0900417 }
Colin Crosse07f2312020-08-13 11:24:56 -0700418 apexesForModule[apex.ApexVariationName] = apexesForModule[apex.ApexVariationName] || directDep
Colin Crossd6b25252020-08-11 12:17:01 -0700419 for _, apexName := range apex.InApexes {
420 apexesForModule[apexName] = apexesForModule[apex.ApexVariationName] || directDep
421 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900422}
423
Jooyung Han671f1ce2019-12-17 12:47:13 +0900424// TODO(b/146393795): remove this when b/146393795 is fixed
425func ClearApexDependency() {
426 m := apexNamesMap()
427 for k := range m {
428 delete(m, k)
429 }
430}
431
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900432// Tests whether a module named moduleName is directly depended on by an APEX
433// named apexName.
434func DirectlyInApex(apexName string, moduleName string) bool {
435 apexNamesMapMutex.Lock()
436 defer apexNamesMapMutex.Unlock()
Colin Crossd6b25252020-08-11 12:17:01 -0700437 if apexNamesForModule, ok := apexNamesMap()[moduleName]; ok {
438 return apexNamesForModule[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900439 }
440 return false
441}
442
Colin Crossd6b25252020-08-11 12:17:01 -0700443// Tests whether a module named moduleName is directly depended on by all APEXes
444// in a list of apexNames.
445func DirectlyInAllApexes(apexNames []string, moduleName string) bool {
446 apexNamesMapMutex.Lock()
447 defer apexNamesMapMutex.Unlock()
448 for _, apexName := range apexNames {
449 apexNamesForModule := apexNamesMap()[moduleName]
450 if !apexNamesForModule[apexName] {
451 return false
452 }
453 }
454 return true
455}
456
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000457type hostContext interface {
458 Host() bool
459}
460
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900461// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000462func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
463 if ctx.Host() {
464 // Host has no APEX.
465 return false
466 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900467 apexNamesMapMutex.Lock()
468 defer apexNamesMapMutex.Unlock()
469 if apexNames, ok := apexNamesMap()[moduleName]; ok {
470 for an := range apexNames {
471 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900472 return true
473 }
474 }
475 }
476 return false
477}
478
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900479// Tests whether a module named module is depended on (including both
480// direct and indirect dependencies) by any APEX.
481func InAnyApex(moduleName string) bool {
482 apexNamesMapMutex.Lock()
483 defer apexNamesMapMutex.Unlock()
484 apexNames, ok := apexNamesMap()[moduleName]
485 return ok && len(apexNames) > 0
486}
487
488func GetApexesForModule(moduleName string) []string {
489 ret := []string{}
490 apexNamesMapMutex.Lock()
491 defer apexNamesMapMutex.Unlock()
492 if apexNames, ok := apexNamesMap()[moduleName]; ok {
493 for an := range apexNames {
494 ret = append(ret, an)
495 }
496 }
497 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900498}
499
Jiyong Park9d452992018-10-03 00:38:19 +0900500func InitApexModule(m ApexModule) {
501 base := m.apexModuleBase()
502 base.canHaveApexVariants = true
503
504 m.AddProperties(&base.ApexProperties)
505}
Artur Satayev872a1442020-04-27 17:08:37 +0100506
507// A dependency info for a single ApexModule, either direct or transitive.
508type ApexModuleDepInfo struct {
509 // Name of the dependency
510 To string
511 // List of dependencies To belongs to. Includes APEX itself, if a direct dependency.
512 From []string
513 // Whether the dependency belongs to the final compiled APEX.
514 IsExternal bool
Artur Satayev480e25b2020-04-27 18:53:18 +0100515 // min_sdk_version of the ApexModule
516 MinSdkVersion string
Artur Satayev872a1442020-04-27 17:08:37 +0100517}
518
519// A map of a dependency name to its ApexModuleDepInfo
520type DepNameToDepInfoMap map[string]ApexModuleDepInfo
521
522type ApexBundleDepsInfo struct {
Jooyung Han98d63e12020-05-14 07:44:03 +0900523 flatListPath OutputPath
524 fullListPath OutputPath
Artur Satayev872a1442020-04-27 17:08:37 +0100525}
526
Artur Satayev849f8442020-04-28 14:57:42 +0100527type ApexBundleDepsInfoIntf interface {
528 Updatable() bool
Artur Satayeva8bd1132020-04-27 18:07:06 +0100529 FlatListPath() Path
Artur Satayev872a1442020-04-27 17:08:37 +0100530 FullListPath() Path
531}
532
Artur Satayeva8bd1132020-04-27 18:07:06 +0100533func (d *ApexBundleDepsInfo) FlatListPath() Path {
534 return d.flatListPath
535}
536
Artur Satayev872a1442020-04-27 17:08:37 +0100537func (d *ApexBundleDepsInfo) FullListPath() Path {
538 return d.fullListPath
539}
540
Artur Satayeva8bd1132020-04-27 18:07:06 +0100541// Generate two module out files:
542// 1. FullList with transitive deps and their parents in the dep graph
543// 2. FlatList with a flat list of transitive deps
Artur Satayev480e25b2020-04-27 18:53:18 +0100544func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) {
Artur Satayeva8bd1132020-04-27 18:07:06 +0100545 var fullContent strings.Builder
546 var flatContent strings.Builder
547
Artur Satayev480e25b2020-04-27 18:53:18 +0100548 fmt.Fprintf(&flatContent, "%s(minSdkVersion:%s):\\n", ctx.ModuleName(), minSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100549 for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
550 info := depInfos[key]
Artur Satayev480e25b2020-04-27 18:53:18 +0100551 toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100552 if info.IsExternal {
553 toName = toName + " (external)"
554 }
Artur Satayeva8bd1132020-04-27 18:07:06 +0100555 fmt.Fprintf(&fullContent, "%s <- %s\\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
556 fmt.Fprintf(&flatContent, " %s\\n", toName)
Artur Satayev872a1442020-04-27 17:08:37 +0100557 }
558
559 d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
560 ctx.Build(pctx, BuildParams{
561 Rule: WriteFile,
562 Description: "Full Dependency Info",
563 Output: d.fullListPath,
564 Args: map[string]string{
Artur Satayeva8bd1132020-04-27 18:07:06 +0100565 "content": fullContent.String(),
566 },
567 })
568
569 d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
570 ctx.Build(pctx, BuildParams{
571 Rule: WriteFile,
572 Description: "Flat Dependency Info",
573 Output: d.flatListPath,
574 Args: map[string]string{
575 "content": flatContent.String(),
Artur Satayev872a1442020-04-27 17:08:37 +0100576 },
577 })
578}
Jooyung Han749dc692020-04-15 11:03:39 +0900579
580// TODO(b/158059172): remove minSdkVersion allowlist
581var minSdkVersionAllowlist = map[string]int{
582 "adbd": 30,
583 "android.net.ipsec.ike": 30,
584 "androidx-constraintlayout_constraintlayout-solver": 30,
585 "androidx.annotation_annotation": 28,
586 "androidx.arch.core_core-common": 28,
587 "androidx.collection_collection": 28,
588 "androidx.lifecycle_lifecycle-common": 28,
589 "apache-commons-compress": 29,
590 "bouncycastle_ike_digests": 30,
591 "brotli-java": 29,
592 "captiveportal-lib": 28,
593 "flatbuffer_headers": 30,
594 "framework-permission": 30,
595 "framework-statsd": 30,
596 "gemmlowp_headers": 30,
597 "ike-internals": 30,
598 "kotlinx-coroutines-android": 28,
599 "kotlinx-coroutines-core": 28,
600 "libadb_crypto": 30,
601 "libadb_pairing_auth": 30,
602 "libadb_pairing_connection": 30,
603 "libadb_pairing_server": 30,
604 "libadb_protos": 30,
605 "libadb_tls_connection": 30,
606 "libadbconnection_client": 30,
607 "libadbconnection_server": 30,
608 "libadbd_core": 30,
609 "libadbd_services": 30,
610 "libadbd": 30,
611 "libapp_processes_protos_lite": 30,
612 "libasyncio": 30,
613 "libbrotli": 30,
614 "libbuildversion": 30,
615 "libcrypto_static": 30,
616 "libcrypto_utils": 30,
617 "libdiagnose_usb": 30,
618 "libeigen": 30,
619 "liblz4": 30,
620 "libmdnssd": 30,
621 "libneuralnetworks_common": 30,
622 "libneuralnetworks_headers": 30,
623 "libneuralnetworks": 30,
624 "libprocpartition": 30,
625 "libprotobuf-java-lite": 30,
626 "libprotoutil": 30,
627 "libqemu_pipe": 30,
628 "libstats_jni": 30,
629 "libstatslog_statsd": 30,
630 "libstatsmetadata": 30,
631 "libstatspull": 30,
632 "libstatssocket": 30,
633 "libsync": 30,
634 "libtextclassifier_hash_headers": 30,
635 "libtextclassifier_hash_static": 30,
636 "libtflite_kernel_utils": 30,
637 "libwatchdog": 29,
638 "libzstd": 30,
639 "metrics-constants-protos": 28,
640 "net-utils-framework-common": 29,
641 "permissioncontroller-statsd": 28,
642 "philox_random_headers": 30,
643 "philox_random": 30,
644 "service-permission": 30,
645 "service-statsd": 30,
646 "statsd-aidl-ndk_platform": 30,
647 "statsd": 30,
648 "tensorflow_headers": 30,
649 "xz-java": 29,
650}
651
652// Function called while walking an APEX's payload dependencies.
653//
654// Return true if the `to` module should be visited, false otherwise.
655type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool
656
657// UpdatableModule represents updatable APEX/APK
658type UpdatableModule interface {
659 Module
660 WalkPayloadDeps(ctx ModuleContext, do PayloadDepsCallback)
661}
662
663// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version accordingly
664func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion int) {
665 // do not enforce min_sdk_version for host
666 if ctx.Host() {
667 return
668 }
669
670 // do not enforce for coverage build
671 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() {
672 return
673 }
674
675 // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version or
676 // min_sdk_version is not finalized (e.g. current or codenames)
677 if minSdkVersion == FutureApiLevel {
678 return
679 }
680
681 m.WalkPayloadDeps(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool {
682 if externalDep {
683 // external deps are outside the payload boundary, which is "stable" interface.
684 // We don't have to check min_sdk_version for external dependencies.
685 return false
686 }
687 if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
688 return false
689 }
690 if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil {
691 toName := ctx.OtherModuleName(to)
692 if ver, ok := minSdkVersionAllowlist[toName]; !ok || ver > minSdkVersion {
693 ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v. Dependency path: %s",
694 minSdkVersion, ctx.ModuleName(), err.Error(), ctx.GetPathString(false))
695 return false
696 }
697 }
698 return true
699 })
700}