blob: c01b7169266af1caa4c89626e7afa691c0f4c757 [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
Jooyung Han75568392020-03-20 04:29:24 +0900148 // Returns the highest version which is <= maxSdkVersion.
149 // For example, with maxSdkVersion is 10 and versionList is [9,11]
150 // it returns 9 as string
Colin Cross7812fd32020-09-25 12:35:10 -0700151 ChooseSdkVersion(ctx BaseModuleContext, versionList []string, maxSdkVersion ApiLevel) (string, error)
Ulya Trafimovich7c140d82020-04-22 18:05:58 +0100152
Jiyong Park62304bb2020-04-13 16:19:48 +0900153 // List of APEXes that this module tests. The module has access to
154 // the private part of the listed APEXes even when it is not included in the
155 // APEXes.
156 TestFor() []string
Jooyung Han749dc692020-04-15 11:03:39 +0900157
158 // Returns nil if this module supports sdkVersion
159 // Otherwise, returns error with reason
Dan Albertc8060532020-07-22 22:32:17 -0700160 ShouldSupportSdkVersion(ctx BaseModuleContext, sdkVersion ApiLevel) error
Colin Crossaede88c2020-08-11 12:17:01 -0700161
162 // Returns true if this module needs a unique variation per apex, for example if
163 // use_apex_name_macro is set.
164 UniqueApexVariations() bool
Jiyong Park9d452992018-10-03 00:38:19 +0900165}
166
167type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000168 // Availability of this module in APEXes. Only the listed APEXes can contain
169 // this module. If the module has stubs then other APEXes and the platform may
170 // access it through them (subject to visibility).
171 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900172 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
173 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Yifan Hongd22a84a2020-07-28 17:37:46 -0700174 // "com.android.gki.*" matches any APEX module name with the prefix "com.android.gki.".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900175 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900176 Apex_available []string
177
Colin Cross56a83212020-09-15 18:30:11 -0700178 // AnyVariantDirectlyInAnyApex is true in the primary variant of a module if _any_ variant
179 // of the module is directly in any apex. This includes host, arch, asan, etc. variants.
180 // It is unused in any variant that is not the primary variant.
181 // Ideally this wouldn't be used, as it incorrectly mixes arch variants if only one arch
182 // is in an apex, but a few places depend on it, for example when an ASAN variant is
183 // created before the apexMutator.
184 AnyVariantDirectlyInAnyApex bool `blueprint:"mutated"`
185
186 // DirectlyInAnyApex is true if any APEX variant (including the "" variant used for the
187 // platform) of this module is directly in any APEX.
188 DirectlyInAnyApex bool `blueprint:"mutated"`
189
190 // DirectlyInAnyApex is true if any APEX variant (including the "" variant used for the
191 // platform) of this module is directly or indirectly in any APEX.
192 InAnyApex bool `blueprint:"mutated"`
Jiyong Park89e850a2020-04-07 16:37:39 +0900193
194 NotAvailableForPlatform bool `blueprint:"mutated"`
Colin Crossaede88c2020-08-11 12:17:01 -0700195
196 UniqueApexVariationsForDeps bool `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900197}
198
Paul Duffindddd5462020-04-07 15:25:44 +0100199// Marker interface that identifies dependencies that are excluded from APEX
200// contents.
201type ExcludeFromApexContentsTag interface {
202 blueprint.DependencyTag
203
204 // Method that differentiates this interface from others.
205 ExcludeFromApexContents()
206}
207
Colin Cross56a83212020-09-15 18:30:11 -0700208// Marker interface that identifies dependencies that should inherit the DirectlyInAnyApex
209// state from the parent to the child. For example, stubs libraries are marked as
210// DirectlyInAnyApex if their implementation is in an apex.
211type CopyDirectlyInAnyApexTag interface {
212 blueprint.DependencyTag
213
214 CopyDirectlyInAnyApex()
215}
216
Jiyong Park9d452992018-10-03 00:38:19 +0900217// Provides default implementation for the ApexModule interface. APEX-aware
218// modules are expected to include this struct and call InitApexModule().
219type ApexModuleBase struct {
220 ApexProperties ApexProperties
221
222 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700223
224 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800225 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900226}
227
228func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
229 return m
230}
231
Paul Duffinbefa4b92020-03-04 14:22:45 +0000232func (m *ApexModuleBase) ApexAvailable() []string {
233 return m.ApexProperties.Apex_available
234}
235
Jiyong Park62304bb2020-04-13 16:19:48 +0900236func (m *ApexModuleBase) TestFor() []string {
237 // To be implemented by concrete types inheriting ApexModuleBase
238 return nil
239}
240
Colin Crossaede88c2020-08-11 12:17:01 -0700241func (m *ApexModuleBase) UniqueApexVariations() bool {
242 return false
243}
244
Jooyung Han698dd9f2020-07-22 15:17:19 +0900245func (m *ApexModuleBase) BuildForApex(apex ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700246 m.apexVariationsLock.Lock()
247 defer m.apexVariationsLock.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900248 for _, v := range m.apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700249 if v.ApexVariationName == apex.ApexVariationName {
Jooyung Han698dd9f2020-07-22 15:17:19 +0900250 return
Jiyong Parkf760cae2020-02-12 07:53:12 +0900251 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900252 }
Jooyung Han698dd9f2020-07-22 15:17:19 +0900253 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900254}
255
Colin Cross56a83212020-09-15 18:30:11 -0700256func (m *ApexModuleBase) DirectlyInAnyApex() bool {
257 return m.ApexProperties.DirectlyInAnyApex
Jiyong Park9d452992018-10-03 00:38:19 +0900258}
259
Colin Cross56a83212020-09-15 18:30:11 -0700260func (m *ApexModuleBase) AnyVariantDirectlyInAnyApex() bool {
261 return m.ApexProperties.AnyVariantDirectlyInAnyApex
Colin Crossaede88c2020-08-11 12:17:01 -0700262}
263
Colin Cross56a83212020-09-15 18:30:11 -0700264func (m *ApexModuleBase) InAnyApex() bool {
265 return m.ApexProperties.InAnyApex
Jiyong Park9d452992018-10-03 00:38:19 +0900266}
267
268func (m *ApexModuleBase) CanHaveApexVariants() bool {
269 return m.canHaveApexVariants
270}
271
272func (m *ApexModuleBase) IsInstallableToApex() bool {
273 // should be overriden if needed
274 return false
275}
276
Jiyong Park127b40b2019-09-30 16:04:35 +0900277const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900278 AvailableToPlatform = "//apex_available:platform"
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000279 AvailableToAnyApex = "//apex_available:anyapex"
Yifan Hongd22a84a2020-07-28 17:37:46 -0700280 AvailableToGkiApex = "com.android.gki.*"
Jiyong Park127b40b2019-09-30 16:04:35 +0900281)
282
Jiyong Parka90ca002019-10-07 15:47:24 +0900283func CheckAvailableForApex(what string, apex_available []string) bool {
284 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000285 // apex_available defaults to ["//apex_available:platform"],
286 // which means 'available to the platform but no apexes'.
287 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900288 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900289 return InList(what, apex_available) ||
Yifan Hongd22a84a2020-07-28 17:37:46 -0700290 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) ||
291 (strings.HasPrefix(what, "com.android.gki.") && InList(AvailableToGkiApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900292}
293
294func (m *ApexModuleBase) AvailableFor(what string) bool {
295 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900296}
297
Jiyong Park89e850a2020-04-07 16:37:39 +0900298func (m *ApexModuleBase) NotAvailableForPlatform() bool {
299 return m.ApexProperties.NotAvailableForPlatform
300}
301
302func (m *ApexModuleBase) SetNotAvailableForPlatform() {
303 m.ApexProperties.NotAvailableForPlatform = true
304}
305
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900306func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
307 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
308 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
309 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
310 return true
311}
312
Colin Cross7812fd32020-09-25 12:35:10 -0700313func (m *ApexModuleBase) ChooseSdkVersion(ctx BaseModuleContext, versionList []string, maxSdkVersion ApiLevel) (string, error) {
Jooyung Han03b51852020-02-26 22:45:42 +0900314 for i := range versionList {
Colin Cross7812fd32020-09-25 12:35:10 -0700315 version := versionList[len(versionList)-i-1]
316 ver, err := ApiLevelFromUser(ctx, version)
317 if err != nil {
318 return "", err
319 }
320 if ver.LessThanOrEqualTo(maxSdkVersion) {
321 return version, nil
Jooyung Han03b51852020-02-26 22:45:42 +0900322 }
323 }
Colin Cross7812fd32020-09-25 12:35:10 -0700324 return "", fmt.Errorf("not found a version(<=%s) in versionList: %v", maxSdkVersion, versionList)
Jooyung Han03b51852020-02-26 22:45:42 +0900325}
326
Jiyong Park127b40b2019-09-30 16:04:35 +0900327func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
328 for _, n := range m.ApexProperties.Apex_available {
Yifan Hongd22a84a2020-07-28 17:37:46 -0700329 if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900330 continue
331 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100332 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900333 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
334 }
335 }
336}
337
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800338type byApexName []ApexInfo
339
340func (a byApexName) Len() int { return len(a) }
341func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
Colin Crosse07f2312020-08-13 11:24:56 -0700342func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800343
Colin Crossaede88c2020-08-11 12:17:01 -0700344// mergeApexVariations deduplicates APEX variations that would build identically into a common
345// variation. It returns the reduced list of variations and a list of aliases from the original
346// variation names to the new variation names.
Colin Cross9f720ce2020-10-02 10:26:04 -0700347func mergeApexVariations(ctx PathContext, apexVariations []ApexInfo) (merged []ApexInfo, aliases [][2]string) {
Colin Crossaede88c2020-08-11 12:17:01 -0700348 sort.Sort(byApexName(apexVariations))
349 seen := make(map[string]int)
350 for _, apexInfo := range apexVariations {
351 apexName := apexInfo.ApexVariationName
Dan Albertc8060532020-07-22 22:32:17 -0700352 mergedName := apexInfo.mergedName(ctx)
Colin Crossaede88c2020-08-11 12:17:01 -0700353 if index, exists := seen[mergedName]; exists {
354 merged[index].InApexes = append(merged[index].InApexes, apexName)
Colin Cross56a83212020-09-15 18:30:11 -0700355 merged[index].ApexContents = append(merged[index].ApexContents, apexInfo.ApexContents...)
Colin Crossaede88c2020-08-11 12:17:01 -0700356 merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable
357 } else {
358 seen[mergedName] = len(merged)
Dan Albertc8060532020-07-22 22:32:17 -0700359 apexInfo.ApexVariationName = apexInfo.mergedName(ctx)
Colin Crossaede88c2020-08-11 12:17:01 -0700360 apexInfo.InApexes = CopyOf(apexInfo.InApexes)
Colin Cross56a83212020-09-15 18:30:11 -0700361 apexInfo.ApexContents = append([]*ApexContents(nil), apexInfo.ApexContents...)
Colin Crossaede88c2020-08-11 12:17:01 -0700362 merged = append(merged, apexInfo)
363 }
364 aliases = append(aliases, [2]string{apexName, mergedName})
365 }
366 return merged, aliases
367}
368
Colin Cross56a83212020-09-15 18:30:11 -0700369func CreateApexVariations(mctx BottomUpMutatorContext, module ApexModule) []Module {
370 base := module.apexModuleBase()
371 if len(base.apexVariations) > 0 {
372 base.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900373
Colin Crossaede88c2020-08-11 12:17:01 -0700374 var apexVariations []ApexInfo
375 var aliases [][2]string
Colin Cross56a83212020-09-15 18:30:11 -0700376 if !mctx.Module().(ApexModule).UniqueApexVariations() && !base.ApexProperties.UniqueApexVariationsForDeps {
377 apexVariations, aliases = mergeApexVariations(mctx, base.apexVariations)
Colin Crossaede88c2020-08-11 12:17:01 -0700378 } else {
Colin Cross56a83212020-09-15 18:30:11 -0700379 apexVariations = base.apexVariations
Colin Crossaede88c2020-08-11 12:17:01 -0700380 }
Colin Cross56a83212020-09-15 18:30:11 -0700381 // base.apexVariations is only needed to propagate the list of apexes from
382 // apexDepsMutator to apexMutator. It is no longer accurate after
383 // mergeApexVariations, and won't be copied to all but the first created
384 // variant. Clear it so it doesn't accidentally get used later.
385 base.apexVariations = nil
Colin Crossaede88c2020-08-11 12:17:01 -0700386
387 sort.Sort(byApexName(apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900388 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900389 variations = append(variations, "") // Original variation for platform
Colin Crossaede88c2020-08-11 12:17:01 -0700390 for _, apex := range apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700391 variations = append(variations, apex.ApexVariationName)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800392 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800393
Jiyong Park3ff16992019-12-27 14:11:47 +0900394 defaultVariation := ""
395 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900396
Colin Cross56a83212020-09-15 18:30:11 -0700397 var inApex ApexMembership
398 for _, a := range apexVariations {
399 for _, apexContents := range a.ApexContents {
400 inApex = inApex.merge(apexContents.contents[mctx.ModuleName()])
401 }
402 }
403
404 base.ApexProperties.InAnyApex = true
405 base.ApexProperties.DirectlyInAnyApex = inApex == directlyInApex
406
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900407 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800408 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900409 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800410 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +0100411 // Do not install the module for platform, but still allow it to output
412 // uninstallable AndroidMk entries in certain cases when they have
413 // side effects.
414 mod.MakeUninstallable()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900415 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800416 if !platformVariation {
Colin Cross56a83212020-09-15 18:30:11 -0700417 mctx.SetVariationProvider(mod, ApexInfoProvider, apexVariations[i-1])
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800418 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900419 }
Colin Crossaede88c2020-08-11 12:17:01 -0700420
421 for _, alias := range aliases {
422 mctx.CreateAliasVariation(alias[0], alias[1])
423 }
424
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900425 return modules
426 }
427 return nil
428}
429
Colin Cross56a83212020-09-15 18:30:11 -0700430// UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies
431// that are in the same APEX have unique APEX variations so that the module can link against
432// the right variant.
433func UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext, am ApexModule) {
434 // anyInSameApex returns true if the two ApexInfo lists contain any values in an InApexes list
435 // in common. It is used instead of DepIsInSameApex because it needs to determine if the dep
436 // is in the same APEX due to being directly included, not only if it is included _because_ it
437 // is a dependency.
438 anyInSameApex := func(a, b []ApexInfo) bool {
439 collectApexes := func(infos []ApexInfo) []string {
440 var ret []string
441 for _, info := range infos {
442 ret = append(ret, info.InApexes...)
443 }
444 return ret
445 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900446
Colin Cross56a83212020-09-15 18:30:11 -0700447 aApexes := collectApexes(a)
448 bApexes := collectApexes(b)
449 sort.Strings(bApexes)
450 for _, aApex := range aApexes {
451 index := sort.SearchStrings(bApexes, aApex)
452 if index < len(bApexes) && bApexes[index] == aApex {
453 return true
454 }
455 }
456 return false
457 }
458
459 mctx.VisitDirectDeps(func(dep Module) {
460 if depApexModule, ok := dep.(ApexModule); ok {
461 if anyInSameApex(depApexModule.apexModuleBase().apexVariations, am.apexModuleBase().apexVariations) &&
462 (depApexModule.UniqueApexVariations() ||
463 depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) {
464 am.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps = true
465 }
466 }
467 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900468}
469
Colin Cross56a83212020-09-15 18:30:11 -0700470// UpdateDirectlyInAnyApex uses the final module to store if any variant of this
471// module is directly in any APEX, and then copies the final value to all the modules.
472// It also copies the DirectlyInAnyApex value to any direct dependencies with a
473// CopyDirectlyInAnyApexTag dependency tag.
474func UpdateDirectlyInAnyApex(mctx BottomUpMutatorContext, am ApexModule) {
475 base := am.apexModuleBase()
476 // Copy DirectlyInAnyApex and InAnyApex from any direct dependencies with a
477 // CopyDirectlyInAnyApexTag dependency tag.
478 mctx.VisitDirectDeps(func(dep Module) {
479 if _, ok := mctx.OtherModuleDependencyTag(dep).(CopyDirectlyInAnyApexTag); ok {
480 depBase := dep.(ApexModule).apexModuleBase()
481 base.ApexProperties.DirectlyInAnyApex = depBase.ApexProperties.DirectlyInAnyApex
482 base.ApexProperties.InAnyApex = depBase.ApexProperties.InAnyApex
483 }
484 })
485
486 if base.ApexProperties.DirectlyInAnyApex {
487 // Variants of a module are always visited sequentially in order, so it is safe to
488 // write to another variant of this module.
489 // For a BottomUpMutator the PrimaryModule() is visited first and FinalModule() is
490 // visited last.
491 mctx.FinalModule().(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex = true
Jiyong Park25fc6a92018-11-18 18:02:45 +0900492 }
Colin Cross56a83212020-09-15 18:30:11 -0700493
494 // If this is the FinalModule (last visited module) copy AnyVariantDirectlyInAnyApex to
495 // all the other variants
496 if am == mctx.FinalModule().(ApexModule) {
497 mctx.VisitAllModuleVariants(func(variant Module) {
498 variant.(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex =
499 base.ApexProperties.AnyVariantDirectlyInAnyApex
500 })
Colin Crossaede88c2020-08-11 12:17:01 -0700501 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900502}
503
Colin Cross56a83212020-09-15 18:30:11 -0700504type ApexMembership int
505
506const (
507 notInApex ApexMembership = 0
508 indirectlyInApex = iota
509 directlyInApex
510)
511
512// Each apexBundle has an apexContents, and modules in that apex have a provider containing the
513// apexContents of each apexBundle they are part of.
514type ApexContents struct {
515 ApexName string
516 contents map[string]ApexMembership
517}
518
519func NewApexContents(name string, contents map[string]ApexMembership) *ApexContents {
520 return &ApexContents{
521 ApexName: name,
522 contents: contents,
Jooyung Han671f1ce2019-12-17 12:47:13 +0900523 }
524}
525
Colin Cross56a83212020-09-15 18:30:11 -0700526func (i ApexMembership) Add(direct bool) ApexMembership {
527 if direct || i == directlyInApex {
528 return directlyInApex
Jiyong Park25fc6a92018-11-18 18:02:45 +0900529 }
Colin Cross56a83212020-09-15 18:30:11 -0700530 return indirectlyInApex
531}
532
533func (i ApexMembership) merge(other ApexMembership) ApexMembership {
534 if other == directlyInApex || i == directlyInApex {
535 return directlyInApex
536 }
537
538 if other == indirectlyInApex || i == indirectlyInApex {
539 return indirectlyInApex
540 }
541 return notInApex
542}
543
544func (ac *ApexContents) DirectlyInApex(name string) bool {
545 return ac.contents[name] == directlyInApex
546}
547
548func (ac *ApexContents) InApex(name string) bool {
549 return ac.contents[name] != notInApex
Jiyong Park25fc6a92018-11-18 18:02:45 +0900550}
551
Colin Crossaede88c2020-08-11 12:17:01 -0700552// Tests whether a module named moduleName is directly depended on by all APEXes
Colin Cross56a83212020-09-15 18:30:11 -0700553// in an ApexInfo.
554func DirectlyInAllApexes(apexInfo ApexInfo, moduleName string) bool {
555 for _, contents := range apexInfo.ApexContents {
556 if !contents.DirectlyInApex(moduleName) {
Colin Crossaede88c2020-08-11 12:17:01 -0700557 return false
558 }
559 }
560 return true
561}
562
Jiyong Park9d452992018-10-03 00:38:19 +0900563func InitApexModule(m ApexModule) {
564 base := m.apexModuleBase()
565 base.canHaveApexVariants = true
566
567 m.AddProperties(&base.ApexProperties)
568}
Artur Satayev872a1442020-04-27 17:08:37 +0100569
570// A dependency info for a single ApexModule, either direct or transitive.
571type ApexModuleDepInfo struct {
572 // Name of the dependency
573 To string
574 // List of dependencies To belongs to. Includes APEX itself, if a direct dependency.
575 From []string
576 // Whether the dependency belongs to the final compiled APEX.
577 IsExternal bool
Artur Satayev480e25b2020-04-27 18:53:18 +0100578 // min_sdk_version of the ApexModule
579 MinSdkVersion string
Artur Satayev872a1442020-04-27 17:08:37 +0100580}
581
582// A map of a dependency name to its ApexModuleDepInfo
583type DepNameToDepInfoMap map[string]ApexModuleDepInfo
584
585type ApexBundleDepsInfo struct {
Jooyung Han98d63e12020-05-14 07:44:03 +0900586 flatListPath OutputPath
587 fullListPath OutputPath
Artur Satayev872a1442020-04-27 17:08:37 +0100588}
589
Artur Satayev849f8442020-04-28 14:57:42 +0100590type ApexBundleDepsInfoIntf interface {
591 Updatable() bool
Artur Satayeva8bd1132020-04-27 18:07:06 +0100592 FlatListPath() Path
Artur Satayev872a1442020-04-27 17:08:37 +0100593 FullListPath() Path
594}
595
Artur Satayeva8bd1132020-04-27 18:07:06 +0100596func (d *ApexBundleDepsInfo) FlatListPath() Path {
597 return d.flatListPath
598}
599
Artur Satayev872a1442020-04-27 17:08:37 +0100600func (d *ApexBundleDepsInfo) FullListPath() Path {
601 return d.fullListPath
602}
603
Artur Satayeva8bd1132020-04-27 18:07:06 +0100604// Generate two module out files:
605// 1. FullList with transitive deps and their parents in the dep graph
606// 2. FlatList with a flat list of transitive deps
Artur Satayev480e25b2020-04-27 18:53:18 +0100607func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) {
Artur Satayeva8bd1132020-04-27 18:07:06 +0100608 var fullContent strings.Builder
609 var flatContent strings.Builder
610
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100611 fmt.Fprintf(&fullContent, "%s(minSdkVersion:%s):\\n", ctx.ModuleName(), minSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100612 for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
613 info := depInfos[key]
Artur Satayev480e25b2020-04-27 18:53:18 +0100614 toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100615 if info.IsExternal {
616 toName = toName + " (external)"
617 }
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100618 fmt.Fprintf(&fullContent, " %s <- %s\\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
619 fmt.Fprintf(&flatContent, "%s\\n", toName)
Artur Satayev872a1442020-04-27 17:08:37 +0100620 }
621
622 d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
623 ctx.Build(pctx, BuildParams{
624 Rule: WriteFile,
625 Description: "Full Dependency Info",
626 Output: d.fullListPath,
627 Args: map[string]string{
Artur Satayeva8bd1132020-04-27 18:07:06 +0100628 "content": fullContent.String(),
629 },
630 })
631
632 d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
633 ctx.Build(pctx, BuildParams{
634 Rule: WriteFile,
635 Description: "Flat Dependency Info",
636 Output: d.flatListPath,
637 Args: map[string]string{
638 "content": flatContent.String(),
Artur Satayev872a1442020-04-27 17:08:37 +0100639 },
640 })
641}
Jooyung Han749dc692020-04-15 11:03:39 +0900642
643// TODO(b/158059172): remove minSdkVersion allowlist
Dan Albertc8060532020-07-22 22:32:17 -0700644var minSdkVersionAllowlist = func(apiMap map[string]int) map[string]ApiLevel {
645 list := make(map[string]ApiLevel, len(apiMap))
646 for name, finalApiInt := range apiMap {
647 list[name] = uncheckedFinalApiLevel(finalApiInt)
648 }
649 return list
650}(map[string]int{
Jooyung Han749dc692020-04-15 11:03:39 +0900651 "adbd": 30,
652 "android.net.ipsec.ike": 30,
653 "androidx-constraintlayout_constraintlayout-solver": 30,
654 "androidx.annotation_annotation": 28,
655 "androidx.arch.core_core-common": 28,
656 "androidx.collection_collection": 28,
657 "androidx.lifecycle_lifecycle-common": 28,
658 "apache-commons-compress": 29,
659 "bouncycastle_ike_digests": 30,
660 "brotli-java": 29,
661 "captiveportal-lib": 28,
662 "flatbuffer_headers": 30,
663 "framework-permission": 30,
664 "framework-statsd": 30,
665 "gemmlowp_headers": 30,
666 "ike-internals": 30,
667 "kotlinx-coroutines-android": 28,
668 "kotlinx-coroutines-core": 28,
669 "libadb_crypto": 30,
670 "libadb_pairing_auth": 30,
671 "libadb_pairing_connection": 30,
672 "libadb_pairing_server": 30,
673 "libadb_protos": 30,
674 "libadb_tls_connection": 30,
675 "libadbconnection_client": 30,
676 "libadbconnection_server": 30,
677 "libadbd_core": 30,
678 "libadbd_services": 30,
679 "libadbd": 30,
680 "libapp_processes_protos_lite": 30,
681 "libasyncio": 30,
682 "libbrotli": 30,
683 "libbuildversion": 30,
684 "libcrypto_static": 30,
685 "libcrypto_utils": 30,
686 "libdiagnose_usb": 30,
687 "libeigen": 30,
688 "liblz4": 30,
689 "libmdnssd": 30,
690 "libneuralnetworks_common": 30,
691 "libneuralnetworks_headers": 30,
692 "libneuralnetworks": 30,
693 "libprocpartition": 30,
694 "libprotobuf-java-lite": 30,
695 "libprotoutil": 30,
696 "libqemu_pipe": 30,
697 "libstats_jni": 30,
698 "libstatslog_statsd": 30,
699 "libstatsmetadata": 30,
700 "libstatspull": 30,
701 "libstatssocket": 30,
702 "libsync": 30,
703 "libtextclassifier_hash_headers": 30,
704 "libtextclassifier_hash_static": 30,
705 "libtflite_kernel_utils": 30,
706 "libwatchdog": 29,
707 "libzstd": 30,
708 "metrics-constants-protos": 28,
709 "net-utils-framework-common": 29,
710 "permissioncontroller-statsd": 28,
711 "philox_random_headers": 30,
712 "philox_random": 30,
713 "service-permission": 30,
714 "service-statsd": 30,
715 "statsd-aidl-ndk_platform": 30,
716 "statsd": 30,
717 "tensorflow_headers": 30,
718 "xz-java": 29,
Dan Albertc8060532020-07-22 22:32:17 -0700719})
Jooyung Han749dc692020-04-15 11:03:39 +0900720
721// Function called while walking an APEX's payload dependencies.
722//
723// Return true if the `to` module should be visited, false otherwise.
724type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool
725
726// UpdatableModule represents updatable APEX/APK
727type UpdatableModule interface {
728 Module
729 WalkPayloadDeps(ctx ModuleContext, do PayloadDepsCallback)
730}
731
732// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version accordingly
Dan Albertc8060532020-07-22 22:32:17 -0700733func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion ApiLevel) {
Jooyung Han749dc692020-04-15 11:03:39 +0900734 // do not enforce min_sdk_version for host
735 if ctx.Host() {
736 return
737 }
738
739 // do not enforce for coverage build
740 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() {
741 return
742 }
743
744 // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version or
745 // min_sdk_version is not finalized (e.g. current or codenames)
Dan Albertc8060532020-07-22 22:32:17 -0700746 if minSdkVersion.IsCurrent() {
Jooyung Han749dc692020-04-15 11:03:39 +0900747 return
748 }
749
750 m.WalkPayloadDeps(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool {
751 if externalDep {
752 // external deps are outside the payload boundary, which is "stable" interface.
753 // We don't have to check min_sdk_version for external dependencies.
754 return false
755 }
756 if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
757 return false
758 }
759 if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil {
760 toName := ctx.OtherModuleName(to)
Dan Albertc8060532020-07-22 22:32:17 -0700761 if ver, ok := minSdkVersionAllowlist[toName]; !ok || ver.GreaterThan(minSdkVersion) {
Jooyung Han749dc692020-04-15 11:03:39 +0900762 ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v. Dependency path: %s",
763 minSdkVersion, ctx.ModuleName(), err.Error(), ctx.GetPathString(false))
764 return false
765 }
766 }
767 return true
768 })
769}