blob: cd84f8aa5cd86b94bff76cbdf2401bf5d37f292f [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 {
32 // Name of the apex variant that this module is mutated into
33 ApexName string
34
Jooyung Han03b51852020-02-26 22:45:42 +090035 MinSdkVersion int
Ulya Trafimovich7c140d82020-04-22 18:05:58 +010036 Updatable bool
Peter Collingbournedc4f9862020-02-12 17:13:25 -080037}
38
Paul Duffin923e8a52020-03-30 15:33:32 +010039// Extracted from ApexModule to make it easier to define custom subsets of the
40// ApexModule interface and improve code navigation within the IDE.
41type DepIsInSameApex interface {
42 // DepIsInSameApex tests if the other module 'dep' is installed to the same
43 // APEX as this module
44 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
45}
46
Jiyong Park9d452992018-10-03 00:38:19 +090047// ApexModule is the interface that a module type is expected to implement if
48// the module has to be built differently depending on whether the module
49// is destined for an apex or not (installed to one of the regular partitions).
50//
51// Native shared libraries are one such module type; when it is built for an
52// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
53// or C APIs from other APEXs.
54//
55// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090056// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090057// in one or more APEXs. Specifically, if a module is included in apex.foo and
58// apex.bar then three apex variants are created: platform, apex.foo and
59// apex.bar. The platform variant is for the regular partitions
60// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
61// respectively.
62type ApexModule interface {
63 Module
Paul Duffin923e8a52020-03-30 15:33:32 +010064 DepIsInSameApex
65
Jiyong Park9d452992018-10-03 00:38:19 +090066 apexModuleBase() *ApexModuleBase
67
Jooyung Han698dd9f2020-07-22 15:17:19 +090068 // Marks that this module should be built for the specified APEX.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090069 // Call this before apex.apexMutator is run.
Jooyung Han698dd9f2020-07-22 15:17:19 +090070 BuildForApex(apex ApexInfo)
Jiyong Parkf760cae2020-02-12 07:53:12 +090071
Peter Collingbournedc4f9862020-02-12 17:13:25 -080072 // Returns the APEXes that this module will be built for
73 ApexVariations() []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +090074
Jiyong Park9d452992018-10-03 00:38:19 +090075 // Returns the name of APEX that this module will be built for. Empty string
76 // is returned when 'IsForPlatform() == true'. Note that a module can be
Jiyong Park0ddfcd12018-12-11 01:35:25 +090077 // included in multiple APEXes, in which case, the module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090078 // multiple modules each of which for an APEX. This method returns the
79 // name of the APEX that a variant module is for.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090080 // Call this after apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090081 ApexName() string
82
Jiyong Park0ddfcd12018-12-11 01:35:25 +090083 // Tests whether this module will be built for the platform or not.
84 // This is a shortcut for ApexName() == ""
85 IsForPlatform() bool
86
87 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +090088 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +090089 // for not creating APEX variants for certain types of shared libraries
90 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +090091 CanHaveApexVariants() bool
92
93 // Tests if this module can be installed to APEX as a file. For example,
94 // this would return true for shared libs while return false for static
95 // libs.
96 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +090097
98 // Mutate this module into one or more variants each of which is built
Jooyung Han698dd9f2020-07-22 15:17:19 +090099 // for an APEX marked via BuildForApex().
Colin Cross43b92e02019-11-18 15:28:57 -0800100 CreateApexVariations(mctx BottomUpMutatorContext) []Module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900101
Jiyong Park127b40b2019-09-30 16:04:35 +0900102 // Tests if this module is available for the specified APEX or ":platform"
103 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900104
Jiyong Park89e850a2020-04-07 16:37:39 +0900105 // Return true if this module is not available to platform (i.e. apex_available
106 // property doesn't have "//apex_available:platform"), or shouldn't be available
107 // to platform, which is the case when this module depends on other module that
108 // isn't available to platform.
109 NotAvailableForPlatform() bool
110
111 // Mark that this module is not available to platform. Set by the
112 // check-platform-availability mutator in the apex package.
113 SetNotAvailableForPlatform()
114
Jooyung Han75568392020-03-20 04:29:24 +0900115 // Returns the highest version which is <= maxSdkVersion.
116 // For example, with maxSdkVersion is 10 and versionList is [9,11]
117 // it returns 9 as string
118 ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error)
Ulya Trafimovich7c140d82020-04-22 18:05:58 +0100119
120 // Tests if the module comes from an updatable APEX.
121 Updatable() bool
Jiyong Park62304bb2020-04-13 16:19:48 +0900122
123 // List of APEXes that this module tests. The module has access to
124 // the private part of the listed APEXes even when it is not included in the
125 // APEXes.
126 TestFor() []string
Jooyung Han749dc692020-04-15 11:03:39 +0900127
128 // Returns nil if this module supports sdkVersion
129 // Otherwise, returns error with reason
130 ShouldSupportSdkVersion(ctx BaseModuleContext, sdkVersion int) error
Jiyong Park9d452992018-10-03 00:38:19 +0900131}
132
133type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000134 // Availability of this module in APEXes. Only the listed APEXes can contain
135 // this module. If the module has stubs then other APEXes and the platform may
136 // access it through them (subject to visibility).
137 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900138 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
139 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Yifan Hongd22a84a2020-07-28 17:37:46 -0700140 // "com.android.gki.*" matches any APEX module name with the prefix "com.android.gki.".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900141 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900142 Apex_available []string
143
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800144 Info ApexInfo `blueprint:"mutated"`
Jiyong Park89e850a2020-04-07 16:37:39 +0900145
146 NotAvailableForPlatform bool `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900147}
148
Paul Duffindddd5462020-04-07 15:25:44 +0100149// Marker interface that identifies dependencies that are excluded from APEX
150// contents.
151type ExcludeFromApexContentsTag interface {
152 blueprint.DependencyTag
153
154 // Method that differentiates this interface from others.
155 ExcludeFromApexContents()
156}
157
Jiyong Park9d452992018-10-03 00:38:19 +0900158// Provides default implementation for the ApexModule interface. APEX-aware
159// modules are expected to include this struct and call InitApexModule().
160type ApexModuleBase struct {
161 ApexProperties ApexProperties
162
163 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700164
165 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800166 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900167}
168
169func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
170 return m
171}
172
Paul Duffinbefa4b92020-03-04 14:22:45 +0000173func (m *ApexModuleBase) ApexAvailable() []string {
174 return m.ApexProperties.Apex_available
175}
176
Jiyong Park62304bb2020-04-13 16:19:48 +0900177func (m *ApexModuleBase) TestFor() []string {
178 // To be implemented by concrete types inheriting ApexModuleBase
179 return nil
180}
181
Jooyung Han698dd9f2020-07-22 15:17:19 +0900182func (m *ApexModuleBase) BuildForApex(apex ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700183 m.apexVariationsLock.Lock()
184 defer m.apexVariationsLock.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900185 for _, v := range m.apexVariations {
186 if v.ApexName == apex.ApexName {
187 return
Jiyong Parkf760cae2020-02-12 07:53:12 +0900188 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900189 }
Jooyung Han698dd9f2020-07-22 15:17:19 +0900190 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900191}
192
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800193func (m *ApexModuleBase) ApexVariations() []ApexInfo {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900194 return m.apexVariations
195}
196
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900197func (m *ApexModuleBase) ApexName() string {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800198 return m.ApexProperties.Info.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900199}
200
201func (m *ApexModuleBase) IsForPlatform() bool {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800202 return m.ApexProperties.Info.ApexName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900203}
204
205func (m *ApexModuleBase) CanHaveApexVariants() bool {
206 return m.canHaveApexVariants
207}
208
209func (m *ApexModuleBase) IsInstallableToApex() bool {
210 // should be overriden if needed
211 return false
212}
213
Jiyong Park127b40b2019-09-30 16:04:35 +0900214const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900215 AvailableToPlatform = "//apex_available:platform"
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000216 AvailableToAnyApex = "//apex_available:anyapex"
Yifan Hongd22a84a2020-07-28 17:37:46 -0700217 AvailableToGkiApex = "com.android.gki.*"
Jiyong Park127b40b2019-09-30 16:04:35 +0900218)
219
Jiyong Parka90ca002019-10-07 15:47:24 +0900220func CheckAvailableForApex(what string, apex_available []string) bool {
221 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000222 // apex_available defaults to ["//apex_available:platform"],
223 // which means 'available to the platform but no apexes'.
224 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900225 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900226 return InList(what, apex_available) ||
Yifan Hongd22a84a2020-07-28 17:37:46 -0700227 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) ||
228 (strings.HasPrefix(what, "com.android.gki.") && InList(AvailableToGkiApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900229}
230
231func (m *ApexModuleBase) AvailableFor(what string) bool {
232 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900233}
234
Jiyong Park89e850a2020-04-07 16:37:39 +0900235func (m *ApexModuleBase) NotAvailableForPlatform() bool {
236 return m.ApexProperties.NotAvailableForPlatform
237}
238
239func (m *ApexModuleBase) SetNotAvailableForPlatform() {
240 m.ApexProperties.NotAvailableForPlatform = true
241}
242
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900243func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
244 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
245 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
246 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
247 return true
248}
249
Jooyung Han75568392020-03-20 04:29:24 +0900250func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) {
Jooyung Han03b51852020-02-26 22:45:42 +0900251 for i := range versionList {
252 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
Jooyung Han75568392020-03-20 04:29:24 +0900253 if ver <= maxSdkVersion {
Jooyung Han03b51852020-02-26 22:45:42 +0900254 return versionList[len(versionList)-i-1], nil
255 }
256 }
Jooyung Han75568392020-03-20 04:29:24 +0900257 return "", fmt.Errorf("not found a version(<=%d) in versionList: %v", maxSdkVersion, versionList)
Jooyung Han03b51852020-02-26 22:45:42 +0900258}
259
Jiyong Park127b40b2019-09-30 16:04:35 +0900260func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
261 for _, n := range m.ApexProperties.Apex_available {
Yifan Hongd22a84a2020-07-28 17:37:46 -0700262 if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900263 continue
264 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100265 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900266 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
267 }
268 }
269}
270
Ulya Trafimovich7c140d82020-04-22 18:05:58 +0100271func (m *ApexModuleBase) Updatable() bool {
272 return m.ApexProperties.Info.Updatable
273}
274
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800275type byApexName []ApexInfo
276
277func (a byApexName) Len() int { return len(a) }
278func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
279func (a byApexName) Less(i, j int) bool { return a[i].ApexName < a[j].ApexName }
280
Colin Cross43b92e02019-11-18 15:28:57 -0800281func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900282 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900283 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900284
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800285 sort.Sort(byApexName(m.apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900286 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900287 variations = append(variations, "") // Original variation for platform
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800288 for _, apex := range m.apexVariations {
289 variations = append(variations, apex.ApexName)
290 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800291
Jiyong Park3ff16992019-12-27 14:11:47 +0900292 defaultVariation := ""
293 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900294
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900295 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800296 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900297 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800298 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
299 mod.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900300 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800301 if !platformVariation {
302 mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1]
303 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900304 }
305 return modules
306 }
307 return nil
308}
309
310var apexData OncePer
311var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800312var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900313
314// This structure maintains the global mapping in between modules and APEXes.
315// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900316//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900317// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
318// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
319// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
320func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800321 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322 return make(map[string]map[string]bool)
323 }).(map[string]map[string]bool)
324}
325
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900326// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900327// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900328// is explicitly listed in the build definition of the APEX via properties like
329// native_shared_libs, java_libs, etc.
Jooyung Han698dd9f2020-07-22 15:17:19 +0900330func UpdateApexDependency(apex ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900331 apexNamesMapMutex.Lock()
332 defer apexNamesMapMutex.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900333 apexesForModule, ok := apexNamesMap()[moduleName]
334 if !ok {
335 apexesForModule = make(map[string]bool)
336 apexNamesMap()[moduleName] = apexesForModule
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 }
Jooyung Han698dd9f2020-07-22 15:17:19 +0900338 apexesForModule[apex.ApexName] = apexesForModule[apex.ApexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900339}
340
Jooyung Han671f1ce2019-12-17 12:47:13 +0900341// TODO(b/146393795): remove this when b/146393795 is fixed
342func ClearApexDependency() {
343 m := apexNamesMap()
344 for k := range m {
345 delete(m, k)
346 }
347}
348
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900349// Tests whether a module named moduleName is directly depended on by an APEX
350// named apexName.
351func DirectlyInApex(apexName string, moduleName string) bool {
352 apexNamesMapMutex.Lock()
353 defer apexNamesMapMutex.Unlock()
354 if apexNames, ok := apexNamesMap()[moduleName]; ok {
355 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900356 }
357 return false
358}
359
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000360type hostContext interface {
361 Host() bool
362}
363
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900364// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000365func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
366 if ctx.Host() {
367 // Host has no APEX.
368 return false
369 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900370 apexNamesMapMutex.Lock()
371 defer apexNamesMapMutex.Unlock()
372 if apexNames, ok := apexNamesMap()[moduleName]; ok {
373 for an := range apexNames {
374 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900375 return true
376 }
377 }
378 }
379 return false
380}
381
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900382// Tests whether a module named module is depended on (including both
383// direct and indirect dependencies) by any APEX.
384func InAnyApex(moduleName string) bool {
385 apexNamesMapMutex.Lock()
386 defer apexNamesMapMutex.Unlock()
387 apexNames, ok := apexNamesMap()[moduleName]
388 return ok && len(apexNames) > 0
389}
390
391func GetApexesForModule(moduleName string) []string {
392 ret := []string{}
393 apexNamesMapMutex.Lock()
394 defer apexNamesMapMutex.Unlock()
395 if apexNames, ok := apexNamesMap()[moduleName]; ok {
396 for an := range apexNames {
397 ret = append(ret, an)
398 }
399 }
400 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900401}
402
Jiyong Park9d452992018-10-03 00:38:19 +0900403func InitApexModule(m ApexModule) {
404 base := m.apexModuleBase()
405 base.canHaveApexVariants = true
406
407 m.AddProperties(&base.ApexProperties)
408}
Artur Satayev872a1442020-04-27 17:08:37 +0100409
410// A dependency info for a single ApexModule, either direct or transitive.
411type ApexModuleDepInfo struct {
412 // Name of the dependency
413 To string
414 // List of dependencies To belongs to. Includes APEX itself, if a direct dependency.
415 From []string
416 // Whether the dependency belongs to the final compiled APEX.
417 IsExternal bool
Artur Satayev480e25b2020-04-27 18:53:18 +0100418 // min_sdk_version of the ApexModule
419 MinSdkVersion string
Artur Satayev872a1442020-04-27 17:08:37 +0100420}
421
422// A map of a dependency name to its ApexModuleDepInfo
423type DepNameToDepInfoMap map[string]ApexModuleDepInfo
424
425type ApexBundleDepsInfo struct {
Jooyung Han98d63e12020-05-14 07:44:03 +0900426 flatListPath OutputPath
427 fullListPath OutputPath
Artur Satayev872a1442020-04-27 17:08:37 +0100428}
429
Artur Satayev849f8442020-04-28 14:57:42 +0100430type ApexBundleDepsInfoIntf interface {
431 Updatable() bool
Artur Satayeva8bd1132020-04-27 18:07:06 +0100432 FlatListPath() Path
Artur Satayev872a1442020-04-27 17:08:37 +0100433 FullListPath() Path
434}
435
Artur Satayeva8bd1132020-04-27 18:07:06 +0100436func (d *ApexBundleDepsInfo) FlatListPath() Path {
437 return d.flatListPath
438}
439
Artur Satayev872a1442020-04-27 17:08:37 +0100440func (d *ApexBundleDepsInfo) FullListPath() Path {
441 return d.fullListPath
442}
443
Artur Satayeva8bd1132020-04-27 18:07:06 +0100444// Generate two module out files:
445// 1. FullList with transitive deps and their parents in the dep graph
446// 2. FlatList with a flat list of transitive deps
Artur Satayev480e25b2020-04-27 18:53:18 +0100447func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) {
Artur Satayeva8bd1132020-04-27 18:07:06 +0100448 var fullContent strings.Builder
449 var flatContent strings.Builder
450
Artur Satayev480e25b2020-04-27 18:53:18 +0100451 fmt.Fprintf(&flatContent, "%s(minSdkVersion:%s):\\n", ctx.ModuleName(), minSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100452 for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
453 info := depInfos[key]
Artur Satayev480e25b2020-04-27 18:53:18 +0100454 toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100455 if info.IsExternal {
456 toName = toName + " (external)"
457 }
Artur Satayeva8bd1132020-04-27 18:07:06 +0100458 fmt.Fprintf(&fullContent, "%s <- %s\\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
459 fmt.Fprintf(&flatContent, " %s\\n", toName)
Artur Satayev872a1442020-04-27 17:08:37 +0100460 }
461
462 d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
463 ctx.Build(pctx, BuildParams{
464 Rule: WriteFile,
465 Description: "Full Dependency Info",
466 Output: d.fullListPath,
467 Args: map[string]string{
Artur Satayeva8bd1132020-04-27 18:07:06 +0100468 "content": fullContent.String(),
469 },
470 })
471
472 d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
473 ctx.Build(pctx, BuildParams{
474 Rule: WriteFile,
475 Description: "Flat Dependency Info",
476 Output: d.flatListPath,
477 Args: map[string]string{
478 "content": flatContent.String(),
Artur Satayev872a1442020-04-27 17:08:37 +0100479 },
480 })
481}
Jooyung Han749dc692020-04-15 11:03:39 +0900482
483// TODO(b/158059172): remove minSdkVersion allowlist
484var minSdkVersionAllowlist = map[string]int{
485 "adbd": 30,
486 "android.net.ipsec.ike": 30,
487 "androidx-constraintlayout_constraintlayout-solver": 30,
488 "androidx.annotation_annotation": 28,
489 "androidx.arch.core_core-common": 28,
490 "androidx.collection_collection": 28,
491 "androidx.lifecycle_lifecycle-common": 28,
492 "apache-commons-compress": 29,
493 "bouncycastle_ike_digests": 30,
494 "brotli-java": 29,
495 "captiveportal-lib": 28,
496 "flatbuffer_headers": 30,
497 "framework-permission": 30,
498 "framework-statsd": 30,
499 "gemmlowp_headers": 30,
500 "ike-internals": 30,
501 "kotlinx-coroutines-android": 28,
502 "kotlinx-coroutines-core": 28,
503 "libadb_crypto": 30,
504 "libadb_pairing_auth": 30,
505 "libadb_pairing_connection": 30,
506 "libadb_pairing_server": 30,
507 "libadb_protos": 30,
508 "libadb_tls_connection": 30,
509 "libadbconnection_client": 30,
510 "libadbconnection_server": 30,
511 "libadbd_core": 30,
512 "libadbd_services": 30,
513 "libadbd": 30,
514 "libapp_processes_protos_lite": 30,
515 "libasyncio": 30,
516 "libbrotli": 30,
517 "libbuildversion": 30,
518 "libcrypto_static": 30,
519 "libcrypto_utils": 30,
520 "libdiagnose_usb": 30,
521 "libeigen": 30,
522 "liblz4": 30,
523 "libmdnssd": 30,
524 "libneuralnetworks_common": 30,
525 "libneuralnetworks_headers": 30,
526 "libneuralnetworks": 30,
527 "libprocpartition": 30,
528 "libprotobuf-java-lite": 30,
529 "libprotoutil": 30,
530 "libqemu_pipe": 30,
531 "libstats_jni": 30,
532 "libstatslog_statsd": 30,
533 "libstatsmetadata": 30,
534 "libstatspull": 30,
535 "libstatssocket": 30,
536 "libsync": 30,
537 "libtextclassifier_hash_headers": 30,
538 "libtextclassifier_hash_static": 30,
539 "libtflite_kernel_utils": 30,
540 "libwatchdog": 29,
541 "libzstd": 30,
542 "metrics-constants-protos": 28,
543 "net-utils-framework-common": 29,
544 "permissioncontroller-statsd": 28,
545 "philox_random_headers": 30,
546 "philox_random": 30,
547 "service-permission": 30,
548 "service-statsd": 30,
549 "statsd-aidl-ndk_platform": 30,
550 "statsd": 30,
551 "tensorflow_headers": 30,
552 "xz-java": 29,
553}
554
555// Function called while walking an APEX's payload dependencies.
556//
557// Return true if the `to` module should be visited, false otherwise.
558type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool
559
560// UpdatableModule represents updatable APEX/APK
561type UpdatableModule interface {
562 Module
563 WalkPayloadDeps(ctx ModuleContext, do PayloadDepsCallback)
564}
565
566// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version accordingly
567func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion int) {
568 // do not enforce min_sdk_version for host
569 if ctx.Host() {
570 return
571 }
572
573 // do not enforce for coverage build
574 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() {
575 return
576 }
577
578 // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version or
579 // min_sdk_version is not finalized (e.g. current or codenames)
580 if minSdkVersion == FutureApiLevel {
581 return
582 }
583
584 m.WalkPayloadDeps(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool {
585 if externalDep {
586 // external deps are outside the payload boundary, which is "stable" interface.
587 // We don't have to check min_sdk_version for external dependencies.
588 return false
589 }
590 if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
591 return false
592 }
593 if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil {
594 toName := ctx.OtherModuleName(to)
595 if ver, ok := minSdkVersionAllowlist[toName]; !ok || ver > minSdkVersion {
596 ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v. Dependency path: %s",
597 minSdkVersion, ctx.ModuleName(), err.Error(), ctx.GetPathString(false))
598 return false
599 }
600 }
601 return true
602 })
603}