blob: 5c3256a6c3a33fe24437dcc3f9aeb108f9542c3c [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 Han0c4e0162020-02-26 22:45:42 +090018 "fmt"
Colin Crosscefa94bd2019-06-03 15:07:03 -070019 "sort"
Jooyung Han0c4e0162020-02-26 22:45:42 +090020 "strconv"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090021 "sync"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090022)
Jiyong Park25fc6a92018-11-18 18:02:45 +090023
Peter Collingbournedc4f9862020-02-12 17:13:25 -080024type ApexInfo struct {
25 // Name of the apex variant that this module is mutated into
26 ApexName string
27
28 // Whether this apex variant needs to target Android 10
29 LegacyAndroid10Support bool
Jooyung Han0c4e0162020-02-26 22:45:42 +090030
31 MinSdkVersion int
Peter Collingbournedc4f9862020-02-12 17:13:25 -080032}
33
Jiyong Park9d452992018-10-03 00:38:19 +090034// ApexModule is the interface that a module type is expected to implement if
35// the module has to be built differently depending on whether the module
36// is destined for an apex or not (installed to one of the regular partitions).
37//
38// Native shared libraries are one such module type; when it is built for an
39// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
40// or C APIs from other APEXs.
41//
42// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090043// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090044// in one or more APEXs. Specifically, if a module is included in apex.foo and
45// apex.bar then three apex variants are created: platform, apex.foo and
46// apex.bar. The platform variant is for the regular partitions
47// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
48// respectively.
49type ApexModule interface {
50 Module
51 apexModuleBase() *ApexModuleBase
52
Peter Collingbournedc4f9862020-02-12 17:13:25 -080053 // Marks that this module should be built for the specified APEXes.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090054 // Call this before apex.apexMutator is run.
Peter Collingbournedc4f9862020-02-12 17:13:25 -080055 BuildForApexes(apexes []ApexInfo)
Jiyong Parkf760cae2020-02-12 07:53:12 +090056
Peter Collingbournedc4f9862020-02-12 17:13:25 -080057 // Returns the APEXes that this module will be built for
58 ApexVariations() []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +090059
Jiyong Park9d452992018-10-03 00:38:19 +090060 // Returns the name of APEX that this module will be built for. Empty string
61 // is returned when 'IsForPlatform() == true'. Note that a module can be
Jiyong Park0ddfcd12018-12-11 01:35:25 +090062 // included in multiple APEXes, in which case, the module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090063 // multiple modules each of which for an APEX. This method returns the
64 // name of the APEX that a variant module is for.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090065 // Call this after apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090066 ApexName() string
67
Jiyong Park0ddfcd12018-12-11 01:35:25 +090068 // Tests whether this module will be built for the platform or not.
69 // This is a shortcut for ApexName() == ""
70 IsForPlatform() bool
71
72 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +090073 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +090074 // for not creating APEX variants for certain types of shared libraries
75 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +090076 CanHaveApexVariants() bool
77
78 // Tests if this module can be installed to APEX as a file. For example,
79 // this would return true for shared libs while return false for static
80 // libs.
81 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +090082
83 // Mutate this module into one or more variants each of which is built
Jiyong Parkf760cae2020-02-12 07:53:12 +090084 // for an APEX marked via BuildForApexes().
Colin Cross43b92e02019-11-18 15:28:57 -080085 CreateApexVariations(mctx BottomUpMutatorContext) []Module
Jiyong Park0ddfcd12018-12-11 01:35:25 +090086
Jiyong Park127b40b2019-09-30 16:04:35 +090087 // Tests if this module is available for the specified APEX or ":platform"
88 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090089
90 // DepIsInSameApex tests if the other module 'dep' is installed to the same
91 // APEX as this module
92 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
Jooyung Han0c4e0162020-02-26 22:45:42 +090093
94 // Returns the highest version which is <= min_sdk_version.
95 // For example, with min_sdk_version is 10 and versionList is [9,11]
96 // it returns 9.
97 ChooseSdkVersion(versionList []string, useLatest bool) (string, error)
98
99 ShouldSupportAndroid10() bool
Jiyong Park9d452992018-10-03 00:38:19 +0900100}
101
102type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000103 // Availability of this module in APEXes. Only the listed APEXes can contain
104 // this module. If the module has stubs then other APEXes and the platform may
105 // access it through them (subject to visibility).
106 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900107 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
108 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900109 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900110 Apex_available []string
111
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800112 Info ApexInfo `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900113}
114
115// Provides default implementation for the ApexModule interface. APEX-aware
116// modules are expected to include this struct and call InitApexModule().
117type ApexModuleBase struct {
118 ApexProperties ApexProperties
119
120 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700121
122 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800123 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900124}
125
126func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
127 return m
128}
129
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800130func (m *ApexModuleBase) BuildForApexes(apexes []ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700131 m.apexVariationsLock.Lock()
132 defer m.apexVariationsLock.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800133nextApex:
134 for _, apex := range apexes {
135 for _, v := range m.apexVariations {
136 if v.ApexName == apex.ApexName {
137 continue nextApex
138 }
Jiyong Parkf760cae2020-02-12 07:53:12 +0900139 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800140 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900141 }
142}
143
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800144func (m *ApexModuleBase) ApexVariations() []ApexInfo {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900145 return m.apexVariations
146}
147
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900148func (m *ApexModuleBase) ApexName() string {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800149 return m.ApexProperties.Info.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900150}
151
152func (m *ApexModuleBase) IsForPlatform() bool {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800153 return m.ApexProperties.Info.ApexName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900154}
155
156func (m *ApexModuleBase) CanHaveApexVariants() bool {
157 return m.canHaveApexVariants
158}
159
160func (m *ApexModuleBase) IsInstallableToApex() bool {
161 // should be overriden if needed
162 return false
163}
164
Jiyong Park127b40b2019-09-30 16:04:35 +0900165const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900166 AvailableToPlatform = "//apex_available:platform"
Jiyong Park127b40b2019-09-30 16:04:35 +0900167 availableToAnyApex = "//apex_available:anyapex"
168)
169
Jiyong Parka90ca002019-10-07 15:47:24 +0900170func CheckAvailableForApex(what string, apex_available []string) bool {
171 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000172 // apex_available defaults to ["//apex_available:platform"],
173 // which means 'available to the platform but no apexes'.
174 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900175 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900176 return InList(what, apex_available) ||
Jiyong Parkb02bb402019-12-03 00:43:57 +0900177 (what != AvailableToPlatform && InList(availableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900178}
179
180func (m *ApexModuleBase) AvailableFor(what string) bool {
181 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900182}
183
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900184func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
185 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
186 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
187 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
188 return true
189}
190
Jooyung Han0c4e0162020-02-26 22:45:42 +0900191func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, useLatest bool) (string, error) {
192 if useLatest {
193 return versionList[len(versionList)-1], nil
194 }
195 minSdkVersion := m.ApexProperties.Info.MinSdkVersion
196 for i := range versionList {
197 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
198 if ver <= minSdkVersion {
199 return versionList[len(versionList)-i-1], nil
200 }
201 }
202 return "", fmt.Errorf("min_sdk_version is set %v, but not found in %v", minSdkVersion, versionList)
203}
204
205func (m *ApexModuleBase) ShouldSupportAndroid10() bool {
206 return !m.IsForPlatform() && (m.ApexProperties.Info.MinSdkVersion <= 29 || m.ApexProperties.Info.LegacyAndroid10Support)
207}
208
Jiyong Park127b40b2019-09-30 16:04:35 +0900209func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
210 for _, n := range m.ApexProperties.Apex_available {
Jiyong Parkb02bb402019-12-03 00:43:57 +0900211 if n == AvailableToPlatform || n == availableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900212 continue
213 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100214 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900215 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
216 }
217 }
218}
219
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800220type byApexName []ApexInfo
221
222func (a byApexName) Len() int { return len(a) }
223func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
224func (a byApexName) Less(i, j int) bool { return a[i].ApexName < a[j].ApexName }
225
Colin Cross43b92e02019-11-18 15:28:57 -0800226func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900227 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900228 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900229
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800230 sort.Sort(byApexName(m.apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900231 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900232 variations = append(variations, "") // Original variation for platform
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800233 for _, apex := range m.apexVariations {
234 variations = append(variations, apex.ApexName)
235 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800236
Jiyong Park3ff16992019-12-27 14:11:47 +0900237 defaultVariation := ""
238 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900239
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900240 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800241 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900242 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800243 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
244 mod.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900245 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800246 if !platformVariation {
247 mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1]
248 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900249 }
250 return modules
251 }
252 return nil
253}
254
255var apexData OncePer
256var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800257var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900258
259// This structure maintains the global mapping in between modules and APEXes.
260// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900262// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
263// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
264// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
265func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800266 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900267 return make(map[string]map[string]bool)
268 }).(map[string]map[string]bool)
269}
270
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900271// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900272// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900273// is explicitly listed in the build definition of the APEX via properties like
274// native_shared_libs, java_libs, etc.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800275func UpdateApexDependency(apexes []ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900276 apexNamesMapMutex.Lock()
277 defer apexNamesMapMutex.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800278 for _, apex := range apexes {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900279 apexesForModule, ok := apexNamesMap()[moduleName]
280 if !ok {
281 apexesForModule = make(map[string]bool)
282 apexNamesMap()[moduleName] = apexesForModule
283 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800284 apexesForModule[apex.ApexName] = apexesForModule[apex.ApexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900285 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900286}
287
Jooyung Han671f1ce2019-12-17 12:47:13 +0900288// TODO(b/146393795): remove this when b/146393795 is fixed
289func ClearApexDependency() {
290 m := apexNamesMap()
291 for k := range m {
292 delete(m, k)
293 }
294}
295
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900296// Tests whether a module named moduleName is directly depended on by an APEX
297// named apexName.
298func DirectlyInApex(apexName string, moduleName string) bool {
299 apexNamesMapMutex.Lock()
300 defer apexNamesMapMutex.Unlock()
301 if apexNames, ok := apexNamesMap()[moduleName]; ok {
302 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900303 }
304 return false
305}
306
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000307type hostContext interface {
308 Host() bool
309}
310
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900311// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000312func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
313 if ctx.Host() {
314 // Host has no APEX.
315 return false
316 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900317 apexNamesMapMutex.Lock()
318 defer apexNamesMapMutex.Unlock()
319 if apexNames, ok := apexNamesMap()[moduleName]; ok {
320 for an := range apexNames {
321 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322 return true
323 }
324 }
325 }
326 return false
327}
328
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900329// Tests whether a module named module is depended on (including both
330// direct and indirect dependencies) by any APEX.
331func InAnyApex(moduleName string) bool {
332 apexNamesMapMutex.Lock()
333 defer apexNamesMapMutex.Unlock()
334 apexNames, ok := apexNamesMap()[moduleName]
335 return ok && len(apexNames) > 0
336}
337
338func GetApexesForModule(moduleName string) []string {
339 ret := []string{}
340 apexNamesMapMutex.Lock()
341 defer apexNamesMapMutex.Unlock()
342 if apexNames, ok := apexNamesMap()[moduleName]; ok {
343 for an := range apexNames {
344 ret = append(ret, an)
345 }
346 }
347 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900348}
349
Jiyong Park9d452992018-10-03 00:38:19 +0900350func InitApexModule(m ApexModule) {
351 base := m.apexModuleBase()
352 base.canHaveApexVariants = true
353
354 m.AddProperties(&base.ApexProperties)
355}