blob: 3039e794444ed3742a2e3db7f9cd02c38e8277f3 [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"
Artur Satayev872a1442020-04-27 17:08:37 +010021 "strings"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090022 "sync"
Paul Duffindddd5462020-04-07 15:25:44 +010023
24 "github.com/google/blueprint"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090025)
Jiyong Park25fc6a92018-11-18 18:02:45 +090026
Dan Albertc8060532020-07-22 22:32:17 -070027var (
28 SdkVersion_Android10 = uncheckedFinalApiLevel(29)
Jooyung Han5417f772020-03-12 18:37:20 +090029)
30
Colin Cross56a83212020-09-15 18:30:11 -070031// ApexInfo describes the metadata common to all modules in an apexBundle.
Peter Collingbournedc4f9862020-02-12 17:13:25 -080032type ApexInfo struct {
Colin Cross56a83212020-09-15 18:30:11 -070033 // Name of the apex variation that this module is mutated into, or "" for
34 // a platform variant. Note that a module can be included in multiple APEXes,
35 // in which case, the module is mutated into one or more variants, each of
36 // which is for one or more APEXes.
Colin Crosse07f2312020-08-13 11:24:56 -070037 ApexVariationName string
Peter Collingbournedc4f9862020-02-12 17:13:25 -080038
Dan Albertc8060532020-07-22 22:32:17 -070039 // Serialized ApiLevel. Use via MinSdkVersion() method. Cannot be stored in
40 // its struct form because this is cloned into properties structs, and
41 // ApiLevel has private members.
42 MinSdkVersionStr string
Colin Crossaede88c2020-08-11 12:17:01 -070043
Colin Cross56a83212020-09-15 18:30:11 -070044 // True if the module comes from an updatable APEX.
45 Updatable bool
46 RequiredSdks SdkRefs
47
48 InApexes []string
49 ApexContents []*ApexContents
Colin Crossaede88c2020-08-11 12:17:01 -070050}
51
Colin Cross56a83212020-09-15 18:30:11 -070052var ApexInfoProvider = blueprint.NewMutatorProvider(ApexInfo{}, "apex")
53
Colin Cross9f720ce2020-10-02 10:26:04 -070054func (i ApexInfo) mergedName(ctx PathContext) string {
Dan Albertc8060532020-07-22 22:32:17 -070055 name := "apex" + strconv.Itoa(i.MinSdkVersion(ctx).FinalOrFutureInt())
Colin Crossaede88c2020-08-11 12:17:01 -070056 for _, sdk := range i.RequiredSdks {
57 name += "_" + sdk.Name + "_" + sdk.Version
58 }
59 return name
Peter Collingbournedc4f9862020-02-12 17:13:25 -080060}
61
Colin Cross9f720ce2020-10-02 10:26:04 -070062func (this *ApexInfo) MinSdkVersion(ctx PathContext) ApiLevel {
Dan Albertc8060532020-07-22 22:32:17 -070063 return ApiLevelOrPanic(ctx, this.MinSdkVersionStr)
64}
65
Colin Cross56a83212020-09-15 18:30:11 -070066func (i ApexInfo) IsForPlatform() bool {
67 return i.ApexVariationName == ""
68}
69
70// ApexTestForInfo stores the contents of APEXes for which this module is a test and thus has
71// access to APEX internals.
72type ApexTestForInfo struct {
73 ApexContents []*ApexContents
74}
75
76var ApexTestForInfoProvider = blueprint.NewMutatorProvider(ApexTestForInfo{}, "apex_test_for")
77
Paul Duffin923e8a52020-03-30 15:33:32 +010078// Extracted from ApexModule to make it easier to define custom subsets of the
79// ApexModule interface and improve code navigation within the IDE.
80type DepIsInSameApex interface {
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
84}
85
Jiyong Park9d452992018-10-03 00:38:19 +090086// ApexModule is the interface that a module type is expected to implement if
87// the module has to be built differently depending on whether the module
88// is destined for an apex or not (installed to one of the regular partitions).
89//
90// Native shared libraries are one such module type; when it is built for an
91// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
92// or C APIs from other APEXs.
93//
94// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090095// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090096// in one or more APEXs. Specifically, if a module is included in apex.foo and
97// apex.bar then three apex variants are created: platform, apex.foo and
98// apex.bar. The platform variant is for the regular partitions
99// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
100// respectively.
101type ApexModule interface {
102 Module
Paul Duffin923e8a52020-03-30 15:33:32 +0100103 DepIsInSameApex
104
Jiyong Park9d452992018-10-03 00:38:19 +0900105 apexModuleBase() *ApexModuleBase
106
Jooyung Han698dd9f2020-07-22 15:17:19 +0900107 // Marks that this module should be built for the specified APEX.
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900108 // Call this before apex.apexMutator is run.
Jooyung Han698dd9f2020-07-22 15:17:19 +0900109 BuildForApex(apex ApexInfo)
Jiyong Parkf760cae2020-02-12 07:53:12 +0900110
Colin Cross56a83212020-09-15 18:30:11 -0700111 // Returns true if this module is present in any APEXes
112 // directly or indirectly.
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900113 // Call this after apex.apexMutator is run.
Colin Cross56a83212020-09-15 18:30:11 -0700114 InAnyApex() bool
Jiyong Park9d452992018-10-03 00:38:19 +0900115
Colin Cross56a83212020-09-15 18:30:11 -0700116 // Returns true if this module is directly in any APEXes.
Colin Crossaede88c2020-08-11 12:17:01 -0700117 // Call this after apex.apexMutator is run.
Colin Cross56a83212020-09-15 18:30:11 -0700118 DirectlyInAnyApex() bool
Colin Crossaede88c2020-08-11 12:17:01 -0700119
Colin Cross56a83212020-09-15 18:30:11 -0700120 // Returns true if any variant of this module is directly in any APEXes.
121 // Call this after apex.apexMutator is run.
122 AnyVariantDirectlyInAnyApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900123
124 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +0900125 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900126 // for not creating APEX variants for certain types of shared libraries
127 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +0900128 CanHaveApexVariants() bool
129
130 // Tests if this module can be installed to APEX as a file. For example,
131 // this would return true for shared libs while return false for static
132 // libs.
133 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900134
Jiyong Park127b40b2019-09-30 16:04:35 +0900135 // Tests if this module is available for the specified APEX or ":platform"
136 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900137
Jiyong Park89e850a2020-04-07 16:37:39 +0900138 // Return true if this module is not available to platform (i.e. apex_available
139 // property doesn't have "//apex_available:platform"), or shouldn't be available
140 // to platform, which is the case when this module depends on other module that
141 // isn't available to platform.
142 NotAvailableForPlatform() bool
143
144 // Mark that this module is not available to platform. Set by the
145 // check-platform-availability mutator in the apex package.
146 SetNotAvailableForPlatform()
147
Jiyong Park62304bb2020-04-13 16:19:48 +0900148 // List of APEXes that this module tests. The module has access to
149 // the private part of the listed APEXes even when it is not included in the
150 // APEXes.
151 TestFor() []string
Jooyung Han749dc692020-04-15 11:03:39 +0900152
153 // Returns nil if this module supports sdkVersion
154 // Otherwise, returns error with reason
Dan Albertc8060532020-07-22 22:32:17 -0700155 ShouldSupportSdkVersion(ctx BaseModuleContext, sdkVersion ApiLevel) error
Colin Crossaede88c2020-08-11 12:17:01 -0700156
157 // Returns true if this module needs a unique variation per apex, for example if
158 // use_apex_name_macro is set.
159 UniqueApexVariations() bool
Jiyong Park9d452992018-10-03 00:38:19 +0900160}
161
162type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000163 // Availability of this module in APEXes. Only the listed APEXes can contain
164 // this module. If the module has stubs then other APEXes and the platform may
165 // access it through them (subject to visibility).
166 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900167 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
168 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Yifan Hongd22a84a2020-07-28 17:37:46 -0700169 // "com.android.gki.*" matches any APEX module name with the prefix "com.android.gki.".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900170 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900171 Apex_available []string
172
Colin Cross56a83212020-09-15 18:30:11 -0700173 // AnyVariantDirectlyInAnyApex is true in the primary variant of a module if _any_ variant
174 // of the module is directly in any apex. This includes host, arch, asan, etc. variants.
175 // It is unused in any variant that is not the primary variant.
176 // Ideally this wouldn't be used, as it incorrectly mixes arch variants if only one arch
177 // is in an apex, but a few places depend on it, for example when an ASAN variant is
178 // created before the apexMutator.
179 AnyVariantDirectlyInAnyApex bool `blueprint:"mutated"`
180
181 // DirectlyInAnyApex is true if any APEX variant (including the "" variant used for the
182 // platform) of this module is directly in any APEX.
183 DirectlyInAnyApex bool `blueprint:"mutated"`
184
185 // DirectlyInAnyApex is true if any APEX variant (including the "" variant used for the
186 // platform) of this module is directly or indirectly in any APEX.
187 InAnyApex bool `blueprint:"mutated"`
Jiyong Park89e850a2020-04-07 16:37:39 +0900188
189 NotAvailableForPlatform bool `blueprint:"mutated"`
Colin Crossaede88c2020-08-11 12:17:01 -0700190
191 UniqueApexVariationsForDeps bool `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900192}
193
Paul Duffindddd5462020-04-07 15:25:44 +0100194// Marker interface that identifies dependencies that are excluded from APEX
195// contents.
196type ExcludeFromApexContentsTag interface {
197 blueprint.DependencyTag
198
199 // Method that differentiates this interface from others.
200 ExcludeFromApexContents()
201}
202
Colin Cross56a83212020-09-15 18:30:11 -0700203// Marker interface that identifies dependencies that should inherit the DirectlyInAnyApex
204// state from the parent to the child. For example, stubs libraries are marked as
205// DirectlyInAnyApex if their implementation is in an apex.
206type CopyDirectlyInAnyApexTag interface {
207 blueprint.DependencyTag
208
209 CopyDirectlyInAnyApex()
210}
211
Jiyong Park9d452992018-10-03 00:38:19 +0900212// Provides default implementation for the ApexModule interface. APEX-aware
213// modules are expected to include this struct and call InitApexModule().
214type ApexModuleBase struct {
215 ApexProperties ApexProperties
216
217 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700218
219 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800220 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900221}
222
223func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
224 return m
225}
226
Paul Duffinbefa4b92020-03-04 14:22:45 +0000227func (m *ApexModuleBase) ApexAvailable() []string {
228 return m.ApexProperties.Apex_available
229}
230
Jiyong Park62304bb2020-04-13 16:19:48 +0900231func (m *ApexModuleBase) TestFor() []string {
232 // To be implemented by concrete types inheriting ApexModuleBase
233 return nil
234}
235
Colin Crossaede88c2020-08-11 12:17:01 -0700236func (m *ApexModuleBase) UniqueApexVariations() bool {
237 return false
238}
239
Jooyung Han698dd9f2020-07-22 15:17:19 +0900240func (m *ApexModuleBase) BuildForApex(apex ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700241 m.apexVariationsLock.Lock()
242 defer m.apexVariationsLock.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900243 for _, v := range m.apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700244 if v.ApexVariationName == apex.ApexVariationName {
Jooyung Han698dd9f2020-07-22 15:17:19 +0900245 return
Jiyong Parkf760cae2020-02-12 07:53:12 +0900246 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900247 }
Jooyung Han698dd9f2020-07-22 15:17:19 +0900248 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900249}
250
Colin Cross56a83212020-09-15 18:30:11 -0700251func (m *ApexModuleBase) DirectlyInAnyApex() bool {
252 return m.ApexProperties.DirectlyInAnyApex
Jiyong Park9d452992018-10-03 00:38:19 +0900253}
254
Colin Cross56a83212020-09-15 18:30:11 -0700255func (m *ApexModuleBase) AnyVariantDirectlyInAnyApex() bool {
256 return m.ApexProperties.AnyVariantDirectlyInAnyApex
Colin Crossaede88c2020-08-11 12:17:01 -0700257}
258
Colin Cross56a83212020-09-15 18:30:11 -0700259func (m *ApexModuleBase) InAnyApex() bool {
260 return m.ApexProperties.InAnyApex
Jiyong Park9d452992018-10-03 00:38:19 +0900261}
262
263func (m *ApexModuleBase) CanHaveApexVariants() bool {
264 return m.canHaveApexVariants
265}
266
267func (m *ApexModuleBase) IsInstallableToApex() bool {
268 // should be overriden if needed
269 return false
270}
271
Jiyong Park127b40b2019-09-30 16:04:35 +0900272const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900273 AvailableToPlatform = "//apex_available:platform"
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000274 AvailableToAnyApex = "//apex_available:anyapex"
Yifan Hongd22a84a2020-07-28 17:37:46 -0700275 AvailableToGkiApex = "com.android.gki.*"
Jiyong Park127b40b2019-09-30 16:04:35 +0900276)
277
Jiyong Parka90ca002019-10-07 15:47:24 +0900278func CheckAvailableForApex(what string, apex_available []string) bool {
279 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000280 // apex_available defaults to ["//apex_available:platform"],
281 // which means 'available to the platform but no apexes'.
282 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900283 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900284 return InList(what, apex_available) ||
Yifan Hongd22a84a2020-07-28 17:37:46 -0700285 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) ||
286 (strings.HasPrefix(what, "com.android.gki.") && InList(AvailableToGkiApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900287}
288
289func (m *ApexModuleBase) AvailableFor(what string) bool {
290 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900291}
292
Jiyong Park89e850a2020-04-07 16:37:39 +0900293func (m *ApexModuleBase) NotAvailableForPlatform() bool {
294 return m.ApexProperties.NotAvailableForPlatform
295}
296
297func (m *ApexModuleBase) SetNotAvailableForPlatform() {
298 m.ApexProperties.NotAvailableForPlatform = true
299}
300
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900301func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
302 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
303 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
304 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
305 return true
306}
307
Jiyong Park127b40b2019-09-30 16:04:35 +0900308func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
309 for _, n := range m.ApexProperties.Apex_available {
Yifan Hongd22a84a2020-07-28 17:37:46 -0700310 if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900311 continue
312 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100313 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900314 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
315 }
316 }
317}
318
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800319type byApexName []ApexInfo
320
321func (a byApexName) Len() int { return len(a) }
322func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
Colin Crosse07f2312020-08-13 11:24:56 -0700323func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800324
Colin Crossaede88c2020-08-11 12:17:01 -0700325// mergeApexVariations deduplicates APEX variations that would build identically into a common
326// variation. It returns the reduced list of variations and a list of aliases from the original
327// variation names to the new variation names.
Colin Cross9f720ce2020-10-02 10:26:04 -0700328func mergeApexVariations(ctx PathContext, apexVariations []ApexInfo) (merged []ApexInfo, aliases [][2]string) {
Colin Crossaede88c2020-08-11 12:17:01 -0700329 sort.Sort(byApexName(apexVariations))
330 seen := make(map[string]int)
331 for _, apexInfo := range apexVariations {
332 apexName := apexInfo.ApexVariationName
Dan Albertc8060532020-07-22 22:32:17 -0700333 mergedName := apexInfo.mergedName(ctx)
Colin Crossaede88c2020-08-11 12:17:01 -0700334 if index, exists := seen[mergedName]; exists {
335 merged[index].InApexes = append(merged[index].InApexes, apexName)
Colin Cross56a83212020-09-15 18:30:11 -0700336 merged[index].ApexContents = append(merged[index].ApexContents, apexInfo.ApexContents...)
Colin Crossaede88c2020-08-11 12:17:01 -0700337 merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable
338 } else {
339 seen[mergedName] = len(merged)
Dan Albertc8060532020-07-22 22:32:17 -0700340 apexInfo.ApexVariationName = apexInfo.mergedName(ctx)
Colin Crossaede88c2020-08-11 12:17:01 -0700341 apexInfo.InApexes = CopyOf(apexInfo.InApexes)
Colin Cross56a83212020-09-15 18:30:11 -0700342 apexInfo.ApexContents = append([]*ApexContents(nil), apexInfo.ApexContents...)
Colin Crossaede88c2020-08-11 12:17:01 -0700343 merged = append(merged, apexInfo)
344 }
345 aliases = append(aliases, [2]string{apexName, mergedName})
346 }
347 return merged, aliases
348}
349
Colin Cross56a83212020-09-15 18:30:11 -0700350func CreateApexVariations(mctx BottomUpMutatorContext, module ApexModule) []Module {
351 base := module.apexModuleBase()
352 if len(base.apexVariations) > 0 {
353 base.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900354
Colin Crossaede88c2020-08-11 12:17:01 -0700355 var apexVariations []ApexInfo
356 var aliases [][2]string
Colin Cross56a83212020-09-15 18:30:11 -0700357 if !mctx.Module().(ApexModule).UniqueApexVariations() && !base.ApexProperties.UniqueApexVariationsForDeps {
358 apexVariations, aliases = mergeApexVariations(mctx, base.apexVariations)
Colin Crossaede88c2020-08-11 12:17:01 -0700359 } else {
Colin Cross56a83212020-09-15 18:30:11 -0700360 apexVariations = base.apexVariations
Colin Crossaede88c2020-08-11 12:17:01 -0700361 }
Colin Cross56a83212020-09-15 18:30:11 -0700362 // base.apexVariations is only needed to propagate the list of apexes from
363 // apexDepsMutator to apexMutator. It is no longer accurate after
364 // mergeApexVariations, and won't be copied to all but the first created
365 // variant. Clear it so it doesn't accidentally get used later.
366 base.apexVariations = nil
Colin Crossaede88c2020-08-11 12:17:01 -0700367
368 sort.Sort(byApexName(apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900369 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900370 variations = append(variations, "") // Original variation for platform
Colin Crossaede88c2020-08-11 12:17:01 -0700371 for _, apex := range apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700372 variations = append(variations, apex.ApexVariationName)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800373 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800374
Jiyong Park3ff16992019-12-27 14:11:47 +0900375 defaultVariation := ""
376 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900377
Colin Cross56a83212020-09-15 18:30:11 -0700378 var inApex ApexMembership
379 for _, a := range apexVariations {
380 for _, apexContents := range a.ApexContents {
381 inApex = inApex.merge(apexContents.contents[mctx.ModuleName()])
382 }
383 }
384
385 base.ApexProperties.InAnyApex = true
386 base.ApexProperties.DirectlyInAnyApex = inApex == directlyInApex
387
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900388 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800389 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900390 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800391 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +0100392 // Do not install the module for platform, but still allow it to output
393 // uninstallable AndroidMk entries in certain cases when they have
394 // side effects.
395 mod.MakeUninstallable()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900396 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800397 if !platformVariation {
Colin Cross56a83212020-09-15 18:30:11 -0700398 mctx.SetVariationProvider(mod, ApexInfoProvider, apexVariations[i-1])
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800399 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900400 }
Colin Crossaede88c2020-08-11 12:17:01 -0700401
402 for _, alias := range aliases {
403 mctx.CreateAliasVariation(alias[0], alias[1])
404 }
405
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900406 return modules
407 }
408 return nil
409}
410
Colin Cross56a83212020-09-15 18:30:11 -0700411// UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies
412// that are in the same APEX have unique APEX variations so that the module can link against
413// the right variant.
414func UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext, am ApexModule) {
415 // anyInSameApex returns true if the two ApexInfo lists contain any values in an InApexes list
416 // in common. It is used instead of DepIsInSameApex because it needs to determine if the dep
417 // is in the same APEX due to being directly included, not only if it is included _because_ it
418 // is a dependency.
419 anyInSameApex := func(a, b []ApexInfo) bool {
420 collectApexes := func(infos []ApexInfo) []string {
421 var ret []string
422 for _, info := range infos {
423 ret = append(ret, info.InApexes...)
424 }
425 return ret
426 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900427
Colin Cross56a83212020-09-15 18:30:11 -0700428 aApexes := collectApexes(a)
429 bApexes := collectApexes(b)
430 sort.Strings(bApexes)
431 for _, aApex := range aApexes {
432 index := sort.SearchStrings(bApexes, aApex)
433 if index < len(bApexes) && bApexes[index] == aApex {
434 return true
435 }
436 }
437 return false
438 }
439
440 mctx.VisitDirectDeps(func(dep Module) {
441 if depApexModule, ok := dep.(ApexModule); ok {
442 if anyInSameApex(depApexModule.apexModuleBase().apexVariations, am.apexModuleBase().apexVariations) &&
443 (depApexModule.UniqueApexVariations() ||
444 depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) {
445 am.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps = true
446 }
447 }
448 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900449}
450
Colin Cross56a83212020-09-15 18:30:11 -0700451// UpdateDirectlyInAnyApex uses the final module to store if any variant of this
452// module is directly in any APEX, and then copies the final value to all the modules.
453// It also copies the DirectlyInAnyApex value to any direct dependencies with a
454// CopyDirectlyInAnyApexTag dependency tag.
455func UpdateDirectlyInAnyApex(mctx BottomUpMutatorContext, am ApexModule) {
456 base := am.apexModuleBase()
457 // Copy DirectlyInAnyApex and InAnyApex from any direct dependencies with a
458 // CopyDirectlyInAnyApexTag dependency tag.
459 mctx.VisitDirectDeps(func(dep Module) {
460 if _, ok := mctx.OtherModuleDependencyTag(dep).(CopyDirectlyInAnyApexTag); ok {
461 depBase := dep.(ApexModule).apexModuleBase()
462 base.ApexProperties.DirectlyInAnyApex = depBase.ApexProperties.DirectlyInAnyApex
463 base.ApexProperties.InAnyApex = depBase.ApexProperties.InAnyApex
464 }
465 })
466
467 if base.ApexProperties.DirectlyInAnyApex {
468 // Variants of a module are always visited sequentially in order, so it is safe to
469 // write to another variant of this module.
470 // For a BottomUpMutator the PrimaryModule() is visited first and FinalModule() is
471 // visited last.
472 mctx.FinalModule().(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex = true
Jiyong Park25fc6a92018-11-18 18:02:45 +0900473 }
Colin Cross56a83212020-09-15 18:30:11 -0700474
475 // If this is the FinalModule (last visited module) copy AnyVariantDirectlyInAnyApex to
476 // all the other variants
477 if am == mctx.FinalModule().(ApexModule) {
478 mctx.VisitAllModuleVariants(func(variant Module) {
479 variant.(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex =
480 base.ApexProperties.AnyVariantDirectlyInAnyApex
481 })
Colin Crossaede88c2020-08-11 12:17:01 -0700482 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900483}
484
Colin Cross56a83212020-09-15 18:30:11 -0700485type ApexMembership int
486
487const (
488 notInApex ApexMembership = 0
489 indirectlyInApex = iota
490 directlyInApex
491)
492
493// Each apexBundle has an apexContents, and modules in that apex have a provider containing the
494// apexContents of each apexBundle they are part of.
495type ApexContents struct {
496 ApexName string
497 contents map[string]ApexMembership
498}
499
500func NewApexContents(name string, contents map[string]ApexMembership) *ApexContents {
501 return &ApexContents{
502 ApexName: name,
503 contents: contents,
Jooyung Han671f1ce2019-12-17 12:47:13 +0900504 }
505}
506
Colin Cross56a83212020-09-15 18:30:11 -0700507func (i ApexMembership) Add(direct bool) ApexMembership {
508 if direct || i == directlyInApex {
509 return directlyInApex
Jiyong Park25fc6a92018-11-18 18:02:45 +0900510 }
Colin Cross56a83212020-09-15 18:30:11 -0700511 return indirectlyInApex
512}
513
514func (i ApexMembership) merge(other ApexMembership) ApexMembership {
515 if other == directlyInApex || i == directlyInApex {
516 return directlyInApex
517 }
518
519 if other == indirectlyInApex || i == indirectlyInApex {
520 return indirectlyInApex
521 }
522 return notInApex
523}
524
525func (ac *ApexContents) DirectlyInApex(name string) bool {
526 return ac.contents[name] == directlyInApex
527}
528
529func (ac *ApexContents) InApex(name string) bool {
530 return ac.contents[name] != notInApex
Jiyong Park25fc6a92018-11-18 18:02:45 +0900531}
532
Colin Crossaede88c2020-08-11 12:17:01 -0700533// Tests whether a module named moduleName is directly depended on by all APEXes
Colin Cross56a83212020-09-15 18:30:11 -0700534// in an ApexInfo.
535func DirectlyInAllApexes(apexInfo ApexInfo, moduleName string) bool {
536 for _, contents := range apexInfo.ApexContents {
537 if !contents.DirectlyInApex(moduleName) {
Colin Crossaede88c2020-08-11 12:17:01 -0700538 return false
539 }
540 }
541 return true
542}
543
Jiyong Park9d452992018-10-03 00:38:19 +0900544func InitApexModule(m ApexModule) {
545 base := m.apexModuleBase()
546 base.canHaveApexVariants = true
547
548 m.AddProperties(&base.ApexProperties)
549}
Artur Satayev872a1442020-04-27 17:08:37 +0100550
551// A dependency info for a single ApexModule, either direct or transitive.
552type ApexModuleDepInfo struct {
553 // Name of the dependency
554 To string
555 // List of dependencies To belongs to. Includes APEX itself, if a direct dependency.
556 From []string
557 // Whether the dependency belongs to the final compiled APEX.
558 IsExternal bool
Artur Satayev480e25b2020-04-27 18:53:18 +0100559 // min_sdk_version of the ApexModule
560 MinSdkVersion string
Artur Satayev872a1442020-04-27 17:08:37 +0100561}
562
563// A map of a dependency name to its ApexModuleDepInfo
564type DepNameToDepInfoMap map[string]ApexModuleDepInfo
565
566type ApexBundleDepsInfo struct {
Jooyung Han98d63e12020-05-14 07:44:03 +0900567 flatListPath OutputPath
568 fullListPath OutputPath
Artur Satayev872a1442020-04-27 17:08:37 +0100569}
570
Artur Satayev849f8442020-04-28 14:57:42 +0100571type ApexBundleDepsInfoIntf interface {
572 Updatable() bool
Artur Satayeva8bd1132020-04-27 18:07:06 +0100573 FlatListPath() Path
Artur Satayev872a1442020-04-27 17:08:37 +0100574 FullListPath() Path
575}
576
Artur Satayeva8bd1132020-04-27 18:07:06 +0100577func (d *ApexBundleDepsInfo) FlatListPath() Path {
578 return d.flatListPath
579}
580
Artur Satayev872a1442020-04-27 17:08:37 +0100581func (d *ApexBundleDepsInfo) FullListPath() Path {
582 return d.fullListPath
583}
584
Artur Satayeva8bd1132020-04-27 18:07:06 +0100585// Generate two module out files:
586// 1. FullList with transitive deps and their parents in the dep graph
587// 2. FlatList with a flat list of transitive deps
Artur Satayev480e25b2020-04-27 18:53:18 +0100588func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) {
Artur Satayeva8bd1132020-04-27 18:07:06 +0100589 var fullContent strings.Builder
590 var flatContent strings.Builder
591
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100592 fmt.Fprintf(&fullContent, "%s(minSdkVersion:%s):\\n", ctx.ModuleName(), minSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100593 for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
594 info := depInfos[key]
Artur Satayev480e25b2020-04-27 18:53:18 +0100595 toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100596 if info.IsExternal {
597 toName = toName + " (external)"
598 }
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100599 fmt.Fprintf(&fullContent, " %s <- %s\\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
600 fmt.Fprintf(&flatContent, "%s\\n", toName)
Artur Satayev872a1442020-04-27 17:08:37 +0100601 }
602
603 d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
604 ctx.Build(pctx, BuildParams{
605 Rule: WriteFile,
606 Description: "Full Dependency Info",
607 Output: d.fullListPath,
608 Args: map[string]string{
Artur Satayeva8bd1132020-04-27 18:07:06 +0100609 "content": fullContent.String(),
610 },
611 })
612
613 d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
614 ctx.Build(pctx, BuildParams{
615 Rule: WriteFile,
616 Description: "Flat Dependency Info",
617 Output: d.flatListPath,
618 Args: map[string]string{
619 "content": flatContent.String(),
Artur Satayev872a1442020-04-27 17:08:37 +0100620 },
621 })
622}
Jooyung Han749dc692020-04-15 11:03:39 +0900623
624// TODO(b/158059172): remove minSdkVersion allowlist
Dan Albertc8060532020-07-22 22:32:17 -0700625var minSdkVersionAllowlist = func(apiMap map[string]int) map[string]ApiLevel {
626 list := make(map[string]ApiLevel, len(apiMap))
627 for name, finalApiInt := range apiMap {
628 list[name] = uncheckedFinalApiLevel(finalApiInt)
629 }
630 return list
631}(map[string]int{
Jooyung Han749dc692020-04-15 11:03:39 +0900632 "adbd": 30,
633 "android.net.ipsec.ike": 30,
634 "androidx-constraintlayout_constraintlayout-solver": 30,
635 "androidx.annotation_annotation": 28,
636 "androidx.arch.core_core-common": 28,
637 "androidx.collection_collection": 28,
638 "androidx.lifecycle_lifecycle-common": 28,
639 "apache-commons-compress": 29,
640 "bouncycastle_ike_digests": 30,
641 "brotli-java": 29,
642 "captiveportal-lib": 28,
643 "flatbuffer_headers": 30,
644 "framework-permission": 30,
645 "framework-statsd": 30,
646 "gemmlowp_headers": 30,
647 "ike-internals": 30,
648 "kotlinx-coroutines-android": 28,
649 "kotlinx-coroutines-core": 28,
650 "libadb_crypto": 30,
651 "libadb_pairing_auth": 30,
652 "libadb_pairing_connection": 30,
653 "libadb_pairing_server": 30,
654 "libadb_protos": 30,
655 "libadb_tls_connection": 30,
656 "libadbconnection_client": 30,
657 "libadbconnection_server": 30,
658 "libadbd_core": 30,
659 "libadbd_services": 30,
660 "libadbd": 30,
661 "libapp_processes_protos_lite": 30,
662 "libasyncio": 30,
663 "libbrotli": 30,
664 "libbuildversion": 30,
665 "libcrypto_static": 30,
666 "libcrypto_utils": 30,
667 "libdiagnose_usb": 30,
668 "libeigen": 30,
669 "liblz4": 30,
670 "libmdnssd": 30,
671 "libneuralnetworks_common": 30,
672 "libneuralnetworks_headers": 30,
673 "libneuralnetworks": 30,
674 "libprocpartition": 30,
675 "libprotobuf-java-lite": 30,
676 "libprotoutil": 30,
677 "libqemu_pipe": 30,
678 "libstats_jni": 30,
679 "libstatslog_statsd": 30,
680 "libstatsmetadata": 30,
681 "libstatspull": 30,
682 "libstatssocket": 30,
683 "libsync": 30,
684 "libtextclassifier_hash_headers": 30,
685 "libtextclassifier_hash_static": 30,
686 "libtflite_kernel_utils": 30,
687 "libwatchdog": 29,
688 "libzstd": 30,
689 "metrics-constants-protos": 28,
690 "net-utils-framework-common": 29,
691 "permissioncontroller-statsd": 28,
692 "philox_random_headers": 30,
693 "philox_random": 30,
694 "service-permission": 30,
695 "service-statsd": 30,
696 "statsd-aidl-ndk_platform": 30,
697 "statsd": 30,
698 "tensorflow_headers": 30,
699 "xz-java": 29,
Dan Albertc8060532020-07-22 22:32:17 -0700700})
Jooyung Han749dc692020-04-15 11:03:39 +0900701
702// Function called while walking an APEX's payload dependencies.
703//
704// Return true if the `to` module should be visited, false otherwise.
705type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool
706
707// UpdatableModule represents updatable APEX/APK
708type UpdatableModule interface {
709 Module
710 WalkPayloadDeps(ctx ModuleContext, do PayloadDepsCallback)
711}
712
713// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version accordingly
Dan Albertc8060532020-07-22 22:32:17 -0700714func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion ApiLevel) {
Jooyung Han749dc692020-04-15 11:03:39 +0900715 // do not enforce min_sdk_version for host
716 if ctx.Host() {
717 return
718 }
719
720 // do not enforce for coverage build
721 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() {
722 return
723 }
724
725 // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version or
726 // min_sdk_version is not finalized (e.g. current or codenames)
Dan Albertc8060532020-07-22 22:32:17 -0700727 if minSdkVersion.IsCurrent() {
Jooyung Han749dc692020-04-15 11:03:39 +0900728 return
729 }
730
731 m.WalkPayloadDeps(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool {
732 if externalDep {
733 // external deps are outside the payload boundary, which is "stable" interface.
734 // We don't have to check min_sdk_version for external dependencies.
735 return false
736 }
737 if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
738 return false
739 }
740 if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil {
741 toName := ctx.OtherModuleName(to)
Dan Albertc8060532020-07-22 22:32:17 -0700742 if ver, ok := minSdkVersionAllowlist[toName]; !ok || ver.GreaterThan(minSdkVersion) {
Jooyung Han749dc692020-04-15 11:03:39 +0900743 ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v. Dependency path: %s",
744 minSdkVersion, ctx.ModuleName(), err.Error(), ctx.GetPathString(false))
745 return false
746 }
747 }
748 return true
749 })
750}