1// 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 17import ( 18 "fmt" 19 "sort" 20 "strconv" 21 "strings" 22 "sync" 23 24 "github.com/google/blueprint" 25) 26 27var ( 28 // This is the sdk version when APEX was first introduced 29 SdkVersion_Android10 = uncheckedFinalApiLevel(29) 30) 31 32// ApexInfo describes the metadata about one or more apexBundles that an apex variant of a module is 33// part of. When an apex variant is created, the variant is associated with one apexBundle. But 34// when multiple apex variants are merged for deduping (see mergeApexVariations), this holds the 35// information about the apexBundles that are merged together. 36// Accessible via `ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)` 37type ApexInfo struct { 38 // Name of the apex variation that this module (i.e. the apex variant of the module) is 39 // mutated into, or "" for a platform (i.e. non-APEX) variant. Note that this name and the 40 // Soong module name of the APEX can be different. That happens when there is 41 // `override_apex` that overrides `apex`. In that case, both Soong modules have the same 42 // apex variation name which usually is `com.android.foo`. This name is also the `name` 43 // in the path `/apex/<name>` where this apex is activated on at runtime. 44 // 45 // Also note that a module can be included in multiple APEXes, in which case, the module is 46 // mutated into one or more variants, each of which is for an APEX. The variants then can 47 // later be deduped if they don't need to be compiled differently. This is an optimization 48 // done in mergeApexVariations. 49 ApexVariationName string 50 51 // ApiLevel that this module has to support at minimum. 52 MinSdkVersion ApiLevel 53 54 // True if this module comes from an updatable apexBundle. 55 Updatable bool 56 57 // True if this module can use private platform APIs. Only non-updatable APEX can set this 58 // to true. 59 UsePlatformApis bool 60 61 // List of Apex variant names that this module is associated with. This initially is the 62 // same as the `ApexVariationName` field. Then when multiple apex variants are merged in 63 // mergeApexVariations, ApexInfo struct of the merged variant holds the list of apexBundles 64 // that are merged together. 65 InApexVariants []string 66 67 // List of APEX Soong module names that this module is part of. Note that the list includes 68 // different variations of the same APEX. For example, if module `foo` is included in the 69 // apex `com.android.foo`, and also if there is an override_apex module 70 // `com.mycompany.android.foo` overriding `com.android.foo`, then this list contains both 71 // `com.android.foo` and `com.mycompany.android.foo`. If the APEX Soong module is a 72 // prebuilt, the name here doesn't have the `prebuilt_` prefix. 73 InApexModules []string 74 75 // Pointers to the ApexContents struct each of which is for apexBundle modules that this 76 // module is part of. The ApexContents gives information about which modules the apexBundle 77 // has and whether a module became part of the apexBundle via a direct dependency or not. 78 ApexContents []*ApexContents 79 80 // True if this is for a prebuilt_apex. 81 // 82 // If true then this will customize the apex processing to make it suitable for handling 83 // prebuilt_apex, e.g. it will prevent ApexInfos from being merged together. 84 // 85 // See Prebuilt.ApexInfoMutator for more information. 86 ForPrebuiltApex bool 87} 88 89var ApexInfoProvider = blueprint.NewMutatorProvider(ApexInfo{}, "apex") 90 91func (i ApexInfo) AddJSONData(d *map[string]interface{}) { 92 (*d)["Apex"] = map[string]interface{}{ 93 "ApexVariationName": i.ApexVariationName, 94 "MinSdkVersion": i.MinSdkVersion, 95 "InApexModules": i.InApexModules, 96 "InApexVariants": i.InApexVariants, 97 "ForPrebuiltApex": i.ForPrebuiltApex, 98 } 99} 100 101// mergedName gives the name of the alias variation that will be used when multiple apex variations 102// of a module can be deduped into one variation. For example, if libfoo is included in both apex.a 103// and apex.b, and if the two APEXes have the same min_sdk_version (say 29), then libfoo doesn't 104// have to be built twice, but only once. In that case, the two apex variations apex.a and apex.b 105// are configured to have the same alias variation named apex29. Whether platform APIs is allowed 106// or not also matters; if two APEXes don't have the same allowance, they get different names and 107// thus wouldn't be merged. 108func (i ApexInfo) mergedName(ctx PathContext) string { 109 name := "apex" + strconv.Itoa(i.MinSdkVersion.FinalOrFutureInt()) 110 return name 111} 112 113// IsForPlatform tells whether this module is for the platform or not. If false is returned, it 114// means that this apex variant of the module is built for an APEX. 115func (i ApexInfo) IsForPlatform() bool { 116 return i.ApexVariationName == "" 117} 118 119// InApexVariant tells whether this apex variant of the module is part of the given apexVariant or 120// not. 121func (i ApexInfo) InApexVariant(apexVariant string) bool { 122 for _, a := range i.InApexVariants { 123 if a == apexVariant { 124 return true 125 } 126 } 127 return false 128} 129 130func (i ApexInfo) InApexModule(apexModuleName string) bool { 131 for _, a := range i.InApexModules { 132 if a == apexModuleName { 133 return true 134 } 135 } 136 return false 137} 138 139// ApexTestForInfo stores the contents of APEXes for which this module is a test - although this 140// module is not part of the APEX - and thus has access to APEX internals. 141type ApexTestForInfo struct { 142 ApexContents []*ApexContents 143} 144 145var ApexTestForInfoProvider = blueprint.NewMutatorProvider(ApexTestForInfo{}, "apex_test_for") 146 147// DepIsInSameApex defines an interface that should be used to determine whether a given dependency 148// should be considered as part of the same APEX as the current module or not. Note: this was 149// extracted from ApexModule to make it easier to define custom subsets of the ApexModule interface 150// and improve code navigation within the IDE. 151type DepIsInSameApex interface { 152 // DepIsInSameApex tests if the other module 'dep' is considered as part of the same APEX as 153 // this module. For example, a static lib dependency usually returns true here, while a 154 // shared lib dependency to a stub library returns false. 155 // 156 // This method must not be called directly without first ignoring dependencies whose tags 157 // implement ExcludeFromApexContentsTag. Calls from within the func passed to WalkPayloadDeps() 158 // are fine as WalkPayloadDeps() will ignore those dependencies automatically. Otherwise, use 159 // IsDepInSameApex instead. 160 DepIsInSameApex(ctx BaseModuleContext, dep Module) bool 161} 162 163func IsDepInSameApex(ctx BaseModuleContext, module, dep Module) bool { 164 depTag := ctx.OtherModuleDependencyTag(dep) 165 if _, ok := depTag.(ExcludeFromApexContentsTag); ok { 166 // The tag defines a dependency that never requires the child module to be part of the same 167 // apex as the parent. 168 return false 169 } 170 return module.(DepIsInSameApex).DepIsInSameApex(ctx, dep) 171} 172 173// ApexModule is the interface that a module type is expected to implement if the module has to be 174// built differently depending on whether the module is destined for an APEX or not (i.e., installed 175// to one of the regular partitions). 176// 177// Native shared libraries are one such module type; when it is built for an APEX, it should depend 178// only on stable interfaces such as NDK, stable AIDL, or C APIs from other APEXes. 179// 180// A module implementing this interface will be mutated into multiple variations by apex.apexMutator 181// if it is directly or indirectly included in one or more APEXes. Specifically, if a module is 182// included in apex.foo and apex.bar then three apex variants are created: platform, apex.foo and 183// apex.bar. The platform variant is for the regular partitions (e.g., /system or /vendor, etc.) 184// while the other two are for the APEXs, respectively. The latter two variations can be merged (see 185// mergedName) when the two APEXes have the same min_sdk_version requirement. 186type ApexModule interface { 187 Module 188 DepIsInSameApex 189 190 apexModuleBase() *ApexModuleBase 191 192 // Marks that this module should be built for the specified APEX. Call this BEFORE 193 // apex.apexMutator is run. 194 BuildForApex(apex ApexInfo) 195 196 // Returns true if this module is present in any APEX either directly or indirectly. Call 197 // this after apex.apexMutator is run. 198 InAnyApex() bool 199 200 // Returns true if this module is directly in any APEX. Call this AFTER apex.apexMutator is 201 // run. 202 DirectlyInAnyApex() bool 203 204 // NotInPlatform tells whether or not this module is included in an APEX and therefore 205 // shouldn't be exposed to the platform (i.e. outside of the APEX) directly. A module is 206 // considered to be included in an APEX either when there actually is an APEX that 207 // explicitly has the module as its dependency or the module is not available to the 208 // platform, which indicates that the module belongs to at least one or more other APEXes. 209 NotInPlatform() bool 210 211 // Tests if this module could have APEX variants. Even when a module type implements 212 // ApexModule interface, APEX variants are created only for the module instances that return 213 // true here. This is useful for not creating APEX variants for certain types of shared 214 // libraries such as NDK stubs. 215 CanHaveApexVariants() bool 216 217 // Tests if this module can be installed to APEX as a file. For example, this would return 218 // true for shared libs while return false for static libs because static libs are not 219 // installable module (but it can still be mutated for APEX) 220 IsInstallableToApex() bool 221 222 // Tests if this module is available for the specified APEX or ":platform". This is from the 223 // apex_available property of the module. 224 AvailableFor(what string) bool 225 226 // AlwaysRequiresPlatformApexVariant allows the implementing module to determine whether an 227 // APEX mutator should always be created for it. 228 // 229 // Returns false by default. 230 AlwaysRequiresPlatformApexVariant() bool 231 232 // Returns true if this module is not available to platform (i.e. apex_available property 233 // doesn't have "//apex_available:platform"), or shouldn't be available to platform, which 234 // is the case when this module depends on other module that isn't available to platform. 235 NotAvailableForPlatform() bool 236 237 // Marks that this module is not available to platform. Set by the 238 // check-platform-availability mutator in the apex package. 239 SetNotAvailableForPlatform() 240 241 // Returns the list of APEXes that this module is a test for. The module has access to the 242 // private part of the listed APEXes even when it is not included in the APEXes. This by 243 // default returns nil. A module type should override the default implementation. For 244 // example, cc_test module type returns the value of test_for here. 245 TestFor() []string 246 247 // Returns nil (success) if this module should support the given sdk version. Returns an 248 // error if not. No default implementation is provided for this method. A module type 249 // implementing this interface should provide an implementation. A module supports an sdk 250 // version when the module's min_sdk_version is equal to or less than the given sdk version. 251 ShouldSupportSdkVersion(ctx BaseModuleContext, sdkVersion ApiLevel) error 252 253 // Returns true if this module needs a unique variation per apex, effectively disabling the 254 // deduping. This is turned on when, for example if use_apex_name_macro is set so that each 255 // apex variant should be built with different macro definitions. 256 UniqueApexVariations() bool 257} 258 259// Properties that are common to all module types implementing ApexModule interface. 260type ApexProperties struct { 261 // Availability of this module in APEXes. Only the listed APEXes can contain this module. If 262 // the module has stubs then other APEXes and the platform may access it through them 263 // (subject to visibility). 264 // 265 // "//apex_available:anyapex" is a pseudo APEX name that matches to any APEX. 266 // "//apex_available:platform" refers to non-APEX partitions like "system.img". 267 // "com.android.gki.*" matches any APEX module name with the prefix "com.android.gki.". 268 // Default is ["//apex_available:platform"]. 269 Apex_available []string 270 271 // See ApexModule.InAnyApex() 272 InAnyApex bool `blueprint:"mutated"` 273 274 // See ApexModule.DirectlyInAnyApex() 275 DirectlyInAnyApex bool `blueprint:"mutated"` 276 277 // AnyVariantDirectlyInAnyApex is true in the primary variant of a module if _any_ variant 278 // of the module is directly in any apex. This includes host, arch, asan, etc. variants. It 279 // is unused in any variant that is not the primary variant. Ideally this wouldn't be used, 280 // as it incorrectly mixes arch variants if only one arch is in an apex, but a few places 281 // depend on it, for example when an ASAN variant is created before the apexMutator. Call 282 // this after apex.apexMutator is run. 283 AnyVariantDirectlyInAnyApex bool `blueprint:"mutated"` 284 285 // See ApexModule.NotAvailableForPlatform() 286 NotAvailableForPlatform bool `blueprint:"mutated"` 287 288 // See ApexModule.UniqueApexVariants() 289 UniqueApexVariationsForDeps bool `blueprint:"mutated"` 290} 291 292// Marker interface that identifies dependencies that are excluded from APEX contents. 293// 294// Unless the tag also implements the AlwaysRequireApexVariantTag this will prevent an apex variant 295// from being created for the module. 296// 297// At the moment the sdk.sdkRequirementsMutator relies on the fact that the existing tags which 298// implement this interface do not define dependencies onto members of an sdk_snapshot. If that 299// changes then sdk.sdkRequirementsMutator will need fixing. 300type ExcludeFromApexContentsTag interface { 301 blueprint.DependencyTag 302 303 // Method that differentiates this interface from others. 304 ExcludeFromApexContents() 305} 306 307// Marker interface that identifies dependencies that always requires an APEX variant to be created. 308// 309// It is possible for a dependency to require an apex variant but exclude the module from the APEX 310// contents. See sdk.sdkMemberDependencyTag. 311type AlwaysRequireApexVariantTag interface { 312 blueprint.DependencyTag 313 314 // Return true if this tag requires that the target dependency has an apex variant. 315 AlwaysRequireApexVariant() bool 316} 317 318// Marker interface that identifies dependencies that should inherit the DirectlyInAnyApex state 319// from the parent to the child. For example, stubs libraries are marked as DirectlyInAnyApex if 320// their implementation is in an apex. 321type CopyDirectlyInAnyApexTag interface { 322 blueprint.DependencyTag 323 324 // Method that differentiates this interface from others. 325 CopyDirectlyInAnyApex() 326} 327 328// Interface that identifies dependencies to skip Apex dependency check 329type SkipApexAllowedDependenciesCheck interface { 330 // Returns true to skip the Apex dependency check, which limits the allowed dependency in build. 331 SkipApexAllowedDependenciesCheck() bool 332} 333 334// ApexModuleBase provides the default implementation for the ApexModule interface. APEX-aware 335// modules are expected to include this struct and call InitApexModule(). 336type ApexModuleBase struct { 337 ApexProperties ApexProperties 338 339 canHaveApexVariants bool 340 341 apexInfos []ApexInfo 342 apexInfosLock sync.Mutex // protects apexInfos during parallel apexInfoMutator 343} 344 345// Initializes ApexModuleBase struct. Not calling this (even when inheriting from ApexModuleBase) 346// prevents the module from being mutated for apexBundle. 347func InitApexModule(m ApexModule) { 348 base := m.apexModuleBase() 349 base.canHaveApexVariants = true 350 351 m.AddProperties(&base.ApexProperties) 352} 353 354// Implements ApexModule 355func (m *ApexModuleBase) apexModuleBase() *ApexModuleBase { 356 return m 357} 358 359var ( 360 availableToPlatformList = []string{AvailableToPlatform} 361) 362 363// Implements ApexModule 364func (m *ApexModuleBase) ApexAvailable() []string { 365 aa := m.ApexProperties.Apex_available 366 if len(aa) > 0 { 367 return aa 368 } 369 // Default is availability to platform 370 return CopyOf(availableToPlatformList) 371} 372 373// Implements ApexModule 374func (m *ApexModuleBase) BuildForApex(apex ApexInfo) { 375 m.apexInfosLock.Lock() 376 defer m.apexInfosLock.Unlock() 377 for i, v := range m.apexInfos { 378 if v.ApexVariationName == apex.ApexVariationName { 379 if len(apex.InApexModules) != 1 { 380 panic(fmt.Errorf("Newly created apexInfo must be for a single APEX")) 381 } 382 // Even when the ApexVariantNames are the same, the given ApexInfo might 383 // actually be for different APEX. This can happen when an APEX is 384 // overridden via override_apex. For example, there can be two apexes 385 // `com.android.foo` (from the `apex` module type) and 386 // `com.mycompany.android.foo` (from the `override_apex` module type), both 387 // of which has the same ApexVariantName `com.android.foo`. Add the apex 388 // name to the list so that it's not lost. 389 if !InList(apex.InApexModules[0], v.InApexModules) { 390 m.apexInfos[i].InApexModules = append(m.apexInfos[i].InApexModules, apex.InApexModules[0]) 391 } 392 return 393 } 394 } 395 m.apexInfos = append(m.apexInfos, apex) 396} 397 398// Implements ApexModule 399func (m *ApexModuleBase) InAnyApex() bool { 400 return m.ApexProperties.InAnyApex 401} 402 403// Implements ApexModule 404func (m *ApexModuleBase) DirectlyInAnyApex() bool { 405 return m.ApexProperties.DirectlyInAnyApex 406} 407 408// Implements ApexModule 409func (m *ApexModuleBase) NotInPlatform() bool { 410 return m.ApexProperties.AnyVariantDirectlyInAnyApex || !m.AvailableFor(AvailableToPlatform) 411} 412 413// Implements ApexModule 414func (m *ApexModuleBase) CanHaveApexVariants() bool { 415 return m.canHaveApexVariants 416} 417 418// Implements ApexModule 419func (m *ApexModuleBase) IsInstallableToApex() bool { 420 // If needed, this will bel overridden by concrete types inheriting 421 // ApexModuleBase 422 return false 423} 424 425// Implements ApexModule 426func (m *ApexModuleBase) TestFor() []string { 427 // If needed, this will be overridden by concrete types inheriting 428 // ApexModuleBase 429 return nil 430} 431 432// Implements ApexModule 433func (m *ApexModuleBase) UniqueApexVariations() bool { 434 // If needed, this will bel overridden by concrete types inheriting 435 // ApexModuleBase 436 return false 437} 438 439// Implements ApexModule 440func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool { 441 // By default, if there is a dependency from A to B, we try to include both in the same 442 // APEX, unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning 443 // true. This is overridden by some module types like apex.ApexBundle, cc.Module, 444 // java.Module, etc. 445 return true 446} 447 448const ( 449 AvailableToPlatform = "//apex_available:platform" 450 AvailableToAnyApex = "//apex_available:anyapex" 451 AvailableToGkiApex = "com.android.gki.*" 452) 453 454// CheckAvailableForApex provides the default algorithm for checking the apex availability. When the 455// availability is empty, it defaults to ["//apex_available:platform"] which means "available to the 456// platform but not available to any APEX". When the list is not empty, `what` is matched against 457// the list. If there is any matching element in the list, thus function returns true. The special 458// availability "//apex_available:anyapex" matches with anything except for 459// "//apex_available:platform". 460func CheckAvailableForApex(what string, apex_available []string) bool { 461 if len(apex_available) == 0 { 462 return what == AvailableToPlatform 463 } 464 return InList(what, apex_available) || 465 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) || 466 (strings.HasPrefix(what, "com.android.gki.") && InList(AvailableToGkiApex, apex_available)) || 467 (what == "com.google.mainline.primary.libs") || // TODO b/248601389 468 (what == "com.google.mainline.go.primary.libs") // TODO b/248601389 469} 470 471// Implements ApexModule 472func (m *ApexModuleBase) AvailableFor(what string) bool { 473 return CheckAvailableForApex(what, m.ApexProperties.Apex_available) 474} 475 476// Implements ApexModule 477func (m *ApexModuleBase) AlwaysRequiresPlatformApexVariant() bool { 478 return false 479} 480 481// Implements ApexModule 482func (m *ApexModuleBase) NotAvailableForPlatform() bool { 483 return m.ApexProperties.NotAvailableForPlatform 484} 485 486// Implements ApexModule 487func (m *ApexModuleBase) SetNotAvailableForPlatform() { 488 m.ApexProperties.NotAvailableForPlatform = true 489} 490 491// This function makes sure that the apex_available property is valid 492func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) { 493 for _, n := range m.ApexProperties.Apex_available { 494 if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex { 495 continue 496 } 497 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() { 498 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n) 499 } 500 } 501} 502 503// AvailableToSameApexes returns true if the two modules are apex_available to 504// exactly the same set of APEXes (and platform), i.e. if their apex_available 505// properties have the same elements. 506func AvailableToSameApexes(mod1, mod2 ApexModule) bool { 507 mod1ApexAvail := SortedUniqueStrings(mod1.apexModuleBase().ApexProperties.Apex_available) 508 mod2ApexAvail := SortedUniqueStrings(mod2.apexModuleBase().ApexProperties.Apex_available) 509 if len(mod1ApexAvail) != len(mod2ApexAvail) { 510 return false 511 } 512 for i, v := range mod1ApexAvail { 513 if v != mod2ApexAvail[i] { 514 return false 515 } 516 } 517 return true 518} 519 520type byApexName []ApexInfo 521 522func (a byApexName) Len() int { return len(a) } 523func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 524func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName } 525 526// mergeApexVariations deduplicates apex variations that would build identically into a common 527// variation. It returns the reduced list of variations and a list of aliases from the original 528// variation names to the new variation names. 529func mergeApexVariations(ctx PathContext, apexInfos []ApexInfo) (merged []ApexInfo, aliases [][2]string) { 530 sort.Sort(byApexName(apexInfos)) 531 seen := make(map[string]int) 532 for _, apexInfo := range apexInfos { 533 // If this is for a prebuilt apex then use the actual name of the apex variation to prevent this 534 // from being merged with other ApexInfo. See Prebuilt.ApexInfoMutator for more information. 535 if apexInfo.ForPrebuiltApex { 536 merged = append(merged, apexInfo) 537 continue 538 } 539 540 // Merge the ApexInfo together. If a compatible ApexInfo exists then merge the information from 541 // this one into it, otherwise create a new merged ApexInfo from this one and save it away so 542 // other ApexInfo instances can be merged into it. 543 variantName := apexInfo.ApexVariationName 544 mergedName := apexInfo.mergedName(ctx) 545 if index, exists := seen[mergedName]; exists { 546 // Variants having the same mergedName are deduped 547 merged[index].InApexVariants = append(merged[index].InApexVariants, variantName) 548 merged[index].InApexModules = append(merged[index].InApexModules, apexInfo.InApexModules...) 549 merged[index].ApexContents = append(merged[index].ApexContents, apexInfo.ApexContents...) 550 merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable 551 // Platform APIs is allowed for this module only when all APEXes containing 552 // the module are with `use_platform_apis: true`. 553 merged[index].UsePlatformApis = merged[index].UsePlatformApis && apexInfo.UsePlatformApis 554 } else { 555 seen[mergedName] = len(merged) 556 apexInfo.ApexVariationName = mergedName 557 apexInfo.InApexVariants = CopyOf(apexInfo.InApexVariants) 558 apexInfo.InApexModules = CopyOf(apexInfo.InApexModules) 559 apexInfo.ApexContents = append([]*ApexContents(nil), apexInfo.ApexContents...) 560 merged = append(merged, apexInfo) 561 } 562 aliases = append(aliases, [2]string{variantName, mergedName}) 563 } 564 return merged, aliases 565} 566 567// CreateApexVariations mutates a given module into multiple apex variants each of which is for an 568// apexBundle (and/or the platform) where the module is part of. 569func CreateApexVariations(mctx BottomUpMutatorContext, module ApexModule) []Module { 570 base := module.apexModuleBase() 571 572 // Shortcut 573 if len(base.apexInfos) == 0 { 574 return nil 575 } 576 577 // Do some validity checks. 578 // TODO(jiyong): is this the right place? 579 base.checkApexAvailableProperty(mctx) 580 581 var apexInfos []ApexInfo 582 var aliases [][2]string 583 if !mctx.Module().(ApexModule).UniqueApexVariations() && !base.ApexProperties.UniqueApexVariationsForDeps { 584 apexInfos, aliases = mergeApexVariations(mctx, base.apexInfos) 585 } else { 586 apexInfos = base.apexInfos 587 } 588 // base.apexInfos is only needed to propagate the list of apexes from apexInfoMutator to 589 // apexMutator. It is no longer accurate after mergeApexVariations, and won't be copied to 590 // all but the first created variant. Clear it so it doesn't accidentally get used later. 591 base.apexInfos = nil 592 sort.Sort(byApexName(apexInfos)) 593 594 var inApex ApexMembership 595 for _, a := range apexInfos { 596 for _, apexContents := range a.ApexContents { 597 inApex = inApex.merge(apexContents.contents[mctx.ModuleName()]) 598 } 599 } 600 base.ApexProperties.InAnyApex = true 601 base.ApexProperties.DirectlyInAnyApex = inApex == directlyInApex 602 603 defaultVariation := "" 604 mctx.SetDefaultDependencyVariation(&defaultVariation) 605 606 variations := []string{defaultVariation} 607 for _, a := range apexInfos { 608 variations = append(variations, a.ApexVariationName) 609 } 610 modules := mctx.CreateVariations(variations...) 611 for i, mod := range modules { 612 platformVariation := i == 0 613 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) { 614 // Do not install the module for platform, but still allow it to output 615 // uninstallable AndroidMk entries in certain cases when they have side 616 // effects. TODO(jiyong): move this routine to somewhere else 617 mod.MakeUninstallable() 618 } 619 if !platformVariation { 620 mctx.SetVariationProvider(mod, ApexInfoProvider, apexInfos[i-1]) 621 } 622 } 623 624 for _, alias := range aliases { 625 mctx.CreateAliasVariation(alias[0], alias[1]) 626 } 627 628 return modules 629} 630 631// UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies that are 632// in the same APEX have unique APEX variations so that the module can link against the right 633// variant. 634func UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext, am ApexModule) { 635 // anyInSameApex returns true if the two ApexInfo lists contain any values in an 636 // InApexVariants list in common. It is used instead of DepIsInSameApex because it needs to 637 // determine if the dep is in the same APEX due to being directly included, not only if it 638 // is included _because_ it is a dependency. 639 anyInSameApex := func(a, b []ApexInfo) bool { 640 collectApexes := func(infos []ApexInfo) []string { 641 var ret []string 642 for _, info := range infos { 643 ret = append(ret, info.InApexVariants...) 644 } 645 return ret 646 } 647 648 aApexes := collectApexes(a) 649 bApexes := collectApexes(b) 650 sort.Strings(bApexes) 651 for _, aApex := range aApexes { 652 index := sort.SearchStrings(bApexes, aApex) 653 if index < len(bApexes) && bApexes[index] == aApex { 654 return true 655 } 656 } 657 return false 658 } 659 660 // If any of the dependencies requires unique apex variations, so does this module. 661 mctx.VisitDirectDeps(func(dep Module) { 662 if depApexModule, ok := dep.(ApexModule); ok { 663 if anyInSameApex(depApexModule.apexModuleBase().apexInfos, am.apexModuleBase().apexInfos) && 664 (depApexModule.UniqueApexVariations() || 665 depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) { 666 am.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps = true 667 } 668 } 669 }) 670} 671 672// UpdateDirectlyInAnyApex uses the final module to store if any variant of this module is directly 673// in any APEX, and then copies the final value to all the modules. It also copies the 674// DirectlyInAnyApex value to any direct dependencies with a CopyDirectlyInAnyApexTag dependency 675// tag. 676func UpdateDirectlyInAnyApex(mctx BottomUpMutatorContext, am ApexModule) { 677 base := am.apexModuleBase() 678 // Copy DirectlyInAnyApex and InAnyApex from any direct dependencies with a 679 // CopyDirectlyInAnyApexTag dependency tag. 680 mctx.VisitDirectDeps(func(dep Module) { 681 if _, ok := mctx.OtherModuleDependencyTag(dep).(CopyDirectlyInAnyApexTag); ok { 682 depBase := dep.(ApexModule).apexModuleBase() 683 depBase.ApexProperties.DirectlyInAnyApex = base.ApexProperties.DirectlyInAnyApex 684 depBase.ApexProperties.InAnyApex = base.ApexProperties.InAnyApex 685 } 686 }) 687 688 if base.ApexProperties.DirectlyInAnyApex { 689 // Variants of a module are always visited sequentially in order, so it is safe to 690 // write to another variant of this module. For a BottomUpMutator the 691 // PrimaryModule() is visited first and FinalModule() is visited last. 692 mctx.FinalModule().(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex = true 693 } 694 695 // If this is the FinalModule (last visited module) copy 696 // AnyVariantDirectlyInAnyApex to all the other variants 697 if am == mctx.FinalModule().(ApexModule) { 698 mctx.VisitAllModuleVariants(func(variant Module) { 699 variant.(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex = 700 base.ApexProperties.AnyVariantDirectlyInAnyApex 701 }) 702 } 703} 704 705// ApexMembership tells how a module became part of an APEX. 706type ApexMembership int 707 708const ( 709 notInApex ApexMembership = 0 710 indirectlyInApex = iota 711 directlyInApex 712) 713 714// ApexContents gives an information about member modules of an apexBundle. Each apexBundle has an 715// apexContents, and modules in that apex have a provider containing the apexContents of each 716// apexBundle they are part of. 717type ApexContents struct { 718 // map from a module name to its membership in this apexBundle 719 contents map[string]ApexMembership 720} 721 722// NewApexContents creates and initializes an ApexContents that is suitable 723// for use with an apex module. 724// - contents is a map from a module name to information about its membership within 725// the apex. 726func NewApexContents(contents map[string]ApexMembership) *ApexContents { 727 return &ApexContents{ 728 contents: contents, 729 } 730} 731 732// Updates an existing membership by adding a new direct (or indirect) membership 733func (i ApexMembership) Add(direct bool) ApexMembership { 734 if direct || i == directlyInApex { 735 return directlyInApex 736 } 737 return indirectlyInApex 738} 739 740// Merges two membership into one. Merging is needed because a module can be a part of an apexBundle 741// in many different paths. For example, it could be dependend on by the apexBundle directly, but at 742// the same time, there might be an indirect dependency to the module. In that case, the more 743// specific dependency (the direct one) is chosen. 744func (i ApexMembership) merge(other ApexMembership) ApexMembership { 745 if other == directlyInApex || i == directlyInApex { 746 return directlyInApex 747 } 748 749 if other == indirectlyInApex || i == indirectlyInApex { 750 return indirectlyInApex 751 } 752 return notInApex 753} 754 755// Tests whether a module named moduleName is directly included in the apexBundle where this 756// ApexContents is tagged. 757func (ac *ApexContents) DirectlyInApex(moduleName string) bool { 758 return ac.contents[moduleName] == directlyInApex 759} 760 761// Tests whether a module named moduleName is included in the apexBundle where this ApexContent is 762// tagged. 763func (ac *ApexContents) InApex(moduleName string) bool { 764 return ac.contents[moduleName] != notInApex 765} 766 767// Tests whether a module named moduleName is directly depended on by all APEXes in an ApexInfo. 768func DirectlyInAllApexes(apexInfo ApexInfo, moduleName string) bool { 769 for _, contents := range apexInfo.ApexContents { 770 if !contents.DirectlyInApex(moduleName) { 771 return false 772 } 773 } 774 return true 775} 776 777//////////////////////////////////////////////////////////////////////////////////////////////////// 778//Below are routines for extra safety checks. 779// 780// BuildDepsInfoLists is to flatten the dependency graph for an apexBundle into a text file 781// (actually two in slightly different formats). The files are mostly for debugging, for example to 782// see why a certain module is included in an APEX via which dependency path. 783// 784// CheckMinSdkVersion is to make sure that all modules in an apexBundle satisfy the min_sdk_version 785// requirement of the apexBundle. 786 787// A dependency info for a single ApexModule, either direct or transitive. 788type ApexModuleDepInfo struct { 789 // Name of the dependency 790 To string 791 // List of dependencies To belongs to. Includes APEX itself, if a direct dependency. 792 From []string 793 // Whether the dependency belongs to the final compiled APEX. 794 IsExternal bool 795 // min_sdk_version of the ApexModule 796 MinSdkVersion string 797} 798 799// A map of a dependency name to its ApexModuleDepInfo 800type DepNameToDepInfoMap map[string]ApexModuleDepInfo 801 802type ApexBundleDepsInfo struct { 803 flatListPath OutputPath 804 fullListPath OutputPath 805} 806 807type ApexBundleDepsInfoIntf interface { 808 Updatable() bool 809 FlatListPath() Path 810 FullListPath() Path 811} 812 813func (d *ApexBundleDepsInfo) FlatListPath() Path { 814 return d.flatListPath 815} 816 817func (d *ApexBundleDepsInfo) FullListPath() Path { 818 return d.fullListPath 819} 820 821// Generate two module out files: 822// 1. FullList with transitive deps and their parents in the dep graph 823// 2. FlatList with a flat list of transitive deps 824// In both cases transitive deps of external deps are not included. Neither are deps that are only 825// available to APEXes; they are developed with updatability in mind and don't need manual approval. 826func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) { 827 var fullContent strings.Builder 828 var flatContent strings.Builder 829 830 fmt.Fprintf(&fullContent, "%s(minSdkVersion:%s):\n", ctx.ModuleName(), minSdkVersion) 831 for _, key := range FirstUniqueStrings(SortedKeys(depInfos)) { 832 info := depInfos[key] 833 toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion) 834 if info.IsExternal { 835 toName = toName + " (external)" 836 } 837 fmt.Fprintf(&fullContent, " %s <- %s\n", toName, strings.Join(SortedUniqueStrings(info.From), ", ")) 838 fmt.Fprintf(&flatContent, "%s\n", toName) 839 } 840 841 d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath 842 WriteFileRule(ctx, d.fullListPath, fullContent.String()) 843 844 d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath 845 WriteFileRule(ctx, d.flatListPath, flatContent.String()) 846 847 ctx.Phony(fmt.Sprintf("%s-depsinfo", ctx.ModuleName()), d.fullListPath, d.flatListPath) 848} 849 850// Function called while walking an APEX's payload dependencies. 851// 852// Return true if the `to` module should be visited, false otherwise. 853type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool 854type WalkPayloadDepsFunc func(ctx ModuleContext, do PayloadDepsCallback) 855 856// ModuleWithMinSdkVersionCheck represents a module that implements min_sdk_version checks 857type ModuleWithMinSdkVersionCheck interface { 858 Module 859 MinSdkVersion(ctx EarlyModuleContext) ApiLevel 860 CheckMinSdkVersion(ctx ModuleContext) 861} 862 863// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version 864// accordingly 865func CheckMinSdkVersion(ctx ModuleContext, minSdkVersion ApiLevel, walk WalkPayloadDepsFunc) { 866 // do not enforce min_sdk_version for host 867 if ctx.Host() { 868 return 869 } 870 871 // do not enforce for coverage build 872 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() { 873 return 874 } 875 876 // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version 877 if minSdkVersion.IsNone() { 878 return 879 } 880 881 walk(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool { 882 if externalDep { 883 // external deps are outside the payload boundary, which is "stable" 884 // interface. We don't have to check min_sdk_version for external 885 // dependencies. 886 return false 887 } 888 if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) { 889 return false 890 } 891 if m, ok := to.(ModuleWithMinSdkVersionCheck); ok { 892 // This dependency performs its own min_sdk_version check, just make sure it sets min_sdk_version 893 // to trigger the check. 894 if !m.MinSdkVersion(ctx).Specified() { 895 ctx.OtherModuleErrorf(m, "must set min_sdk_version") 896 } 897 return false 898 } 899 if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil { 900 toName := ctx.OtherModuleName(to) 901 ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v."+ 902 "\n\nDependency path: %s\n\n"+ 903 "Consider adding 'min_sdk_version: %q' to %q", 904 minSdkVersion, ctx.ModuleName(), err.Error(), 905 ctx.GetPathString(false), 906 minSdkVersion, toName) 907 return false 908 } 909 return true 910 }) 911} 912