blob: d3c071056ad258d51663834cfac02d5b558bf54d [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
Jiyong Parka90ca002019-10-07 15:47:24 +0900143func CheckAvailableForApex(what string, apex_available []string) bool {
144 if len(apex_available) == 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900145 // apex_available defaults to ["//apex_available:platform", "//apex_available:anyapex"],
146 // which means 'available to everybody'.
147 return true
148 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900149 return InList(what, apex_available) ||
150 (what != availableToPlatform && InList(availableToAnyApex, apex_available))
151}
152
153func (m *ApexModuleBase) AvailableFor(what string) bool {
154 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900155}
156
157func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
158 for _, n := range m.ApexProperties.Apex_available {
159 if n == availableToPlatform || n == availableToAnyApex {
160 continue
161 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100162 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900163 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
164 }
165 }
166}
167
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900168func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []blueprint.Module {
169 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900170 m.checkApexAvailableProperty(mctx)
Colin Crosscefa94bd2019-06-03 15:07:03 -0700171 sort.Strings(m.apexVariations)
Jiyong Park127b40b2019-09-30 16:04:35 +0900172 variations := []string{}
Jiyong Parka90ca002019-10-07 15:47:24 +0900173 availableForPlatform := mctx.Module().(ApexModule).AvailableFor(availableToPlatform)
Jiyong Park127b40b2019-09-30 16:04:35 +0900174 if availableForPlatform {
175 variations = append(variations, "") // Original variation for platform
176 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800177 variations = append(variations, m.apexVariations...)
178
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900179 modules := mctx.CreateVariations(variations...)
180 for i, m := range modules {
Jiyong Park127b40b2019-09-30 16:04:35 +0900181 if availableForPlatform && i == 0 {
Logan Chien3aeedc92018-12-26 15:32:21 +0800182 continue
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900183 }
184 m.(ApexModule).setApexName(variations[i])
185 }
186 return modules
187 }
188 return nil
189}
190
191var apexData OncePer
192var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800193var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900194
195// This structure maintains the global mapping in between modules and APEXes.
196// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900197//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900198// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
199// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
200// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
201func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800202 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900203 return make(map[string]map[string]bool)
204 }).(map[string]map[string]bool)
205}
206
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900207// Update the map to mark that a module named moduleName is directly or indirectly
208// depended on by an APEX named apexName. Directly depending means that a module
209// is explicitly listed in the build definition of the APEX via properties like
210// native_shared_libs, java_libs, etc.
211func UpdateApexDependency(apexName string, moduleName string, directDep bool) {
212 apexNamesMapMutex.Lock()
213 defer apexNamesMapMutex.Unlock()
214 apexNames, ok := apexNamesMap()[moduleName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900215 if !ok {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900216 apexNames = make(map[string]bool)
217 apexNamesMap()[moduleName] = apexNames
Jiyong Park25fc6a92018-11-18 18:02:45 +0900218 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900219 apexNames[apexName] = apexNames[apexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900220}
221
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900222// Tests whether a module named moduleName is directly depended on by an APEX
223// named apexName.
224func DirectlyInApex(apexName string, moduleName string) bool {
225 apexNamesMapMutex.Lock()
226 defer apexNamesMapMutex.Unlock()
227 if apexNames, ok := apexNamesMap()[moduleName]; ok {
228 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229 }
230 return false
231}
232
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000233type hostContext interface {
234 Host() bool
235}
236
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900237// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000238func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
239 if ctx.Host() {
240 // Host has no APEX.
241 return false
242 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900243 apexNamesMapMutex.Lock()
244 defer apexNamesMapMutex.Unlock()
245 if apexNames, ok := apexNamesMap()[moduleName]; ok {
246 for an := range apexNames {
247 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900248 return true
249 }
250 }
251 }
252 return false
253}
254
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900255// Tests whether a module named module is depended on (including both
256// direct and indirect dependencies) by any APEX.
257func InAnyApex(moduleName string) bool {
258 apexNamesMapMutex.Lock()
259 defer apexNamesMapMutex.Unlock()
260 apexNames, ok := apexNamesMap()[moduleName]
261 return ok && len(apexNames) > 0
262}
263
264func GetApexesForModule(moduleName string) []string {
265 ret := []string{}
266 apexNamesMapMutex.Lock()
267 defer apexNamesMapMutex.Unlock()
268 if apexNames, ok := apexNamesMap()[moduleName]; ok {
269 for an := range apexNames {
270 ret = append(ret, an)
271 }
272 }
273 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900274}
275
Jiyong Park9d452992018-10-03 00:38:19 +0900276func InitApexModule(m ApexModule) {
277 base := m.apexModuleBase()
278 base.canHaveApexVariants = true
279
280 m.AddProperties(&base.ApexProperties)
281}