blob: 557febfc9a304470bda8896759c2d88bd813b158 [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"
Jiyong Park4f7dd9b2019-08-12 10:37:49 +090022 "github.com/google/blueprint/proptools"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090023)
Jiyong Park25fc6a92018-11-18 18:02:45 +090024
Jiyong Park9d452992018-10-03 00:38:19 +090025// ApexModule is the interface that a module type is expected to implement if
26// the module has to be built differently depending on whether the module
27// is destined for an apex or not (installed to one of the regular partitions).
28//
29// Native shared libraries are one such module type; when it is built for an
30// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
31// or C APIs from other APEXs.
32//
33// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090034// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090035// in one or more APEXs. Specifically, if a module is included in apex.foo and
36// apex.bar then three apex variants are created: platform, apex.foo and
37// apex.bar. The platform variant is for the regular partitions
38// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
39// respectively.
40type ApexModule interface {
41 Module
42 apexModuleBase() *ApexModuleBase
43
Jiyong Park0ddfcd12018-12-11 01:35:25 +090044 // Marks that this module should be built for the APEX of the specified name.
45 // Call this before apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090046 BuildForApex(apexName string)
47
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
72 // for an APEX marked via BuildForApex().
73 CreateApexVariations(mctx BottomUpMutatorContext) []blueprint.Module
74
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
79 // Return the no_apex property
80 NoApex() bool
Jiyong Park127b40b2019-09-30 16:04:35 +090081
82 // Tests if this module is available for the specified APEX or ":platform"
83 AvailableFor(what string) bool
Jiyong Park9d452992018-10-03 00:38:19 +090084}
85
86type ApexProperties struct {
Jiyong Park4f7dd9b2019-08-12 10:37:49 +090087 // Whether this module should not be part of any APEX. Default is false.
Jiyong Park127b40b2019-09-30 16:04:35 +090088 // TODO(b/128708192): remove this as this is equal to apex_available: [":platform"]
Jiyong Park4f7dd9b2019-08-12 10:37:49 +090089 No_apex *bool
90
Jiyong Park127b40b2019-09-30 16:04:35 +090091 // Availability of this module in APEXes. Only the listed APEXes can include this module.
92 // "//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".
94 // Default is ["//apex_available:platform", "//apex_available:anyapex"].
95 // TODO(b/128708192) change the default to ["//apex_available:platform"]
96 Apex_available []string
97
Jiyong Park0ddfcd12018-12-11 01:35:25 +090098 // Name of the apex variant that this module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090099 ApexName string `blueprint:"mutated"`
100}
101
102// Provides default implementation for the ApexModule interface. APEX-aware
103// modules are expected to include this struct and call InitApexModule().
104type ApexModuleBase struct {
105 ApexProperties ApexProperties
106
107 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700108
109 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
110 apexVariations []string
Jiyong Park9d452992018-10-03 00:38:19 +0900111}
112
113func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
114 return m
115}
116
117func (m *ApexModuleBase) BuildForApex(apexName string) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700118 m.apexVariationsLock.Lock()
119 defer m.apexVariationsLock.Unlock()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900120 if !InList(apexName, m.apexVariations) {
121 m.apexVariations = append(m.apexVariations, apexName)
122 }
123}
124
125func (m *ApexModuleBase) ApexName() string {
126 return m.ApexProperties.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900127}
128
129func (m *ApexModuleBase) IsForPlatform() bool {
130 return m.ApexProperties.ApexName == ""
131}
132
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900133func (m *ApexModuleBase) setApexName(apexName string) {
134 m.ApexProperties.ApexName = apexName
Jiyong Park9d452992018-10-03 00:38:19 +0900135}
136
137func (m *ApexModuleBase) CanHaveApexVariants() bool {
138 return m.canHaveApexVariants
139}
140
141func (m *ApexModuleBase) IsInstallableToApex() bool {
142 // should be overriden if needed
143 return false
144}
145
Jiyong Park4f7dd9b2019-08-12 10:37:49 +0900146func (m *ApexModuleBase) NoApex() bool {
147 return proptools.Bool(m.ApexProperties.No_apex)
148}
149
Jiyong Park127b40b2019-09-30 16:04:35 +0900150const (
151 availableToPlatform = "//apex_available:platform"
152 availableToAnyApex = "//apex_available:anyapex"
153)
154
155func (m *ApexModuleBase) AvailableFor(what string) bool {
156 if len(m.ApexProperties.Apex_available) == 0 {
157 // apex_available defaults to ["//apex_available:platform", "//apex_available:anyapex"],
158 // which means 'available to everybody'.
159 return true
160 }
161 return InList(what, m.ApexProperties.Apex_available) ||
162 (what != availableToPlatform && InList(availableToAnyApex, m.ApexProperties.Apex_available))
163}
164
165func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
166 for _, n := range m.ApexProperties.Apex_available {
167 if n == availableToPlatform || n == availableToAnyApex {
168 continue
169 }
170 if !mctx.OtherModuleExists(n) {
171 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
172 }
173 }
174}
175
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900176func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []blueprint.Module {
177 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900178 m.checkApexAvailableProperty(mctx)
Colin Crosscefa94bd2019-06-03 15:07:03 -0700179 sort.Strings(m.apexVariations)
Jiyong Park127b40b2019-09-30 16:04:35 +0900180 variations := []string{}
181 availableForPlatform := m.AvailableFor(availableToPlatform)
182 if availableForPlatform {
183 variations = append(variations, "") // Original variation for platform
184 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800185 variations = append(variations, m.apexVariations...)
186
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900187 modules := mctx.CreateVariations(variations...)
188 for i, m := range modules {
Jiyong Park127b40b2019-09-30 16:04:35 +0900189 if availableForPlatform && i == 0 {
Logan Chien3aeedc92018-12-26 15:32:21 +0800190 continue
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900191 }
192 m.(ApexModule).setApexName(variations[i])
193 }
194 return modules
195 }
196 return nil
197}
198
199var apexData OncePer
200var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800201var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900202
203// This structure maintains the global mapping in between modules and APEXes.
204// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900205//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900206// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
207// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
208// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
209func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800210 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900211 return make(map[string]map[string]bool)
212 }).(map[string]map[string]bool)
213}
214
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900215// Update the map to mark that a module named moduleName is directly or indirectly
216// depended on by an APEX named apexName. Directly depending means that a module
217// is explicitly listed in the build definition of the APEX via properties like
218// native_shared_libs, java_libs, etc.
219func UpdateApexDependency(apexName string, moduleName string, directDep bool) {
220 apexNamesMapMutex.Lock()
221 defer apexNamesMapMutex.Unlock()
222 apexNames, ok := apexNamesMap()[moduleName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900223 if !ok {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900224 apexNames = make(map[string]bool)
225 apexNamesMap()[moduleName] = apexNames
Jiyong Park25fc6a92018-11-18 18:02:45 +0900226 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900227 apexNames[apexName] = apexNames[apexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900228}
229
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900230// Tests whether a module named moduleName is directly depended on by an APEX
231// named apexName.
232func DirectlyInApex(apexName string, moduleName string) bool {
233 apexNamesMapMutex.Lock()
234 defer apexNamesMapMutex.Unlock()
235 if apexNames, ok := apexNamesMap()[moduleName]; ok {
236 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900237 }
238 return false
239}
240
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000241type hostContext interface {
242 Host() bool
243}
244
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900245// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000246func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
247 if ctx.Host() {
248 // Host has no APEX.
249 return false
250 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900251 apexNamesMapMutex.Lock()
252 defer apexNamesMapMutex.Unlock()
253 if apexNames, ok := apexNamesMap()[moduleName]; ok {
254 for an := range apexNames {
255 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900256 return true
257 }
258 }
259 }
260 return false
261}
262
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900263// Tests whether a module named module is depended on (including both
264// direct and indirect dependencies) by any APEX.
265func InAnyApex(moduleName string) bool {
266 apexNamesMapMutex.Lock()
267 defer apexNamesMapMutex.Unlock()
268 apexNames, ok := apexNamesMap()[moduleName]
269 return ok && len(apexNames) > 0
270}
271
272func GetApexesForModule(moduleName string) []string {
273 ret := []string{}
274 apexNamesMapMutex.Lock()
275 defer apexNamesMapMutex.Unlock()
276 if apexNames, ok := apexNamesMap()[moduleName]; ok {
277 for an := range apexNames {
278 ret = append(ret, an)
279 }
280 }
281 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900282}
283
Jiyong Park9d452992018-10-03 00:38:19 +0900284func InitApexModule(m ApexModule) {
285 base := m.apexModuleBase()
286 base.canHaveApexVariants = true
287
288 m.AddProperties(&base.ApexProperties)
289}