blob: 026d685ae39f76df6087a4fc40d0aadb55930497 [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
Paul Duffinbefa4b92020-03-04 14:22:45 +0000119func (m *ApexModuleBase) ApexAvailable() []string {
120 return m.ApexProperties.Apex_available
121}
122
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800123func (m *ApexModuleBase) BuildForApexes(apexes []ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700124 m.apexVariationsLock.Lock()
125 defer m.apexVariationsLock.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800126nextApex:
127 for _, apex := range apexes {
128 for _, v := range m.apexVariations {
129 if v.ApexName == apex.ApexName {
130 continue nextApex
131 }
Jiyong Parkf760cae2020-02-12 07:53:12 +0900132 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800133 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900134 }
135}
136
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800137func (m *ApexModuleBase) ApexVariations() []ApexInfo {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900138 return m.apexVariations
139}
140
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900141func (m *ApexModuleBase) ApexName() string {
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) IsForPlatform() bool {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800146 return m.ApexProperties.Info.ApexName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900147}
148
149func (m *ApexModuleBase) CanHaveApexVariants() bool {
150 return m.canHaveApexVariants
151}
152
153func (m *ApexModuleBase) IsInstallableToApex() bool {
154 // should be overriden if needed
155 return false
156}
157
Jiyong Park127b40b2019-09-30 16:04:35 +0900158const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900159 AvailableToPlatform = "//apex_available:platform"
Jiyong Park127b40b2019-09-30 16:04:35 +0900160 availableToAnyApex = "//apex_available:anyapex"
161)
162
Jiyong Parka90ca002019-10-07 15:47:24 +0900163func CheckAvailableForApex(what string, apex_available []string) bool {
164 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000165 // apex_available defaults to ["//apex_available:platform"],
166 // which means 'available to the platform but no apexes'.
167 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900168 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900169 return InList(what, apex_available) ||
Jiyong Parkb02bb402019-12-03 00:43:57 +0900170 (what != AvailableToPlatform && InList(availableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900171}
172
173func (m *ApexModuleBase) AvailableFor(what string) bool {
174 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900175}
176
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900177func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
178 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
179 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
180 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
181 return true
182}
183
Jiyong Park127b40b2019-09-30 16:04:35 +0900184func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
185 for _, n := range m.ApexProperties.Apex_available {
Jiyong Parkb02bb402019-12-03 00:43:57 +0900186 if n == AvailableToPlatform || n == availableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900187 continue
188 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100189 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900190 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
191 }
192 }
193}
194
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800195type byApexName []ApexInfo
196
197func (a byApexName) Len() int { return len(a) }
198func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
199func (a byApexName) Less(i, j int) bool { return a[i].ApexName < a[j].ApexName }
200
Colin Cross43b92e02019-11-18 15:28:57 -0800201func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900202 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900203 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900204
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800205 sort.Sort(byApexName(m.apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900206 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900207 variations = append(variations, "") // Original variation for platform
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800208 for _, apex := range m.apexVariations {
209 variations = append(variations, apex.ApexName)
210 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800211
Jiyong Park3ff16992019-12-27 14:11:47 +0900212 defaultVariation := ""
213 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900214
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900215 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800216 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900217 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800218 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
219 mod.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900220 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800221 if !platformVariation {
222 mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1]
223 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900224 }
225 return modules
226 }
227 return nil
228}
229
230var apexData OncePer
231var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800232var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900233
234// This structure maintains the global mapping in between modules and APEXes.
235// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900236//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900237// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
238// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
239// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
240func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800241 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242 return make(map[string]map[string]bool)
243 }).(map[string]map[string]bool)
244}
245
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900246// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900247// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900248// is explicitly listed in the build definition of the APEX via properties like
249// native_shared_libs, java_libs, etc.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800250func UpdateApexDependency(apexes []ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900251 apexNamesMapMutex.Lock()
252 defer apexNamesMapMutex.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800253 for _, apex := range apexes {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900254 apexesForModule, ok := apexNamesMap()[moduleName]
255 if !ok {
256 apexesForModule = make(map[string]bool)
257 apexNamesMap()[moduleName] = apexesForModule
258 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800259 apexesForModule[apex.ApexName] = apexesForModule[apex.ApexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900260 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261}
262
Jooyung Han671f1ce2019-12-17 12:47:13 +0900263// TODO(b/146393795): remove this when b/146393795 is fixed
264func ClearApexDependency() {
265 m := apexNamesMap()
266 for k := range m {
267 delete(m, k)
268 }
269}
270
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900271// Tests whether a module named moduleName is directly depended on by an APEX
272// named apexName.
273func DirectlyInApex(apexName string, moduleName string) bool {
274 apexNamesMapMutex.Lock()
275 defer apexNamesMapMutex.Unlock()
276 if apexNames, ok := apexNamesMap()[moduleName]; ok {
277 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900278 }
279 return false
280}
281
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000282type hostContext interface {
283 Host() bool
284}
285
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900286// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000287func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
288 if ctx.Host() {
289 // Host has no APEX.
290 return false
291 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900292 apexNamesMapMutex.Lock()
293 defer apexNamesMapMutex.Unlock()
294 if apexNames, ok := apexNamesMap()[moduleName]; ok {
295 for an := range apexNames {
296 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900297 return true
298 }
299 }
300 }
301 return false
302}
303
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900304// Tests whether a module named module is depended on (including both
305// direct and indirect dependencies) by any APEX.
306func InAnyApex(moduleName string) bool {
307 apexNamesMapMutex.Lock()
308 defer apexNamesMapMutex.Unlock()
309 apexNames, ok := apexNamesMap()[moduleName]
310 return ok && len(apexNames) > 0
311}
312
313func GetApexesForModule(moduleName string) []string {
314 ret := []string{}
315 apexNamesMapMutex.Lock()
316 defer apexNamesMapMutex.Unlock()
317 if apexNames, ok := apexNamesMap()[moduleName]; ok {
318 for an := range apexNames {
319 ret = append(ret, an)
320 }
321 }
322 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900323}
324
Jiyong Park9d452992018-10-03 00:38:19 +0900325func InitApexModule(m ApexModule) {
326 base := m.apexModuleBase()
327 base.canHaveApexVariants = true
328
329 m.AddProperties(&base.ApexProperties)
330}