blob: 276f7a4bdd41a89c2d0a0556f3c2161534f0b4f8 [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
Paul Duffin9a89a2a2020-10-28 19:20:06 +000070func (i ApexInfo) InApex(apex string) bool {
71 for _, a := range i.InApexes {
72 if a == apex {
73 return true
74 }
75 }
76 return false
77}
78
Colin Cross56a83212020-09-15 18:30:11 -070079// ApexTestForInfo stores the contents of APEXes for which this module is a test and thus has
80// access to APEX internals.
81type ApexTestForInfo struct {
82 ApexContents []*ApexContents
83}
84
85var ApexTestForInfoProvider = blueprint.NewMutatorProvider(ApexTestForInfo{}, "apex_test_for")
86
Paul Duffin923e8a52020-03-30 15:33:32 +010087// Extracted from ApexModule to make it easier to define custom subsets of the
88// ApexModule interface and improve code navigation within the IDE.
89type DepIsInSameApex interface {
90 // DepIsInSameApex tests if the other module 'dep' is installed to the same
91 // APEX as this module
92 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
93}
94
Jiyong Park9d452992018-10-03 00:38:19 +090095// ApexModule is the interface that a module type is expected to implement if
96// the module has to be built differently depending on whether the module
97// is destined for an apex or not (installed to one of the regular partitions).
98//
99// Native shared libraries are one such module type; when it is built for an
100// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
101// or C APIs from other APEXs.
102//
103// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900104// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +0900105// in one or more APEXs. Specifically, if a module is included in apex.foo and
106// apex.bar then three apex variants are created: platform, apex.foo and
107// apex.bar. The platform variant is for the regular partitions
108// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
109// respectively.
110type ApexModule interface {
111 Module
Paul Duffin923e8a52020-03-30 15:33:32 +0100112 DepIsInSameApex
113
Jiyong Park9d452992018-10-03 00:38:19 +0900114 apexModuleBase() *ApexModuleBase
115
Jooyung Han698dd9f2020-07-22 15:17:19 +0900116 // Marks that this module should be built for the specified APEX.
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900117 // Call this before apex.apexMutator is run.
Jooyung Han698dd9f2020-07-22 15:17:19 +0900118 BuildForApex(apex ApexInfo)
Jiyong Parkf760cae2020-02-12 07:53:12 +0900119
Colin Cross56a83212020-09-15 18:30:11 -0700120 // Returns true if this module is present in any APEXes
121 // directly or indirectly.
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900122 // Call this after apex.apexMutator is run.
Colin Cross56a83212020-09-15 18:30:11 -0700123 InAnyApex() bool
Jiyong Park9d452992018-10-03 00:38:19 +0900124
Colin Cross56a83212020-09-15 18:30:11 -0700125 // Returns true if this module is directly in any APEXes.
Colin Crossaede88c2020-08-11 12:17:01 -0700126 // Call this after apex.apexMutator is run.
Colin Cross56a83212020-09-15 18:30:11 -0700127 DirectlyInAnyApex() bool
Colin Crossaede88c2020-08-11 12:17:01 -0700128
Colin Cross56a83212020-09-15 18:30:11 -0700129 // Returns true if any variant of this module is directly in any APEXes.
130 // Call this after apex.apexMutator is run.
131 AnyVariantDirectlyInAnyApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900132
133 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +0900134 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900135 // for not creating APEX variants for certain types of shared libraries
136 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +0900137 CanHaveApexVariants() bool
138
139 // Tests if this module can be installed to APEX as a file. For example,
140 // this would return true for shared libs while return false for static
141 // libs.
142 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900143
Jiyong Park127b40b2019-09-30 16:04:35 +0900144 // Tests if this module is available for the specified APEX or ":platform"
145 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900146
Jiyong Park89e850a2020-04-07 16:37:39 +0900147 // Return true if this module is not available to platform (i.e. apex_available
148 // property doesn't have "//apex_available:platform"), or shouldn't be available
149 // to platform, which is the case when this module depends on other module that
150 // isn't available to platform.
151 NotAvailableForPlatform() bool
152
153 // Mark that this module is not available to platform. Set by the
154 // check-platform-availability mutator in the apex package.
155 SetNotAvailableForPlatform()
156
Jiyong Park62304bb2020-04-13 16:19:48 +0900157 // List of APEXes that this module tests. The module has access to
158 // the private part of the listed APEXes even when it is not included in the
159 // APEXes.
160 TestFor() []string
Jooyung Han749dc692020-04-15 11:03:39 +0900161
162 // Returns nil if this module supports sdkVersion
163 // Otherwise, returns error with reason
Dan Albertc8060532020-07-22 22:32:17 -0700164 ShouldSupportSdkVersion(ctx BaseModuleContext, sdkVersion ApiLevel) error
Colin Crossaede88c2020-08-11 12:17:01 -0700165
166 // Returns true if this module needs a unique variation per apex, for example if
167 // use_apex_name_macro is set.
168 UniqueApexVariations() bool
Jiyong Park9d452992018-10-03 00:38:19 +0900169}
170
171type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000172 // Availability of this module in APEXes. Only the listed APEXes can contain
173 // this module. If the module has stubs then other APEXes and the platform may
174 // access it through them (subject to visibility).
175 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900176 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
177 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Yifan Hongd22a84a2020-07-28 17:37:46 -0700178 // "com.android.gki.*" matches any APEX module name with the prefix "com.android.gki.".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900179 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900180 Apex_available []string
181
Colin Cross56a83212020-09-15 18:30:11 -0700182 // AnyVariantDirectlyInAnyApex is true in the primary variant of a module if _any_ variant
183 // of the module is directly in any apex. This includes host, arch, asan, etc. variants.
184 // It is unused in any variant that is not the primary variant.
185 // Ideally this wouldn't be used, as it incorrectly mixes arch variants if only one arch
186 // is in an apex, but a few places depend on it, for example when an ASAN variant is
187 // created before the apexMutator.
188 AnyVariantDirectlyInAnyApex 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 in any APEX.
192 DirectlyInAnyApex bool `blueprint:"mutated"`
193
194 // DirectlyInAnyApex is true if any APEX variant (including the "" variant used for the
195 // platform) of this module is directly or indirectly in any APEX.
196 InAnyApex bool `blueprint:"mutated"`
Jiyong Park89e850a2020-04-07 16:37:39 +0900197
198 NotAvailableForPlatform bool `blueprint:"mutated"`
Colin Crossaede88c2020-08-11 12:17:01 -0700199
200 UniqueApexVariationsForDeps bool `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900201}
202
Paul Duffindddd5462020-04-07 15:25:44 +0100203// Marker interface that identifies dependencies that are excluded from APEX
204// contents.
205type ExcludeFromApexContentsTag interface {
206 blueprint.DependencyTag
207
208 // Method that differentiates this interface from others.
209 ExcludeFromApexContents()
210}
211
Colin Cross56a83212020-09-15 18:30:11 -0700212// Marker interface that identifies dependencies that should inherit the DirectlyInAnyApex
213// state from the parent to the child. For example, stubs libraries are marked as
214// DirectlyInAnyApex if their implementation is in an apex.
215type CopyDirectlyInAnyApexTag interface {
216 blueprint.DependencyTag
217
218 CopyDirectlyInAnyApex()
219}
220
Jiyong Park9d452992018-10-03 00:38:19 +0900221// Provides default implementation for the ApexModule interface. APEX-aware
222// modules are expected to include this struct and call InitApexModule().
223type ApexModuleBase struct {
224 ApexProperties ApexProperties
225
226 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700227
228 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800229 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900230}
231
232func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
233 return m
234}
235
Paul Duffinbefa4b92020-03-04 14:22:45 +0000236func (m *ApexModuleBase) ApexAvailable() []string {
237 return m.ApexProperties.Apex_available
238}
239
Jiyong Park62304bb2020-04-13 16:19:48 +0900240func (m *ApexModuleBase) TestFor() []string {
241 // To be implemented by concrete types inheriting ApexModuleBase
242 return nil
243}
244
Colin Crossaede88c2020-08-11 12:17:01 -0700245func (m *ApexModuleBase) UniqueApexVariations() bool {
246 return false
247}
248
Jooyung Han698dd9f2020-07-22 15:17:19 +0900249func (m *ApexModuleBase) BuildForApex(apex ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700250 m.apexVariationsLock.Lock()
251 defer m.apexVariationsLock.Unlock()
Jooyung Han698dd9f2020-07-22 15:17:19 +0900252 for _, v := range m.apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700253 if v.ApexVariationName == apex.ApexVariationName {
Jooyung Han698dd9f2020-07-22 15:17:19 +0900254 return
Jiyong Parkf760cae2020-02-12 07:53:12 +0900255 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900256 }
Jooyung Han698dd9f2020-07-22 15:17:19 +0900257 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900258}
259
Colin Cross56a83212020-09-15 18:30:11 -0700260func (m *ApexModuleBase) DirectlyInAnyApex() bool {
261 return m.ApexProperties.DirectlyInAnyApex
Jiyong Park9d452992018-10-03 00:38:19 +0900262}
263
Colin Cross56a83212020-09-15 18:30:11 -0700264func (m *ApexModuleBase) AnyVariantDirectlyInAnyApex() bool {
265 return m.ApexProperties.AnyVariantDirectlyInAnyApex
Colin Crossaede88c2020-08-11 12:17:01 -0700266}
267
Colin Cross56a83212020-09-15 18:30:11 -0700268func (m *ApexModuleBase) InAnyApex() bool {
269 return m.ApexProperties.InAnyApex
Jiyong Park9d452992018-10-03 00:38:19 +0900270}
271
272func (m *ApexModuleBase) CanHaveApexVariants() bool {
273 return m.canHaveApexVariants
274}
275
276func (m *ApexModuleBase) IsInstallableToApex() bool {
277 // should be overriden if needed
278 return false
279}
280
Jiyong Park127b40b2019-09-30 16:04:35 +0900281const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900282 AvailableToPlatform = "//apex_available:platform"
Paul Duffin7d74e7b2020-03-06 12:30:13 +0000283 AvailableToAnyApex = "//apex_available:anyapex"
Yifan Hongd22a84a2020-07-28 17:37:46 -0700284 AvailableToGkiApex = "com.android.gki.*"
Jiyong Park127b40b2019-09-30 16:04:35 +0900285)
286
Jiyong Parka90ca002019-10-07 15:47:24 +0900287func CheckAvailableForApex(what string, apex_available []string) bool {
288 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000289 // apex_available defaults to ["//apex_available:platform"],
290 // which means 'available to the platform but no apexes'.
291 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900292 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900293 return InList(what, apex_available) ||
Yifan Hongd22a84a2020-07-28 17:37:46 -0700294 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) ||
295 (strings.HasPrefix(what, "com.android.gki.") && InList(AvailableToGkiApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900296}
297
298func (m *ApexModuleBase) AvailableFor(what string) bool {
299 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900300}
301
Jiyong Park89e850a2020-04-07 16:37:39 +0900302func (m *ApexModuleBase) NotAvailableForPlatform() bool {
303 return m.ApexProperties.NotAvailableForPlatform
304}
305
306func (m *ApexModuleBase) SetNotAvailableForPlatform() {
307 m.ApexProperties.NotAvailableForPlatform = true
308}
309
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900310func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
311 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
312 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
313 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
314 return true
315}
316
Jiyong Park127b40b2019-09-30 16:04:35 +0900317func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
318 for _, n := range m.ApexProperties.Apex_available {
Yifan Hongd22a84a2020-07-28 17:37:46 -0700319 if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900320 continue
321 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100322 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900323 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
324 }
325 }
326}
327
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800328type byApexName []ApexInfo
329
330func (a byApexName) Len() int { return len(a) }
331func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
Colin Crosse07f2312020-08-13 11:24:56 -0700332func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800333
Colin Crossaede88c2020-08-11 12:17:01 -0700334// mergeApexVariations deduplicates APEX variations that would build identically into a common
335// variation. It returns the reduced list of variations and a list of aliases from the original
336// variation names to the new variation names.
Colin Cross9f720ce2020-10-02 10:26:04 -0700337func mergeApexVariations(ctx PathContext, apexVariations []ApexInfo) (merged []ApexInfo, aliases [][2]string) {
Colin Crossaede88c2020-08-11 12:17:01 -0700338 sort.Sort(byApexName(apexVariations))
339 seen := make(map[string]int)
340 for _, apexInfo := range apexVariations {
341 apexName := apexInfo.ApexVariationName
Dan Albertc8060532020-07-22 22:32:17 -0700342 mergedName := apexInfo.mergedName(ctx)
Colin Crossaede88c2020-08-11 12:17:01 -0700343 if index, exists := seen[mergedName]; exists {
344 merged[index].InApexes = append(merged[index].InApexes, apexName)
Colin Cross56a83212020-09-15 18:30:11 -0700345 merged[index].ApexContents = append(merged[index].ApexContents, apexInfo.ApexContents...)
Colin Crossaede88c2020-08-11 12:17:01 -0700346 merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable
347 } else {
348 seen[mergedName] = len(merged)
Dan Albertc8060532020-07-22 22:32:17 -0700349 apexInfo.ApexVariationName = apexInfo.mergedName(ctx)
Colin Crossaede88c2020-08-11 12:17:01 -0700350 apexInfo.InApexes = CopyOf(apexInfo.InApexes)
Colin Cross56a83212020-09-15 18:30:11 -0700351 apexInfo.ApexContents = append([]*ApexContents(nil), apexInfo.ApexContents...)
Colin Crossaede88c2020-08-11 12:17:01 -0700352 merged = append(merged, apexInfo)
353 }
354 aliases = append(aliases, [2]string{apexName, mergedName})
355 }
356 return merged, aliases
357}
358
Colin Cross56a83212020-09-15 18:30:11 -0700359func CreateApexVariations(mctx BottomUpMutatorContext, module ApexModule) []Module {
360 base := module.apexModuleBase()
361 if len(base.apexVariations) > 0 {
362 base.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900363
Colin Crossaede88c2020-08-11 12:17:01 -0700364 var apexVariations []ApexInfo
365 var aliases [][2]string
Colin Cross56a83212020-09-15 18:30:11 -0700366 if !mctx.Module().(ApexModule).UniqueApexVariations() && !base.ApexProperties.UniqueApexVariationsForDeps {
367 apexVariations, aliases = mergeApexVariations(mctx, base.apexVariations)
Colin Crossaede88c2020-08-11 12:17:01 -0700368 } else {
Colin Cross56a83212020-09-15 18:30:11 -0700369 apexVariations = base.apexVariations
Colin Crossaede88c2020-08-11 12:17:01 -0700370 }
Colin Cross56a83212020-09-15 18:30:11 -0700371 // base.apexVariations is only needed to propagate the list of apexes from
372 // apexDepsMutator to apexMutator. It is no longer accurate after
373 // mergeApexVariations, and won't be copied to all but the first created
374 // variant. Clear it so it doesn't accidentally get used later.
375 base.apexVariations = nil
Colin Crossaede88c2020-08-11 12:17:01 -0700376
377 sort.Sort(byApexName(apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900378 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900379 variations = append(variations, "") // Original variation for platform
Colin Crossaede88c2020-08-11 12:17:01 -0700380 for _, apex := range apexVariations {
Colin Crosse07f2312020-08-13 11:24:56 -0700381 variations = append(variations, apex.ApexVariationName)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800382 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800383
Jiyong Park3ff16992019-12-27 14:11:47 +0900384 defaultVariation := ""
385 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900386
Colin Cross56a83212020-09-15 18:30:11 -0700387 var inApex ApexMembership
388 for _, a := range apexVariations {
389 for _, apexContents := range a.ApexContents {
390 inApex = inApex.merge(apexContents.contents[mctx.ModuleName()])
391 }
392 }
393
394 base.ApexProperties.InAnyApex = true
395 base.ApexProperties.DirectlyInAnyApex = inApex == directlyInApex
396
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900397 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800398 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900399 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800400 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
Martin Stjernholm9e9bb7f2020-08-06 22:34:42 +0100401 // Do not install the module for platform, but still allow it to output
402 // uninstallable AndroidMk entries in certain cases when they have
403 // side effects.
404 mod.MakeUninstallable()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900405 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800406 if !platformVariation {
Colin Cross56a83212020-09-15 18:30:11 -0700407 mctx.SetVariationProvider(mod, ApexInfoProvider, apexVariations[i-1])
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800408 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900409 }
Colin Crossaede88c2020-08-11 12:17:01 -0700410
411 for _, alias := range aliases {
412 mctx.CreateAliasVariation(alias[0], alias[1])
413 }
414
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900415 return modules
416 }
417 return nil
418}
419
Colin Cross56a83212020-09-15 18:30:11 -0700420// UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies
421// that are in the same APEX have unique APEX variations so that the module can link against
422// the right variant.
423func UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext, am ApexModule) {
424 // anyInSameApex returns true if the two ApexInfo lists contain any values in an InApexes list
425 // in common. It is used instead of DepIsInSameApex because it needs to determine if the dep
426 // is in the same APEX due to being directly included, not only if it is included _because_ it
427 // is a dependency.
428 anyInSameApex := func(a, b []ApexInfo) bool {
429 collectApexes := func(infos []ApexInfo) []string {
430 var ret []string
431 for _, info := range infos {
432 ret = append(ret, info.InApexes...)
433 }
434 return ret
435 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900436
Colin Cross56a83212020-09-15 18:30:11 -0700437 aApexes := collectApexes(a)
438 bApexes := collectApexes(b)
439 sort.Strings(bApexes)
440 for _, aApex := range aApexes {
441 index := sort.SearchStrings(bApexes, aApex)
442 if index < len(bApexes) && bApexes[index] == aApex {
443 return true
444 }
445 }
446 return false
447 }
448
449 mctx.VisitDirectDeps(func(dep Module) {
450 if depApexModule, ok := dep.(ApexModule); ok {
451 if anyInSameApex(depApexModule.apexModuleBase().apexVariations, am.apexModuleBase().apexVariations) &&
452 (depApexModule.UniqueApexVariations() ||
453 depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) {
454 am.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps = true
455 }
456 }
457 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900458}
459
Colin Cross56a83212020-09-15 18:30:11 -0700460// UpdateDirectlyInAnyApex uses the final module to store if any variant of this
461// module is directly in any APEX, and then copies the final value to all the modules.
462// It also copies the DirectlyInAnyApex value to any direct dependencies with a
463// CopyDirectlyInAnyApexTag dependency tag.
464func UpdateDirectlyInAnyApex(mctx BottomUpMutatorContext, am ApexModule) {
465 base := am.apexModuleBase()
466 // Copy DirectlyInAnyApex and InAnyApex from any direct dependencies with a
467 // CopyDirectlyInAnyApexTag dependency tag.
468 mctx.VisitDirectDeps(func(dep Module) {
469 if _, ok := mctx.OtherModuleDependencyTag(dep).(CopyDirectlyInAnyApexTag); ok {
470 depBase := dep.(ApexModule).apexModuleBase()
471 base.ApexProperties.DirectlyInAnyApex = depBase.ApexProperties.DirectlyInAnyApex
472 base.ApexProperties.InAnyApex = depBase.ApexProperties.InAnyApex
473 }
474 })
475
476 if base.ApexProperties.DirectlyInAnyApex {
477 // Variants of a module are always visited sequentially in order, so it is safe to
478 // write to another variant of this module.
479 // For a BottomUpMutator the PrimaryModule() is visited first and FinalModule() is
480 // visited last.
481 mctx.FinalModule().(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex = true
Jiyong Park25fc6a92018-11-18 18:02:45 +0900482 }
Colin Cross56a83212020-09-15 18:30:11 -0700483
484 // If this is the FinalModule (last visited module) copy AnyVariantDirectlyInAnyApex to
485 // all the other variants
486 if am == mctx.FinalModule().(ApexModule) {
487 mctx.VisitAllModuleVariants(func(variant Module) {
488 variant.(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex =
489 base.ApexProperties.AnyVariantDirectlyInAnyApex
490 })
Colin Crossaede88c2020-08-11 12:17:01 -0700491 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900492}
493
Colin Cross56a83212020-09-15 18:30:11 -0700494type ApexMembership int
495
496const (
497 notInApex ApexMembership = 0
498 indirectlyInApex = iota
499 directlyInApex
500)
501
502// Each apexBundle has an apexContents, and modules in that apex have a provider containing the
503// apexContents of each apexBundle they are part of.
504type ApexContents struct {
505 ApexName string
506 contents map[string]ApexMembership
507}
508
509func NewApexContents(name string, contents map[string]ApexMembership) *ApexContents {
510 return &ApexContents{
511 ApexName: name,
512 contents: contents,
Jooyung Han671f1ce2019-12-17 12:47:13 +0900513 }
514}
515
Colin Cross56a83212020-09-15 18:30:11 -0700516func (i ApexMembership) Add(direct bool) ApexMembership {
517 if direct || i == directlyInApex {
518 return directlyInApex
Jiyong Park25fc6a92018-11-18 18:02:45 +0900519 }
Colin Cross56a83212020-09-15 18:30:11 -0700520 return indirectlyInApex
521}
522
523func (i ApexMembership) merge(other ApexMembership) ApexMembership {
524 if other == directlyInApex || i == directlyInApex {
525 return directlyInApex
526 }
527
528 if other == indirectlyInApex || i == indirectlyInApex {
529 return indirectlyInApex
530 }
531 return notInApex
532}
533
534func (ac *ApexContents) DirectlyInApex(name string) bool {
535 return ac.contents[name] == directlyInApex
536}
537
538func (ac *ApexContents) InApex(name string) bool {
539 return ac.contents[name] != notInApex
Jiyong Park25fc6a92018-11-18 18:02:45 +0900540}
541
Colin Crossaede88c2020-08-11 12:17:01 -0700542// Tests whether a module named moduleName is directly depended on by all APEXes
Colin Cross56a83212020-09-15 18:30:11 -0700543// in an ApexInfo.
544func DirectlyInAllApexes(apexInfo ApexInfo, moduleName string) bool {
545 for _, contents := range apexInfo.ApexContents {
546 if !contents.DirectlyInApex(moduleName) {
Colin Crossaede88c2020-08-11 12:17:01 -0700547 return false
548 }
549 }
550 return true
551}
552
Jiyong Park9d452992018-10-03 00:38:19 +0900553func InitApexModule(m ApexModule) {
554 base := m.apexModuleBase()
555 base.canHaveApexVariants = true
556
557 m.AddProperties(&base.ApexProperties)
558}
Artur Satayev872a1442020-04-27 17:08:37 +0100559
560// A dependency info for a single ApexModule, either direct or transitive.
561type ApexModuleDepInfo struct {
562 // Name of the dependency
563 To string
564 // List of dependencies To belongs to. Includes APEX itself, if a direct dependency.
565 From []string
566 // Whether the dependency belongs to the final compiled APEX.
567 IsExternal bool
Artur Satayev480e25b2020-04-27 18:53:18 +0100568 // min_sdk_version of the ApexModule
569 MinSdkVersion string
Artur Satayev872a1442020-04-27 17:08:37 +0100570}
571
572// A map of a dependency name to its ApexModuleDepInfo
573type DepNameToDepInfoMap map[string]ApexModuleDepInfo
574
575type ApexBundleDepsInfo struct {
Jooyung Han98d63e12020-05-14 07:44:03 +0900576 flatListPath OutputPath
577 fullListPath OutputPath
Artur Satayev872a1442020-04-27 17:08:37 +0100578}
579
Artur Satayev849f8442020-04-28 14:57:42 +0100580type ApexBundleDepsInfoIntf interface {
581 Updatable() bool
Artur Satayeva8bd1132020-04-27 18:07:06 +0100582 FlatListPath() Path
Artur Satayev872a1442020-04-27 17:08:37 +0100583 FullListPath() Path
584}
585
Artur Satayeva8bd1132020-04-27 18:07:06 +0100586func (d *ApexBundleDepsInfo) FlatListPath() Path {
587 return d.flatListPath
588}
589
Artur Satayev872a1442020-04-27 17:08:37 +0100590func (d *ApexBundleDepsInfo) FullListPath() Path {
591 return d.fullListPath
592}
593
Artur Satayeva8bd1132020-04-27 18:07:06 +0100594// Generate two module out files:
595// 1. FullList with transitive deps and their parents in the dep graph
596// 2. FlatList with a flat list of transitive deps
Artur Satayev480e25b2020-04-27 18:53:18 +0100597func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) {
Artur Satayeva8bd1132020-04-27 18:07:06 +0100598 var fullContent strings.Builder
599 var flatContent strings.Builder
600
Colin Crosscf371cc2020-11-13 11:48:42 -0800601 fmt.Fprintf(&fullContent, "%s(minSdkVersion:%s):\n", ctx.ModuleName(), minSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100602 for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
603 info := depInfos[key]
Artur Satayev480e25b2020-04-27 18:53:18 +0100604 toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
Artur Satayev872a1442020-04-27 17:08:37 +0100605 if info.IsExternal {
606 toName = toName + " (external)"
607 }
Colin Crosscf371cc2020-11-13 11:48:42 -0800608 fmt.Fprintf(&fullContent, " %s <- %s\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
609 fmt.Fprintf(&flatContent, "%s\n", toName)
Artur Satayev872a1442020-04-27 17:08:37 +0100610 }
611
612 d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
Colin Crosscf371cc2020-11-13 11:48:42 -0800613 WriteFileRule(ctx, d.fullListPath, fullContent.String())
Artur Satayeva8bd1132020-04-27 18:07:06 +0100614
615 d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
Colin Crosscf371cc2020-11-13 11:48:42 -0800616 WriteFileRule(ctx, d.flatListPath, flatContent.String())
Artur Satayev872a1442020-04-27 17:08:37 +0100617}
Jooyung Han749dc692020-04-15 11:03:39 +0900618
619// TODO(b/158059172): remove minSdkVersion allowlist
Dan Albertc8060532020-07-22 22:32:17 -0700620var minSdkVersionAllowlist = func(apiMap map[string]int) map[string]ApiLevel {
621 list := make(map[string]ApiLevel, len(apiMap))
622 for name, finalApiInt := range apiMap {
623 list[name] = uncheckedFinalApiLevel(finalApiInt)
624 }
625 return list
626}(map[string]int{
Jooyung Han749dc692020-04-15 11:03:39 +0900627 "adbd": 30,
628 "android.net.ipsec.ike": 30,
629 "androidx-constraintlayout_constraintlayout-solver": 30,
630 "androidx.annotation_annotation": 28,
631 "androidx.arch.core_core-common": 28,
632 "androidx.collection_collection": 28,
633 "androidx.lifecycle_lifecycle-common": 28,
634 "apache-commons-compress": 29,
635 "bouncycastle_ike_digests": 30,
636 "brotli-java": 29,
637 "captiveportal-lib": 28,
638 "flatbuffer_headers": 30,
639 "framework-permission": 30,
640 "framework-statsd": 30,
641 "gemmlowp_headers": 30,
642 "ike-internals": 30,
643 "kotlinx-coroutines-android": 28,
644 "kotlinx-coroutines-core": 28,
645 "libadb_crypto": 30,
646 "libadb_pairing_auth": 30,
647 "libadb_pairing_connection": 30,
648 "libadb_pairing_server": 30,
649 "libadb_protos": 30,
650 "libadb_tls_connection": 30,
651 "libadbconnection_client": 30,
652 "libadbconnection_server": 30,
653 "libadbd_core": 30,
654 "libadbd_services": 30,
655 "libadbd": 30,
656 "libapp_processes_protos_lite": 30,
657 "libasyncio": 30,
658 "libbrotli": 30,
659 "libbuildversion": 30,
660 "libcrypto_static": 30,
661 "libcrypto_utils": 30,
662 "libdiagnose_usb": 30,
663 "libeigen": 30,
664 "liblz4": 30,
665 "libmdnssd": 30,
666 "libneuralnetworks_common": 30,
667 "libneuralnetworks_headers": 30,
668 "libneuralnetworks": 30,
669 "libprocpartition": 30,
670 "libprotobuf-java-lite": 30,
671 "libprotoutil": 30,
672 "libqemu_pipe": 30,
673 "libstats_jni": 30,
674 "libstatslog_statsd": 30,
675 "libstatsmetadata": 30,
676 "libstatspull": 30,
677 "libstatssocket": 30,
678 "libsync": 30,
679 "libtextclassifier_hash_headers": 30,
680 "libtextclassifier_hash_static": 30,
681 "libtflite_kernel_utils": 30,
682 "libwatchdog": 29,
683 "libzstd": 30,
684 "metrics-constants-protos": 28,
685 "net-utils-framework-common": 29,
686 "permissioncontroller-statsd": 28,
687 "philox_random_headers": 30,
688 "philox_random": 30,
689 "service-permission": 30,
690 "service-statsd": 30,
691 "statsd-aidl-ndk_platform": 30,
692 "statsd": 30,
693 "tensorflow_headers": 30,
694 "xz-java": 29,
Dan Albertc8060532020-07-22 22:32:17 -0700695})
Jooyung Han749dc692020-04-15 11:03:39 +0900696
697// Function called while walking an APEX's payload dependencies.
698//
699// Return true if the `to` module should be visited, false otherwise.
700type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool
701
702// UpdatableModule represents updatable APEX/APK
703type UpdatableModule interface {
704 Module
705 WalkPayloadDeps(ctx ModuleContext, do PayloadDepsCallback)
706}
707
708// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version accordingly
Dan Albertc8060532020-07-22 22:32:17 -0700709func CheckMinSdkVersion(m UpdatableModule, ctx ModuleContext, minSdkVersion ApiLevel) {
Jooyung Han749dc692020-04-15 11:03:39 +0900710 // do not enforce min_sdk_version for host
711 if ctx.Host() {
712 return
713 }
714
715 // do not enforce for coverage build
716 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() {
717 return
718 }
719
720 // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version or
721 // min_sdk_version is not finalized (e.g. current or codenames)
Dan Albertc8060532020-07-22 22:32:17 -0700722 if minSdkVersion.IsCurrent() {
Jooyung Han749dc692020-04-15 11:03:39 +0900723 return
724 }
725
726 m.WalkPayloadDeps(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool {
727 if externalDep {
728 // external deps are outside the payload boundary, which is "stable" interface.
729 // We don't have to check min_sdk_version for external dependencies.
730 return false
731 }
732 if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) {
733 return false
734 }
735 if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil {
736 toName := ctx.OtherModuleName(to)
Dan Albertc8060532020-07-22 22:32:17 -0700737 if ver, ok := minSdkVersionAllowlist[toName]; !ok || ver.GreaterThan(minSdkVersion) {
Jooyung Han749dc692020-04-15 11:03:39 +0900738 ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v. Dependency path: %s",
739 minSdkVersion, ctx.ModuleName(), err.Error(), ctx.GetPathString(false))
740 return false
741 }
742 }
743 return true
744 })
745}