blob: 0b901ae87f7fd3c6521d8822e7b7027440a58248 [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
Jiyong Park9d452992018-10-03 00:38:19 +090022// ApexModule is the interface that a module type is expected to implement if
23// the module has to be built differently depending on whether the module
24// is destined for an apex or not (installed to one of the regular partitions).
25//
26// Native shared libraries are one such module type; when it is built for an
27// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
28// or C APIs from other APEXs.
29//
30// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090031// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090032// in one or more APEXs. Specifically, if a module is included in apex.foo and
33// apex.bar then three apex variants are created: platform, apex.foo and
34// apex.bar. The platform variant is for the regular partitions
35// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
36// respectively.
37type ApexModule interface {
38 Module
39 apexModuleBase() *ApexModuleBase
40
Jiyong Park0ddfcd12018-12-11 01:35:25 +090041 // Marks that this module should be built for the APEX of the specified name.
42 // Call this before apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090043 BuildForApex(apexName string)
44
Jiyong Park9d452992018-10-03 00:38:19 +090045 // Returns the name of APEX that this module will be built for. Empty string
46 // is returned when 'IsForPlatform() == true'. Note that a module can be
Jiyong Park0ddfcd12018-12-11 01:35:25 +090047 // included in multiple APEXes, in which case, the module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090048 // multiple modules each of which for an APEX. This method returns the
49 // name of the APEX that a variant module is for.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090050 // Call this after apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090051 ApexName() string
52
Jiyong Park0ddfcd12018-12-11 01:35:25 +090053 // Tests whether this module will be built for the platform or not.
54 // This is a shortcut for ApexName() == ""
55 IsForPlatform() bool
56
57 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +090058 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +090059 // for not creating APEX variants for certain types of shared libraries
60 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +090061 CanHaveApexVariants() bool
62
63 // Tests if this module can be installed to APEX as a file. For example,
64 // this would return true for shared libs while return false for static
65 // libs.
66 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +090067
68 // Mutate this module into one or more variants each of which is built
69 // for an APEX marked via BuildForApex().
Colin Cross43b92e02019-11-18 15:28:57 -080070 CreateApexVariations(mctx BottomUpMutatorContext) []Module
Jiyong Park0ddfcd12018-12-11 01:35:25 +090071
72 // Sets the name of the apex variant of this module. Called inside
73 // CreateApexVariations.
74 setApexName(apexName string)
Jiyong Park4f7dd9b2019-08-12 10:37:49 +090075
Jiyong Park127b40b2019-09-30 16:04:35 +090076 // Tests if this module is available for the specified APEX or ":platform"
77 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090078
79 // DepIsInSameApex tests if the other module 'dep' is installed to the same
80 // APEX as this module
81 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
Jiyong Park9d452992018-10-03 00:38:19 +090082}
83
84type ApexProperties struct {
Jiyong Park127b40b2019-09-30 16:04:35 +090085 // Availability of this module in APEXes. Only the listed APEXes can include this module.
86 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
87 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
88 // Default is ["//apex_available:platform", "//apex_available:anyapex"].
89 // TODO(b/128708192) change the default to ["//apex_available:platform"]
90 Apex_available []string
91
Jiyong Park0ddfcd12018-12-11 01:35:25 +090092 // Name of the apex variant that this module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090093 ApexName string `blueprint:"mutated"`
94}
95
96// Provides default implementation for the ApexModule interface. APEX-aware
97// modules are expected to include this struct and call InitApexModule().
98type ApexModuleBase struct {
99 ApexProperties ApexProperties
100
101 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700102
103 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
104 apexVariations []string
Jiyong Park9d452992018-10-03 00:38:19 +0900105}
106
107func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
108 return m
109}
110
111func (m *ApexModuleBase) BuildForApex(apexName string) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700112 m.apexVariationsLock.Lock()
113 defer m.apexVariationsLock.Unlock()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900114 if !InList(apexName, m.apexVariations) {
115 m.apexVariations = append(m.apexVariations, apexName)
116 }
117}
118
119func (m *ApexModuleBase) ApexName() string {
120 return m.ApexProperties.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900121}
122
123func (m *ApexModuleBase) IsForPlatform() bool {
124 return m.ApexProperties.ApexName == ""
125}
126
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900127func (m *ApexModuleBase) setApexName(apexName string) {
128 m.ApexProperties.ApexName = apexName
Jiyong Park9d452992018-10-03 00:38:19 +0900129}
130
131func (m *ApexModuleBase) CanHaveApexVariants() bool {
132 return m.canHaveApexVariants
133}
134
135func (m *ApexModuleBase) IsInstallableToApex() bool {
136 // should be overriden if needed
137 return false
138}
139
Jiyong Park127b40b2019-09-30 16:04:35 +0900140const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900141 AvailableToPlatform = "//apex_available:platform"
Jiyong Park127b40b2019-09-30 16:04:35 +0900142 availableToAnyApex = "//apex_available:anyapex"
143)
144
Jiyong Parka90ca002019-10-07 15:47:24 +0900145func CheckAvailableForApex(what string, apex_available []string) bool {
146 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000147 // apex_available defaults to ["//apex_available:platform"],
148 // which means 'available to the platform but no apexes'.
149 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900150 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900151 return InList(what, apex_available) ||
Jiyong Parkb02bb402019-12-03 00:43:57 +0900152 (what != AvailableToPlatform && InList(availableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900153}
154
155func (m *ApexModuleBase) AvailableFor(what string) bool {
156 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900157}
158
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900159func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
160 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
161 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
162 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
163 return true
164}
165
Jiyong Park127b40b2019-09-30 16:04:35 +0900166func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
167 for _, n := range m.ApexProperties.Apex_available {
Jiyong Parkb02bb402019-12-03 00:43:57 +0900168 if n == AvailableToPlatform || n == availableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900169 continue
170 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100171 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900172 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
173 }
174 }
175}
176
Colin Cross43b92e02019-11-18 15:28:57 -0800177func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900178 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900179 m.checkApexAvailableProperty(mctx)
Colin Crosscefa94bd2019-06-03 15:07:03 -0700180 sort.Strings(m.apexVariations)
Jiyong Park127b40b2019-09-30 16:04:35 +0900181 variations := []string{}
Jiyong Parkb02bb402019-12-03 00:43:57 +0900182 availableForPlatform := mctx.Module().(ApexModule).AvailableFor(AvailableToPlatform) || mctx.Host()
Jiyong Park127b40b2019-09-30 16:04:35 +0900183 if availableForPlatform {
184 variations = append(variations, "") // Original variation for platform
185 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800186 variations = append(variations, m.apexVariations...)
187
Jiyong Park3ff16992019-12-27 14:11:47 +0900188 defaultVariation := ""
189 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900190 modules := mctx.CreateVariations(variations...)
191 for i, m := range modules {
Jiyong Park127b40b2019-09-30 16:04:35 +0900192 if availableForPlatform && i == 0 {
Logan Chien3aeedc92018-12-26 15:32:21 +0800193 continue
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900194 }
195 m.(ApexModule).setApexName(variations[i])
196 }
197 return modules
198 }
199 return nil
200}
201
202var apexData OncePer
203var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800204var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900205
206// This structure maintains the global mapping in between modules and APEXes.
207// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900208//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900209// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
210// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
211// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
212func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800213 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900214 return make(map[string]map[string]bool)
215 }).(map[string]map[string]bool)
216}
217
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900218// Update the map to mark that a module named moduleName is directly or indirectly
219// depended on by an APEX named apexName. Directly depending means that a module
220// is explicitly listed in the build definition of the APEX via properties like
221// native_shared_libs, java_libs, etc.
222func UpdateApexDependency(apexName string, moduleName string, directDep bool) {
223 apexNamesMapMutex.Lock()
224 defer apexNamesMapMutex.Unlock()
225 apexNames, ok := apexNamesMap()[moduleName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900226 if !ok {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900227 apexNames = make(map[string]bool)
228 apexNamesMap()[moduleName] = apexNames
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900230 apexNames[apexName] = apexNames[apexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900231}
232
Jooyung Han671f1ce2019-12-17 12:47:13 +0900233// TODO(b/146393795): remove this when b/146393795 is fixed
234func ClearApexDependency() {
235 m := apexNamesMap()
236 for k := range m {
237 delete(m, k)
238 }
239}
240
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900241// Tests whether a module named moduleName is directly depended on by an APEX
242// named apexName.
243func DirectlyInApex(apexName string, moduleName string) bool {
244 apexNamesMapMutex.Lock()
245 defer apexNamesMapMutex.Unlock()
246 if apexNames, ok := apexNamesMap()[moduleName]; ok {
247 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900248 }
249 return false
250}
251
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000252type hostContext interface {
253 Host() bool
254}
255
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900256// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000257func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
258 if ctx.Host() {
259 // Host has no APEX.
260 return false
261 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900262 apexNamesMapMutex.Lock()
263 defer apexNamesMapMutex.Unlock()
264 if apexNames, ok := apexNamesMap()[moduleName]; ok {
265 for an := range apexNames {
266 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900267 return true
268 }
269 }
270 }
271 return false
272}
273
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900274// Tests whether a module named module is depended on (including both
275// direct and indirect dependencies) by any APEX.
276func InAnyApex(moduleName string) bool {
277 apexNamesMapMutex.Lock()
278 defer apexNamesMapMutex.Unlock()
279 apexNames, ok := apexNamesMap()[moduleName]
280 return ok && len(apexNames) > 0
281}
282
283func GetApexesForModule(moduleName string) []string {
284 ret := []string{}
285 apexNamesMapMutex.Lock()
286 defer apexNamesMapMutex.Unlock()
287 if apexNames, ok := apexNamesMap()[moduleName]; ok {
288 for an := range apexNames {
289 ret = append(ret, an)
290 }
291 }
292 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900293}
294
Jiyong Park9d452992018-10-03 00:38:19 +0900295func InitApexModule(m ApexModule) {
296 base := m.apexModuleBase()
297 base.canHaveApexVariants = true
298
299 m.AddProperties(&base.ApexProperties)
300}