blob: cbaf1c71ad3cf7cfd51bffc36d054e3a841f40d6 [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"
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
Jooyung Han5417f772020-03-12 18:37:20 +090024const (
25 SdkVersion_Android10 = 29
26)
27
Peter Collingbournedc4f9862020-02-12 17:13:25 -080028type ApexInfo struct {
29 // Name of the apex variant that this module is mutated into
30 ApexName string
31
Jooyung Han03b51852020-02-26 22:45:42 +090032 MinSdkVersion int
Peter Collingbournedc4f9862020-02-12 17:13:25 -080033}
34
Jiyong Park9d452992018-10-03 00:38:19 +090035// ApexModule is the interface that a module type is expected to implement if
36// the module has to be built differently depending on whether the module
37// is destined for an apex or not (installed to one of the regular partitions).
38//
39// Native shared libraries are one such module type; when it is built for an
40// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
41// or C APIs from other APEXs.
42//
43// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090044// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090045// in one or more APEXs. Specifically, if a module is included in apex.foo and
46// apex.bar then three apex variants are created: platform, apex.foo and
47// apex.bar. The platform variant is for the regular partitions
48// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
49// respectively.
50type ApexModule interface {
51 Module
52 apexModuleBase() *ApexModuleBase
53
Peter Collingbournedc4f9862020-02-12 17:13:25 -080054 // Marks that this module should be built for the specified APEXes.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090055 // Call this before apex.apexMutator is run.
Peter Collingbournedc4f9862020-02-12 17:13:25 -080056 BuildForApexes(apexes []ApexInfo)
Jiyong Parkf760cae2020-02-12 07:53:12 +090057
Peter Collingbournedc4f9862020-02-12 17:13:25 -080058 // Returns the APEXes that this module will be built for
59 ApexVariations() []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +090060
Jiyong Park9d452992018-10-03 00:38:19 +090061 // Returns the name of APEX that this module will be built for. Empty string
62 // is returned when 'IsForPlatform() == true'. Note that a module can be
Jiyong Park0ddfcd12018-12-11 01:35:25 +090063 // included in multiple APEXes, in which case, the module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090064 // multiple modules each of which for an APEX. This method returns the
65 // name of the APEX that a variant module is for.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090066 // Call this after apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090067 ApexName() string
68
Jiyong Park0ddfcd12018-12-11 01:35:25 +090069 // Tests whether this module will be built for the platform or not.
70 // This is a shortcut for ApexName() == ""
71 IsForPlatform() bool
72
73 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +090074 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +090075 // for not creating APEX variants for certain types of shared libraries
76 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +090077 CanHaveApexVariants() bool
78
79 // Tests if this module can be installed to APEX as a file. For example,
80 // this would return true for shared libs while return false for static
81 // libs.
82 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +090083
84 // Mutate this module into one or more variants each of which is built
Jiyong Parkf760cae2020-02-12 07:53:12 +090085 // for an APEX marked via BuildForApexes().
Colin Cross43b92e02019-11-18 15:28:57 -080086 CreateApexVariations(mctx BottomUpMutatorContext) []Module
Jiyong Park0ddfcd12018-12-11 01:35:25 +090087
Jiyong Park127b40b2019-09-30 16:04:35 +090088 // Tests if this module is available for the specified APEX or ":platform"
89 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090090
91 // DepIsInSameApex tests if the other module 'dep' is installed to the same
92 // APEX as this module
93 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
Jooyung Han03b51852020-02-26 22:45:42 +090094
Jooyung Han75568392020-03-20 04:29:24 +090095 // Returns the highest version which is <= maxSdkVersion.
96 // For example, with maxSdkVersion is 10 and versionList is [9,11]
97 // it returns 9 as string
98 ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error)
Jiyong Park9d452992018-10-03 00:38:19 +090099}
100
101type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000102 // Availability of this module in APEXes. Only the listed APEXes can contain
103 // this module. If the module has stubs then other APEXes and the platform may
104 // access it through them (subject to visibility).
105 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900106 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
107 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900108 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900109 Apex_available []string
110
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800111 Info ApexInfo `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900112}
113
114// Provides default implementation for the ApexModule interface. APEX-aware
115// modules are expected to include this struct and call InitApexModule().
116type ApexModuleBase struct {
117 ApexProperties ApexProperties
118
119 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700120
121 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800122 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900123}
124
125func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
126 return m
127}
128
Paul Duffinbefa4b92020-03-04 14:22:45 +0000129func (m *ApexModuleBase) ApexAvailable() []string {
130 return m.ApexProperties.Apex_available
131}
132
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800133func (m *ApexModuleBase) BuildForApexes(apexes []ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700134 m.apexVariationsLock.Lock()
135 defer m.apexVariationsLock.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800136nextApex:
137 for _, apex := range apexes {
138 for _, v := range m.apexVariations {
139 if v.ApexName == apex.ApexName {
140 continue nextApex
141 }
Jiyong Parkf760cae2020-02-12 07:53:12 +0900142 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800143 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900144 }
145}
146
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800147func (m *ApexModuleBase) ApexVariations() []ApexInfo {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900148 return m.apexVariations
149}
150
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900151func (m *ApexModuleBase) ApexName() string {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800152 return m.ApexProperties.Info.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900153}
154
155func (m *ApexModuleBase) IsForPlatform() bool {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800156 return m.ApexProperties.Info.ApexName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900157}
158
159func (m *ApexModuleBase) CanHaveApexVariants() bool {
160 return m.canHaveApexVariants
161}
162
163func (m *ApexModuleBase) IsInstallableToApex() bool {
164 // should be overriden if needed
165 return false
166}
167
Jiyong Park127b40b2019-09-30 16:04:35 +0900168const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900169 AvailableToPlatform = "//apex_available:platform"
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000170 AvailableToAnyApex = "//apex_available:anyapex"
Jiyong Park127b40b2019-09-30 16:04:35 +0900171)
172
Jiyong Parka90ca002019-10-07 15:47:24 +0900173func CheckAvailableForApex(what string, apex_available []string) bool {
174 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000175 // apex_available defaults to ["//apex_available:platform"],
176 // which means 'available to the platform but no apexes'.
177 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900178 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900179 return InList(what, apex_available) ||
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000180 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900181}
182
183func (m *ApexModuleBase) AvailableFor(what string) bool {
184 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900185}
186
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900187func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
188 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
189 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
190 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
191 return true
192}
193
Jooyung Han75568392020-03-20 04:29:24 +0900194func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) {
Jooyung Han03b51852020-02-26 22:45:42 +0900195 for i := range versionList {
196 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
Jooyung Han75568392020-03-20 04:29:24 +0900197 if ver <= maxSdkVersion {
Jooyung Han03b51852020-02-26 22:45:42 +0900198 return versionList[len(versionList)-i-1], nil
199 }
200 }
Jooyung Han75568392020-03-20 04:29:24 +0900201 return "", fmt.Errorf("not found a version(<=%d) in versionList: %v", maxSdkVersion, versionList)
Jooyung Han03b51852020-02-26 22:45:42 +0900202}
203
Jiyong Park127b40b2019-09-30 16:04:35 +0900204func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
205 for _, n := range m.ApexProperties.Apex_available {
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000206 if n == AvailableToPlatform || n == AvailableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900207 continue
208 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100209 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900210 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
211 }
212 }
213}
214
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800215type byApexName []ApexInfo
216
217func (a byApexName) Len() int { return len(a) }
218func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
219func (a byApexName) Less(i, j int) bool { return a[i].ApexName < a[j].ApexName }
220
Colin Cross43b92e02019-11-18 15:28:57 -0800221func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900222 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900223 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900224
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800225 sort.Sort(byApexName(m.apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900226 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900227 variations = append(variations, "") // Original variation for platform
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800228 for _, apex := range m.apexVariations {
229 variations = append(variations, apex.ApexName)
230 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800231
Jiyong Park3ff16992019-12-27 14:11:47 +0900232 defaultVariation := ""
233 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900234
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900235 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800236 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900237 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800238 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
239 mod.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900240 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800241 if !platformVariation {
242 mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1]
243 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900244 }
245 return modules
246 }
247 return nil
248}
249
250var apexData OncePer
251var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800252var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900253
254// This structure maintains the global mapping in between modules and APEXes.
255// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900256//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900257// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
258// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
259// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
260func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800261 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 return make(map[string]map[string]bool)
263 }).(map[string]map[string]bool)
264}
265
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900266// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900267// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900268// is explicitly listed in the build definition of the APEX via properties like
269// native_shared_libs, java_libs, etc.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800270func UpdateApexDependency(apexes []ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900271 apexNamesMapMutex.Lock()
272 defer apexNamesMapMutex.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800273 for _, apex := range apexes {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900274 apexesForModule, ok := apexNamesMap()[moduleName]
275 if !ok {
276 apexesForModule = make(map[string]bool)
277 apexNamesMap()[moduleName] = apexesForModule
278 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800279 apexesForModule[apex.ApexName] = apexesForModule[apex.ApexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281}
282
Jooyung Han671f1ce2019-12-17 12:47:13 +0900283// TODO(b/146393795): remove this when b/146393795 is fixed
284func ClearApexDependency() {
285 m := apexNamesMap()
286 for k := range m {
287 delete(m, k)
288 }
289}
290
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900291// Tests whether a module named moduleName is directly depended on by an APEX
292// named apexName.
293func DirectlyInApex(apexName string, moduleName string) bool {
294 apexNamesMapMutex.Lock()
295 defer apexNamesMapMutex.Unlock()
296 if apexNames, ok := apexNamesMap()[moduleName]; ok {
297 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900298 }
299 return false
300}
301
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000302type hostContext interface {
303 Host() bool
304}
305
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900306// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000307func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
308 if ctx.Host() {
309 // Host has no APEX.
310 return false
311 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900312 apexNamesMapMutex.Lock()
313 defer apexNamesMapMutex.Unlock()
314 if apexNames, ok := apexNamesMap()[moduleName]; ok {
315 for an := range apexNames {
316 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317 return true
318 }
319 }
320 }
321 return false
322}
323
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900324// Tests whether a module named module is depended on (including both
325// direct and indirect dependencies) by any APEX.
326func InAnyApex(moduleName string) bool {
327 apexNamesMapMutex.Lock()
328 defer apexNamesMapMutex.Unlock()
329 apexNames, ok := apexNamesMap()[moduleName]
330 return ok && len(apexNames) > 0
331}
332
333func GetApexesForModule(moduleName string) []string {
334 ret := []string{}
335 apexNamesMapMutex.Lock()
336 defer apexNamesMapMutex.Unlock()
337 if apexNames, ok := apexNamesMap()[moduleName]; ok {
338 for an := range apexNames {
339 ret = append(ret, an)
340 }
341 }
342 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900343}
344
Jiyong Park9d452992018-10-03 00:38:19 +0900345func InitApexModule(m ApexModule) {
346 base := m.apexModuleBase()
347 base.canHaveApexVariants = true
348
349 m.AddProperties(&base.ApexProperties)
350}