blob: 3cc663bab416433bc7bb0c17e20b9e24beeb3990 [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
Dan Albertc8060532020-07-22 22:32:17 -070027var (
28 SdkVersion_Android10 = uncheckedFinalApiLevel(29)
Jooyung Han5417f772020-03-12 18:37:20 +090029)
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
Dan Albertc8060532020-07-22 22:32:17 -070035 // Serialized ApiLevel. Use via MinSdkVersion() method. Cannot be stored in
36 // its struct form because this is cloned into properties structs, and
37 // ApiLevel has private members.
38 MinSdkVersionStr string
39 Updatable bool
40 RequiredSdks SdkRefs
Colin Crossaede88c2020-08-11 12:17:01 -070041
42 InApexes []string
43}
44
Dan Albertc8060532020-07-22 22:32:17 -070045func (i ApexInfo) mergedName(ctx EarlyModuleContext) string {
46 name := "apex" + strconv.Itoa(i.MinSdkVersion(ctx).FinalOrFutureInt())
Colin Crossaede88c2020-08-11 12:17:01 -070047 for _, sdk := range i.RequiredSdks {
48 name += "_" + sdk.Name + "_" + sdk.Version
49 }
50 return name
Peter Collingbournedc4f9862020-02-12 17:13:25 -080051}
52
Dan Albertc8060532020-07-22 22:32:17 -070053func (this *ApexInfo) MinSdkVersion(ctx EarlyModuleContext) ApiLevel {
54 return ApiLevelOrPanic(ctx, this.MinSdkVersionStr)
55}
56
Paul Duffin923e8a52020-03-30 15:33:32 +010057// Extracted from ApexModule to make it easier to define custom subsets of the
58// ApexModule interface and improve code navigation within the IDE.
59type DepIsInSameApex interface {
60 // DepIsInSameApex tests if the other module 'dep' is installed to the same
61 // APEX as this module
62 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
63}
64
Jiyong Park9d452992018-10-03 00:38:19 +090065// ApexModule is the interface that a module type is expected to implement if
66// the module has to be built differently depending on whether the module
67// is destined for an apex or not (installed to one of the regular partitions).
68//
69// Native shared libraries are one such module type; when it is built for an
70// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
71// or C APIs from other APEXs.
72//
73// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090074// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090075// in one or more APEXs. Specifically, if a module is included in apex.foo and
76// apex.bar then three apex variants are created: platform, apex.foo and
77// apex.bar. The platform variant is for the regular partitions
78// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
79// respectively.
80type ApexModule interface {
81 Module
Paul Duffin923e8a52020-03-30 15:33:32 +010082 DepIsInSameApex
83
Jiyong Park9d452992018-10-03 00:38:19 +090084 apexModuleBase() *ApexModuleBase
85
Jooyung Han698dd9f2020-07-22 15:17:19 +090086 // Marks that this module should be built for the specified APEX.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090087 // Call this before apex.apexMutator is run.
Jooyung Han698dd9f2020-07-22 15:17:19 +090088 BuildForApex(apex ApexInfo)
Jiyong Parkf760cae2020-02-12 07:53:12 +090089
Colin Crosse07f2312020-08-13 11:24:56 -070090 // Returns the name of APEX variation that this module will be built for.
Colin Crossaede88c2020-08-11 12:17:01 -070091 // Empty string is returned when 'IsForPlatform() == true'. Note that a
92 // module can beincluded in multiple APEXes, in which case, the module
93 // is mutated into one or more variants, each of which is for one or
94 // more APEXes. This method returns the name of the APEX variation of
95 // the module.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090096 // Call this after apex.apexMutator is run.
Colin Crosse07f2312020-08-13 11:24:56 -070097 ApexVariationName() string
Jiyong Park9d452992018-10-03 00:38:19 +090098
Colin Crossaede88c2020-08-11 12:17:01 -070099 // Returns the name of the APEX modules that this variant of this module
100 // is present in.
101 // Call this after apex.apexMutator is run.
102 InApexes() []string
103
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900104 // Tests whether this module will be built for the platform or not.
Colin Crosse07f2312020-08-13 11:24:56 -0700105 // This is a shortcut for ApexVariationName() == ""
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900106 IsForPlatform() bool
107
108 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +0900109 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900110 // for not creating APEX variants for certain types of shared libraries
111 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +0900112 CanHaveApexVariants() bool
113
114 // Tests if this module can be installed to APEX as a file. For example,
115 // this would return true for shared libs while return false for static
116 // libs.
117 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900118
119 // Mutate this module into one or more variants each of which is built
Jooyung Han698dd9f2020-07-22 15:17:19 +0900120 // for an APEX marked via BuildForApex().
Colin Cross43b92e02019-11-18 15:28:57 -0800121 CreateApexVariations(mctx BottomUpMutatorContext) []Module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900122
Jiyong Park127b40b2019-09-30 16:04:35 +0900123 // Tests if this module is available for the specified APEX or ":platform"
124 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900125
Jiyong Park89e850a2020-04-07 16:37:39 +0900126 // Return true if this module is not available to platform (i.e. apex_available
127 // property doesn't have "//apex_available:platform"), or shouldn't be available
128 // to platform, which is the case when this module depends on other module that
129 // isn't available to platform.
130 NotAvailableForPlatform() bool
131
132 // Mark that this module is not available to platform. Set by the
133 // check-platform-availability mutator in the apex package.
134 SetNotAvailableForPlatform()
135
Jooyung Han75568392020-03-20 04:29:24 +0900136 // Returns the highest version which is <= maxSdkVersion.
137 // For example, with maxSdkVersion is 10 and versionList is [9,11]
138 // it returns 9 as string
139 ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error)
Ulya Trafimovich7c140d82020-04-22 18:05:58 +0100140
141 // Tests if the module comes from an updatable APEX.
142 Updatable() bool
Jiyong Park62304bb2020-04-13 16:19:48 +0900143
144 // List of APEXes that this module tests. The module has access to
145 // the private part of the listed APEXes even when it is not included in the
146 // APEXes.
147 TestFor() []string
Jooyung Han749dc692020-04-15 11:03:39 +0900148
149 // Returns nil if this module supports sdkVersion
150 // Otherwise, returns error with reason
Dan Albertc8060532020-07-22 22:32:17 -0700151 ShouldSupportSdkVersion(ctx BaseModuleContext, sdkVersion ApiLevel) error
Colin Crossaede88c2020-08-11 12:17:01 -0700152
153 // Returns true if this module needs a unique variation per apex, for example if
154 // use_apex_name_macro is set.
155 UniqueApexVariations() bool
156
157 // UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies
158 // that are in the same APEX have unique APEX variations so that the module can link against
159 // the right variant.
160 UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext)
Jiyong Park9d452992018-10-03 00:38:19 +0900161}
162
163type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000164 // Availability of this module in APEXes. Only the listed APEXes can contain
165 // this module. If the module has stubs then other APEXes and the platform may
166 // access it through them (subject to visibility).
167 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900168 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
169 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Yifan Hongd22a84a2020-07-28 17:37:46 -0700170 // "com.android.gki.*" matches any APEX module name with the prefix "com.android.gki.".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900171 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900172 Apex_available []string
173
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800174 Info ApexInfo `blueprint:"mutated"`
Jiyong Park89e850a2020-04-07 16:37:39 +0900175
176 NotAvailableForPlatform bool `blueprint:"mutated"`
Colin Crossaede88c2020-08-11 12:17:01 -0700177
178 UniqueApexVariationsForDeps bool `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900179}
180
Paul Duffindddd5462020-04-07 15:25:44 +0100181// Marker interface that identifies dependencies that are excluded from APEX
182// contents.
183type ExcludeFromApexContentsTag interface {
184 blueprint.DependencyTag
185
186 // Method that differentiates this interface from others.
187 ExcludeFromApexContents()
188}
189
Jiyong Park9d452992018-10-03 00:38:19 +0900190// Provides default implementation for the ApexModule interface. APEX-aware
191// modules are expected to include this struct and call InitApexModule().
192type ApexModuleBase struct {
193 ApexProperties ApexProperties
194
195 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700196
197 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800198 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900199}
200
201func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
202 return m
203}
204
Paul Duffinbefa4b92020-03-04 14:22:45 +0000205func (m *ApexModuleBase) ApexAvailable() []string {
206 return m.ApexProperties.Apex_available
207}
208
Jiyong Park62304bb2020-04-13 16:19:48 +0900209func (m *ApexModuleBase) TestFor() []string {
210 // To be implemented by concrete types inheriting ApexModuleBase
211 return nil
212}
213
Colin Crossaede88c2020-08-11 12:17:01 -0700214func (m *ApexModuleBase) UniqueApexVariations() bool {
215 return false
216}
217
218func (m *ApexModuleBase) UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext) {
219 // anyInSameApex returns true if the two ApexInfo lists contain any values in an InApexes list
220 // in common. It is used instead of DepIsInSameApex because it needs to determine if the dep
221 // is in the same APEX due to being directly included, not only if it is included _because_ it
222 // is a dependency.
223 anyInSameApex := func(a, b []ApexInfo) bool {
224 collectApexes := func(infos []ApexInfo) []string {
225 var ret []string
226 for _, info := range infos {
227 ret = append(ret, info.InApexes...)
228 }
229 return ret
230 }
231
232 aApexes := collectApexes(a)
233 bApexes := collectApexes(b)
234 sort.Strings(bApexes)
235 for _, aApex := range aApexes {
236 index := sort.SearchStrings(bApexes, aApex)
237 if index < len(bApexes) && bApexes[index] == aApex {
238 return true
239 }
240 }
241 return false
242 }
243
244 mctx.VisitDirectDeps(func(dep Module) {
245 if depApexModule, ok := dep.(ApexModule); ok {
246 if anyInSameApex(depApexModule.apexModuleBase().apexVariations, m.apexVariations) &&
247 (depApexModule.UniqueApexVariations() ||
248 depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) {
249 m.ApexProperties.UniqueApexVariationsForDeps = true
250 }
251 }
252 })
253}
254
Jooyung Han698dd9f2020-07-22 15:17:19 +0900255func (m *ApexModuleBase) BuildForApex(apex ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700256 m.apexVariationsLock.Lock()
257 defer m.apexVariationsLock.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900258 for _, v := range m.apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700259 if v.ApexVariationName == apex.ApexVariationName {
Jooyung Han698dd9f2020-07-22 15:17:19 +0900260 return
Jiyong Parkf760cae2020-02-12 07:53:12 +0900261 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900262 }
Jooyung Han698dd9f2020-07-22 15:17:19 +0900263 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900264}
265
Colin Crosse07f2312020-08-13 11:24:56 -0700266func (m *ApexModuleBase) ApexVariationName() string {
267 return m.ApexProperties.Info.ApexVariationName
Jiyong Park9d452992018-10-03 00:38:19 +0900268}
269
Colin Crossaede88c2020-08-11 12:17:01 -0700270func (m *ApexModuleBase) InApexes() []string {
271 return m.ApexProperties.Info.InApexes
272}
273
Jiyong Park9d452992018-10-03 00:38:19 +0900274func (m *ApexModuleBase) IsForPlatform() bool {
Colin Crosse07f2312020-08-13 11:24:56 -0700275 return m.ApexProperties.Info.ApexVariationName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900276}
277
278func (m *ApexModuleBase) CanHaveApexVariants() bool {
279 return m.canHaveApexVariants
280}
281
282func (m *ApexModuleBase) IsInstallableToApex() bool {
283 // should be overriden if needed
284 return false
285}
286
Jiyong Park127b40b2019-09-30 16:04:35 +0900287const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900288 AvailableToPlatform = "//apex_available:platform"
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000289 AvailableToAnyApex = "//apex_available:anyapex"
Yifan Hongd22a84a2020-07-28 17:37:46 -0700290 AvailableToGkiApex = "com.android.gki.*"
Jiyong Park127b40b2019-09-30 16:04:35 +0900291)
292
Jiyong Parka90ca002019-10-07 15:47:24 +0900293func CheckAvailableForApex(what string, apex_available []string) bool {
294 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000295 // apex_available defaults to ["//apex_available:platform"],
296 // which means 'available to the platform but no apexes'.
297 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900298 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900299 return InList(what, apex_available) ||
Yifan Hongd22a84a2020-07-28 17:37:46 -0700300 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) ||
301 (strings.HasPrefix(what, "com.android.gki.") && InList(AvailableToGkiApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900302}
303
304func (m *ApexModuleBase) AvailableFor(what string) bool {
305 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900306}
307
Jiyong Park89e850a2020-04-07 16:37:39 +0900308func (m *ApexModuleBase) NotAvailableForPlatform() bool {
309 return m.ApexProperties.NotAvailableForPlatform
310}
311
312func (m *ApexModuleBase) SetNotAvailableForPlatform() {
313 m.ApexProperties.NotAvailableForPlatform = true
314}
315
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900316func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
317 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
318 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
319 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
320 return true
321}
322
Jooyung Han75568392020-03-20 04:29:24 +0900323func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) {
Jooyung Han03b51852020-02-26 22:45:42 +0900324 for i := range versionList {
325 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
Jooyung Han75568392020-03-20 04:29:24 +0900326 if ver <= maxSdkVersion {
Jooyung Han03b51852020-02-26 22:45:42 +0900327 return versionList[len(versionList)-i-1], nil
328 }
329 }
Jooyung Han75568392020-03-20 04:29:24 +0900330 return "", fmt.Errorf("not found a version(<=%d) in versionList: %v", maxSdkVersion, versionList)
Jooyung Han03b51852020-02-26 22:45:42 +0900331}
332
Jiyong Park127b40b2019-09-30 16:04:35 +0900333func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
334 for _, n := range m.ApexProperties.Apex_available {
Yifan Hongd22a84a2020-07-28 17:37:46 -0700335 if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900336 continue
337 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100338 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900339 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
340 }
341 }
342}
343
Ulya Trafimovich7c140d82020-04-22 18:05:58 +0100344func (m *ApexModuleBase) Updatable() bool {
345 return m.ApexProperties.Info.Updatable
346}
347
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800348type byApexName []ApexInfo
349
350func (a byApexName) Len() int { return len(a) }
351func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
Colin Crosse07f2312020-08-13 11:24:56 -0700352func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800353
Colin Crossaede88c2020-08-11 12:17:01 -0700354// mergeApexVariations deduplicates APEX variations that would build identically into a common
355// variation. It returns the reduced list of variations and a list of aliases from the original
356// variation names to the new variation names.
Dan Albertc8060532020-07-22 22:32:17 -0700357func mergeApexVariations(ctx EarlyModuleContext, apexVariations []ApexInfo) (merged []ApexInfo, aliases [][2]string) {
Colin Crossaede88c2020-08-11 12:17:01 -0700358 sort.Sort(byApexName(apexVariations))
359 seen := make(map[string]int)
360 for _, apexInfo := range apexVariations {
361 apexName := apexInfo.ApexVariationName
Dan Albertc8060532020-07-22 22:32:17 -0700362 mergedName := apexInfo.mergedName(ctx)
Colin Crossaede88c2020-08-11 12:17:01 -0700363 if index, exists := seen[mergedName]; exists {
364 merged[index].InApexes = append(merged[index].InApexes, apexName)
365 merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable
366 } else {
367 seen[mergedName] = len(merged)
Dan Albertc8060532020-07-22 22:32:17 -0700368 apexInfo.ApexVariationName = apexInfo.mergedName(ctx)
Colin Crossaede88c2020-08-11 12:17:01 -0700369 apexInfo.InApexes = CopyOf(apexInfo.InApexes)
370 merged = append(merged, apexInfo)
371 }
372 aliases = append(aliases, [2]string{apexName, mergedName})
373 }
374 return merged, aliases
375}
376
Colin Cross43b92e02019-11-18 15:28:57 -0800377func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900378 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900379 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900380
Colin Crossaede88c2020-08-11 12:17:01 -0700381 var apexVariations []ApexInfo
382 var aliases [][2]string
383 if !mctx.Module().(ApexModule).UniqueApexVariations() && !m.ApexProperties.UniqueApexVariationsForDeps {
Dan Albertc8060532020-07-22 22:32:17 -0700384 apexVariations, aliases = mergeApexVariations(mctx, m.apexVariations)
Colin Crossaede88c2020-08-11 12:17:01 -0700385 } else {
386 apexVariations = m.apexVariations
387 }
388
389 sort.Sort(byApexName(apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900390 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900391 variations = append(variations, "") // Original variation for platform
Colin Crossaede88c2020-08-11 12:17:01 -0700392 for _, apex := range apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700393 variations = append(variations, apex.ApexVariationName)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800394 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800395
Jiyong Park3ff16992019-12-27 14:11:47 +0900396 defaultVariation := ""
397 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900398
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900399 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800400 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900401 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800402 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +0100403 // Do not install the module for platform, but still allow it to output
404 // uninstallable AndroidMk entries in certain cases when they have
405 // side effects.
406 mod.MakeUninstallable()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900407 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800408 if !platformVariation {
Colin Crossaede88c2020-08-11 12:17:01 -0700409 mod.(ApexModule).apexModuleBase().ApexProperties.Info = apexVariations[i-1]
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800410 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900411 }
Colin Crossaede88c2020-08-11 12:17:01 -0700412
413 for _, alias := range aliases {
414 mctx.CreateAliasVariation(alias[0], alias[1])
415 }
416
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900417 return modules
418 }
419 return nil
420}
421
422var apexData OncePer
423var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800424var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900425
426// This structure maintains the global mapping in between modules and APEXes.
427// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900428//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900429// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
430// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
431// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
432func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800433 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900434 return make(map[string]map[string]bool)
435 }).(map[string]map[string]bool)
436}
437
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900438// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900439// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900440// is explicitly listed in the build definition of the APEX via properties like
441// native_shared_libs, java_libs, etc.
Jooyung Han698dd9f2020-07-22 15:17:19 +0900442func UpdateApexDependency(apex ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900443 apexNamesMapMutex.Lock()
444 defer apexNamesMapMutex.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900445 apexesForModule, ok := apexNamesMap()[moduleName]
446 if !ok {
447 apexesForModule = make(map[string]bool)
448 apexNamesMap()[moduleName] = apexesForModule
Jiyong Park25fc6a92018-11-18 18:02:45 +0900449 }
Colin Crosse07f2312020-08-13 11:24:56 -0700450 apexesForModule[apex.ApexVariationName] = apexesForModule[apex.ApexVariationName] || directDep
Colin Crossaede88c2020-08-11 12:17:01 -0700451 for _, apexName := range apex.InApexes {
452 apexesForModule[apexName] = apexesForModule[apex.ApexVariationName] || directDep
453 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900454}
455
Jooyung Han671f1ce2019-12-17 12:47:13 +0900456// TODO(b/146393795): remove this when b/146393795 is fixed
457func ClearApexDependency() {
458 m := apexNamesMap()
459 for k := range m {
460 delete(m, k)
461 }
462}
463
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900464// Tests whether a module named moduleName is directly depended on by an APEX
465// named apexName.
466func DirectlyInApex(apexName string, moduleName string) bool {
467 apexNamesMapMutex.Lock()
468 defer apexNamesMapMutex.Unlock()
Colin Crossaede88c2020-08-11 12:17:01 -0700469 if apexNamesForModule, ok := apexNamesMap()[moduleName]; ok {
470 return apexNamesForModule[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900471 }
472 return false
473}
474
Colin Crossaede88c2020-08-11 12:17:01 -0700475// Tests whether a module named moduleName is directly depended on by all APEXes
476// in a list of apexNames.
477func DirectlyInAllApexes(apexNames []string, moduleName string) bool {
478 apexNamesMapMutex.Lock()
479 defer apexNamesMapMutex.Unlock()
480 for _, apexName := range apexNames {
481 apexNamesForModule := apexNamesMap()[moduleName]
482 if !apexNamesForModule[apexName] {
483 return false
484 }
485 }
486 return true
487}
488
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000489type hostContext interface {
490 Host() bool
491}
492
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900493// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000494func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
495 if ctx.Host() {
496 // Host has no APEX.
497 return false
498 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900499 apexNamesMapMutex.Lock()
500 defer apexNamesMapMutex.Unlock()
501 if apexNames, ok := apexNamesMap()[moduleName]; ok {
502 for an := range apexNames {
503 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900504 return true
505 }
506 }
507 }
508 return false
509}
510
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900511// Tests whether a module named module is depended on (including both
512// direct and indirect dependencies) by any APEX.
513func InAnyApex(moduleName string) bool {
514 apexNamesMapMutex.Lock()
515 defer apexNamesMapMutex.Unlock()
516 apexNames, ok := apexNamesMap()[moduleName]
517 return ok && len(apexNames) > 0
518}
519
520func GetApexesForModule(moduleName string) []string {
521 ret := []string{}
522 apexNamesMapMutex.Lock()
523 defer apexNamesMapMutex.Unlock()
524 if apexNames, ok := apexNamesMap()[moduleName]; ok {
525 for an := range apexNames {
526 ret = append(ret, an)
527 }
528 }
529 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900530}
531
Jiyong Park9d452992018-10-03 00:38:19 +0900532func InitApexModule(m ApexModule) {
533 base := m.apexModuleBase()
534 base.canHaveApexVariants = true
535
536 m.AddProperties(&base.ApexProperties)
537}
Artur Satayev872a1442020-04-27 17:08:37 +0100538
539// A dependency info for a single ApexModule, either direct or transitive.
540type ApexModuleDepInfo struct {
541 // Name of the dependency
542 To string
543 // List of dependencies To belongs to. Includes APEX itself, if a direct dependency.
544 From []string
545 // Whether the dependency belongs to the final compiled APEX.
546 IsExternal bool
Artur Satayev480e25b2020-04-27 18:53:18 +0100547 // min_sdk_version of the ApexModule
548 MinSdkVersion string
Artur Satayev872a1442020-04-27 17:08:37 +0100549}
550
551// A map of a dependency name to its ApexModuleDepInfo
552type DepNameToDepInfoMap map[string]ApexModuleDepInfo
553
554type ApexBundleDepsInfo struct {
Jooyung Han98d63e12020-05-14 07:44:03 +0900555 flatListPath OutputPath
556 fullListPath OutputPath
Artur Satayev872a1442020-04-27 17:08:37 +0100557}
558
Artur Satayev849f8442020-04-28 14:57:42 +0100559type ApexBundleDepsInfoIntf interface {
560 Updatable() bool
Artur Satayeva8bd1132020-04-27 18:07:06 +0100561 FlatListPath() Path
Artur Satayev872a1442020-04-27 17:08:37 +0100562 FullListPath() Path
563}
564
Artur Satayeva8bd1132020-04-27 18:07:06 +0100565func (d *ApexBundleDepsInfo) FlatListPath() Path {
566 return d.flatListPath
567}
568
Artur Satayev872a1442020-04-27 17:08:37 +0100569func (d *ApexBundleDepsInfo) FullListPath() Path {
570 return d.fullListPath
571}
572
Artur Satayeva8bd1132020-04-27 18:07:06 +0100573// Generate two module out files:
574// 1. FullList with transitive deps and their parents in the dep graph
575// 2. FlatList with a flat list of transitive deps
Artur Satayev480e25b2020-04-27 18:53:18 +0100576func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) {
Artur Satayeva8bd1132020-04-27 18:07:06 +0100577 var fullContent strings.Builder
578 var flatContent strings.Builder
579
Artur Satayev480e25b2020-04-27 18:53:18 +0100580 fmt.Fprintf(&flatContent, "%s(minSdkVersion:%s):\\n", ctx.ModuleName(), minSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100581 for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
582 info := depInfos[key]
Artur Satayev480e25b2020-04-27 18:53:18 +0100583 toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100584 if info.IsExternal {
585 toName = toName + " (external)"
586 }
Artur Satayeva8bd1132020-04-27 18:07:06 +0100587 fmt.Fprintf(&fullContent, "%s <- %s\\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
588 fmt.Fprintf(&flatContent, " %s\\n", toName)
Artur Satayev872a1442020-04-27 17:08:37 +0100589 }
590
591 d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
592 ctx.Build(pctx, BuildParams{
593 Rule: WriteFile,
594 Description: "Full Dependency Info",
595 Output: d.fullListPath,
596 Args: map[string]string{
Artur Satayeva8bd1132020-04-27 18:07:06 +0100597 "content": fullContent.String(),
598 },
599 })
600
601 d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
602 ctx.Build(pctx, BuildParams{
603 Rule: WriteFile,
604 Description: "Flat Dependency Info",
605 Output: d.flatListPath,
606 Args: map[string]string{
607 "content": flatContent.String(),
Artur Satayev872a1442020-04-27 17:08:37 +0100608 },
609 })
610}
Jooyung Han749dc692020-04-15 11:03:39 +0900611
612// TODO(b/158059172): remove minSdkVersion allowlist
Dan Albertc8060532020-07-22 22:32:17 -0700613var minSdkVersionAllowlist = func(apiMap map[string]int) map[string]ApiLevel {
614 list := make(map[string]ApiLevel, len(apiMap))
615 for name, finalApiInt := range apiMap {
616 list[name] = uncheckedFinalApiLevel(finalApiInt)
617 }
618 return list
619}(map[string]int{
Jooyung Han749dc692020-04-15 11:03:39 +0900620 "adbd": 30,
621 "android.net.ipsec.ike": 30,
622 "androidx-constraintlayout_constraintlayout-solver": 30,
623 "androidx.annotation_annotation": 28,
624 "androidx.arch.core_core-common": 28,
625 "androidx.collection_collection": 28,
626 "androidx.lifecycle_lifecycle-common": 28,
627 "apache-commons-compress": 29,
628 "bouncycastle_ike_digests": 30,
629 "brotli-java": 29,
630 "captiveportal-lib": 28,
631 "flatbuffer_headers": 30,
632 "framework-permission": 30,
633 "framework-statsd": 30,
634 "gemmlowp_headers": 30,
635 "ike-internals": 30,
636 "kotlinx-coroutines-android": 28,
637 "kotlinx-coroutines-core": 28,
638 "libadb_crypto": 30,
639 "libadb_pairing_auth": 30,
640 "libadb_pairing_connection": 30,
641 "libadb_pairing_server": 30,
642 "libadb_protos": 30,
643 "libadb_tls_connection": 30,
644 "libadbconnection_client": 30,
645 "libadbconnection_server": 30,
646 "libadbd_core": 30,
647 "libadbd_services": 30,
648 "libadbd": 30,
649 "libapp_processes_protos_lite": 30,
650 "libasyncio": 30,
651 "libbrotli": 30,
652 "libbuildversion": 30,
653 "libcrypto_static": 30,
654 "libcrypto_utils": 30,
655 "libdiagnose_usb": 30,
656 "libeigen": 30,
657 "liblz4": 30,
658 "libmdnssd": 30,
659 "libneuralnetworks_common": 30,
660 "libneuralnetworks_headers": 30,
661 "libneuralnetworks": 30,
662 "libprocpartition": 30,
663 "libprotobuf-java-lite": 30,
664 "libprotoutil": 30,
665 "libqemu_pipe": 30,
666 "libstats_jni": 30,
667 "libstatslog_statsd": 30,
668 "libstatsmetadata": 30,
669 "libstatspull": 30,
670 "libstatssocket": 30,
671 "libsync": 30,
672 "libtextclassifier_hash_headers": 30,
673 "libtextclassifier_hash_static": 30,
674 "libtflite_kernel_utils": 30,
675 "libwatchdog": 29,
676 "libzstd": 30,
677 "metrics-constants-protos": 28,
678 "net-utils-framework-common": 29,
679 "permissioncontroller-statsd": 28,
680 "philox_random_headers": 30,
681 "philox_random": 30,
682 "service-permission": 30,
683 "service-statsd": 30,
684 "statsd-aidl-ndk_platform": 30,
685 "statsd": 30,
686 "tensorflow_headers": 30,
687 "xz-java": 29,
Dan Albertc8060532020-07-22 22:32:17 -0700688})
Jooyung Han749dc692020-04-15 11:03:39 +0900689
690// Function called while walking an APEX's payload dependencies.
691//
692// Return true if the `to` module should be visited, false otherwise.
693type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool
694
695// UpdatableModule represents updatable APEX/APK
696type UpdatableModule interface {
697 Module
698 WalkPayloadDeps(ctx ModuleContext, do PayloadDepsCallback)
699}
700
701// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version accordingly
Dan Albertc8060532020-07-22 22:32:17 -0700702func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion ApiLevel) {
Jooyung Han749dc692020-04-15 11:03:39 +0900703 // do not enforce min_sdk_version for host
704 if ctx.Host() {
705 return
706 }
707
708 // do not enforce for coverage build
709 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() {
710 return
711 }
712
713 // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version or
714 // min_sdk_version is not finalized (e.g. current or codenames)
Dan Albertc8060532020-07-22 22:32:17 -0700715 if minSdkVersion.IsCurrent() {
Jooyung Han749dc692020-04-15 11:03:39 +0900716 return
717 }
718
719 m.WalkPayloadDeps(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool {
720 if externalDep {
721 // external deps are outside the payload boundary, which is "stable" interface.
722 // We don't have to check min_sdk_version for external dependencies.
723 return false
724 }
725 if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
726 return false
727 }
728 if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil {
729 toName := ctx.OtherModuleName(to)
Dan Albertc8060532020-07-22 22:32:17 -0700730 if ver, ok := minSdkVersionAllowlist[toName]; !ok || ver.GreaterThan(minSdkVersion) {
Jooyung Han749dc692020-04-15 11:03:39 +0900731 ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v. Dependency path: %s",
732 minSdkVersion, ctx.ModuleName(), err.Error(), ctx.GetPathString(false))
733 return false
734 }
735 }
736 return true
737 })
738}