blob: 9bf6fc7179386cb24557916ab95d0f66bdf7b410 [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 (
Jooyung Han03b51852020-02-26 22:45:42 +090018 "fmt"
Colin Crosscefa94bd2019-06-03 15:07:03 -070019 "sort"
Jooyung Han03b51852020-02-26 22:45:42 +090020 "strconv"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090021 "sync"
Paul Duffindddd5462020-04-07 15:25:44 +010022
23 "github.com/google/blueprint"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090024)
Jiyong Park25fc6a92018-11-18 18:02:45 +090025
Jooyung Han5417f772020-03-12 18:37:20 +090026const (
27 SdkVersion_Android10 = 29
28)
29
Peter Collingbournedc4f9862020-02-12 17:13:25 -080030type ApexInfo struct {
31 // Name of the apex variant that this module is mutated into
32 ApexName string
33
Jooyung Han03b51852020-02-26 22:45:42 +090034 MinSdkVersion int
Peter Collingbournedc4f9862020-02-12 17:13:25 -080035}
36
Paul Duffin923e8a52020-03-30 15:33:32 +010037// Extracted from ApexModule to make it easier to define custom subsets of the
38// ApexModule interface and improve code navigation within the IDE.
39type DepIsInSameApex interface {
40 // DepIsInSameApex tests if the other module 'dep' is installed to the same
41 // APEX as this module
42 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
43}
44
Jiyong Park9d452992018-10-03 00:38:19 +090045// ApexModule is the interface that a module type is expected to implement if
46// the module has to be built differently depending on whether the module
47// is destined for an apex or not (installed to one of the regular partitions).
48//
49// Native shared libraries are one such module type; when it is built for an
50// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
51// or C APIs from other APEXs.
52//
53// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090054// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090055// in one or more APEXs. Specifically, if a module is included in apex.foo and
56// apex.bar then three apex variants are created: platform, apex.foo and
57// apex.bar. The platform variant is for the regular partitions
58// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
59// respectively.
60type ApexModule interface {
61 Module
Paul Duffin923e8a52020-03-30 15:33:32 +010062 DepIsInSameApex
63
Jiyong Park9d452992018-10-03 00:38:19 +090064 apexModuleBase() *ApexModuleBase
65
Peter Collingbournedc4f9862020-02-12 17:13:25 -080066 // Marks that this module should be built for the specified APEXes.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090067 // Call this before apex.apexMutator is run.
Peter Collingbournedc4f9862020-02-12 17:13:25 -080068 BuildForApexes(apexes []ApexInfo)
Jiyong Parkf760cae2020-02-12 07:53:12 +090069
Peter Collingbournedc4f9862020-02-12 17:13:25 -080070 // Returns the APEXes that this module will be built for
71 ApexVariations() []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +090072
Jiyong Park9d452992018-10-03 00:38:19 +090073 // Returns the name of APEX that this module will be built for. Empty string
74 // is returned when 'IsForPlatform() == true'. Note that a module can be
Jiyong Park0ddfcd12018-12-11 01:35:25 +090075 // included in multiple APEXes, in which case, the module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090076 // multiple modules each of which for an APEX. This method returns the
77 // name of the APEX that a variant module is for.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090078 // Call this after apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090079 ApexName() string
80
Jiyong Park0ddfcd12018-12-11 01:35:25 +090081 // Tests whether this module will be built for the platform or not.
82 // This is a shortcut for ApexName() == ""
83 IsForPlatform() bool
84
85 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +090086 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +090087 // for not creating APEX variants for certain types of shared libraries
88 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +090089 CanHaveApexVariants() bool
90
91 // Tests if this module can be installed to APEX as a file. For example,
92 // this would return true for shared libs while return false for static
93 // libs.
94 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +090095
96 // Mutate this module into one or more variants each of which is built
Jiyong Parkf760cae2020-02-12 07:53:12 +090097 // for an APEX marked via BuildForApexes().
Colin Cross43b92e02019-11-18 15:28:57 -080098 CreateApexVariations(mctx BottomUpMutatorContext) []Module
Jiyong Park0ddfcd12018-12-11 01:35:25 +090099
Jiyong Park127b40b2019-09-30 16:04:35 +0900100 // Tests if this module is available for the specified APEX or ":platform"
101 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900102
Jooyung Han75568392020-03-20 04:29:24 +0900103 // Returns the highest version which is <= maxSdkVersion.
104 // For example, with maxSdkVersion is 10 and versionList is [9,11]
105 // it returns 9 as string
106 ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error)
Jiyong Park9d452992018-10-03 00:38:19 +0900107}
108
109type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000110 // Availability of this module in APEXes. Only the listed APEXes can contain
111 // this module. If the module has stubs then other APEXes and the platform may
112 // access it through them (subject to visibility).
113 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900114 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
115 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900116 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900117 Apex_available []string
118
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800119 Info ApexInfo `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900120}
121
Paul Duffindddd5462020-04-07 15:25:44 +0100122// Marker interface that identifies dependencies that are excluded from APEX
123// contents.
124type ExcludeFromApexContentsTag interface {
125 blueprint.DependencyTag
126
127 // Method that differentiates this interface from others.
128 ExcludeFromApexContents()
129}
130
Jiyong Park9d452992018-10-03 00:38:19 +0900131// Provides default implementation for the ApexModule interface. APEX-aware
132// modules are expected to include this struct and call InitApexModule().
133type ApexModuleBase struct {
134 ApexProperties ApexProperties
135
136 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700137
138 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800139 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900140}
141
142func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
143 return m
144}
145
Paul Duffinbefa4b92020-03-04 14:22:45 +0000146func (m *ApexModuleBase) ApexAvailable() []string {
147 return m.ApexProperties.Apex_available
148}
149
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800150func (m *ApexModuleBase) BuildForApexes(apexes []ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700151 m.apexVariationsLock.Lock()
152 defer m.apexVariationsLock.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800153nextApex:
154 for _, apex := range apexes {
155 for _, v := range m.apexVariations {
156 if v.ApexName == apex.ApexName {
157 continue nextApex
158 }
Jiyong Parkf760cae2020-02-12 07:53:12 +0900159 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800160 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900161 }
162}
163
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800164func (m *ApexModuleBase) ApexVariations() []ApexInfo {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900165 return m.apexVariations
166}
167
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900168func (m *ApexModuleBase) ApexName() string {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800169 return m.ApexProperties.Info.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900170}
171
172func (m *ApexModuleBase) IsForPlatform() bool {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800173 return m.ApexProperties.Info.ApexName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900174}
175
176func (m *ApexModuleBase) CanHaveApexVariants() bool {
177 return m.canHaveApexVariants
178}
179
180func (m *ApexModuleBase) IsInstallableToApex() bool {
181 // should be overriden if needed
182 return false
183}
184
Jiyong Park127b40b2019-09-30 16:04:35 +0900185const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900186 AvailableToPlatform = "//apex_available:platform"
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000187 AvailableToAnyApex = "//apex_available:anyapex"
Jiyong Park127b40b2019-09-30 16:04:35 +0900188)
189
Jiyong Parka90ca002019-10-07 15:47:24 +0900190func CheckAvailableForApex(what string, apex_available []string) bool {
191 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000192 // apex_available defaults to ["//apex_available:platform"],
193 // which means 'available to the platform but no apexes'.
194 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900195 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900196 return InList(what, apex_available) ||
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000197 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900198}
199
200func (m *ApexModuleBase) AvailableFor(what string) bool {
201 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900202}
203
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900204func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
205 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
206 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
207 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
208 return true
209}
210
Jooyung Han75568392020-03-20 04:29:24 +0900211func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) {
Jooyung Han03b51852020-02-26 22:45:42 +0900212 for i := range versionList {
213 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
Jooyung Han75568392020-03-20 04:29:24 +0900214 if ver <= maxSdkVersion {
Jooyung Han03b51852020-02-26 22:45:42 +0900215 return versionList[len(versionList)-i-1], nil
216 }
217 }
Jooyung Han75568392020-03-20 04:29:24 +0900218 return "", fmt.Errorf("not found a version(<=%d) in versionList: %v", maxSdkVersion, versionList)
Jooyung Han03b51852020-02-26 22:45:42 +0900219}
220
Jiyong Park127b40b2019-09-30 16:04:35 +0900221func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
222 for _, n := range m.ApexProperties.Apex_available {
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000223 if n == AvailableToPlatform || n == AvailableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900224 continue
225 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100226 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900227 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
228 }
229 }
230}
231
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800232type byApexName []ApexInfo
233
234func (a byApexName) Len() int { return len(a) }
235func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
236func (a byApexName) Less(i, j int) bool { return a[i].ApexName < a[j].ApexName }
237
Colin Cross43b92e02019-11-18 15:28:57 -0800238func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900239 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900240 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900241
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800242 sort.Sort(byApexName(m.apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900243 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900244 variations = append(variations, "") // Original variation for platform
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800245 for _, apex := range m.apexVariations {
246 variations = append(variations, apex.ApexName)
247 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800248
Jiyong Park3ff16992019-12-27 14:11:47 +0900249 defaultVariation := ""
250 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900251
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900252 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800253 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900254 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800255 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
256 mod.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900257 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800258 if !platformVariation {
259 mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1]
260 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900261 }
262 return modules
263 }
264 return nil
265}
266
267var apexData OncePer
268var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800269var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900270
271// This structure maintains the global mapping in between modules and APEXes.
272// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900274// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
275// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
276// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
277func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800278 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900279 return make(map[string]map[string]bool)
280 }).(map[string]map[string]bool)
281}
282
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900283// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900284// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900285// is explicitly listed in the build definition of the APEX via properties like
286// native_shared_libs, java_libs, etc.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800287func UpdateApexDependency(apexes []ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900288 apexNamesMapMutex.Lock()
289 defer apexNamesMapMutex.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800290 for _, apex := range apexes {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900291 apexesForModule, ok := apexNamesMap()[moduleName]
292 if !ok {
293 apexesForModule = make(map[string]bool)
294 apexNamesMap()[moduleName] = apexesForModule
295 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800296 apexesForModule[apex.ApexName] = apexesForModule[apex.ApexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900297 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900298}
299
Jooyung Han671f1ce2019-12-17 12:47:13 +0900300// TODO(b/146393795): remove this when b/146393795 is fixed
301func ClearApexDependency() {
302 m := apexNamesMap()
303 for k := range m {
304 delete(m, k)
305 }
306}
307
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900308// Tests whether a module named moduleName is directly depended on by an APEX
309// named apexName.
310func DirectlyInApex(apexName string, moduleName string) bool {
311 apexNamesMapMutex.Lock()
312 defer apexNamesMapMutex.Unlock()
313 if apexNames, ok := apexNamesMap()[moduleName]; ok {
314 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 }
316 return false
317}
318
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000319type hostContext interface {
320 Host() bool
321}
322
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900323// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000324func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
325 if ctx.Host() {
326 // Host has no APEX.
327 return false
328 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900329 apexNamesMapMutex.Lock()
330 defer apexNamesMapMutex.Unlock()
331 if apexNames, ok := apexNamesMap()[moduleName]; ok {
332 for an := range apexNames {
333 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900334 return true
335 }
336 }
337 }
338 return false
339}
340
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900341// Tests whether a module named module is depended on (including both
342// direct and indirect dependencies) by any APEX.
343func InAnyApex(moduleName string) bool {
344 apexNamesMapMutex.Lock()
345 defer apexNamesMapMutex.Unlock()
346 apexNames, ok := apexNamesMap()[moduleName]
347 return ok && len(apexNames) > 0
348}
349
350func GetApexesForModule(moduleName string) []string {
351 ret := []string{}
352 apexNamesMapMutex.Lock()
353 defer apexNamesMapMutex.Unlock()
354 if apexNames, ok := apexNamesMap()[moduleName]; ok {
355 for an := range apexNames {
356 ret = append(ret, an)
357 }
358 }
359 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900360}
361
Jiyong Park9d452992018-10-03 00:38:19 +0900362func InitApexModule(m ApexModule) {
363 base := m.apexModuleBase()
364 base.canHaveApexVariants = true
365
366 m.AddProperties(&base.ApexProperties)
367}