blob: 5118a0abb4b5381c3d0177c3d5b813742f1708d1 [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 Parka7bc8ad2019-10-15 15:20:07 +090080
81 // DepIsInSameApex tests if the other module 'dep' is installed to the same
82 // APEX as this module
83 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
Jiyong Park9d452992018-10-03 00:38:19 +090084}
85
86type ApexProperties struct {
Jiyong Park127b40b2019-09-30 16:04:35 +090087 // Availability of this module in APEXes. Only the listed APEXes can include this module.
88 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
89 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
90 // Default is ["//apex_available:platform", "//apex_available:anyapex"].
91 // TODO(b/128708192) change the default to ["//apex_available:platform"]
92 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 (
143 availableToPlatform = "//apex_available:platform"
144 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 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900149 // apex_available defaults to ["//apex_available:platform", "//apex_available:anyapex"],
150 // which means 'available to everybody'.
151 return true
152 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900153 return InList(what, apex_available) ||
154 (what != availableToPlatform && InList(availableToAnyApex, apex_available))
155}
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 {
170 if n == availableToPlatform || n == availableToAnyApex {
171 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
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900179func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []blueprint.Module {
180 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900181 m.checkApexAvailableProperty(mctx)
Colin Crosscefa94bd2019-06-03 15:07:03 -0700182 sort.Strings(m.apexVariations)
Jiyong Park127b40b2019-09-30 16:04:35 +0900183 variations := []string{}
Jiyong Parka90ca002019-10-07 15:47:24 +0900184 availableForPlatform := mctx.Module().(ApexModule).AvailableFor(availableToPlatform)
Jiyong Park127b40b2019-09-30 16:04:35 +0900185 if availableForPlatform {
186 variations = append(variations, "") // Original variation for platform
187 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800188 variations = append(variations, m.apexVariations...)
189
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900190 modules := mctx.CreateVariations(variations...)
191 for i, m := range modules {
Jiyong Park127b40b2019-09-30 16:04:35 +0900192 if availableForPlatform && i == 0 {
Logan Chien3aeedc92018-12-26 15:32:21 +0800193 continue
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900194 }
195 m.(ApexModule).setApexName(variations[i])
196 }
197 return modules
198 }
199 return nil
200}
201
202var apexData OncePer
203var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800204var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900205
206// This structure maintains the global mapping in between modules and APEXes.
207// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900208//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900209// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
210// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
211// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
212func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800213 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900214 return make(map[string]map[string]bool)
215 }).(map[string]map[string]bool)
216}
217
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900218// Update the map to mark that a module named moduleName is directly or indirectly
219// depended on by an APEX named apexName. Directly depending means that a module
220// is explicitly listed in the build definition of the APEX via properties like
221// native_shared_libs, java_libs, etc.
222func UpdateApexDependency(apexName string, moduleName string, directDep bool) {
223 apexNamesMapMutex.Lock()
224 defer apexNamesMapMutex.Unlock()
225 apexNames, ok := apexNamesMap()[moduleName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900226 if !ok {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900227 apexNames = make(map[string]bool)
228 apexNamesMap()[moduleName] = apexNames
Jiyong Park25fc6a92018-11-18 18:02:45 +0900229 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900230 apexNames[apexName] = apexNames[apexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900231}
232
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900233// Tests whether a module named moduleName is directly depended on by an APEX
234// named apexName.
235func DirectlyInApex(apexName string, moduleName string) bool {
236 apexNamesMapMutex.Lock()
237 defer apexNamesMapMutex.Unlock()
238 if apexNames, ok := apexNamesMap()[moduleName]; ok {
239 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900240 }
241 return false
242}
243
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000244type hostContext interface {
245 Host() bool
246}
247
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900248// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000249func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
250 if ctx.Host() {
251 // Host has no APEX.
252 return false
253 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900254 apexNamesMapMutex.Lock()
255 defer apexNamesMapMutex.Unlock()
256 if apexNames, ok := apexNamesMap()[moduleName]; ok {
257 for an := range apexNames {
258 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259 return true
260 }
261 }
262 }
263 return false
264}
265
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900266// Tests whether a module named module is depended on (including both
267// direct and indirect dependencies) by any APEX.
268func InAnyApex(moduleName string) bool {
269 apexNamesMapMutex.Lock()
270 defer apexNamesMapMutex.Unlock()
271 apexNames, ok := apexNamesMap()[moduleName]
272 return ok && len(apexNames) > 0
273}
274
275func GetApexesForModule(moduleName string) []string {
276 ret := []string{}
277 apexNamesMapMutex.Lock()
278 defer apexNamesMapMutex.Unlock()
279 if apexNames, ok := apexNamesMap()[moduleName]; ok {
280 for an := range apexNames {
281 ret = append(ret, an)
282 }
283 }
284 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900285}
286
Jiyong Park9d452992018-10-03 00:38:19 +0900287func InitApexModule(m ApexModule) {
288 base := m.apexModuleBase()
289 base.canHaveApexVariants = true
290
291 m.AddProperties(&base.ApexProperties)
292}