blob: 205ec95c269ede8c9d1e29a57ff3d72d2274d9be [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
Jooyung Han23b0adf2020-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 Han0c4e0162020-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 Han0c4e0162020-02-26 22:45:42 +090094
95 // Returns the highest version which is <= min_sdk_version.
96 // For example, with min_sdk_version is 10 and versionList is [9,11]
97 // it returns 9.
98 ChooseSdkVersion(versionList []string, useLatest bool) (string, error)
99
100 ShouldSupportAndroid10() bool
Jiyong Park9d452992018-10-03 00:38:19 +0900101}
102
103type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000104 // Availability of this module in APEXes. Only the listed APEXes can contain
105 // this module. If the module has stubs then other APEXes and the platform may
106 // access it through them (subject to visibility).
107 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900108 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
109 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900110 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900111 Apex_available []string
112
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800113 Info ApexInfo `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900114}
115
116// Provides default implementation for the ApexModule interface. APEX-aware
117// modules are expected to include this struct and call InitApexModule().
118type ApexModuleBase struct {
119 ApexProperties ApexProperties
120
121 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700122
123 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800124 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900125}
126
127func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
128 return m
129}
130
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800131func (m *ApexModuleBase) BuildForApexes(apexes []ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700132 m.apexVariationsLock.Lock()
133 defer m.apexVariationsLock.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800134nextApex:
135 for _, apex := range apexes {
136 for _, v := range m.apexVariations {
137 if v.ApexName == apex.ApexName {
138 continue nextApex
139 }
Jiyong Parkf760cae2020-02-12 07:53:12 +0900140 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800141 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900142 }
143}
144
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800145func (m *ApexModuleBase) ApexVariations() []ApexInfo {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900146 return m.apexVariations
147}
148
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900149func (m *ApexModuleBase) ApexName() string {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800150 return m.ApexProperties.Info.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900151}
152
153func (m *ApexModuleBase) IsForPlatform() bool {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800154 return m.ApexProperties.Info.ApexName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900155}
156
157func (m *ApexModuleBase) CanHaveApexVariants() bool {
158 return m.canHaveApexVariants
159}
160
161func (m *ApexModuleBase) IsInstallableToApex() bool {
162 // should be overriden if needed
163 return false
164}
165
Jiyong Park127b40b2019-09-30 16:04:35 +0900166const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900167 AvailableToPlatform = "//apex_available:platform"
Jiyong Park127b40b2019-09-30 16:04:35 +0900168 availableToAnyApex = "//apex_available:anyapex"
169)
170
Jiyong Parka90ca002019-10-07 15:47:24 +0900171func CheckAvailableForApex(what string, apex_available []string) bool {
172 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000173 // apex_available defaults to ["//apex_available:platform"],
174 // which means 'available to the platform but no apexes'.
175 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900176 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900177 return InList(what, apex_available) ||
Jiyong Parkb02bb402019-12-03 00:43:57 +0900178 (what != AvailableToPlatform && InList(availableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900179}
180
181func (m *ApexModuleBase) AvailableFor(what string) bool {
182 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900183}
184
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900185func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
186 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
187 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
188 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
189 return true
190}
191
Jooyung Han0c4e0162020-02-26 22:45:42 +0900192func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, useLatest bool) (string, error) {
193 if useLatest {
194 return versionList[len(versionList)-1], nil
195 }
196 minSdkVersion := m.ApexProperties.Info.MinSdkVersion
197 for i := range versionList {
198 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
199 if ver <= minSdkVersion {
200 return versionList[len(versionList)-i-1], nil
201 }
202 }
203 return "", fmt.Errorf("min_sdk_version is set %v, but not found in %v", minSdkVersion, versionList)
204}
205
206func (m *ApexModuleBase) ShouldSupportAndroid10() bool {
Jooyung Han23b0adf2020-03-12 18:37:20 +0900207 return !m.IsForPlatform() && (m.ApexProperties.Info.MinSdkVersion <= SdkVersion_Android10)
Jooyung Han0c4e0162020-02-26 22:45:42 +0900208}
209
Jiyong Park127b40b2019-09-30 16:04:35 +0900210func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
211 for _, n := range m.ApexProperties.Apex_available {
Jiyong Parkb02bb402019-12-03 00:43:57 +0900212 if n == AvailableToPlatform || n == availableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900213 continue
214 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100215 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900216 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
217 }
218 }
219}
220
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800221type byApexName []ApexInfo
222
223func (a byApexName) Len() int { return len(a) }
224func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
225func (a byApexName) Less(i, j int) bool { return a[i].ApexName < a[j].ApexName }
226
Colin Cross43b92e02019-11-18 15:28:57 -0800227func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900228 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900229 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900230
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800231 sort.Sort(byApexName(m.apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900232 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900233 variations = append(variations, "") // Original variation for platform
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800234 for _, apex := range m.apexVariations {
235 variations = append(variations, apex.ApexName)
236 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800237
Jiyong Park3ff16992019-12-27 14:11:47 +0900238 defaultVariation := ""
239 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900240
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900241 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800242 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900243 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800244 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
245 mod.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900246 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800247 if !platformVariation {
248 mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1]
249 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900250 }
251 return modules
252 }
253 return nil
254}
255
256var apexData OncePer
257var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800258var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900259
260// This structure maintains the global mapping in between modules and APEXes.
261// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900263// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
264// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
265// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
266func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800267 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900268 return make(map[string]map[string]bool)
269 }).(map[string]map[string]bool)
270}
271
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900272// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900273// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900274// is explicitly listed in the build definition of the APEX via properties like
275// native_shared_libs, java_libs, etc.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800276func UpdateApexDependency(apexes []ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900277 apexNamesMapMutex.Lock()
278 defer apexNamesMapMutex.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800279 for _, apex := range apexes {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900280 apexesForModule, ok := apexNamesMap()[moduleName]
281 if !ok {
282 apexesForModule = make(map[string]bool)
283 apexNamesMap()[moduleName] = apexesForModule
284 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800285 apexesForModule[apex.ApexName] = apexesForModule[apex.ApexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900286 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287}
288
Jooyung Han671f1ce2019-12-17 12:47:13 +0900289// TODO(b/146393795): remove this when b/146393795 is fixed
290func ClearApexDependency() {
291 m := apexNamesMap()
292 for k := range m {
293 delete(m, k)
294 }
295}
296
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900297// Tests whether a module named moduleName is directly depended on by an APEX
298// named apexName.
299func DirectlyInApex(apexName string, moduleName string) bool {
300 apexNamesMapMutex.Lock()
301 defer apexNamesMapMutex.Unlock()
302 if apexNames, ok := apexNamesMap()[moduleName]; ok {
303 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900304 }
305 return false
306}
307
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000308type hostContext interface {
309 Host() bool
310}
311
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900312// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000313func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
314 if ctx.Host() {
315 // Host has no APEX.
316 return false
317 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900318 apexNamesMapMutex.Lock()
319 defer apexNamesMapMutex.Unlock()
320 if apexNames, ok := apexNamesMap()[moduleName]; ok {
321 for an := range apexNames {
322 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 return true
324 }
325 }
326 }
327 return false
328}
329
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900330// Tests whether a module named module is depended on (including both
331// direct and indirect dependencies) by any APEX.
332func InAnyApex(moduleName string) bool {
333 apexNamesMapMutex.Lock()
334 defer apexNamesMapMutex.Unlock()
335 apexNames, ok := apexNamesMap()[moduleName]
336 return ok && len(apexNames) > 0
337}
338
339func GetApexesForModule(moduleName string) []string {
340 ret := []string{}
341 apexNamesMapMutex.Lock()
342 defer apexNamesMapMutex.Unlock()
343 if apexNames, ok := apexNamesMap()[moduleName]; ok {
344 for an := range apexNames {
345 ret = append(ret, an)
346 }
347 }
348 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900349}
350
Jiyong Park9d452992018-10-03 00:38:19 +0900351func InitApexModule(m ApexModule) {
352 base := m.apexModuleBase()
353 base.canHaveApexVariants = true
354
355 m.AddProperties(&base.ApexProperties)
356}