blob: c247e7269676a2ca7652308b8da043db65d7f5f7 [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 Parkf760cae2020-02-12 07:53:12 +090041 // Marks that this module should be built for the APEXes of the specified names.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090042 // Call this before apex.apexMutator is run.
Jiyong Parkf760cae2020-02-12 07:53:12 +090043 BuildForApexes(apexNames []string)
44
45 // Returns the name of the APEXes that this modoule will be built for
46 ApexVariations() []string
Jiyong Park9d452992018-10-03 00:38:19 +090047
Jiyong Park9d452992018-10-03 00:38:19 +090048 // Returns the name of APEX that this module will be built for. Empty string
49 // is returned when 'IsForPlatform() == true'. Note that a module can be
Jiyong Park0ddfcd12018-12-11 01:35:25 +090050 // included in multiple APEXes, in which case, the module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090051 // multiple modules each of which for an APEX. This method returns the
52 // name of the APEX that a variant module is for.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090053 // Call this after apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090054 ApexName() string
55
Jiyong Park0ddfcd12018-12-11 01:35:25 +090056 // Tests whether this module will be built for the platform or not.
57 // This is a shortcut for ApexName() == ""
58 IsForPlatform() bool
59
60 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +090061 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +090062 // for not creating APEX variants for certain types of shared libraries
63 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +090064 CanHaveApexVariants() bool
65
66 // Tests if this module can be installed to APEX as a file. For example,
67 // this would return true for shared libs while return false for static
68 // libs.
69 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +090070
71 // Mutate this module into one or more variants each of which is built
Jiyong Parkf760cae2020-02-12 07:53:12 +090072 // for an APEX marked via BuildForApexes().
Colin Cross43b92e02019-11-18 15:28:57 -080073 CreateApexVariations(mctx BottomUpMutatorContext) []Module
Jiyong Park0ddfcd12018-12-11 01:35:25 +090074
75 // Sets the name of the apex variant of this module. Called inside
76 // CreateApexVariations.
77 setApexName(apexName string)
Jiyong Park4f7dd9b2019-08-12 10:37:49 +090078
Jiyong Park127b40b2019-09-30 16:04:35 +090079 // Tests if this module is available for the specified APEX or ":platform"
80 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090081
82 // DepIsInSameApex tests if the other module 'dep' is installed to the same
83 // APEX as this module
84 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
Jiyong Park9d452992018-10-03 00:38:19 +090085}
86
87type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +000088 // Availability of this module in APEXes. Only the listed APEXes can contain
89 // this module. If the module has stubs then other APEXes and the platform may
90 // access it through them (subject to visibility).
91 //
Jiyong Park127b40b2019-09-30 16:04:35 +090092 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
93 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
94 // Default is ["//apex_available:platform", "//apex_available:anyapex"].
95 // TODO(b/128708192) change the default to ["//apex_available:platform"]
96 Apex_available []string
97
Jiyong Park0ddfcd12018-12-11 01:35:25 +090098 // Name of the apex variant that this module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090099 ApexName string `blueprint:"mutated"`
100}
101
102// Provides default implementation for the ApexModule interface. APEX-aware
103// modules are expected to include this struct and call InitApexModule().
104type ApexModuleBase struct {
105 ApexProperties ApexProperties
106
107 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700108
109 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
110 apexVariations []string
Jiyong Park9d452992018-10-03 00:38:19 +0900111}
112
113func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
114 return m
115}
116
Jiyong Parkf760cae2020-02-12 07:53:12 +0900117func (m *ApexModuleBase) BuildForApexes(apexNames []string) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700118 m.apexVariationsLock.Lock()
119 defer m.apexVariationsLock.Unlock()
Jiyong Parkf760cae2020-02-12 07:53:12 +0900120 for _, apexName := range apexNames {
121 if !InList(apexName, m.apexVariations) {
122 m.apexVariations = append(m.apexVariations, apexName)
123 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900124 }
125}
126
Jiyong Parkf760cae2020-02-12 07:53:12 +0900127func (m *ApexModuleBase) ApexVariations() []string {
128 return m.apexVariations
129}
130
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900131func (m *ApexModuleBase) ApexName() string {
132 return m.ApexProperties.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900133}
134
135func (m *ApexModuleBase) IsForPlatform() bool {
136 return m.ApexProperties.ApexName == ""
137}
138
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900139func (m *ApexModuleBase) setApexName(apexName string) {
140 m.ApexProperties.ApexName = apexName
Jiyong Park9d452992018-10-03 00:38:19 +0900141}
142
143func (m *ApexModuleBase) CanHaveApexVariants() bool {
144 return m.canHaveApexVariants
145}
146
147func (m *ApexModuleBase) IsInstallableToApex() bool {
148 // should be overriden if needed
149 return false
150}
151
Jiyong Park127b40b2019-09-30 16:04:35 +0900152const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900153 AvailableToPlatform = "//apex_available:platform"
Jiyong Park127b40b2019-09-30 16:04:35 +0900154 availableToAnyApex = "//apex_available:anyapex"
155)
156
Jiyong Parka90ca002019-10-07 15:47:24 +0900157func CheckAvailableForApex(what string, apex_available []string) bool {
158 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000159 // apex_available defaults to ["//apex_available:platform"],
160 // which means 'available to the platform but no apexes'.
161 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900162 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900163 return InList(what, apex_available) ||
Jiyong Parkb02bb402019-12-03 00:43:57 +0900164 (what != AvailableToPlatform && InList(availableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900165}
166
167func (m *ApexModuleBase) AvailableFor(what string) bool {
168 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900169}
170
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900171func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
172 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
173 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
174 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
175 return true
176}
177
Jiyong Park127b40b2019-09-30 16:04:35 +0900178func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
179 for _, n := range m.ApexProperties.Apex_available {
Jiyong Parkb02bb402019-12-03 00:43:57 +0900180 if n == AvailableToPlatform || n == availableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900181 continue
182 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100183 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900184 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
185 }
186 }
187}
188
Colin Cross43b92e02019-11-18 15:28:57 -0800189func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900190 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900191 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900192
Colin Crosscefa94bd2019-06-03 15:07:03 -0700193 sort.Strings(m.apexVariations)
Jiyong Park127b40b2019-09-30 16:04:35 +0900194 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900195 variations = append(variations, "") // Original variation for platform
Logan Chien3aeedc92018-12-26 15:32:21 +0800196 variations = append(variations, m.apexVariations...)
197
Jiyong Park3ff16992019-12-27 14:11:47 +0900198 defaultVariation := ""
199 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900200
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900201 modules := mctx.CreateVariations(variations...)
202 for i, m := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900203 platformVariation := i == 0
204 if platformVariation && !mctx.Host() && !m.(ApexModule).AvailableFor(AvailableToPlatform) {
205 m.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900206 }
207 m.(ApexModule).setApexName(variations[i])
208 }
209 return modules
210 }
211 return nil
212}
213
214var apexData OncePer
215var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800216var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900217
218// This structure maintains the global mapping in between modules and APEXes.
219// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900220//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900221// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
222// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
223// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
224func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800225 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900226 return make(map[string]map[string]bool)
227 }).(map[string]map[string]bool)
228}
229
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900230// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900231// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900232// is explicitly listed in the build definition of the APEX via properties like
233// native_shared_libs, java_libs, etc.
Jiyong Parkf760cae2020-02-12 07:53:12 +0900234func UpdateApexDependency(apexNames []string, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900235 apexNamesMapMutex.Lock()
236 defer apexNamesMapMutex.Unlock()
Jiyong Parkf760cae2020-02-12 07:53:12 +0900237 for _, apexName := range apexNames {
238 apexesForModule, ok := apexNamesMap()[moduleName]
239 if !ok {
240 apexesForModule = make(map[string]bool)
241 apexNamesMap()[moduleName] = apexesForModule
242 }
243 apexesForModule[apexName] = apexesForModule[apexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245}
246
Jooyung Han671f1ce2019-12-17 12:47:13 +0900247// TODO(b/146393795): remove this when b/146393795 is fixed
248func ClearApexDependency() {
249 m := apexNamesMap()
250 for k := range m {
251 delete(m, k)
252 }
253}
254
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900255// Tests whether a module named moduleName is directly depended on by an APEX
256// named apexName.
257func DirectlyInApex(apexName string, moduleName string) bool {
258 apexNamesMapMutex.Lock()
259 defer apexNamesMapMutex.Unlock()
260 if apexNames, ok := apexNamesMap()[moduleName]; ok {
261 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 }
263 return false
264}
265
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000266type hostContext interface {
267 Host() bool
268}
269
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900270// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000271func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
272 if ctx.Host() {
273 // Host has no APEX.
274 return false
275 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900276 apexNamesMapMutex.Lock()
277 defer apexNamesMapMutex.Unlock()
278 if apexNames, ok := apexNamesMap()[moduleName]; ok {
279 for an := range apexNames {
280 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281 return true
282 }
283 }
284 }
285 return false
286}
287
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900288// Tests whether a module named module is depended on (including both
289// direct and indirect dependencies) by any APEX.
290func InAnyApex(moduleName string) bool {
291 apexNamesMapMutex.Lock()
292 defer apexNamesMapMutex.Unlock()
293 apexNames, ok := apexNamesMap()[moduleName]
294 return ok && len(apexNames) > 0
295}
296
297func GetApexesForModule(moduleName string) []string {
298 ret := []string{}
299 apexNamesMapMutex.Lock()
300 defer apexNamesMapMutex.Unlock()
301 if apexNames, ok := apexNamesMap()[moduleName]; ok {
302 for an := range apexNames {
303 ret = append(ret, an)
304 }
305 }
306 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900307}
308
Jiyong Park9d452992018-10-03 00:38:19 +0900309func InitApexModule(m ApexModule) {
310 base := m.apexModuleBase()
311 base.canHaveApexVariants = true
312
313 m.AddProperties(&base.ApexProperties)
314}