blob: c7410a19c6f5cb1b6e7a2f01fbfb90d0b5f6277e [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 Han0c4e0162020-02-26 22:45:42 +090018 "fmt"
Colin Crosscefa94bd2019-06-03 15:07:03 -070019 "sort"
Jooyung Han0c4e0162020-02-26 22:45:42 +090020 "strconv"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090021 "sync"
Paul Duffin3766cb72020-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 Han23b0adf2020-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 Han0c4e0162020-02-26 22:45:42 +090034 MinSdkVersion int
Peter Collingbournedc4f9862020-02-12 17:13:25 -080035}
36
Paul Duffin03e7d0c2020-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 Duffin03e7d0c2020-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
Jiyong Park6a9ddc32020-04-07 16:37:39 +0900103 // Return true if this module is not available to platform (i.e. apex_available
104 // property doesn't have "//apex_available:platform"), or shouldn't be available
105 // to platform, which is the case when this module depends on other module that
106 // isn't available to platform.
107 NotAvailableForPlatform() bool
108
109 // Mark that this module is not available to platform. Set by the
110 // check-platform-availability mutator in the apex package.
111 SetNotAvailableForPlatform()
112
Jooyung Han74066602020-03-20 04:29:24 +0900113 // Returns the highest version which is <= maxSdkVersion.
114 // For example, with maxSdkVersion is 10 and versionList is [9,11]
115 // it returns 9 as string
116 ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error)
Jiyong Parkf0d01b72020-04-13 16:19:48 +0900117
118 // List of APEXes that this module tests. The module has access to
119 // the private part of the listed APEXes even when it is not included in the
120 // APEXes.
121 TestFor() []string
Jiyong Park9d452992018-10-03 00:38:19 +0900122}
123
124type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000125 // Availability of this module in APEXes. Only the listed APEXes can contain
126 // this module. If the module has stubs then other APEXes and the platform may
127 // access it through them (subject to visibility).
128 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900129 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
130 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900131 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900132 Apex_available []string
133
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800134 Info ApexInfo `blueprint:"mutated"`
Jiyong Park6a9ddc32020-04-07 16:37:39 +0900135
136 NotAvailableForPlatform bool `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900137}
138
Paul Duffin3766cb72020-04-07 15:25:44 +0100139// Marker interface that identifies dependencies that are excluded from APEX
140// contents.
141type ExcludeFromApexContentsTag interface {
142 blueprint.DependencyTag
143
144 // Method that differentiates this interface from others.
145 ExcludeFromApexContents()
146}
147
Jiyong Park9d452992018-10-03 00:38:19 +0900148// Provides default implementation for the ApexModule interface. APEX-aware
149// modules are expected to include this struct and call InitApexModule().
150type ApexModuleBase struct {
151 ApexProperties ApexProperties
152
153 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700154
155 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800156 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900157}
158
159func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
160 return m
161}
162
Paul Duffin3a6c0952020-03-04 14:22:45 +0000163func (m *ApexModuleBase) ApexAvailable() []string {
164 return m.ApexProperties.Apex_available
165}
166
Jiyong Parkf0d01b72020-04-13 16:19:48 +0900167func (m *ApexModuleBase) TestFor() []string {
168 // To be implemented by concrete types inheriting ApexModuleBase
169 return nil
170}
171
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800172func (m *ApexModuleBase) BuildForApexes(apexes []ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700173 m.apexVariationsLock.Lock()
174 defer m.apexVariationsLock.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800175nextApex:
176 for _, apex := range apexes {
177 for _, v := range m.apexVariations {
178 if v.ApexName == apex.ApexName {
179 continue nextApex
180 }
Jiyong Parkf760cae2020-02-12 07:53:12 +0900181 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800182 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900183 }
184}
185
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800186func (m *ApexModuleBase) ApexVariations() []ApexInfo {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900187 return m.apexVariations
188}
189
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900190func (m *ApexModuleBase) ApexName() string {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800191 return m.ApexProperties.Info.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900192}
193
194func (m *ApexModuleBase) IsForPlatform() bool {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800195 return m.ApexProperties.Info.ApexName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900196}
197
198func (m *ApexModuleBase) CanHaveApexVariants() bool {
199 return m.canHaveApexVariants
200}
201
202func (m *ApexModuleBase) IsInstallableToApex() bool {
203 // should be overriden if needed
204 return false
205}
206
Jiyong Park127b40b2019-09-30 16:04:35 +0900207const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900208 AvailableToPlatform = "//apex_available:platform"
Paul Duffin404db3f2020-03-06 12:30:13 +0000209 AvailableToAnyApex = "//apex_available:anyapex"
Jiyong Park127b40b2019-09-30 16:04:35 +0900210)
211
Jiyong Parka90ca002019-10-07 15:47:24 +0900212func CheckAvailableForApex(what string, apex_available []string) bool {
213 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000214 // apex_available defaults to ["//apex_available:platform"],
215 // which means 'available to the platform but no apexes'.
216 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900217 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900218 return InList(what, apex_available) ||
Paul Duffin404db3f2020-03-06 12:30:13 +0000219 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900220}
221
222func (m *ApexModuleBase) AvailableFor(what string) bool {
223 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900224}
225
Jiyong Park6a9ddc32020-04-07 16:37:39 +0900226func (m *ApexModuleBase) NotAvailableForPlatform() bool {
227 return m.ApexProperties.NotAvailableForPlatform
228}
229
230func (m *ApexModuleBase) SetNotAvailableForPlatform() {
231 m.ApexProperties.NotAvailableForPlatform = true
232}
233
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900234func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
235 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
236 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
237 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
238 return true
239}
240
Jooyung Han74066602020-03-20 04:29:24 +0900241func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) {
Jooyung Han0c4e0162020-02-26 22:45:42 +0900242 for i := range versionList {
243 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
Jooyung Han74066602020-03-20 04:29:24 +0900244 if ver <= maxSdkVersion {
Jooyung Han0c4e0162020-02-26 22:45:42 +0900245 return versionList[len(versionList)-i-1], nil
246 }
247 }
Jooyung Han74066602020-03-20 04:29:24 +0900248 return "", fmt.Errorf("not found a version(<=%d) in versionList: %v", maxSdkVersion, versionList)
Jooyung Han0c4e0162020-02-26 22:45:42 +0900249}
250
Jiyong Park127b40b2019-09-30 16:04:35 +0900251func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
252 for _, n := range m.ApexProperties.Apex_available {
Paul Duffin404db3f2020-03-06 12:30:13 +0000253 if n == AvailableToPlatform || n == AvailableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900254 continue
255 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100256 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900257 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
258 }
259 }
260}
261
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800262type byApexName []ApexInfo
263
264func (a byApexName) Len() int { return len(a) }
265func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
266func (a byApexName) Less(i, j int) bool { return a[i].ApexName < a[j].ApexName }
267
Colin Cross43b92e02019-11-18 15:28:57 -0800268func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900269 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900270 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900271
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800272 sort.Sort(byApexName(m.apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900273 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900274 variations = append(variations, "") // Original variation for platform
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800275 for _, apex := range m.apexVariations {
276 variations = append(variations, apex.ApexName)
277 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800278
Jiyong Park3ff16992019-12-27 14:11:47 +0900279 defaultVariation := ""
280 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900281
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900282 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800283 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900284 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800285 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
286 mod.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900287 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800288 if !platformVariation {
289 mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1]
290 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900291 }
292 return modules
293 }
294 return nil
295}
296
297var apexData OncePer
298var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800299var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900300
301// This structure maintains the global mapping in between modules and APEXes.
302// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900303//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900304// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
305// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
306// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
307func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800308 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900309 return make(map[string]map[string]bool)
310 }).(map[string]map[string]bool)
311}
312
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900313// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900314// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900315// is explicitly listed in the build definition of the APEX via properties like
316// native_shared_libs, java_libs, etc.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800317func UpdateApexDependency(apexes []ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900318 apexNamesMapMutex.Lock()
319 defer apexNamesMapMutex.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800320 for _, apex := range apexes {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900321 apexesForModule, ok := apexNamesMap()[moduleName]
322 if !ok {
323 apexesForModule = make(map[string]bool)
324 apexNamesMap()[moduleName] = apexesForModule
325 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800326 apexesForModule[apex.ApexName] = apexesForModule[apex.ApexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900328}
329
Jooyung Han671f1ce2019-12-17 12:47:13 +0900330// TODO(b/146393795): remove this when b/146393795 is fixed
331func ClearApexDependency() {
332 m := apexNamesMap()
333 for k := range m {
334 delete(m, k)
335 }
336}
337
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900338// Tests whether a module named moduleName is directly depended on by an APEX
339// named apexName.
340func DirectlyInApex(apexName string, moduleName string) bool {
341 apexNamesMapMutex.Lock()
342 defer apexNamesMapMutex.Unlock()
343 if apexNames, ok := apexNamesMap()[moduleName]; ok {
344 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900345 }
346 return false
347}
348
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000349type hostContext interface {
350 Host() bool
351}
352
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900353// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000354func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
355 if ctx.Host() {
356 // Host has no APEX.
357 return false
358 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900359 apexNamesMapMutex.Lock()
360 defer apexNamesMapMutex.Unlock()
361 if apexNames, ok := apexNamesMap()[moduleName]; ok {
362 for an := range apexNames {
363 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900364 return true
365 }
366 }
367 }
368 return false
369}
370
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900371// Tests whether a module named module is depended on (including both
372// direct and indirect dependencies) by any APEX.
373func InAnyApex(moduleName string) bool {
374 apexNamesMapMutex.Lock()
375 defer apexNamesMapMutex.Unlock()
376 apexNames, ok := apexNamesMap()[moduleName]
377 return ok && len(apexNames) > 0
378}
379
380func GetApexesForModule(moduleName string) []string {
381 ret := []string{}
382 apexNamesMapMutex.Lock()
383 defer apexNamesMapMutex.Unlock()
384 if apexNames, ok := apexNamesMap()[moduleName]; ok {
385 for an := range apexNames {
386 ret = append(ret, an)
387 }
388 }
389 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900390}
391
Jiyong Park9d452992018-10-03 00:38:19 +0900392func InitApexModule(m ApexModule) {
393 base := m.apexModuleBase()
394 base.canHaveApexVariants = true
395
396 m.AddProperties(&base.ApexProperties)
397}