blob: 43a42df9bfc6616715c819e873e50b14313d5687 [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 (
Colin Crosscefa94bd2019-06-03 15:07:03 -070018 "sort"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090019 "sync"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090020)
Jiyong Park25fc6a92018-11-18 18:02:45 +090021
Peter Collingbournedc4f9862020-02-12 17:13:25 -080022type ApexInfo struct {
23 // Name of the apex variant that this module is mutated into
24 ApexName string
25
26 // Whether this apex variant needs to target Android 10
27 LegacyAndroid10Support bool
28}
29
Jiyong Park9d452992018-10-03 00:38:19 +090030// ApexModule is the interface that a module type is expected to implement if
31// the module has to be built differently depending on whether the module
32// is destined for an apex or not (installed to one of the regular partitions).
33//
34// Native shared libraries are one such module type; when it is built for an
35// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
36// or C APIs from other APEXs.
37//
38// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090039// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090040// in one or more APEXs. Specifically, if a module is included in apex.foo and
41// apex.bar then three apex variants are created: platform, apex.foo and
42// apex.bar. The platform variant is for the regular partitions
43// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
44// respectively.
45type ApexModule interface {
46 Module
47 apexModuleBase() *ApexModuleBase
48
Peter Collingbournedc4f9862020-02-12 17:13:25 -080049 // Marks that this module should be built for the specified APEXes.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090050 // Call this before apex.apexMutator is run.
Peter Collingbournedc4f9862020-02-12 17:13:25 -080051 BuildForApexes(apexes []ApexInfo)
Jiyong Parkf760cae2020-02-12 07:53:12 +090052
Peter Collingbournedc4f9862020-02-12 17:13:25 -080053 // Returns the APEXes that this module will be built for
54 ApexVariations() []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +090055
Jiyong Park9d452992018-10-03 00:38:19 +090056 // Returns the name of APEX that this module will be built for. Empty string
57 // is returned when 'IsForPlatform() == true'. Note that a module can be
Jiyong Park0ddfcd12018-12-11 01:35:25 +090058 // included in multiple APEXes, in which case, the module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090059 // multiple modules each of which for an APEX. This method returns the
60 // name of the APEX that a variant module is for.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090061 // Call this after apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090062 ApexName() string
63
Jiyong Park0ddfcd12018-12-11 01:35:25 +090064 // Tests whether this module will be built for the platform or not.
65 // This is a shortcut for ApexName() == ""
66 IsForPlatform() bool
67
68 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +090069 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +090070 // for not creating APEX variants for certain types of shared libraries
71 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +090072 CanHaveApexVariants() bool
73
74 // Tests if this module can be installed to APEX as a file. For example,
75 // this would return true for shared libs while return false for static
76 // libs.
77 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +090078
79 // Mutate this module into one or more variants each of which is built
Jiyong Parkf760cae2020-02-12 07:53:12 +090080 // for an APEX marked via BuildForApexes().
Colin Cross43b92e02019-11-18 15:28:57 -080081 CreateApexVariations(mctx BottomUpMutatorContext) []Module
Jiyong Park0ddfcd12018-12-11 01:35:25 +090082
Jiyong Park127b40b2019-09-30 16:04:35 +090083 // Tests if this module is available for the specified APEX or ":platform"
84 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090085
86 // DepIsInSameApex tests if the other module 'dep' is installed to the same
87 // APEX as this module
88 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
Jiyong Park9d452992018-10-03 00:38:19 +090089}
90
91type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +000092 // Availability of this module in APEXes. Only the listed APEXes can contain
93 // this module. If the module has stubs then other APEXes and the platform may
94 // access it through them (subject to visibility).
95 //
Jiyong Park127b40b2019-09-30 16:04:35 +090096 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
97 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Jiyong Park9a1e14e2020-02-13 02:30:45 +090098 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +090099 Apex_available []string
100
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800101 Info ApexInfo `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900102}
103
104// Provides default implementation for the ApexModule interface. APEX-aware
105// modules are expected to include this struct and call InitApexModule().
106type ApexModuleBase struct {
107 ApexProperties ApexProperties
108
109 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700110
111 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800112 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900113}
114
115func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
116 return m
117}
118
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800119func (m *ApexModuleBase) BuildForApexes(apexes []ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700120 m.apexVariationsLock.Lock()
121 defer m.apexVariationsLock.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800122nextApex:
123 for _, apex := range apexes {
124 for _, v := range m.apexVariations {
125 if v.ApexName == apex.ApexName {
126 continue nextApex
127 }
Jiyong Parkf760cae2020-02-12 07:53:12 +0900128 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800129 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900130 }
131}
132
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800133func (m *ApexModuleBase) ApexVariations() []ApexInfo {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900134 return m.apexVariations
135}
136
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900137func (m *ApexModuleBase) ApexName() string {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800138 return m.ApexProperties.Info.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900139}
140
141func (m *ApexModuleBase) IsForPlatform() bool {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800142 return m.ApexProperties.Info.ApexName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900143}
144
145func (m *ApexModuleBase) CanHaveApexVariants() bool {
146 return m.canHaveApexVariants
147}
148
149func (m *ApexModuleBase) IsInstallableToApex() bool {
150 // should be overriden if needed
151 return false
152}
153
Jiyong Park127b40b2019-09-30 16:04:35 +0900154const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900155 AvailableToPlatform = "//apex_available:platform"
Jiyong Park127b40b2019-09-30 16:04:35 +0900156 availableToAnyApex = "//apex_available:anyapex"
157)
158
Jiyong Parka90ca002019-10-07 15:47:24 +0900159func CheckAvailableForApex(what string, apex_available []string) bool {
160 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000161 // apex_available defaults to ["//apex_available:platform"],
162 // which means 'available to the platform but no apexes'.
163 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900164 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900165 return InList(what, apex_available) ||
Jiyong Parkb02bb402019-12-03 00:43:57 +0900166 (what != AvailableToPlatform && InList(availableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900167}
168
169func (m *ApexModuleBase) AvailableFor(what string) bool {
170 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900171}
172
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900173func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
174 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
175 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
176 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
177 return true
178}
179
Jiyong Park127b40b2019-09-30 16:04:35 +0900180func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
181 for _, n := range m.ApexProperties.Apex_available {
Jiyong Parkb02bb402019-12-03 00:43:57 +0900182 if n == AvailableToPlatform || n == availableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900183 continue
184 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100185 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900186 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
187 }
188 }
189}
190
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800191type byApexName []ApexInfo
192
193func (a byApexName) Len() int { return len(a) }
194func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
195func (a byApexName) Less(i, j int) bool { return a[i].ApexName < a[j].ApexName }
196
Colin Cross43b92e02019-11-18 15:28:57 -0800197func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900198 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900199 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900200
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800201 sort.Sort(byApexName(m.apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900202 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900203 variations = append(variations, "") // Original variation for platform
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800204 for _, apex := range m.apexVariations {
205 variations = append(variations, apex.ApexName)
206 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800207
Jiyong Park3ff16992019-12-27 14:11:47 +0900208 defaultVariation := ""
209 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900210
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900211 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800212 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900213 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800214 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
215 mod.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900216 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800217 if !platformVariation {
218 mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1]
219 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900220 }
221 return modules
222 }
223 return nil
224}
225
226var apexData OncePer
227var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800228var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900229
230// This structure maintains the global mapping in between modules and APEXes.
231// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900232//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900233// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
234// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
235// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
236func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800237 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238 return make(map[string]map[string]bool)
239 }).(map[string]map[string]bool)
240}
241
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900242// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900243// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900244// is explicitly listed in the build definition of the APEX via properties like
245// native_shared_libs, java_libs, etc.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800246func UpdateApexDependency(apexes []ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900247 apexNamesMapMutex.Lock()
248 defer apexNamesMapMutex.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800249 for _, apex := range apexes {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900250 apexesForModule, ok := apexNamesMap()[moduleName]
251 if !ok {
252 apexesForModule = make(map[string]bool)
253 apexNamesMap()[moduleName] = apexesForModule
254 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800255 apexesForModule[apex.ApexName] = apexesForModule[apex.ApexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900256 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900257}
258
Jooyung Han671f1ce2019-12-17 12:47:13 +0900259// TODO(b/146393795): remove this when b/146393795 is fixed
260func ClearApexDependency() {
261 m := apexNamesMap()
262 for k := range m {
263 delete(m, k)
264 }
265}
266
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900267// Tests whether a module named moduleName is directly depended on by an APEX
268// named apexName.
269func DirectlyInApex(apexName string, moduleName string) bool {
270 apexNamesMapMutex.Lock()
271 defer apexNamesMapMutex.Unlock()
272 if apexNames, ok := apexNamesMap()[moduleName]; ok {
273 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274 }
275 return false
276}
277
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000278type hostContext interface {
279 Host() bool
280}
281
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900282// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000283func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
284 if ctx.Host() {
285 // Host has no APEX.
286 return false
287 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900288 apexNamesMapMutex.Lock()
289 defer apexNamesMapMutex.Unlock()
290 if apexNames, ok := apexNamesMap()[moduleName]; ok {
291 for an := range apexNames {
292 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900293 return true
294 }
295 }
296 }
297 return false
298}
299
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900300// Tests whether a module named module is depended on (including both
301// direct and indirect dependencies) by any APEX.
302func InAnyApex(moduleName string) bool {
303 apexNamesMapMutex.Lock()
304 defer apexNamesMapMutex.Unlock()
305 apexNames, ok := apexNamesMap()[moduleName]
306 return ok && len(apexNames) > 0
307}
308
309func GetApexesForModule(moduleName string) []string {
310 ret := []string{}
311 apexNamesMapMutex.Lock()
312 defer apexNamesMapMutex.Unlock()
313 if apexNames, ok := apexNamesMap()[moduleName]; ok {
314 for an := range apexNames {
315 ret = append(ret, an)
316 }
317 }
318 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900319}
320
Jiyong Park9d452992018-10-03 00:38:19 +0900321func InitApexModule(m ApexModule) {
322 base := m.apexModuleBase()
323 base.canHaveApexVariants = true
324
325 m.AddProperties(&base.ApexProperties)
326}