blob: d1449904feb5488b1c87eff0204504262208f330 [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
Ulya Trafimovich7c140d82020-04-22 18:05:58 +010035 Updatable bool
Peter Collingbournedc4f9862020-02-12 17:13:25 -080036}
37
Paul Duffin923e8a52020-03-30 15:33:32 +010038// Extracted from ApexModule to make it easier to define custom subsets of the
39// ApexModule interface and improve code navigation within the IDE.
40type DepIsInSameApex interface {
41 // DepIsInSameApex tests if the other module 'dep' is installed to the same
42 // APEX as this module
43 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
44}
45
Jiyong Park9d452992018-10-03 00:38:19 +090046// ApexModule is the interface that a module type is expected to implement if
47// the module has to be built differently depending on whether the module
48// is destined for an apex or not (installed to one of the regular partitions).
49//
50// Native shared libraries are one such module type; when it is built for an
51// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
52// or C APIs from other APEXs.
53//
54// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090055// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090056// in one or more APEXs. Specifically, if a module is included in apex.foo and
57// apex.bar then three apex variants are created: platform, apex.foo and
58// apex.bar. The platform variant is for the regular partitions
59// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
60// respectively.
61type ApexModule interface {
62 Module
Paul Duffin923e8a52020-03-30 15:33:32 +010063 DepIsInSameApex
64
Jiyong Park9d452992018-10-03 00:38:19 +090065 apexModuleBase() *ApexModuleBase
66
Peter Collingbournedc4f9862020-02-12 17:13:25 -080067 // Marks that this module should be built for the specified APEXes.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090068 // Call this before apex.apexMutator is run.
Peter Collingbournedc4f9862020-02-12 17:13:25 -080069 BuildForApexes(apexes []ApexInfo)
Jiyong Parkf760cae2020-02-12 07:53:12 +090070
Peter Collingbournedc4f9862020-02-12 17:13:25 -080071 // Returns the APEXes that this module will be built for
72 ApexVariations() []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +090073
Jiyong Park9d452992018-10-03 00:38:19 +090074 // Returns the name of APEX that this module will be built for. Empty string
75 // is returned when 'IsForPlatform() == true'. Note that a module can be
Jiyong Park0ddfcd12018-12-11 01:35:25 +090076 // included in multiple APEXes, in which case, the module is mutated into
Jiyong Park9d452992018-10-03 00:38:19 +090077 // multiple modules each of which for an APEX. This method returns the
78 // name of the APEX that a variant module is for.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090079 // Call this after apex.apexMutator is run.
Jiyong Park9d452992018-10-03 00:38:19 +090080 ApexName() string
81
Jiyong Park0ddfcd12018-12-11 01:35:25 +090082 // Tests whether this module will be built for the platform or not.
83 // This is a shortcut for ApexName() == ""
84 IsForPlatform() bool
85
86 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +090087 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +090088 // for not creating APEX variants for certain types of shared libraries
89 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +090090 CanHaveApexVariants() bool
91
92 // Tests if this module can be installed to APEX as a file. For example,
93 // this would return true for shared libs while return false for static
94 // libs.
95 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +090096
97 // Mutate this module into one or more variants each of which is built
Jiyong Parkf760cae2020-02-12 07:53:12 +090098 // for an APEX marked via BuildForApexes().
Colin Cross43b92e02019-11-18 15:28:57 -080099 CreateApexVariations(mctx BottomUpMutatorContext) []Module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900100
Jiyong Park127b40b2019-09-30 16:04:35 +0900101 // Tests if this module is available for the specified APEX or ":platform"
102 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900103
Jiyong Park89e850a2020-04-07 16:37:39 +0900104 // Return true if this module is not available to platform (i.e. apex_available
105 // property doesn't have "//apex_available:platform"), or shouldn't be available
106 // to platform, which is the case when this module depends on other module that
107 // isn't available to platform.
108 NotAvailableForPlatform() bool
109
110 // Mark that this module is not available to platform. Set by the
111 // check-platform-availability mutator in the apex package.
112 SetNotAvailableForPlatform()
113
Jooyung Han75568392020-03-20 04:29:24 +0900114 // Returns the highest version which is <= maxSdkVersion.
115 // For example, with maxSdkVersion is 10 and versionList is [9,11]
116 // it returns 9 as string
117 ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error)
Ulya Trafimovich7c140d82020-04-22 18:05:58 +0100118
119 // Tests if the module comes from an updatable APEX.
120 Updatable() bool
Jiyong Park9d452992018-10-03 00:38:19 +0900121}
122
123type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000124 // Availability of this module in APEXes. Only the listed APEXes can contain
125 // this module. If the module has stubs then other APEXes and the platform may
126 // access it through them (subject to visibility).
127 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900128 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
129 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900130 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900131 Apex_available []string
132
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800133 Info ApexInfo `blueprint:"mutated"`
Jiyong Park89e850a2020-04-07 16:37:39 +0900134
135 NotAvailableForPlatform bool `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900136}
137
Paul Duffindddd5462020-04-07 15:25:44 +0100138// Marker interface that identifies dependencies that are excluded from APEX
139// contents.
140type ExcludeFromApexContentsTag interface {
141 blueprint.DependencyTag
142
143 // Method that differentiates this interface from others.
144 ExcludeFromApexContents()
145}
146
Jiyong Park9d452992018-10-03 00:38:19 +0900147// Provides default implementation for the ApexModule interface. APEX-aware
148// modules are expected to include this struct and call InitApexModule().
149type ApexModuleBase struct {
150 ApexProperties ApexProperties
151
152 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700153
154 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800155 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900156}
157
158func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
159 return m
160}
161
Paul Duffinbefa4b92020-03-04 14:22:45 +0000162func (m *ApexModuleBase) ApexAvailable() []string {
163 return m.ApexProperties.Apex_available
164}
165
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800166func (m *ApexModuleBase) BuildForApexes(apexes []ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700167 m.apexVariationsLock.Lock()
168 defer m.apexVariationsLock.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800169nextApex:
170 for _, apex := range apexes {
171 for _, v := range m.apexVariations {
172 if v.ApexName == apex.ApexName {
173 continue nextApex
174 }
Jiyong Parkf760cae2020-02-12 07:53:12 +0900175 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800176 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900177 }
178}
179
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800180func (m *ApexModuleBase) ApexVariations() []ApexInfo {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900181 return m.apexVariations
182}
183
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900184func (m *ApexModuleBase) ApexName() string {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800185 return m.ApexProperties.Info.ApexName
Jiyong Park9d452992018-10-03 00:38:19 +0900186}
187
188func (m *ApexModuleBase) IsForPlatform() bool {
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800189 return m.ApexProperties.Info.ApexName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900190}
191
192func (m *ApexModuleBase) CanHaveApexVariants() bool {
193 return m.canHaveApexVariants
194}
195
196func (m *ApexModuleBase) IsInstallableToApex() bool {
197 // should be overriden if needed
198 return false
199}
200
Jiyong Park127b40b2019-09-30 16:04:35 +0900201const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900202 AvailableToPlatform = "//apex_available:platform"
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000203 AvailableToAnyApex = "//apex_available:anyapex"
Jiyong Park127b40b2019-09-30 16:04:35 +0900204)
205
Jiyong Parka90ca002019-10-07 15:47:24 +0900206func CheckAvailableForApex(what string, apex_available []string) bool {
207 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000208 // apex_available defaults to ["//apex_available:platform"],
209 // which means 'available to the platform but no apexes'.
210 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900211 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900212 return InList(what, apex_available) ||
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000213 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900214}
215
216func (m *ApexModuleBase) AvailableFor(what string) bool {
217 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900218}
219
Jiyong Park89e850a2020-04-07 16:37:39 +0900220func (m *ApexModuleBase) NotAvailableForPlatform() bool {
221 return m.ApexProperties.NotAvailableForPlatform
222}
223
224func (m *ApexModuleBase) SetNotAvailableForPlatform() {
225 m.ApexProperties.NotAvailableForPlatform = true
226}
227
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900228func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
229 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
230 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
231 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
232 return true
233}
234
Jooyung Han75568392020-03-20 04:29:24 +0900235func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) {
Jooyung Han03b51852020-02-26 22:45:42 +0900236 for i := range versionList {
237 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
Jooyung Han75568392020-03-20 04:29:24 +0900238 if ver <= maxSdkVersion {
Jooyung Han03b51852020-02-26 22:45:42 +0900239 return versionList[len(versionList)-i-1], nil
240 }
241 }
Jooyung Han75568392020-03-20 04:29:24 +0900242 return "", fmt.Errorf("not found a version(<=%d) in versionList: %v", maxSdkVersion, versionList)
Jooyung Han03b51852020-02-26 22:45:42 +0900243}
244
Jiyong Park127b40b2019-09-30 16:04:35 +0900245func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
246 for _, n := range m.ApexProperties.Apex_available {
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000247 if n == AvailableToPlatform || n == AvailableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900248 continue
249 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100250 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900251 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
252 }
253 }
254}
255
Ulya Trafimovich7c140d82020-04-22 18:05:58 +0100256func (m *ApexModuleBase) Updatable() bool {
257 return m.ApexProperties.Info.Updatable
258}
259
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800260type byApexName []ApexInfo
261
262func (a byApexName) Len() int { return len(a) }
263func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
264func (a byApexName) Less(i, j int) bool { return a[i].ApexName < a[j].ApexName }
265
Colin Cross43b92e02019-11-18 15:28:57 -0800266func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900267 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900268 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900269
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800270 sort.Sort(byApexName(m.apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900271 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900272 variations = append(variations, "") // Original variation for platform
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800273 for _, apex := range m.apexVariations {
274 variations = append(variations, apex.ApexName)
275 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800276
Jiyong Park3ff16992019-12-27 14:11:47 +0900277 defaultVariation := ""
278 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900279
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900280 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800281 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900282 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800283 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
284 mod.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900285 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800286 if !platformVariation {
287 mod.(ApexModule).apexModuleBase().ApexProperties.Info = m.apexVariations[i-1]
288 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900289 }
290 return modules
291 }
292 return nil
293}
294
295var apexData OncePer
296var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800297var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900298
299// This structure maintains the global mapping in between modules and APEXes.
300// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900301//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900302// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
303// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
304// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
305func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800306 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900307 return make(map[string]map[string]bool)
308 }).(map[string]map[string]bool)
309}
310
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900311// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900312// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900313// is explicitly listed in the build definition of the APEX via properties like
314// native_shared_libs, java_libs, etc.
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800315func UpdateApexDependency(apexes []ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900316 apexNamesMapMutex.Lock()
317 defer apexNamesMapMutex.Unlock()
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800318 for _, apex := range apexes {
Jiyong Parkf760cae2020-02-12 07:53:12 +0900319 apexesForModule, ok := apexNamesMap()[moduleName]
320 if !ok {
321 apexesForModule = make(map[string]bool)
322 apexNamesMap()[moduleName] = apexesForModule
323 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800324 apexesForModule[apex.ApexName] = apexesForModule[apex.ApexName] || directDep
Jiyong Park25fc6a92018-11-18 18:02:45 +0900325 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900326}
327
Jooyung Han671f1ce2019-12-17 12:47:13 +0900328// TODO(b/146393795): remove this when b/146393795 is fixed
329func ClearApexDependency() {
330 m := apexNamesMap()
331 for k := range m {
332 delete(m, k)
333 }
334}
335
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900336// Tests whether a module named moduleName is directly depended on by an APEX
337// named apexName.
338func DirectlyInApex(apexName string, moduleName string) bool {
339 apexNamesMapMutex.Lock()
340 defer apexNamesMapMutex.Unlock()
341 if apexNames, ok := apexNamesMap()[moduleName]; ok {
342 return apexNames[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900343 }
344 return false
345}
346
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000347type hostContext interface {
348 Host() bool
349}
350
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900351// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000352func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
353 if ctx.Host() {
354 // Host has no APEX.
355 return false
356 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900357 apexNamesMapMutex.Lock()
358 defer apexNamesMapMutex.Unlock()
359 if apexNames, ok := apexNamesMap()[moduleName]; ok {
360 for an := range apexNames {
361 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362 return true
363 }
364 }
365 }
366 return false
367}
368
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900369// Tests whether a module named module is depended on (including both
370// direct and indirect dependencies) by any APEX.
371func InAnyApex(moduleName string) bool {
372 apexNamesMapMutex.Lock()
373 defer apexNamesMapMutex.Unlock()
374 apexNames, ok := apexNamesMap()[moduleName]
375 return ok && len(apexNames) > 0
376}
377
378func GetApexesForModule(moduleName string) []string {
379 ret := []string{}
380 apexNamesMapMutex.Lock()
381 defer apexNamesMapMutex.Unlock()
382 if apexNames, ok := apexNamesMap()[moduleName]; ok {
383 for an := range apexNames {
384 ret = append(ret, an)
385 }
386 }
387 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900388}
389
Jiyong Park9d452992018-10-03 00:38:19 +0900390func InitApexModule(m ApexModule) {
391 base := m.apexModuleBase()
392 base.canHaveApexVariants = true
393
394 m.AddProperties(&base.ApexProperties)
395}