blob: 6c0d47abc136fd0b31294c408ce2675d71066bee [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 {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +000085 // Availability of this module in APEXes. Only the listed APEXes can contain
86 // this module. If the module has stubs then other APEXes and the platform may
87 // access it through them (subject to visibility).
88 //
Jiyong Park127b40b2019-09-30 16:04:35 +090089 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
90 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
91 // Default is ["//apex_available:platform", "//apex_available:anyapex"].
92 // TODO(b/128708192) change the default to ["//apex_available:platform"]
93 Apex_available []string
94
Jiyong Park0ddfcd12018-12-11 01:35:25 +090095 // Name of the apex variant that this module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090096 ApexName string `blueprint:"mutated"`
97}
98
99// Provides default implementation for the ApexModule interface. APEX-aware
100// modules are expected to include this struct and call InitApexModule().
101type ApexModuleBase struct {
102 ApexProperties ApexProperties
103
104 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700105
106 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
107 apexVariations []string
Jiyong Park9d452992018-10-03 00:38:19 +0900108}
109
110func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
111 return m
112}
113
114func (m *ApexModuleBase) BuildForApex(apexName string) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700115 m.apexVariationsLock.Lock()
116 defer m.apexVariationsLock.Unlock()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900117 if !InList(apexName, m.apexVariations) {
118 m.apexVariations = append(m.apexVariations, apexName)
119 }
120}
121
122func (m *ApexModuleBase) ApexName() string {
123 return m.ApexProperties.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900124}
125
126func (m *ApexModuleBase) IsForPlatform() bool {
127 return m.ApexProperties.ApexName == ""
128}
129
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900130func (m *ApexModuleBase) setApexName(apexName string) {
131 m.ApexProperties.ApexName = apexName
Jiyong Park9d452992018-10-03 00:38:19 +0900132}
133
134func (m *ApexModuleBase) CanHaveApexVariants() bool {
135 return m.canHaveApexVariants
136}
137
138func (m *ApexModuleBase) IsInstallableToApex() bool {
139 // should be overriden if needed
140 return false
141}
142
Jiyong Park127b40b2019-09-30 16:04:35 +0900143const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900144 AvailableToPlatform = "//apex_available:platform"
Jiyong Park127b40b2019-09-30 16:04:35 +0900145 availableToAnyApex = "//apex_available:anyapex"
146)
147
Jiyong Parka90ca002019-10-07 15:47:24 +0900148func CheckAvailableForApex(what string, apex_available []string) bool {
149 if len(apex_available) == 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900150 // apex_available defaults to ["//apex_available:platform", "//apex_available:anyapex"],
151 // which means 'available to everybody'.
152 return true
153 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900154 return InList(what, apex_available) ||
Jiyong Parkb02bb402019-12-03 00:43:57 +0900155 (what != AvailableToPlatform && InList(availableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900156}
157
158func (m *ApexModuleBase) AvailableFor(what string) bool {
159 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900160}
161
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900162func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
163 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
164 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
165 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
166 return true
167}
168
Jiyong Park127b40b2019-09-30 16:04:35 +0900169func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
170 for _, n := range m.ApexProperties.Apex_available {
Jiyong Parkb02bb402019-12-03 00:43:57 +0900171 if n == AvailableToPlatform || n == availableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900172 continue
173 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100174 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900175 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
176 }
177 }
178}
179
Colin Cross43b92e02019-11-18 15:28:57 -0800180func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900181 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900182 m.checkApexAvailableProperty(mctx)
Colin Crosscefa94bd2019-06-03 15:07:03 -0700183 sort.Strings(m.apexVariations)
Jiyong Park127b40b2019-09-30 16:04:35 +0900184 variations := []string{}
Jiyong Parkb02bb402019-12-03 00:43:57 +0900185 availableForPlatform := mctx.Module().(ApexModule).AvailableFor(AvailableToPlatform) || mctx.Host()
Jiyong Park127b40b2019-09-30 16:04:35 +0900186 if availableForPlatform {
187 variations = append(variations, "") // Original variation for platform
188 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800189 variations = append(variations, m.apexVariations...)
190
Jiyong Park3ff16992019-12-27 14:11:47 +0900191 defaultVariation := ""
192 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900193 modules := mctx.CreateVariations(variations...)
194 for i, m := range modules {
Jiyong Park127b40b2019-09-30 16:04:35 +0900195 if availableForPlatform && i == 0 {
Logan Chien3aeedc92018-12-26 15:32:21 +0800196 continue
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900197 }
198 m.(ApexModule).setApexName(variations[i])
199 }
200 return modules
201 }
202 return nil
203}
204
205var apexData OncePer
206var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800207var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900208
209// This structure maintains the global mapping in between modules and APEXes.
210// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900211//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900212// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
213// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
214// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
215func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800216 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900217 return make(map[string]map[string]bool)
218 }).(map[string]map[string]bool)
219}
220
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900221// Update the map to mark that a module named moduleName is directly or indirectly
222// depended on by an APEX named apexName. Directly depending means that a module
223// is explicitly listed in the build definition of the APEX via properties like
224// native_shared_libs, java_libs, etc.
225func UpdateApexDependency(apexName string, moduleName string, directDep bool) {
226 apexNamesMapMutex.Lock()
227 defer apexNamesMapMutex.Unlock()
228 apexNames, ok := apexNamesMap()[moduleName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229 if !ok {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900230 apexNames = make(map[string]bool)
231 apexNamesMap()[moduleName] = apexNames
Jiyong Park25fc6a92018-11-18 18:02:45 +0900232 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900233 apexNames[apexName] = apexNames[apexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900234}
235
Jooyung Han671f1ce2019-12-17 12:47:13 +0900236// TODO(b/146393795): remove this when b/146393795 is fixed
237func ClearApexDependency() {
238 m := apexNamesMap()
239 for k := range m {
240 delete(m, k)
241 }
242}
243
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900244// Tests whether a module named moduleName is directly depended on by an APEX
245// named apexName.
246func DirectlyInApex(apexName string, moduleName string) bool {
247 apexNamesMapMutex.Lock()
248 defer apexNamesMapMutex.Unlock()
249 if apexNames, ok := apexNamesMap()[moduleName]; ok {
250 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900251 }
252 return false
253}
254
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000255type hostContext interface {
256 Host() bool
257}
258
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900259// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000260func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
261 if ctx.Host() {
262 // Host has no APEX.
263 return false
264 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900265 apexNamesMapMutex.Lock()
266 defer apexNamesMapMutex.Unlock()
267 if apexNames, ok := apexNamesMap()[moduleName]; ok {
268 for an := range apexNames {
269 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900270 return true
271 }
272 }
273 }
274 return false
275}
276
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900277// Tests whether a module named module is depended on (including both
278// direct and indirect dependencies) by any APEX.
279func InAnyApex(moduleName string) bool {
280 apexNamesMapMutex.Lock()
281 defer apexNamesMapMutex.Unlock()
282 apexNames, ok := apexNamesMap()[moduleName]
283 return ok && len(apexNames) > 0
284}
285
286func GetApexesForModule(moduleName string) []string {
287 ret := []string{}
288 apexNamesMapMutex.Lock()
289 defer apexNamesMapMutex.Unlock()
290 if apexNames, ok := apexNamesMap()[moduleName]; ok {
291 for an := range apexNames {
292 ret = append(ret, an)
293 }
294 }
295 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900296}
297
Jiyong Park9d452992018-10-03 00:38:19 +0900298func InitApexModule(m ApexModule) {
299 base := m.apexModuleBase()
300 base.canHaveApexVariants = true
301
302 m.AddProperties(&base.ApexProperties)
303}