blob: 7ae46d4be8e7c22d1abcb7f600baf4746337b8cd [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
Colin Cross7812fd32020-09-25 12:35:10 -0700139 ChooseSdkVersion(ctx BaseModuleContext, versionList []string, maxSdkVersion ApiLevel) (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
Colin Cross7812fd32020-09-25 12:35:10 -0700323func (m *ApexModuleBase) ChooseSdkVersion(ctx BaseModuleContext, versionList []string, maxSdkVersion ApiLevel) (string, error) {
Jooyung Han03b51852020-02-26 22:45:42 +0900324 for i := range versionList {
Colin Cross7812fd32020-09-25 12:35:10 -0700325 version := versionList[len(versionList)-i-1]
326 ver, err := ApiLevelFromUser(ctx, version)
327 if err != nil {
328 return "", err
329 }
330 if ver.LessThanOrEqualTo(maxSdkVersion) {
331 return version, nil
Jooyung Han03b51852020-02-26 22:45:42 +0900332 }
333 }
Colin Cross7812fd32020-09-25 12:35:10 -0700334 return "", fmt.Errorf("not found a version(<=%s) in versionList: %v", maxSdkVersion, versionList)
Jooyung Han03b51852020-02-26 22:45:42 +0900335}
336
Jiyong Park127b40b2019-09-30 16:04:35 +0900337func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
338 for _, n := range m.ApexProperties.Apex_available {
Yifan Hongd22a84a2020-07-28 17:37:46 -0700339 if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900340 continue
341 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100342 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900343 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
344 }
345 }
346}
347
Ulya Trafimovich7c140d82020-04-22 18:05:58 +0100348func (m *ApexModuleBase) Updatable() bool {
349 return m.ApexProperties.Info.Updatable
350}
351
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800352type byApexName []ApexInfo
353
354func (a byApexName) Len() int { return len(a) }
355func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
Colin Crosse07f2312020-08-13 11:24:56 -0700356func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800357
Colin Crossaede88c2020-08-11 12:17:01 -0700358// mergeApexVariations deduplicates APEX variations that would build identically into a common
359// variation. It returns the reduced list of variations and a list of aliases from the original
360// variation names to the new variation names.
Dan Albertc8060532020-07-22 22:32:17 -0700361func mergeApexVariations(ctx EarlyModuleContext, apexVariations []ApexInfo) (merged []ApexInfo, aliases [][2]string) {
Colin Crossaede88c2020-08-11 12:17:01 -0700362 sort.Sort(byApexName(apexVariations))
363 seen := make(map[string]int)
364 for _, apexInfo := range apexVariations {
365 apexName := apexInfo.ApexVariationName
Dan Albertc8060532020-07-22 22:32:17 -0700366 mergedName := apexInfo.mergedName(ctx)
Colin Crossaede88c2020-08-11 12:17:01 -0700367 if index, exists := seen[mergedName]; exists {
368 merged[index].InApexes = append(merged[index].InApexes, apexName)
369 merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable
370 } else {
371 seen[mergedName] = len(merged)
Dan Albertc8060532020-07-22 22:32:17 -0700372 apexInfo.ApexVariationName = apexInfo.mergedName(ctx)
Colin Crossaede88c2020-08-11 12:17:01 -0700373 apexInfo.InApexes = CopyOf(apexInfo.InApexes)
374 merged = append(merged, apexInfo)
375 }
376 aliases = append(aliases, [2]string{apexName, mergedName})
377 }
378 return merged, aliases
379}
380
Colin Cross43b92e02019-11-18 15:28:57 -0800381func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900382 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900383 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900384
Colin Crossaede88c2020-08-11 12:17:01 -0700385 var apexVariations []ApexInfo
386 var aliases [][2]string
387 if !mctx.Module().(ApexModule).UniqueApexVariations() && !m.ApexProperties.UniqueApexVariationsForDeps {
Dan Albertc8060532020-07-22 22:32:17 -0700388 apexVariations, aliases = mergeApexVariations(mctx, m.apexVariations)
Colin Crossaede88c2020-08-11 12:17:01 -0700389 } else {
390 apexVariations = m.apexVariations
391 }
392
393 sort.Sort(byApexName(apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900394 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900395 variations = append(variations, "") // Original variation for platform
Colin Crossaede88c2020-08-11 12:17:01 -0700396 for _, apex := range apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700397 variations = append(variations, apex.ApexVariationName)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800398 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800399
Jiyong Park3ff16992019-12-27 14:11:47 +0900400 defaultVariation := ""
401 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900402
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900403 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800404 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900405 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800406 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +0100407 // Do not install the module for platform, but still allow it to output
408 // uninstallable AndroidMk entries in certain cases when they have
409 // side effects.
410 mod.MakeUninstallable()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900411 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800412 if !platformVariation {
Colin Crossaede88c2020-08-11 12:17:01 -0700413 mod.(ApexModule).apexModuleBase().ApexProperties.Info = apexVariations[i-1]
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800414 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900415 }
Colin Crossaede88c2020-08-11 12:17:01 -0700416
417 for _, alias := range aliases {
418 mctx.CreateAliasVariation(alias[0], alias[1])
419 }
420
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900421 return modules
422 }
423 return nil
424}
425
426var apexData OncePer
427var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800428var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900429
430// This structure maintains the global mapping in between modules and APEXes.
431// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900432//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900433// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
434// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
435// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
436func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800437 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900438 return make(map[string]map[string]bool)
439 }).(map[string]map[string]bool)
440}
441
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900442// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900443// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900444// is explicitly listed in the build definition of the APEX via properties like
445// native_shared_libs, java_libs, etc.
Jooyung Han698dd9f2020-07-22 15:17:19 +0900446func UpdateApexDependency(apex ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900447 apexNamesMapMutex.Lock()
448 defer apexNamesMapMutex.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900449 apexesForModule, ok := apexNamesMap()[moduleName]
450 if !ok {
451 apexesForModule = make(map[string]bool)
452 apexNamesMap()[moduleName] = apexesForModule
Jiyong Park25fc6a92018-11-18 18:02:45 +0900453 }
Colin Crosse07f2312020-08-13 11:24:56 -0700454 apexesForModule[apex.ApexVariationName] = apexesForModule[apex.ApexVariationName] || directDep
Colin Crossaede88c2020-08-11 12:17:01 -0700455 for _, apexName := range apex.InApexes {
456 apexesForModule[apexName] = apexesForModule[apex.ApexVariationName] || directDep
457 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900458}
459
Jooyung Han671f1ce2019-12-17 12:47:13 +0900460// TODO(b/146393795): remove this when b/146393795 is fixed
461func ClearApexDependency() {
462 m := apexNamesMap()
463 for k := range m {
464 delete(m, k)
465 }
466}
467
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900468// Tests whether a module named moduleName is directly depended on by an APEX
469// named apexName.
470func DirectlyInApex(apexName string, moduleName string) bool {
471 apexNamesMapMutex.Lock()
472 defer apexNamesMapMutex.Unlock()
Colin Crossaede88c2020-08-11 12:17:01 -0700473 if apexNamesForModule, ok := apexNamesMap()[moduleName]; ok {
474 return apexNamesForModule[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900475 }
476 return false
477}
478
Colin Crossaede88c2020-08-11 12:17:01 -0700479// Tests whether a module named moduleName is directly depended on by all APEXes
480// in a list of apexNames.
481func DirectlyInAllApexes(apexNames []string, moduleName string) bool {
482 apexNamesMapMutex.Lock()
483 defer apexNamesMapMutex.Unlock()
484 for _, apexName := range apexNames {
485 apexNamesForModule := apexNamesMap()[moduleName]
486 if !apexNamesForModule[apexName] {
487 return false
488 }
489 }
490 return true
491}
492
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000493type hostContext interface {
494 Host() bool
495}
496
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900497// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000498func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
499 if ctx.Host() {
500 // Host has no APEX.
501 return false
502 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900503 apexNamesMapMutex.Lock()
504 defer apexNamesMapMutex.Unlock()
505 if apexNames, ok := apexNamesMap()[moduleName]; ok {
506 for an := range apexNames {
507 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900508 return true
509 }
510 }
511 }
512 return false
513}
514
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900515// Tests whether a module named module is depended on (including both
516// direct and indirect dependencies) by any APEX.
517func InAnyApex(moduleName string) bool {
518 apexNamesMapMutex.Lock()
519 defer apexNamesMapMutex.Unlock()
520 apexNames, ok := apexNamesMap()[moduleName]
521 return ok && len(apexNames) > 0
522}
523
524func GetApexesForModule(moduleName string) []string {
525 ret := []string{}
526 apexNamesMapMutex.Lock()
527 defer apexNamesMapMutex.Unlock()
528 if apexNames, ok := apexNamesMap()[moduleName]; ok {
529 for an := range apexNames {
530 ret = append(ret, an)
531 }
532 }
533 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900534}
535
Jiyong Park9d452992018-10-03 00:38:19 +0900536func InitApexModule(m ApexModule) {
537 base := m.apexModuleBase()
538 base.canHaveApexVariants = true
539
540 m.AddProperties(&base.ApexProperties)
541}
Artur Satayev872a1442020-04-27 17:08:37 +0100542
543// A dependency info for a single ApexModule, either direct or transitive.
544type ApexModuleDepInfo struct {
545 // Name of the dependency
546 To string
547 // List of dependencies To belongs to. Includes APEX itself, if a direct dependency.
548 From []string
549 // Whether the dependency belongs to the final compiled APEX.
550 IsExternal bool
Artur Satayev480e25b2020-04-27 18:53:18 +0100551 // min_sdk_version of the ApexModule
552 MinSdkVersion string
Artur Satayev872a1442020-04-27 17:08:37 +0100553}
554
555// A map of a dependency name to its ApexModuleDepInfo
556type DepNameToDepInfoMap map[string]ApexModuleDepInfo
557
558type ApexBundleDepsInfo struct {
Jooyung Han98d63e12020-05-14 07:44:03 +0900559 flatListPath OutputPath
560 fullListPath OutputPath
Artur Satayev872a1442020-04-27 17:08:37 +0100561}
562
Artur Satayev849f8442020-04-28 14:57:42 +0100563type ApexBundleDepsInfoIntf interface {
564 Updatable() bool
Artur Satayeva8bd1132020-04-27 18:07:06 +0100565 FlatListPath() Path
Artur Satayev872a1442020-04-27 17:08:37 +0100566 FullListPath() Path
567}
568
Artur Satayeva8bd1132020-04-27 18:07:06 +0100569func (d *ApexBundleDepsInfo) FlatListPath() Path {
570 return d.flatListPath
571}
572
Artur Satayev872a1442020-04-27 17:08:37 +0100573func (d *ApexBundleDepsInfo) FullListPath() Path {
574 return d.fullListPath
575}
576
Artur Satayeva8bd1132020-04-27 18:07:06 +0100577// Generate two module out files:
578// 1. FullList with transitive deps and their parents in the dep graph
579// 2. FlatList with a flat list of transitive deps
Artur Satayev480e25b2020-04-27 18:53:18 +0100580func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) {
Artur Satayeva8bd1132020-04-27 18:07:06 +0100581 var fullContent strings.Builder
582 var flatContent strings.Builder
583
Artur Satayev480e25b2020-04-27 18:53:18 +0100584 fmt.Fprintf(&flatContent, "%s(minSdkVersion:%s):\\n", ctx.ModuleName(), minSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100585 for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
586 info := depInfos[key]
Artur Satayev480e25b2020-04-27 18:53:18 +0100587 toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100588 if info.IsExternal {
589 toName = toName + " (external)"
590 }
Artur Satayeva8bd1132020-04-27 18:07:06 +0100591 fmt.Fprintf(&fullContent, "%s <- %s\\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
592 fmt.Fprintf(&flatContent, " %s\\n", toName)
Artur Satayev872a1442020-04-27 17:08:37 +0100593 }
594
595 d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
596 ctx.Build(pctx, BuildParams{
597 Rule: WriteFile,
598 Description: "Full Dependency Info",
599 Output: d.fullListPath,
600 Args: map[string]string{
Artur Satayeva8bd1132020-04-27 18:07:06 +0100601 "content": fullContent.String(),
602 },
603 })
604
605 d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
606 ctx.Build(pctx, BuildParams{
607 Rule: WriteFile,
608 Description: "Flat Dependency Info",
609 Output: d.flatListPath,
610 Args: map[string]string{
611 "content": flatContent.String(),
Artur Satayev872a1442020-04-27 17:08:37 +0100612 },
613 })
614}
Jooyung Han749dc692020-04-15 11:03:39 +0900615
616// TODO(b/158059172): remove minSdkVersion allowlist
Dan Albertc8060532020-07-22 22:32:17 -0700617var minSdkVersionAllowlist = func(apiMap map[string]int) map[string]ApiLevel {
618 list := make(map[string]ApiLevel, len(apiMap))
619 for name, finalApiInt := range apiMap {
620 list[name] = uncheckedFinalApiLevel(finalApiInt)
621 }
622 return list
623}(map[string]int{
Jooyung Han749dc692020-04-15 11:03:39 +0900624 "adbd": 30,
625 "android.net.ipsec.ike": 30,
626 "androidx-constraintlayout_constraintlayout-solver": 30,
627 "androidx.annotation_annotation": 28,
628 "androidx.arch.core_core-common": 28,
629 "androidx.collection_collection": 28,
630 "androidx.lifecycle_lifecycle-common": 28,
631 "apache-commons-compress": 29,
632 "bouncycastle_ike_digests": 30,
633 "brotli-java": 29,
634 "captiveportal-lib": 28,
635 "flatbuffer_headers": 30,
636 "framework-permission": 30,
637 "framework-statsd": 30,
638 "gemmlowp_headers": 30,
639 "ike-internals": 30,
640 "kotlinx-coroutines-android": 28,
641 "kotlinx-coroutines-core": 28,
642 "libadb_crypto": 30,
643 "libadb_pairing_auth": 30,
644 "libadb_pairing_connection": 30,
645 "libadb_pairing_server": 30,
646 "libadb_protos": 30,
647 "libadb_tls_connection": 30,
648 "libadbconnection_client": 30,
649 "libadbconnection_server": 30,
650 "libadbd_core": 30,
651 "libadbd_services": 30,
652 "libadbd": 30,
653 "libapp_processes_protos_lite": 30,
654 "libasyncio": 30,
655 "libbrotli": 30,
656 "libbuildversion": 30,
657 "libcrypto_static": 30,
658 "libcrypto_utils": 30,
659 "libdiagnose_usb": 30,
660 "libeigen": 30,
661 "liblz4": 30,
662 "libmdnssd": 30,
663 "libneuralnetworks_common": 30,
664 "libneuralnetworks_headers": 30,
665 "libneuralnetworks": 30,
666 "libprocpartition": 30,
667 "libprotobuf-java-lite": 30,
668 "libprotoutil": 30,
669 "libqemu_pipe": 30,
670 "libstats_jni": 30,
671 "libstatslog_statsd": 30,
672 "libstatsmetadata": 30,
673 "libstatspull": 30,
674 "libstatssocket": 30,
675 "libsync": 30,
676 "libtextclassifier_hash_headers": 30,
677 "libtextclassifier_hash_static": 30,
678 "libtflite_kernel_utils": 30,
679 "libwatchdog": 29,
680 "libzstd": 30,
681 "metrics-constants-protos": 28,
682 "net-utils-framework-common": 29,
683 "permissioncontroller-statsd": 28,
684 "philox_random_headers": 30,
685 "philox_random": 30,
686 "service-permission": 30,
687 "service-statsd": 30,
688 "statsd-aidl-ndk_platform": 30,
689 "statsd": 30,
690 "tensorflow_headers": 30,
691 "xz-java": 29,
Dan Albertc8060532020-07-22 22:32:17 -0700692})
Jooyung Han749dc692020-04-15 11:03:39 +0900693
694// Function called while walking an APEX's payload dependencies.
695//
696// Return true if the `to` module should be visited, false otherwise.
697type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool
698
699// UpdatableModule represents updatable APEX/APK
700type UpdatableModule interface {
701 Module
702 WalkPayloadDeps(ctx ModuleContext, do PayloadDepsCallback)
703}
704
705// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version accordingly
Dan Albertc8060532020-07-22 22:32:17 -0700706func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion ApiLevel) {
Jooyung Han749dc692020-04-15 11:03:39 +0900707 // do not enforce min_sdk_version for host
708 if ctx.Host() {
709 return
710 }
711
712 // do not enforce for coverage build
713 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() {
714 return
715 }
716
717 // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version or
718 // min_sdk_version is not finalized (e.g. current or codenames)
Dan Albertc8060532020-07-22 22:32:17 -0700719 if minSdkVersion.IsCurrent() {
Jooyung Han749dc692020-04-15 11:03:39 +0900720 return
721 }
722
723 m.WalkPayloadDeps(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool {
724 if externalDep {
725 // external deps are outside the payload boundary, which is "stable" interface.
726 // We don't have to check min_sdk_version for external dependencies.
727 return false
728 }
729 if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
730 return false
731 }
732 if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil {
733 toName := ctx.OtherModuleName(to)
Dan Albertc8060532020-07-22 22:32:17 -0700734 if ver, ok := minSdkVersionAllowlist[toName]; !ok || ver.GreaterThan(minSdkVersion) {
Jooyung Han749dc692020-04-15 11:03:39 +0900735 ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v. Dependency path: %s",
736 minSdkVersion, ctx.ModuleName(), err.Error(), ctx.GetPathString(false))
737 return false
738 }
739 }
740 return true
741 })
742}