blob: eb7912323ffa18a80b913efc240fbf998fcd56ba [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
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 Han03b51852020-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 Han03b51852020-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
Paul Duffinbefa4b92020-03-04 14:22:45 +0000130func (m *ApexModuleBase) ApexAvailable() []string {
131 return m.ApexProperties.Apex_available
132}
133
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800134func (m *ApexModuleBase) BuildForApexes(apexes []ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700135 m.apexVariationsLock.Lock()
136 defer m.apexVariationsLock.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800137nextApex:
138 for _, apex := range apexes {
139 for _, v := range m.apexVariations {
140 if v.ApexName == apex.ApexName {
141 continue nextApex
142 }
Jiyong Parkf760cae2020-02-12 07:53:12 +0900143 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800144 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900145 }
146}
147
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800148func (m *ApexModuleBase) ApexVariations() []ApexInfo {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900149 return m.apexVariations
150}
151
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900152func (m *ApexModuleBase) ApexName() string {
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) IsForPlatform() bool {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800157 return m.ApexProperties.Info.ApexName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900158}
159
160func (m *ApexModuleBase) CanHaveApexVariants() bool {
161 return m.canHaveApexVariants
162}
163
164func (m *ApexModuleBase) IsInstallableToApex() bool {
165 // should be overriden if needed
166 return false
167}
168
Jiyong Park127b40b2019-09-30 16:04:35 +0900169const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900170 AvailableToPlatform = "//apex_available:platform"
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000171 AvailableToAnyApex = "//apex_available:anyapex"
Jiyong Park127b40b2019-09-30 16:04:35 +0900172)
173
Jiyong Parka90ca002019-10-07 15:47:24 +0900174func CheckAvailableForApex(what string, apex_available []string) bool {
175 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000176 // apex_available defaults to ["//apex_available:platform"],
177 // which means 'available to the platform but no apexes'.
178 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900179 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900180 return InList(what, apex_available) ||
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000181 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900182}
183
184func (m *ApexModuleBase) AvailableFor(what string) bool {
185 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900186}
187
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900188func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
189 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
190 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
191 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
192 return true
193}
194
Jooyung Han03b51852020-02-26 22:45:42 +0900195func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, useLatest bool) (string, error) {
196 if useLatest {
197 return versionList[len(versionList)-1], nil
198 }
199 minSdkVersion := m.ApexProperties.Info.MinSdkVersion
200 for i := range versionList {
201 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
202 if ver <= minSdkVersion {
203 return versionList[len(versionList)-i-1], nil
204 }
205 }
206 return "", fmt.Errorf("min_sdk_version is set %v, but not found in %v", minSdkVersion, versionList)
207}
208
209func (m *ApexModuleBase) ShouldSupportAndroid10() bool {
210 return !m.IsForPlatform() && (m.ApexProperties.Info.MinSdkVersion <= 29 || m.ApexProperties.Info.LegacyAndroid10Support)
211}
212
Jiyong Park127b40b2019-09-30 16:04:35 +0900213func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
214 for _, n := range m.ApexProperties.Apex_available {
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000215 if n == AvailableToPlatform || n == AvailableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900216 continue
217 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100218 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900219 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
220 }
221 }
222}
223
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800224type byApexName []ApexInfo
225
226func (a byApexName) Len() int { return len(a) }
227func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
228func (a byApexName) Less(i, j int) bool { return a[i].ApexName < a[j].ApexName }
229
Colin Cross43b92e02019-11-18 15:28:57 -0800230func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900231 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900232 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900233
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800234 sort.Sort(byApexName(m.apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900235 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900236 variations = append(variations, "") // Original variation for platform
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800237 for _, apex := range m.apexVariations {
238 variations = append(variations, apex.ApexName)
239 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800240
Jiyong Park3ff16992019-12-27 14:11:47 +0900241 defaultVariation := ""
242 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900243
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900244 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800245 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900246 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800247 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
248 mod.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900249 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800250 if !platformVariation {
251 mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1]
252 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900253 }
254 return modules
255 }
256 return nil
257}
258
259var apexData OncePer
260var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800261var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900262
263// This structure maintains the global mapping in between modules and APEXes.
264// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900265//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900266// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
267// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
268// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
269func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800270 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900271 return make(map[string]map[string]bool)
272 }).(map[string]map[string]bool)
273}
274
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900275// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900276// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900277// is explicitly listed in the build definition of the APEX via properties like
278// native_shared_libs, java_libs, etc.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800279func UpdateApexDependency(apexes []ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900280 apexNamesMapMutex.Lock()
281 defer apexNamesMapMutex.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800282 for _, apex := range apexes {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900283 apexesForModule, ok := apexNamesMap()[moduleName]
284 if !ok {
285 apexesForModule = make(map[string]bool)
286 apexNamesMap()[moduleName] = apexesForModule
287 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800288 apexesForModule[apex.ApexName] = apexesForModule[apex.ApexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900289 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900290}
291
Jooyung Han671f1ce2019-12-17 12:47:13 +0900292// TODO(b/146393795): remove this when b/146393795 is fixed
293func ClearApexDependency() {
294 m := apexNamesMap()
295 for k := range m {
296 delete(m, k)
297 }
298}
299
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900300// Tests whether a module named moduleName is directly depended on by an APEX
301// named apexName.
302func DirectlyInApex(apexName string, moduleName string) bool {
303 apexNamesMapMutex.Lock()
304 defer apexNamesMapMutex.Unlock()
305 if apexNames, ok := apexNamesMap()[moduleName]; ok {
306 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900307 }
308 return false
309}
310
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000311type hostContext interface {
312 Host() bool
313}
314
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900315// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000316func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
317 if ctx.Host() {
318 // Host has no APEX.
319 return false
320 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900321 apexNamesMapMutex.Lock()
322 defer apexNamesMapMutex.Unlock()
323 if apexNames, ok := apexNamesMap()[moduleName]; ok {
324 for an := range apexNames {
325 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900326 return true
327 }
328 }
329 }
330 return false
331}
332
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900333// Tests whether a module named module is depended on (including both
334// direct and indirect dependencies) by any APEX.
335func InAnyApex(moduleName string) bool {
336 apexNamesMapMutex.Lock()
337 defer apexNamesMapMutex.Unlock()
338 apexNames, ok := apexNamesMap()[moduleName]
339 return ok && len(apexNames) > 0
340}
341
342func GetApexesForModule(moduleName string) []string {
343 ret := []string{}
344 apexNamesMapMutex.Lock()
345 defer apexNamesMapMutex.Unlock()
346 if apexNames, ok := apexNamesMap()[moduleName]; ok {
347 for an := range apexNames {
348 ret = append(ret, an)
349 }
350 }
351 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900352}
353
Jiyong Park9d452992018-10-03 00:38:19 +0900354func InitApexModule(m ApexModule) {
355 base := m.apexModuleBase()
356 base.canHaveApexVariants = true
357
358 m.AddProperties(&base.ApexProperties)
359}