blob: c54809547c1d6761e664787d51ea9492fcd37ca7 [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"
20
21 "github.com/google/blueprint"
22)
Jiyong Park25fc6a92018-11-18 18:02:45 +090023
Jiyong Park9d452992018-10-03 00:38:19 +090024// ApexModule is the interface that a module type is expected to implement if
25// the module has to be built differently depending on whether the module
26// is destined for an apex or not (installed to one of the regular partitions).
27//
28// Native shared libraries are one such module type; when it is built for an
29// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
30// or C APIs from other APEXs.
31//
32// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090033// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090034// in one or more APEXs. Specifically, if a module is included in apex.foo and
35// apex.bar then three apex variants are created: platform, apex.foo and
36// apex.bar. The platform variant is for the regular partitions
37// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
38// respectively.
39type ApexModule interface {
40 Module
41 apexModuleBase() *ApexModuleBase
42
Jiyong Park0ddfcd12018-12-11 01:35:25 +090043 // Marks that this module should be built for the APEX of the specified name.
44 // Call this before apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090045 BuildForApex(apexName string)
46
Jiyong Park9d452992018-10-03 00:38:19 +090047 // Returns the name of APEX that this module will be built for. Empty string
48 // is returned when 'IsForPlatform() == true'. Note that a module can be
Jiyong Park0ddfcd12018-12-11 01:35:25 +090049 // included in multiple APEXes, in which case, the module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090050 // multiple modules each of which for an APEX. This method returns the
51 // name of the APEX that a variant module is for.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090052 // Call this after apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090053 ApexName() string
54
Jiyong Park0ddfcd12018-12-11 01:35:25 +090055 // Tests whether this module will be built for the platform or not.
56 // This is a shortcut for ApexName() == ""
57 IsForPlatform() bool
58
59 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +090060 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +090061 // for not creating APEX variants for certain types of shared libraries
62 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +090063 CanHaveApexVariants() bool
64
65 // Tests if this module can be installed to APEX as a file. For example,
66 // this would return true for shared libs while return false for static
67 // libs.
68 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +090069
70 // Mutate this module into one or more variants each of which is built
71 // for an APEX marked via BuildForApex().
72 CreateApexVariations(mctx BottomUpMutatorContext) []blueprint.Module
73
74 // Sets the name of the apex variant of this module. Called inside
75 // CreateApexVariations.
76 setApexName(apexName string)
Jiyong Park4f7dd9b2019-08-12 10:37:49 +090077
Jiyong Park127b40b2019-09-30 16:04:35 +090078 // Tests if this module is available for the specified APEX or ":platform"
79 AvailableFor(what string) bool
Jiyong Park9d452992018-10-03 00:38:19 +090080}
81
82type ApexProperties struct {
Jiyong Park127b40b2019-09-30 16:04:35 +090083 // Availability of this module in APEXes. Only the listed APEXes can include this module.
84 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
85 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
86 // Default is ["//apex_available:platform", "//apex_available:anyapex"].
87 // TODO(b/128708192) change the default to ["//apex_available:platform"]
88 Apex_available []string
89
Jiyong Park0ddfcd12018-12-11 01:35:25 +090090 // Name of the apex variant that this module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090091 ApexName string `blueprint:"mutated"`
92}
93
94// Provides default implementation for the ApexModule interface. APEX-aware
95// modules are expected to include this struct and call InitApexModule().
96type ApexModuleBase struct {
97 ApexProperties ApexProperties
98
99 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700100
101 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
102 apexVariations []string
Jiyong Park9d452992018-10-03 00:38:19 +0900103}
104
105func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
106 return m
107}
108
109func (m *ApexModuleBase) BuildForApex(apexName string) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700110 m.apexVariationsLock.Lock()
111 defer m.apexVariationsLock.Unlock()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900112 if !InList(apexName, m.apexVariations) {
113 m.apexVariations = append(m.apexVariations, apexName)
114 }
115}
116
117func (m *ApexModuleBase) ApexName() string {
118 return m.ApexProperties.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900119}
120
121func (m *ApexModuleBase) IsForPlatform() bool {
122 return m.ApexProperties.ApexName == ""
123}
124
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900125func (m *ApexModuleBase) setApexName(apexName string) {
126 m.ApexProperties.ApexName = apexName
Jiyong Park9d452992018-10-03 00:38:19 +0900127}
128
129func (m *ApexModuleBase) CanHaveApexVariants() bool {
130 return m.canHaveApexVariants
131}
132
133func (m *ApexModuleBase) IsInstallableToApex() bool {
134 // should be overriden if needed
135 return false
136}
137
Jiyong Park127b40b2019-09-30 16:04:35 +0900138const (
139 availableToPlatform = "//apex_available:platform"
140 availableToAnyApex = "//apex_available:anyapex"
141)
142
143func (m *ApexModuleBase) AvailableFor(what string) bool {
144 if len(m.ApexProperties.Apex_available) == 0 {
145 // apex_available defaults to ["//apex_available:platform", "//apex_available:anyapex"],
146 // which means 'available to everybody'.
147 return true
148 }
149 return InList(what, m.ApexProperties.Apex_available) ||
150 (what != availableToPlatform && InList(availableToAnyApex, m.ApexProperties.Apex_available))
151}
152
153func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
154 for _, n := range m.ApexProperties.Apex_available {
155 if n == availableToPlatform || n == availableToAnyApex {
156 continue
157 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100158 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900159 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
160 }
161 }
162}
163
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900164func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []blueprint.Module {
165 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900166 m.checkApexAvailableProperty(mctx)
Colin Crosscefa94bd2019-06-03 15:07:03 -0700167 sort.Strings(m.apexVariations)
Jiyong Park127b40b2019-09-30 16:04:35 +0900168 variations := []string{}
169 availableForPlatform := m.AvailableFor(availableToPlatform)
170 if availableForPlatform {
171 variations = append(variations, "") // Original variation for platform
172 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800173 variations = append(variations, m.apexVariations...)
174
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900175 modules := mctx.CreateVariations(variations...)
176 for i, m := range modules {
Jiyong Park127b40b2019-09-30 16:04:35 +0900177 if availableForPlatform && i == 0 {
Logan Chien3aeedc92018-12-26 15:32:21 +0800178 continue
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900179 }
180 m.(ApexModule).setApexName(variations[i])
181 }
182 return modules
183 }
184 return nil
185}
186
187var apexData OncePer
188var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800189var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900190
191// This structure maintains the global mapping in between modules and APEXes.
192// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900193//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900194// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
195// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
196// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
197func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800198 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900199 return make(map[string]map[string]bool)
200 }).(map[string]map[string]bool)
201}
202
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900203// Update the map to mark that a module named moduleName is directly or indirectly
204// depended on by an APEX named apexName. Directly depending means that a module
205// is explicitly listed in the build definition of the APEX via properties like
206// native_shared_libs, java_libs, etc.
207func UpdateApexDependency(apexName string, moduleName string, directDep bool) {
208 apexNamesMapMutex.Lock()
209 defer apexNamesMapMutex.Unlock()
210 apexNames, ok := apexNamesMap()[moduleName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900211 if !ok {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900212 apexNames = make(map[string]bool)
213 apexNamesMap()[moduleName] = apexNames
Jiyong Park25fc6a92018-11-18 18:02:45 +0900214 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900215 apexNames[apexName] = apexNames[apexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900216}
217
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900218// Tests whether a module named moduleName is directly depended on by an APEX
219// named apexName.
220func DirectlyInApex(apexName string, moduleName string) bool {
221 apexNamesMapMutex.Lock()
222 defer apexNamesMapMutex.Unlock()
223 if apexNames, ok := apexNamesMap()[moduleName]; ok {
224 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900225 }
226 return false
227}
228
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000229type hostContext interface {
230 Host() bool
231}
232
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900233// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000234func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
235 if ctx.Host() {
236 // Host has no APEX.
237 return false
238 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900239 apexNamesMapMutex.Lock()
240 defer apexNamesMapMutex.Unlock()
241 if apexNames, ok := apexNamesMap()[moduleName]; ok {
242 for an := range apexNames {
243 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244 return true
245 }
246 }
247 }
248 return false
249}
250
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900251// Tests whether a module named module is depended on (including both
252// direct and indirect dependencies) by any APEX.
253func InAnyApex(moduleName string) bool {
254 apexNamesMapMutex.Lock()
255 defer apexNamesMapMutex.Unlock()
256 apexNames, ok := apexNamesMap()[moduleName]
257 return ok && len(apexNames) > 0
258}
259
260func GetApexesForModule(moduleName string) []string {
261 ret := []string{}
262 apexNamesMapMutex.Lock()
263 defer apexNamesMapMutex.Unlock()
264 if apexNames, ok := apexNamesMap()[moduleName]; ok {
265 for an := range apexNames {
266 ret = append(ret, an)
267 }
268 }
269 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900270}
271
Jiyong Park9d452992018-10-03 00:38:19 +0900272func InitApexModule(m ApexModule) {
273 base := m.apexModuleBase()
274 base.canHaveApexVariants = true
275
276 m.AddProperties(&base.ApexProperties)
277}