blob: a4a5231223eb495336dcac70b80d42e5732fcc81 [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".
Jiyong Park9a1e14e2020-02-13 02:30:45 +090091 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +090092 Apex_available []string
93
Jiyong Park0ddfcd12018-12-11 01:35:25 +090094 // Name of the apex variant that this module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090095 ApexName string `blueprint:"mutated"`
96}
97
98// Provides default implementation for the ApexModule interface. APEX-aware
99// modules are expected to include this struct and call InitApexModule().
100type ApexModuleBase struct {
101 ApexProperties ApexProperties
102
103 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700104
105 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
106 apexVariations []string
Jiyong Park9d452992018-10-03 00:38:19 +0900107}
108
109func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
110 return m
111}
112
113func (m *ApexModuleBase) BuildForApex(apexName string) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700114 m.apexVariationsLock.Lock()
115 defer m.apexVariationsLock.Unlock()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900116 if !InList(apexName, m.apexVariations) {
117 m.apexVariations = append(m.apexVariations, apexName)
118 }
119}
120
121func (m *ApexModuleBase) ApexName() string {
122 return m.ApexProperties.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900123}
124
125func (m *ApexModuleBase) IsForPlatform() bool {
126 return m.ApexProperties.ApexName == ""
127}
128
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900129func (m *ApexModuleBase) setApexName(apexName string) {
130 m.ApexProperties.ApexName = apexName
Jiyong Park9d452992018-10-03 00:38:19 +0900131}
132
133func (m *ApexModuleBase) CanHaveApexVariants() bool {
134 return m.canHaveApexVariants
135}
136
137func (m *ApexModuleBase) IsInstallableToApex() bool {
138 // should be overriden if needed
139 return false
140}
141
Jiyong Park127b40b2019-09-30 16:04:35 +0900142const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900143 AvailableToPlatform = "//apex_available:platform"
Jiyong Park127b40b2019-09-30 16:04:35 +0900144 availableToAnyApex = "//apex_available:anyapex"
145)
146
Jiyong Parka90ca002019-10-07 15:47:24 +0900147func CheckAvailableForApex(what string, apex_available []string) bool {
148 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000149 // apex_available defaults to ["//apex_available:platform"],
150 // which means 'available to the platform but no apexes'.
151 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900152 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900153 return InList(what, apex_available) ||
Jiyong Parkb02bb402019-12-03 00:43:57 +0900154 (what != AvailableToPlatform && InList(availableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900155}
156
157func (m *ApexModuleBase) AvailableFor(what string) bool {
158 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900159}
160
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900161func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
162 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
163 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
164 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
165 return true
166}
167
Jiyong Park127b40b2019-09-30 16:04:35 +0900168func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
169 for _, n := range m.ApexProperties.Apex_available {
Jiyong Parkb02bb402019-12-03 00:43:57 +0900170 if n == AvailableToPlatform || n == availableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900171 continue
172 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100173 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900174 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
175 }
176 }
177}
178
Colin Cross43b92e02019-11-18 15:28:57 -0800179func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900180 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900181 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900182
Colin Crosscefa94bd2019-06-03 15:07:03 -0700183 sort.Strings(m.apexVariations)
Jiyong Park127b40b2019-09-30 16:04:35 +0900184 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900185 variations = append(variations, "") // Original variation for platform
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 Park0f80c182020-01-31 02:49:53 +0900190
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900191 modules := mctx.CreateVariations(variations...)
192 for i, m := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900193 platformVariation := i == 0
194 if platformVariation && !mctx.Host() && !m.(ApexModule).AvailableFor(AvailableToPlatform) {
195 m.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900196 }
197 m.(ApexModule).setApexName(variations[i])
198 }
199 return modules
200 }
201 return nil
202}
203
204var apexData OncePer
205var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800206var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900207
208// This structure maintains the global mapping in between modules and APEXes.
209// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900210//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900211// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
212// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
213// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
214func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800215 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900216 return make(map[string]map[string]bool)
217 }).(map[string]map[string]bool)
218}
219
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900220// Update the map to mark that a module named moduleName is directly or indirectly
221// depended on by an APEX named apexName. Directly depending means that a module
222// is explicitly listed in the build definition of the APEX via properties like
223// native_shared_libs, java_libs, etc.
224func UpdateApexDependency(apexName string, moduleName string, directDep bool) {
225 apexNamesMapMutex.Lock()
226 defer apexNamesMapMutex.Unlock()
227 apexNames, ok := apexNamesMap()[moduleName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900228 if !ok {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900229 apexNames = make(map[string]bool)
230 apexNamesMap()[moduleName] = apexNames
Jiyong Park25fc6a92018-11-18 18:02:45 +0900231 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900232 apexNames[apexName] = apexNames[apexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900233}
234
Jooyung Han671f1ce2019-12-17 12:47:13 +0900235// TODO(b/146393795): remove this when b/146393795 is fixed
236func ClearApexDependency() {
237 m := apexNamesMap()
238 for k := range m {
239 delete(m, k)
240 }
241}
242
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900243// Tests whether a module named moduleName is directly depended on by an APEX
244// named apexName.
245func DirectlyInApex(apexName string, moduleName string) bool {
246 apexNamesMapMutex.Lock()
247 defer apexNamesMapMutex.Unlock()
248 if apexNames, ok := apexNamesMap()[moduleName]; ok {
249 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900250 }
251 return false
252}
253
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000254type hostContext interface {
255 Host() bool
256}
257
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900258// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000259func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
260 if ctx.Host() {
261 // Host has no APEX.
262 return false
263 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900264 apexNamesMapMutex.Lock()
265 defer apexNamesMapMutex.Unlock()
266 if apexNames, ok := apexNamesMap()[moduleName]; ok {
267 for an := range apexNames {
268 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900269 return true
270 }
271 }
272 }
273 return false
274}
275
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900276// Tests whether a module named module is depended on (including both
277// direct and indirect dependencies) by any APEX.
278func InAnyApex(moduleName string) bool {
279 apexNamesMapMutex.Lock()
280 defer apexNamesMapMutex.Unlock()
281 apexNames, ok := apexNamesMap()[moduleName]
282 return ok && len(apexNames) > 0
283}
284
285func GetApexesForModule(moduleName string) []string {
286 ret := []string{}
287 apexNamesMapMutex.Lock()
288 defer apexNamesMapMutex.Unlock()
289 if apexNames, ok := apexNamesMap()[moduleName]; ok {
290 for an := range apexNames {
291 ret = append(ret, an)
292 }
293 }
294 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900295}
296
Jiyong Park9d452992018-10-03 00:38:19 +0900297func InitApexModule(m ApexModule) {
298 base := m.apexModuleBase()
299 base.canHaveApexVariants = true
300
301 m.AddProperties(&base.ApexProperties)
302}