blob: 17ec9b19577505099f9dea601994853af7304679 [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".
Jiyong Park9a1e14e2020-02-13 02:30:45 +090094 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +090095 Apex_available []string
96
Jiyong Park0ddfcd12018-12-11 01:35:25 +090097 // Name of the apex variant that this module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090098 ApexName string `blueprint:"mutated"`
99}
100
101// Provides default implementation for the ApexModule interface. APEX-aware
102// modules are expected to include this struct and call InitApexModule().
103type ApexModuleBase struct {
104 ApexProperties ApexProperties
105
106 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700107
108 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
109 apexVariations []string
Jiyong Park9d452992018-10-03 00:38:19 +0900110}
111
112func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
113 return m
114}
115
Jiyong Parkf760cae2020-02-12 07:53:12 +0900116func (m *ApexModuleBase) BuildForApexes(apexNames []string) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700117 m.apexVariationsLock.Lock()
118 defer m.apexVariationsLock.Unlock()
Jiyong Parkf760cae2020-02-12 07:53:12 +0900119 for _, apexName := range apexNames {
120 if !InList(apexName, m.apexVariations) {
121 m.apexVariations = append(m.apexVariations, apexName)
122 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900123 }
124}
125
Jiyong Parkf760cae2020-02-12 07:53:12 +0900126func (m *ApexModuleBase) ApexVariations() []string {
127 return m.apexVariations
128}
129
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900130func (m *ApexModuleBase) ApexName() string {
131 return m.ApexProperties.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900132}
133
134func (m *ApexModuleBase) IsForPlatform() bool {
135 return m.ApexProperties.ApexName == ""
136}
137
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900138func (m *ApexModuleBase) setApexName(apexName string) {
139 m.ApexProperties.ApexName = apexName
Jiyong Park9d452992018-10-03 00:38:19 +0900140}
141
142func (m *ApexModuleBase) CanHaveApexVariants() bool {
143 return m.canHaveApexVariants
144}
145
146func (m *ApexModuleBase) IsInstallableToApex() bool {
147 // should be overriden if needed
148 return false
149}
150
Jiyong Park127b40b2019-09-30 16:04:35 +0900151const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900152 AvailableToPlatform = "//apex_available:platform"
Jiyong Park127b40b2019-09-30 16:04:35 +0900153 availableToAnyApex = "//apex_available:anyapex"
154)
155
Jiyong Parka90ca002019-10-07 15:47:24 +0900156func CheckAvailableForApex(what string, apex_available []string) bool {
157 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000158 // apex_available defaults to ["//apex_available:platform"],
159 // which means 'available to the platform but no apexes'.
160 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900161 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900162 return InList(what, apex_available) ||
Jiyong Parkb02bb402019-12-03 00:43:57 +0900163 (what != AvailableToPlatform && InList(availableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900164}
165
166func (m *ApexModuleBase) AvailableFor(what string) bool {
167 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900168}
169
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900170func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
171 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
172 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
173 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
174 return true
175}
176
Jiyong Park127b40b2019-09-30 16:04:35 +0900177func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
178 for _, n := range m.ApexProperties.Apex_available {
Jiyong Parkb02bb402019-12-03 00:43:57 +0900179 if n == AvailableToPlatform || n == availableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900180 continue
181 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100182 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900183 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
184 }
185 }
186}
187
Colin Cross43b92e02019-11-18 15:28:57 -0800188func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900189 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900190 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900191
Colin Crosscefa94bd2019-06-03 15:07:03 -0700192 sort.Strings(m.apexVariations)
Jiyong Park127b40b2019-09-30 16:04:35 +0900193 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900194 variations = append(variations, "") // Original variation for platform
Logan Chien3aeedc92018-12-26 15:32:21 +0800195 variations = append(variations, m.apexVariations...)
196
Jiyong Park3ff16992019-12-27 14:11:47 +0900197 defaultVariation := ""
198 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900199
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900200 modules := mctx.CreateVariations(variations...)
201 for i, m := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900202 platformVariation := i == 0
203 if platformVariation && !mctx.Host() && !m.(ApexModule).AvailableFor(AvailableToPlatform) {
204 m.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900205 }
206 m.(ApexModule).setApexName(variations[i])
207 }
208 return modules
209 }
210 return nil
211}
212
213var apexData OncePer
214var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800215var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900216
217// This structure maintains the global mapping in between modules and APEXes.
218// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900219//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900220// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
221// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
222// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
223func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800224 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900225 return make(map[string]map[string]bool)
226 }).(map[string]map[string]bool)
227}
228
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900229// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900230// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900231// is explicitly listed in the build definition of the APEX via properties like
232// native_shared_libs, java_libs, etc.
Jiyong Parkf760cae2020-02-12 07:53:12 +0900233func UpdateApexDependency(apexNames []string, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900234 apexNamesMapMutex.Lock()
235 defer apexNamesMapMutex.Unlock()
Jiyong Parkf760cae2020-02-12 07:53:12 +0900236 for _, apexName := range apexNames {
237 apexesForModule, ok := apexNamesMap()[moduleName]
238 if !ok {
239 apexesForModule = make(map[string]bool)
240 apexNamesMap()[moduleName] = apexesForModule
241 }
242 apexesForModule[apexName] = apexesForModule[apexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900243 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244}
245
Jooyung Han671f1ce2019-12-17 12:47:13 +0900246// TODO(b/146393795): remove this when b/146393795 is fixed
247func ClearApexDependency() {
248 m := apexNamesMap()
249 for k := range m {
250 delete(m, k)
251 }
252}
253
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900254// Tests whether a module named moduleName is directly depended on by an APEX
255// named apexName.
256func DirectlyInApex(apexName string, moduleName string) bool {
257 apexNamesMapMutex.Lock()
258 defer apexNamesMapMutex.Unlock()
259 if apexNames, ok := apexNamesMap()[moduleName]; ok {
260 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261 }
262 return false
263}
264
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000265type hostContext interface {
266 Host() bool
267}
268
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900269// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000270func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
271 if ctx.Host() {
272 // Host has no APEX.
273 return false
274 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900275 apexNamesMapMutex.Lock()
276 defer apexNamesMapMutex.Unlock()
277 if apexNames, ok := apexNamesMap()[moduleName]; ok {
278 for an := range apexNames {
279 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 return true
281 }
282 }
283 }
284 return false
285}
286
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900287// Tests whether a module named module is depended on (including both
288// direct and indirect dependencies) by any APEX.
289func InAnyApex(moduleName string) bool {
290 apexNamesMapMutex.Lock()
291 defer apexNamesMapMutex.Unlock()
292 apexNames, ok := apexNamesMap()[moduleName]
293 return ok && len(apexNames) > 0
294}
295
296func GetApexesForModule(moduleName string) []string {
297 ret := []string{}
298 apexNamesMapMutex.Lock()
299 defer apexNamesMapMutex.Unlock()
300 if apexNames, ok := apexNamesMap()[moduleName]; ok {
301 for an := range apexNames {
302 ret = append(ret, an)
303 }
304 }
305 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900306}
307
Jiyong Park9d452992018-10-03 00:38:19 +0900308func InitApexModule(m ApexModule) {
309 base := m.apexModuleBase()
310 base.canHaveApexVariants = true
311
312 m.AddProperties(&base.ApexProperties)
313}