blob: f0ff7ad30b540e3a91f7887fb850a160f590b3e3 [file] [log] [blame]
Jiyong Park9d452992018-10-03 00:38:19 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
Jiyong Park0ddfcd12018-12-11 01:35:25 +090017import (
Jooyung Han0c4e0162020-02-26 22:45:42 +090018 "fmt"
Colin Crosscefa94bd2019-06-03 15:07:03 -070019 "sort"
Jooyung Han0c4e0162020-02-26 22:45:42 +090020 "strconv"
Artur Satayev334b5172020-04-27 17:08:37 +010021 "strings"
Jiyong Park0ddfcd12018-12-11 01:35:25 +090022 "sync"
Paul Duffin3766cb72020-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
Jooyung Han23b0adf2020-03-12 18:37:20 +090027const (
28 SdkVersion_Android10 = 29
29)
30
Peter Collingbournedc4f9862020-02-12 17:13:25 -080031type ApexInfo struct {
Colin Cross74385712020-08-13 11:24:56 -070032 // Name of the apex variation that this module is mutated into
33 ApexVariationName string
Peter Collingbournedc4f9862020-02-12 17:13:25 -080034
Jooyung Han0c4e0162020-02-26 22:45:42 +090035 MinSdkVersion int
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +010036 Updatable bool
Colin Cross274a72d2020-08-11 12:17:01 -070037 RequiredSdks SdkRefs
38
39 InApexes []string
40}
41
42func (i ApexInfo) mergedName() string {
43 name := "apex" + strconv.Itoa(i.MinSdkVersion)
44 for _, sdk := range i.RequiredSdks {
45 name += "_" + sdk.Name + "_" + sdk.Version
46 }
47 return name
Peter Collingbournedc4f9862020-02-12 17:13:25 -080048}
49
Paul Duffin03e7d0c2020-03-30 15:33:32 +010050// Extracted from ApexModule to make it easier to define custom subsets of the
51// ApexModule interface and improve code navigation within the IDE.
52type DepIsInSameApex interface {
53 // DepIsInSameApex tests if the other module 'dep' is installed to the same
54 // APEX as this module
55 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool
56}
57
Jiyong Park9d452992018-10-03 00:38:19 +090058// ApexModule is the interface that a module type is expected to implement if
59// the module has to be built differently depending on whether the module
60// is destined for an apex or not (installed to one of the regular partitions).
61//
62// Native shared libraries are one such module type; when it is built for an
63// APEX, it should depend only on stable interfaces such as NDK, stable AIDL,
64// or C APIs from other APEXs.
65//
66// A module implementing this interface will be mutated into multiple
Jiyong Park0ddfcd12018-12-11 01:35:25 +090067// variations by apex.apexMutator if it is directly or indirectly included
Jiyong Park9d452992018-10-03 00:38:19 +090068// in one or more APEXs. Specifically, if a module is included in apex.foo and
69// apex.bar then three apex variants are created: platform, apex.foo and
70// apex.bar. The platform variant is for the regular partitions
71// (e.g., /system or /vendor, etc.) while the other two are for the APEXs,
72// respectively.
73type ApexModule interface {
74 Module
Paul Duffin03e7d0c2020-03-30 15:33:32 +010075 DepIsInSameApex
76
Jiyong Park9d452992018-10-03 00:38:19 +090077 apexModuleBase() *ApexModuleBase
78
Jooyung Han345f0c42020-07-22 15:17:19 +090079 // Marks that this module should be built for the specified APEX.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090080 // Call this before apex.apexMutator is run.
Jooyung Han345f0c42020-07-22 15:17:19 +090081 BuildForApex(apex ApexInfo)
Jiyong Parkf760cae2020-02-12 07:53:12 +090082
Colin Cross74385712020-08-13 11:24:56 -070083 // Returns the name of APEX variation that this module will be built for.
Colin Cross274a72d2020-08-11 12:17:01 -070084 // Empty string is returned when 'IsForPlatform() == true'. Note that a
85 // module can beincluded in multiple APEXes, in which case, the module
86 // is mutated into one or more variants, each of which is for one or
87 // more APEXes. This method returns the name of the APEX variation of
88 // the module.
Jiyong Park0ddfcd12018-12-11 01:35:25 +090089 // Call this after apex.apexMutator is run.
Colin Cross74385712020-08-13 11:24:56 -070090 ApexVariationName() string
Jiyong Park9d452992018-10-03 00:38:19 +090091
Colin Cross274a72d2020-08-11 12:17:01 -070092 // Returns the name of the APEX modules that this variant of this module
93 // is present in.
94 // Call this after apex.apexMutator is run.
95 InApexes() []string
96
Jiyong Park0ddfcd12018-12-11 01:35:25 +090097 // Tests whether this module will be built for the platform or not.
Colin Cross74385712020-08-13 11:24:56 -070098 // This is a shortcut for ApexVariationName() == ""
Jiyong Park0ddfcd12018-12-11 01:35:25 +090099 IsForPlatform() bool
100
101 // Tests if this module could have APEX variants. APEX variants are
Jiyong Park9d452992018-10-03 00:38:19 +0900102 // created only for the modules that returns true here. This is useful
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900103 // for not creating APEX variants for certain types of shared libraries
104 // such as NDK stubs.
Jiyong Park9d452992018-10-03 00:38:19 +0900105 CanHaveApexVariants() bool
106
107 // Tests if this module can be installed to APEX as a file. For example,
108 // this would return true for shared libs while return false for static
109 // libs.
110 IsInstallableToApex() bool
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900111
112 // Mutate this module into one or more variants each of which is built
Jooyung Han345f0c42020-07-22 15:17:19 +0900113 // for an APEX marked via BuildForApex().
Colin Cross43b92e02019-11-18 15:28:57 -0800114 CreateApexVariations(mctx BottomUpMutatorContext) []Module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900115
Jiyong Park127b40b2019-09-30 16:04:35 +0900116 // Tests if this module is available for the specified APEX or ":platform"
117 AvailableFor(what string) bool
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900118
Jiyong Park6a9ddc32020-04-07 16:37:39 +0900119 // Return true if this module is not available to platform (i.e. apex_available
120 // property doesn't have "//apex_available:platform"), or shouldn't be available
121 // to platform, which is the case when this module depends on other module that
122 // isn't available to platform.
123 NotAvailableForPlatform() bool
124
125 // Mark that this module is not available to platform. Set by the
126 // check-platform-availability mutator in the apex package.
127 SetNotAvailableForPlatform()
128
Jooyung Han74066602020-03-20 04:29:24 +0900129 // Returns the highest version which is <= maxSdkVersion.
130 // For example, with maxSdkVersion is 10 and versionList is [9,11]
131 // it returns 9 as string
132 ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error)
Jiyong Parkf0d01b72020-04-13 16:19:48 +0900133
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +0100134 // Tests if the module comes from an updatable APEX.
135 Updatable() bool
136
Jiyong Parkf0d01b72020-04-13 16:19:48 +0900137 // List of APEXes that this module tests. The module has access to
138 // the private part of the listed APEXes even when it is not included in the
139 // APEXes.
140 TestFor() []string
Colin Cross274a72d2020-08-11 12:17:01 -0700141
142 // Returns true if this module needs a unique variation per apex, for example if
143 // use_apex_name_macro is set.
144 UniqueApexVariations() bool
145
146 // UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies
147 // that are in the same APEX have unique APEX variations so that the module can link against
148 // the right variant.
149 UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext)
Jiyong Park9d452992018-10-03 00:38:19 +0900150}
151
152type ApexProperties struct {
Martin Stjernholm06ca82d2020-01-17 13:02:56 +0000153 // Availability of this module in APEXes. Only the listed APEXes can contain
154 // this module. If the module has stubs then other APEXes and the platform may
155 // access it through them (subject to visibility).
156 //
Jiyong Park127b40b2019-09-30 16:04:35 +0900157 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX.
158 // "//apex_available:platform" refers to non-APEX partitions like "system.img".
Jiyong Park9a1e14e2020-02-13 02:30:45 +0900159 // Default is ["//apex_available:platform"].
Jiyong Park127b40b2019-09-30 16:04:35 +0900160 Apex_available []string
161
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800162 Info ApexInfo `blueprint:"mutated"`
Jiyong Park6a9ddc32020-04-07 16:37:39 +0900163
164 NotAvailableForPlatform bool `blueprint:"mutated"`
Colin Cross274a72d2020-08-11 12:17:01 -0700165
166 UniqueApexVariationsForDeps bool `blueprint:"mutated"`
Jiyong Park9d452992018-10-03 00:38:19 +0900167}
168
Paul Duffin3766cb72020-04-07 15:25:44 +0100169// Marker interface that identifies dependencies that are excluded from APEX
170// contents.
171type ExcludeFromApexContentsTag interface {
172 blueprint.DependencyTag
173
174 // Method that differentiates this interface from others.
175 ExcludeFromApexContents()
176}
177
Jiyong Park9d452992018-10-03 00:38:19 +0900178// Provides default implementation for the ApexModule interface. APEX-aware
179// modules are expected to include this struct and call InitApexModule().
180type ApexModuleBase struct {
181 ApexProperties ApexProperties
182
183 canHaveApexVariants bool
Colin Crosscefa94bd2019-06-03 15:07:03 -0700184
185 apexVariationsLock sync.Mutex // protects apexVariations during parallel apexDepsMutator
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800186 apexVariations []ApexInfo
Jiyong Park9d452992018-10-03 00:38:19 +0900187}
188
189func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase {
190 return m
191}
192
Paul Duffin3a6c0952020-03-04 14:22:45 +0000193func (m *ApexModuleBase) ApexAvailable() []string {
194 return m.ApexProperties.Apex_available
195}
196
Jiyong Parkf0d01b72020-04-13 16:19:48 +0900197func (m *ApexModuleBase) TestFor() []string {
198 // To be implemented by concrete types inheriting ApexModuleBase
199 return nil
200}
201
Colin Cross274a72d2020-08-11 12:17:01 -0700202func (m *ApexModuleBase) UniqueApexVariations() bool {
203 return false
204}
205
206func (m *ApexModuleBase) UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext) {
207 // anyInSameApex returns true if the two ApexInfo lists contain any values in an InApexes list
208 // in common. It is used instead of DepIsInSameApex because it needs to determine if the dep
209 // is in the same APEX due to being directly included, not only if it is included _because_ it
210 // is a dependency.
211 anyInSameApex := func(a, b []ApexInfo) bool {
212 collectApexes := func(infos []ApexInfo) []string {
213 var ret []string
214 for _, info := range infos {
215 ret = append(ret, info.InApexes...)
216 }
217 return ret
218 }
219
220 aApexes := collectApexes(a)
221 bApexes := collectApexes(b)
222 sort.Strings(bApexes)
223 for _, aApex := range aApexes {
224 index := sort.SearchStrings(bApexes, aApex)
225 if index < len(bApexes) && bApexes[index] == aApex {
226 return true
227 }
228 }
229 return false
230 }
231
232 mctx.VisitDirectDeps(func(dep Module) {
233 if depApexModule, ok := dep.(ApexModule); ok {
234 if anyInSameApex(depApexModule.apexModuleBase().apexVariations, m.apexVariations) &&
235 (depApexModule.UniqueApexVariations() ||
236 depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) {
237 m.ApexProperties.UniqueApexVariationsForDeps = true
238 }
239 }
240 })
241}
242
Jooyung Han345f0c42020-07-22 15:17:19 +0900243func (m *ApexModuleBase) BuildForApex(apex ApexInfo) {
Colin Crosscefa94bd2019-06-03 15:07:03 -0700244 m.apexVariationsLock.Lock()
245 defer m.apexVariationsLock.Unlock()
Jooyung Han345f0c42020-07-22 15:17:19 +0900246 for _, v := range m.apexVariations {
Colin Cross74385712020-08-13 11:24:56 -0700247 if v.ApexVariationName == apex.ApexVariationName {
Jooyung Han345f0c42020-07-22 15:17:19 +0900248 return
Jiyong Parkf760cae2020-02-12 07:53:12 +0900249 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900250 }
Jooyung Han345f0c42020-07-22 15:17:19 +0900251 m.apexVariations = append(m.apexVariations, apex)
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900252}
253
Colin Cross74385712020-08-13 11:24:56 -0700254func (m *ApexModuleBase) ApexVariationName() string {
255 return m.ApexProperties.Info.ApexVariationName
Jiyong Park9d452992018-10-03 00:38:19 +0900256}
257
Colin Cross274a72d2020-08-11 12:17:01 -0700258func (m *ApexModuleBase) InApexes() []string {
259 return m.ApexProperties.Info.InApexes
260}
261
Jiyong Park9d452992018-10-03 00:38:19 +0900262func (m *ApexModuleBase) IsForPlatform() bool {
Colin Cross74385712020-08-13 11:24:56 -0700263 return m.ApexProperties.Info.ApexVariationName == ""
Jiyong Park9d452992018-10-03 00:38:19 +0900264}
265
266func (m *ApexModuleBase) CanHaveApexVariants() bool {
267 return m.canHaveApexVariants
268}
269
270func (m *ApexModuleBase) IsInstallableToApex() bool {
271 // should be overriden if needed
272 return false
273}
274
Jiyong Park127b40b2019-09-30 16:04:35 +0900275const (
Jiyong Parkb02bb402019-12-03 00:43:57 +0900276 AvailableToPlatform = "//apex_available:platform"
Paul Duffin404db3f2020-03-06 12:30:13 +0000277 AvailableToAnyApex = "//apex_available:anyapex"
Jiyong Park127b40b2019-09-30 16:04:35 +0900278)
279
Jiyong Parka90ca002019-10-07 15:47:24 +0900280func CheckAvailableForApex(what string, apex_available []string) bool {
281 if len(apex_available) == 0 {
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000282 // apex_available defaults to ["//apex_available:platform"],
283 // which means 'available to the platform but no apexes'.
284 return what == AvailableToPlatform
Jiyong Park127b40b2019-09-30 16:04:35 +0900285 }
Jiyong Parka90ca002019-10-07 15:47:24 +0900286 return InList(what, apex_available) ||
Paul Duffin404db3f2020-03-06 12:30:13 +0000287 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available))
Jiyong Parka90ca002019-10-07 15:47:24 +0900288}
289
290func (m *ApexModuleBase) AvailableFor(what string) bool {
291 return CheckAvailableForApex(what, m.ApexProperties.Apex_available)
Jiyong Park127b40b2019-09-30 16:04:35 +0900292}
293
Jiyong Park6a9ddc32020-04-07 16:37:39 +0900294func (m *ApexModuleBase) NotAvailableForPlatform() bool {
295 return m.ApexProperties.NotAvailableForPlatform
296}
297
298func (m *ApexModuleBase) SetNotAvailableForPlatform() {
299 m.ApexProperties.NotAvailableForPlatform = true
300}
301
Jiyong Parka7bc8ad2019-10-15 15:20:07 +0900302func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool {
303 // By default, if there is a dependency from A to B, we try to include both in the same APEX,
304 // unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning true.
305 // This is overridden by some module types like apex.ApexBundle, cc.Module, java.Module, etc.
306 return true
307}
308
Jooyung Han74066602020-03-20 04:29:24 +0900309func (m *ApexModuleBase) ChooseSdkVersion(versionList []string, maxSdkVersion int) (string, error) {
Jooyung Han0c4e0162020-02-26 22:45:42 +0900310 for i := range versionList {
311 ver, _ := strconv.Atoi(versionList[len(versionList)-i-1])
Jooyung Han74066602020-03-20 04:29:24 +0900312 if ver <= maxSdkVersion {
Jooyung Han0c4e0162020-02-26 22:45:42 +0900313 return versionList[len(versionList)-i-1], nil
314 }
315 }
Jooyung Han74066602020-03-20 04:29:24 +0900316 return "", fmt.Errorf("not found a version(<=%d) in versionList: %v", maxSdkVersion, versionList)
Jooyung Han0c4e0162020-02-26 22:45:42 +0900317}
318
Jiyong Park127b40b2019-09-30 16:04:35 +0900319func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) {
320 for _, n := range m.ApexProperties.Apex_available {
Paul Duffin404db3f2020-03-06 12:30:13 +0000321 if n == AvailableToPlatform || n == AvailableToAnyApex {
Jiyong Park127b40b2019-09-30 16:04:35 +0900322 continue
323 }
Orion Hodson4b5438a2019-10-08 10:40:51 +0100324 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() {
Jiyong Park127b40b2019-09-30 16:04:35 +0900325 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n)
326 }
327 }
328}
329
Ulya Trafimovichc0eb0b12020-04-22 18:05:58 +0100330func (m *ApexModuleBase) Updatable() bool {
331 return m.ApexProperties.Info.Updatable
332}
333
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800334type byApexName []ApexInfo
335
336func (a byApexName) Len() int { return len(a) }
337func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
Colin Cross74385712020-08-13 11:24:56 -0700338func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800339
Colin Cross274a72d2020-08-11 12:17:01 -0700340// mergeApexVariations deduplicates APEX variations that would build identically into a common
341// variation. It returns the reduced list of variations and a list of aliases from the original
342// variation names to the new variation names.
343func mergeApexVariations(apexVariations []ApexInfo) (merged []ApexInfo, aliases [][2]string) {
344 sort.Sort(byApexName(apexVariations))
345 seen := make(map[string]int)
346 for _, apexInfo := range apexVariations {
347 apexName := apexInfo.ApexVariationName
348 mergedName := apexInfo.mergedName()
349 if index, exists := seen[mergedName]; exists {
350 merged[index].InApexes = append(merged[index].InApexes, apexName)
351 merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable
352 } else {
353 seen[mergedName] = len(merged)
354 apexInfo.ApexVariationName = apexInfo.mergedName()
355 apexInfo.InApexes = CopyOf(apexInfo.InApexes)
356 merged = append(merged, apexInfo)
357 }
358 aliases = append(aliases, [2]string{apexName, mergedName})
359 }
360 return merged, aliases
361}
362
Colin Cross43b92e02019-11-18 15:28:57 -0800363func (m *ApexModuleBase) CreateApexVariations(mctx BottomUpMutatorContext) []Module {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900364 if len(m.apexVariations) > 0 {
Jiyong Park127b40b2019-09-30 16:04:35 +0900365 m.checkApexAvailableProperty(mctx)
Jiyong Park0f80c182020-01-31 02:49:53 +0900366
Colin Cross274a72d2020-08-11 12:17:01 -0700367 var apexVariations []ApexInfo
368 var aliases [][2]string
369 if !mctx.Module().(ApexModule).UniqueApexVariations() && !m.ApexProperties.UniqueApexVariationsForDeps {
370 apexVariations, aliases = mergeApexVariations(m.apexVariations)
371 } else {
372 apexVariations = m.apexVariations
373 }
374
375 sort.Sort(byApexName(apexVariations))
Jiyong Park127b40b2019-09-30 16:04:35 +0900376 variations := []string{}
Jiyong Park0f80c182020-01-31 02:49:53 +0900377 variations = append(variations, "") // Original variation for platform
Colin Cross274a72d2020-08-11 12:17:01 -0700378 for _, apex := range apexVariations {
Colin Cross74385712020-08-13 11:24:56 -0700379 variations = append(variations, apex.ApexVariationName)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800380 }
Logan Chien3aeedc92018-12-26 15:32:21 +0800381
Jiyong Park3ff16992019-12-27 14:11:47 +0900382 defaultVariation := ""
383 mctx.SetDefaultDependencyVariation(&defaultVariation)
Jiyong Park0f80c182020-01-31 02:49:53 +0900384
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900385 modules := mctx.CreateVariations(variations...)
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800386 for i, mod := range modules {
Jiyong Park0f80c182020-01-31 02:49:53 +0900387 platformVariation := i == 0
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800388 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) {
389 mod.SkipInstall()
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900390 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800391 if !platformVariation {
Colin Cross274a72d2020-08-11 12:17:01 -0700392 mod.(ApexModule).apexModuleBase().ApexProperties.Info = apexVariations[i-1]
Peter Collingbournedc4f9862020-02-12 17:13:25 -0800393 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900394 }
Colin Cross274a72d2020-08-11 12:17:01 -0700395
396 for _, alias := range aliases {
397 mctx.CreateAliasVariation(alias[0], alias[1])
398 }
399
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900400 return modules
401 }
402 return nil
403}
404
405var apexData OncePer
406var apexNamesMapMutex sync.Mutex
Colin Cross571cccf2019-02-04 11:22:08 -0800407var apexNamesKey = NewOnceKey("apexNames")
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900408
409// This structure maintains the global mapping in between modules and APEXes.
410// Examples:
Jiyong Park25fc6a92018-11-18 18:02:45 +0900411//
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900412// apexNamesMap()["foo"]["bar"] == true: module foo is directly depended on by APEX bar
413// apexNamesMap()["foo"]["bar"] == false: module foo is indirectly depended on by APEX bar
414// apexNamesMap()["foo"]["bar"] doesn't exist: foo is not built for APEX bar
415func apexNamesMap() map[string]map[string]bool {
Colin Cross571cccf2019-02-04 11:22:08 -0800416 return apexData.Once(apexNamesKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900417 return make(map[string]map[string]bool)
418 }).(map[string]map[string]bool)
419}
420
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900421// Update the map to mark that a module named moduleName is directly or indirectly
Jiyong Parkf760cae2020-02-12 07:53:12 +0900422// depended on by the specified APEXes. Directly depending means that a module
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900423// is explicitly listed in the build definition of the APEX via properties like
424// native_shared_libs, java_libs, etc.
Jooyung Han345f0c42020-07-22 15:17:19 +0900425func UpdateApexDependency(apex ApexInfo, moduleName string, directDep bool) {
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900426 apexNamesMapMutex.Lock()
427 defer apexNamesMapMutex.Unlock()
Jooyung Han345f0c42020-07-22 15:17:19 +0900428 apexesForModule, ok := apexNamesMap()[moduleName]
429 if !ok {
430 apexesForModule = make(map[string]bool)
431 apexNamesMap()[moduleName] = apexesForModule
Jiyong Park25fc6a92018-11-18 18:02:45 +0900432 }
Colin Cross74385712020-08-13 11:24:56 -0700433 apexesForModule[apex.ApexVariationName] = apexesForModule[apex.ApexVariationName] || directDep
Colin Cross274a72d2020-08-11 12:17:01 -0700434 for _, apexName := range apex.InApexes {
435 apexesForModule[apexName] = apexesForModule[apex.ApexVariationName] || directDep
436 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900437}
438
Jooyung Han671f1ce2019-12-17 12:47:13 +0900439// TODO(b/146393795): remove this when b/146393795 is fixed
440func ClearApexDependency() {
441 m := apexNamesMap()
442 for k := range m {
443 delete(m, k)
444 }
445}
446
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900447// Tests whether a module named moduleName is directly depended on by an APEX
448// named apexName.
449func DirectlyInApex(apexName string, moduleName string) bool {
450 apexNamesMapMutex.Lock()
451 defer apexNamesMapMutex.Unlock()
Colin Cross274a72d2020-08-11 12:17:01 -0700452 if apexNamesForModule, ok := apexNamesMap()[moduleName]; ok {
453 return apexNamesForModule[apexName]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900454 }
455 return false
456}
457
Colin Cross274a72d2020-08-11 12:17:01 -0700458// Tests whether a module named moduleName is directly depended on by all APEXes
459// in a list of apexNames.
460func DirectlyInAllApexes(apexNames []string, moduleName string) bool {
461 apexNamesMapMutex.Lock()
462 defer apexNamesMapMutex.Unlock()
463 for _, apexName := range apexNames {
464 apexNamesForModule := apexNamesMap()[moduleName]
465 if !apexNamesForModule[apexName] {
466 return false
467 }
468 }
469 return true
470}
471
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000472type hostContext interface {
473 Host() bool
474}
475
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900476// Tests whether a module named moduleName is directly depended on by any APEX.
Nicolas Geoffrayc22c1bf2019-01-15 19:53:23 +0000477func DirectlyInAnyApex(ctx hostContext, moduleName string) bool {
478 if ctx.Host() {
479 // Host has no APEX.
480 return false
481 }
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900482 apexNamesMapMutex.Lock()
483 defer apexNamesMapMutex.Unlock()
484 if apexNames, ok := apexNamesMap()[moduleName]; ok {
485 for an := range apexNames {
486 if apexNames[an] {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900487 return true
488 }
489 }
490 }
491 return false
492}
493
Jiyong Park0ddfcd12018-12-11 01:35:25 +0900494// Tests whether a module named module is depended on (including both
495// direct and indirect dependencies) by any APEX.
496func InAnyApex(moduleName string) bool {
497 apexNamesMapMutex.Lock()
498 defer apexNamesMapMutex.Unlock()
499 apexNames, ok := apexNamesMap()[moduleName]
500 return ok && len(apexNames) > 0
501}
502
503func GetApexesForModule(moduleName string) []string {
504 ret := []string{}
505 apexNamesMapMutex.Lock()
506 defer apexNamesMapMutex.Unlock()
507 if apexNames, ok := apexNamesMap()[moduleName]; ok {
508 for an := range apexNames {
509 ret = append(ret, an)
510 }
511 }
512 return ret
Jiyong Parkde866cb2018-12-07 23:08:36 +0900513}
514
Jiyong Park9d452992018-10-03 00:38:19 +0900515func InitApexModule(m ApexModule) {
516 base := m.apexModuleBase()
517 base.canHaveApexVariants = true
518
519 m.AddProperties(&base.ApexProperties)
520}
Artur Satayev334b5172020-04-27 17:08:37 +0100521
522// A dependency info for a single ApexModule, either direct or transitive.
523type ApexModuleDepInfo struct {
524 // Name of the dependency
525 To string
526 // List of dependencies To belongs to. Includes APEX itself, if a direct dependency.
527 From []string
528 // Whether the dependency belongs to the final compiled APEX.
529 IsExternal bool
Artur Satayev388d39b2020-04-27 18:53:18 +0100530 // min_sdk_version of the ApexModule
531 MinSdkVersion string
Artur Satayev334b5172020-04-27 17:08:37 +0100532}
533
534// A map of a dependency name to its ApexModuleDepInfo
535type DepNameToDepInfoMap map[string]ApexModuleDepInfo
536
537type ApexBundleDepsInfo struct {
Jooyung Han93a06c12020-05-14 07:44:03 +0900538 flatListPath OutputPath
539 fullListPath OutputPath
Artur Satayev334b5172020-04-27 17:08:37 +0100540}
541
Artur Satayev2b4b7bb2020-04-28 14:57:42 +0100542type ApexBundleDepsInfoIntf interface {
543 Updatable() bool
Artur Satayev5e7c32d2020-04-27 18:07:06 +0100544 FlatListPath() Path
Artur Satayev334b5172020-04-27 17:08:37 +0100545 FullListPath() Path
546}
547
Artur Satayev5e7c32d2020-04-27 18:07:06 +0100548func (d *ApexBundleDepsInfo) FlatListPath() Path {
549 return d.flatListPath
550}
551
Artur Satayev334b5172020-04-27 17:08:37 +0100552func (d *ApexBundleDepsInfo) FullListPath() Path {
553 return d.fullListPath
554}
555
Artur Satayev5e7c32d2020-04-27 18:07:06 +0100556// Generate two module out files:
557// 1. FullList with transitive deps and their parents in the dep graph
558// 2. FlatList with a flat list of transitive deps
Artur Satayev388d39b2020-04-27 18:53:18 +0100559func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) {
Artur Satayev5e7c32d2020-04-27 18:07:06 +0100560 var fullContent strings.Builder
561 var flatContent strings.Builder
562
Artur Satayev388d39b2020-04-27 18:53:18 +0100563 fmt.Fprintf(&flatContent, "%s(minSdkVersion:%s):\\n", ctx.ModuleName(), minSdkVersion)
Artur Satayev334b5172020-04-27 17:08:37 +0100564 for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) {
565 info := depInfos[key]
Artur Satayev388d39b2020-04-27 18:53:18 +0100566 toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion)
Artur Satayev334b5172020-04-27 17:08:37 +0100567 if info.IsExternal {
568 toName = toName + " (external)"
569 }
Artur Satayev5e7c32d2020-04-27 18:07:06 +0100570 fmt.Fprintf(&fullContent, "%s <- %s\\n", toName, strings.Join(SortedUniqueStrings(info.From), ", "))
571 fmt.Fprintf(&flatContent, " %s\\n", toName)
Artur Satayev334b5172020-04-27 17:08:37 +0100572 }
573
574 d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath
575 ctx.Build(pctx, BuildParams{
576 Rule: WriteFile,
577 Description: "Full Dependency Info",
578 Output: d.fullListPath,
579 Args: map[string]string{
Artur Satayev5e7c32d2020-04-27 18:07:06 +0100580 "content": fullContent.String(),
581 },
582 })
583
584 d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath
585 ctx.Build(pctx, BuildParams{
586 Rule: WriteFile,
587 Description: "Flat Dependency Info",
588 Output: d.flatListPath,
589 Args: map[string]string{
590 "content": flatContent.String(),
Artur Satayev334b5172020-04-27 17:08:37 +0100591 },
592 })
593}