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 359// Implements ApexModule 360func (m *ApexModuleBase) ApexAvailable() []string { 361 return m.ApexProperties.Apex_available 362} 363 364// Implements ApexModule 365func (m *ApexModuleBase) BuildForApex(apex ApexInfo) { 366 m.apexInfosLock.Lock() 367 defer m.apexInfosLock.Unlock() 368 for i, v := range m.apexInfos { 369 if v.ApexVariationName == apex.ApexVariationName { 370 if len(apex.InApexModules) != 1 { 371 panic(fmt.Errorf("Newly created apexInfo must be for a single APEX")) 372 } 373 // Even when the ApexVariantNames are the same, the given ApexInfo might 374 // actually be for different APEX. This can happen when an APEX is 375 // overridden via override_apex. For example, there can be two apexes 376 // `com.android.foo` (from the `apex` module type) and 377 // `com.mycompany.android.foo` (from the `override_apex` module type), both 378 // of which has the same ApexVariantName `com.android.foo`. Add the apex 379 // name to the list so that it's not lost. 380 if !InList(apex.InApexModules[0], v.InApexModules) { 381 m.apexInfos[i].InApexModules = append(m.apexInfos[i].InApexModules, apex.InApexModules[0]) 382 } 383 return 384 } 385 } 386 m.apexInfos = append(m.apexInfos, apex) 387} 388 389// Implements ApexModule 390func (m *ApexModuleBase) InAnyApex() bool { 391 return m.ApexProperties.InAnyApex 392} 393 394// Implements ApexModule 395func (m *ApexModuleBase) DirectlyInAnyApex() bool { 396 return m.ApexProperties.DirectlyInAnyApex 397} 398 399// Implements ApexModule 400func (m *ApexModuleBase) NotInPlatform() bool { 401 return m.ApexProperties.AnyVariantDirectlyInAnyApex || !m.AvailableFor(AvailableToPlatform) 402} 403 404// Implements ApexModule 405func (m *ApexModuleBase) CanHaveApexVariants() bool { 406 return m.canHaveApexVariants 407} 408 409// Implements ApexModule 410func (m *ApexModuleBase) IsInstallableToApex() bool { 411 // If needed, this will bel overridden by concrete types inheriting 412 // ApexModuleBase 413 return false 414} 415 416// Implements ApexModule 417func (m *ApexModuleBase) TestFor() []string { 418 // If needed, this will be overridden by concrete types inheriting 419 // ApexModuleBase 420 return nil 421} 422 423// Implements ApexModule 424func (m *ApexModuleBase) UniqueApexVariations() bool { 425 // If needed, this will bel overridden by concrete types inheriting 426 // ApexModuleBase 427 return false 428} 429 430// Implements ApexModule 431func (m *ApexModuleBase) DepIsInSameApex(ctx BaseModuleContext, dep Module) bool { 432 // By default, if there is a dependency from A to B, we try to include both in the same 433 // APEX, unless B is explicitly from outside of the APEX (i.e. a stubs lib). Thus, returning 434 // true. This is overridden by some module types like apex.ApexBundle, cc.Module, 435 // java.Module, etc. 436 return true 437} 438 439const ( 440 AvailableToPlatform = "//apex_available:platform" 441 AvailableToAnyApex = "//apex_available:anyapex" 442 AvailableToGkiApex = "com.android.gki.*" 443) 444 445// CheckAvailableForApex provides the default algorithm for checking the apex availability. When the 446// availability is empty, it defaults to ["//apex_available:platform"] which means "available to the 447// platform but not available to any APEX". When the list is not empty, `what` is matched against 448// the list. If there is any matching element in the list, thus function returns true. The special 449// availability "//apex_available:anyapex" matches with anything except for 450// "//apex_available:platform". 451func CheckAvailableForApex(what string, apex_available []string) bool { 452 if len(apex_available) == 0 { 453 return what == AvailableToPlatform 454 } 455 return InList(what, apex_available) || 456 (what != AvailableToPlatform && InList(AvailableToAnyApex, apex_available)) || 457 (what == "com.android.btservices" && InList("com.android.bluetooth", apex_available)) || 458 (strings.HasPrefix(what, "com.android.gki.") && InList(AvailableToGkiApex, apex_available)) 459} 460 461// Implements ApexModule 462func (m *ApexModuleBase) AvailableFor(what string) bool { 463 return CheckAvailableForApex(what, m.ApexProperties.Apex_available) 464} 465 466// Implements ApexModule 467func (m *ApexModuleBase) AlwaysRequiresPlatformApexVariant() bool { 468 return false 469} 470 471// Implements ApexModule 472func (m *ApexModuleBase) NotAvailableForPlatform() bool { 473 return m.ApexProperties.NotAvailableForPlatform 474} 475 476// Implements ApexModule 477func (m *ApexModuleBase) SetNotAvailableForPlatform() { 478 m.ApexProperties.NotAvailableForPlatform = true 479} 480 481// This function makes sure that the apex_available property is valid 482func (m *ApexModuleBase) checkApexAvailableProperty(mctx BaseModuleContext) { 483 for _, n := range m.ApexProperties.Apex_available { 484 if n == AvailableToPlatform || n == AvailableToAnyApex || n == AvailableToGkiApex { 485 continue 486 } 487 if !mctx.OtherModuleExists(n) && !mctx.Config().AllowMissingDependencies() { 488 mctx.PropertyErrorf("apex_available", "%q is not a valid module name", n) 489 } 490 } 491} 492 493// AvailableToSameApexes returns true if the two modules are apex_available to 494// exactly the same set of APEXes (and platform), i.e. if their apex_available 495// properties have the same elements. 496func AvailableToSameApexes(mod1, mod2 ApexModule) bool { 497 mod1ApexAvail := SortedUniqueStrings(mod1.apexModuleBase().ApexProperties.Apex_available) 498 mod2ApexAvail := SortedUniqueStrings(mod2.apexModuleBase().ApexProperties.Apex_available) 499 if len(mod1ApexAvail) != len(mod2ApexAvail) { 500 return false 501 } 502 for i, v := range mod1ApexAvail { 503 if v != mod2ApexAvail[i] { 504 return false 505 } 506 } 507 return true 508} 509 510type byApexName []ApexInfo 511 512func (a byApexName) Len() int { return len(a) } 513func (a byApexName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 514func (a byApexName) Less(i, j int) bool { return a[i].ApexVariationName < a[j].ApexVariationName } 515 516// mergeApexVariations deduplicates apex variations that would build identically into a common 517// variation. It returns the reduced list of variations and a list of aliases from the original 518// variation names to the new variation names. 519func mergeApexVariations(ctx PathContext, apexInfos []ApexInfo) (merged []ApexInfo, aliases [][2]string) { 520 sort.Sort(byApexName(apexInfos)) 521 seen := make(map[string]int) 522 for _, apexInfo := range apexInfos { 523 // If this is for a prebuilt apex then use the actual name of the apex variation to prevent this 524 // from being merged with other ApexInfo. See Prebuilt.ApexInfoMutator for more information. 525 if apexInfo.ForPrebuiltApex { 526 merged = append(merged, apexInfo) 527 continue 528 } 529 530 // Merge the ApexInfo together. If a compatible ApexInfo exists then merge the information from 531 // this one into it, otherwise create a new merged ApexInfo from this one and save it away so 532 // other ApexInfo instances can be merged into it. 533 variantName := apexInfo.ApexVariationName 534 mergedName := apexInfo.mergedName(ctx) 535 if index, exists := seen[mergedName]; exists { 536 // Variants having the same mergedName are deduped 537 merged[index].InApexVariants = append(merged[index].InApexVariants, variantName) 538 merged[index].InApexModules = append(merged[index].InApexModules, apexInfo.InApexModules...) 539 merged[index].ApexContents = append(merged[index].ApexContents, apexInfo.ApexContents...) 540 merged[index].Updatable = merged[index].Updatable || apexInfo.Updatable 541 // Platform APIs is allowed for this module only when all APEXes containing 542 // the module are with `use_platform_apis: true`. 543 merged[index].UsePlatformApis = merged[index].UsePlatformApis && apexInfo.UsePlatformApis 544 } else { 545 seen[mergedName] = len(merged) 546 apexInfo.ApexVariationName = mergedName 547 apexInfo.InApexVariants = CopyOf(apexInfo.InApexVariants) 548 apexInfo.InApexModules = CopyOf(apexInfo.InApexModules) 549 apexInfo.ApexContents = append([]*ApexContents(nil), apexInfo.ApexContents...) 550 merged = append(merged, apexInfo) 551 } 552 aliases = append(aliases, [2]string{variantName, mergedName}) 553 } 554 return merged, aliases 555} 556 557// CreateApexVariations mutates a given module into multiple apex variants each of which is for an 558// apexBundle (and/or the platform) where the module is part of. 559func CreateApexVariations(mctx BottomUpMutatorContext, module ApexModule) []Module { 560 base := module.apexModuleBase() 561 562 // Shortcut 563 if len(base.apexInfos) == 0 { 564 return nil 565 } 566 567 // Do some validity checks. 568 // TODO(jiyong): is this the right place? 569 base.checkApexAvailableProperty(mctx) 570 571 var apexInfos []ApexInfo 572 var aliases [][2]string 573 if !mctx.Module().(ApexModule).UniqueApexVariations() && !base.ApexProperties.UniqueApexVariationsForDeps { 574 apexInfos, aliases = mergeApexVariations(mctx, base.apexInfos) 575 } else { 576 apexInfos = base.apexInfos 577 } 578 // base.apexInfos is only needed to propagate the list of apexes from apexInfoMutator to 579 // apexMutator. It is no longer accurate after mergeApexVariations, and won't be copied to 580 // all but the first created variant. Clear it so it doesn't accidentally get used later. 581 base.apexInfos = nil 582 sort.Sort(byApexName(apexInfos)) 583 584 var inApex ApexMembership 585 for _, a := range apexInfos { 586 for _, apexContents := range a.ApexContents { 587 inApex = inApex.merge(apexContents.contents[mctx.ModuleName()]) 588 } 589 } 590 base.ApexProperties.InAnyApex = true 591 base.ApexProperties.DirectlyInAnyApex = inApex == directlyInApex 592 593 defaultVariation := "" 594 mctx.SetDefaultDependencyVariation(&defaultVariation) 595 596 variations := []string{defaultVariation} 597 for _, a := range apexInfos { 598 variations = append(variations, a.ApexVariationName) 599 } 600 modules := mctx.CreateVariations(variations...) 601 for i, mod := range modules { 602 platformVariation := i == 0 603 if platformVariation && !mctx.Host() && !mod.(ApexModule).AvailableFor(AvailableToPlatform) { 604 // Do not install the module for platform, but still allow it to output 605 // uninstallable AndroidMk entries in certain cases when they have side 606 // effects. TODO(jiyong): move this routine to somewhere else 607 mod.MakeUninstallable() 608 } 609 if !platformVariation { 610 mctx.SetVariationProvider(mod, ApexInfoProvider, apexInfos[i-1]) 611 } 612 } 613 614 for _, alias := range aliases { 615 mctx.CreateAliasVariation(alias[0], alias[1]) 616 } 617 618 return modules 619} 620 621// UpdateUniqueApexVariationsForDeps sets UniqueApexVariationsForDeps if any dependencies that are 622// in the same APEX have unique APEX variations so that the module can link against the right 623// variant. 624func UpdateUniqueApexVariationsForDeps(mctx BottomUpMutatorContext, am ApexModule) { 625 // anyInSameApex returns true if the two ApexInfo lists contain any values in an 626 // InApexVariants list in common. It is used instead of DepIsInSameApex because it needs to 627 // determine if the dep is in the same APEX due to being directly included, not only if it 628 // is included _because_ it is a dependency. 629 anyInSameApex := func(a, b []ApexInfo) bool { 630 collectApexes := func(infos []ApexInfo) []string { 631 var ret []string 632 for _, info := range infos { 633 ret = append(ret, info.InApexVariants...) 634 } 635 return ret 636 } 637 638 aApexes := collectApexes(a) 639 bApexes := collectApexes(b) 640 sort.Strings(bApexes) 641 for _, aApex := range aApexes { 642 index := sort.SearchStrings(bApexes, aApex) 643 if index < len(bApexes) && bApexes[index] == aApex { 644 return true 645 } 646 } 647 return false 648 } 649 650 // If any of the dependencies requires unique apex variations, so does this module. 651 mctx.VisitDirectDeps(func(dep Module) { 652 if depApexModule, ok := dep.(ApexModule); ok { 653 if anyInSameApex(depApexModule.apexModuleBase().apexInfos, am.apexModuleBase().apexInfos) && 654 (depApexModule.UniqueApexVariations() || 655 depApexModule.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps) { 656 am.apexModuleBase().ApexProperties.UniqueApexVariationsForDeps = true 657 } 658 } 659 }) 660} 661 662// UpdateDirectlyInAnyApex uses the final module to store if any variant of this module is directly 663// in any APEX, and then copies the final value to all the modules. It also copies the 664// DirectlyInAnyApex value to any direct dependencies with a CopyDirectlyInAnyApexTag dependency 665// tag. 666func UpdateDirectlyInAnyApex(mctx BottomUpMutatorContext, am ApexModule) { 667 base := am.apexModuleBase() 668 // Copy DirectlyInAnyApex and InAnyApex from any direct dependencies with a 669 // CopyDirectlyInAnyApexTag dependency tag. 670 mctx.VisitDirectDeps(func(dep Module) { 671 if _, ok := mctx.OtherModuleDependencyTag(dep).(CopyDirectlyInAnyApexTag); ok { 672 depBase := dep.(ApexModule).apexModuleBase() 673 depBase.ApexProperties.DirectlyInAnyApex = base.ApexProperties.DirectlyInAnyApex 674 depBase.ApexProperties.InAnyApex = base.ApexProperties.InAnyApex 675 } 676 }) 677 678 if base.ApexProperties.DirectlyInAnyApex { 679 // Variants of a module are always visited sequentially in order, so it is safe to 680 // write to another variant of this module. For a BottomUpMutator the 681 // PrimaryModule() is visited first and FinalModule() is visited last. 682 mctx.FinalModule().(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex = true 683 } 684 685 // If this is the FinalModule (last visited module) copy 686 // AnyVariantDirectlyInAnyApex to all the other variants 687 if am == mctx.FinalModule().(ApexModule) { 688 mctx.VisitAllModuleVariants(func(variant Module) { 689 variant.(ApexModule).apexModuleBase().ApexProperties.AnyVariantDirectlyInAnyApex = 690 base.ApexProperties.AnyVariantDirectlyInAnyApex 691 }) 692 } 693} 694 695// ApexMembership tells how a module became part of an APEX. 696type ApexMembership int 697 698const ( 699 notInApex ApexMembership = 0 700 indirectlyInApex = iota 701 directlyInApex 702) 703 704// ApexContents gives an information about member modules of an apexBundle. Each apexBundle has an 705// apexContents, and modules in that apex have a provider containing the apexContents of each 706// apexBundle they are part of. 707type ApexContents struct { 708 // map from a module name to its membership in this apexBundle 709 contents map[string]ApexMembership 710} 711 712// NewApexContents creates and initializes an ApexContents that is suitable 713// for use with an apex module. 714// * contents is a map from a module name to information about its membership within 715// the apex. 716func NewApexContents(contents map[string]ApexMembership) *ApexContents { 717 return &ApexContents{ 718 contents: contents, 719 } 720} 721 722// Updates an existing membership by adding a new direct (or indirect) membership 723func (i ApexMembership) Add(direct bool) ApexMembership { 724 if direct || i == directlyInApex { 725 return directlyInApex 726 } 727 return indirectlyInApex 728} 729 730// Merges two membership into one. Merging is needed because a module can be a part of an apexBundle 731// in many different paths. For example, it could be dependend on by the apexBundle directly, but at 732// the same time, there might be an indirect dependency to the module. In that case, the more 733// specific dependency (the direct one) is chosen. 734func (i ApexMembership) merge(other ApexMembership) ApexMembership { 735 if other == directlyInApex || i == directlyInApex { 736 return directlyInApex 737 } 738 739 if other == indirectlyInApex || i == indirectlyInApex { 740 return indirectlyInApex 741 } 742 return notInApex 743} 744 745// Tests whether a module named moduleName is directly included in the apexBundle where this 746// ApexContents is tagged. 747func (ac *ApexContents) DirectlyInApex(moduleName string) bool { 748 return ac.contents[moduleName] == directlyInApex 749} 750 751// Tests whether a module named moduleName is included in the apexBundle where this ApexContent is 752// tagged. 753func (ac *ApexContents) InApex(moduleName string) bool { 754 return ac.contents[moduleName] != notInApex 755} 756 757// Tests whether a module named moduleName is directly depended on by all APEXes in an ApexInfo. 758func DirectlyInAllApexes(apexInfo ApexInfo, moduleName string) bool { 759 for _, contents := range apexInfo.ApexContents { 760 if !contents.DirectlyInApex(moduleName) { 761 return false 762 } 763 } 764 return true 765} 766 767//////////////////////////////////////////////////////////////////////////////////////////////////// 768//Below are routines for extra safety checks. 769// 770// BuildDepsInfoLists is to flatten the dependency graph for an apexBundle into a text file 771// (actually two in slightly different formats). The files are mostly for debugging, for example to 772// see why a certain module is included in an APEX via which dependency path. 773// 774// CheckMinSdkVersion is to make sure that all modules in an apexBundle satisfy the min_sdk_version 775// requirement of the apexBundle. 776 777// A dependency info for a single ApexModule, either direct or transitive. 778type ApexModuleDepInfo struct { 779 // Name of the dependency 780 To string 781 // List of dependencies To belongs to. Includes APEX itself, if a direct dependency. 782 From []string 783 // Whether the dependency belongs to the final compiled APEX. 784 IsExternal bool 785 // min_sdk_version of the ApexModule 786 MinSdkVersion string 787} 788 789// A map of a dependency name to its ApexModuleDepInfo 790type DepNameToDepInfoMap map[string]ApexModuleDepInfo 791 792type ApexBundleDepsInfo struct { 793 flatListPath OutputPath 794 fullListPath OutputPath 795} 796 797type ApexBundleDepsInfoIntf interface { 798 Updatable() bool 799 FlatListPath() Path 800 FullListPath() Path 801} 802 803func (d *ApexBundleDepsInfo) FlatListPath() Path { 804 return d.flatListPath 805} 806 807func (d *ApexBundleDepsInfo) FullListPath() Path { 808 return d.fullListPath 809} 810 811// Generate two module out files: 812// 1. FullList with transitive deps and their parents in the dep graph 813// 2. FlatList with a flat list of transitive deps 814// In both cases transitive deps of external deps are not included. Neither are deps that are only 815// available to APEXes; they are developed with updatability in mind and don't need manual approval. 816func (d *ApexBundleDepsInfo) BuildDepsInfoLists(ctx ModuleContext, minSdkVersion string, depInfos DepNameToDepInfoMap) { 817 var fullContent strings.Builder 818 var flatContent strings.Builder 819 820 fmt.Fprintf(&fullContent, "%s(minSdkVersion:%s):\n", ctx.ModuleName(), minSdkVersion) 821 for _, key := range FirstUniqueStrings(SortedStringKeys(depInfos)) { 822 info := depInfos[key] 823 toName := fmt.Sprintf("%s(minSdkVersion:%s)", info.To, info.MinSdkVersion) 824 if info.IsExternal { 825 toName = toName + " (external)" 826 } 827 fmt.Fprintf(&fullContent, " %s <- %s\n", toName, strings.Join(SortedUniqueStrings(info.From), ", ")) 828 fmt.Fprintf(&flatContent, "%s\n", toName) 829 } 830 831 d.fullListPath = PathForModuleOut(ctx, "depsinfo", "fulllist.txt").OutputPath 832 WriteFileRule(ctx, d.fullListPath, fullContent.String()) 833 834 d.flatListPath = PathForModuleOut(ctx, "depsinfo", "flatlist.txt").OutputPath 835 WriteFileRule(ctx, d.flatListPath, flatContent.String()) 836 837 ctx.Phony(fmt.Sprintf("%s-depsinfo", ctx.ModuleName()), d.fullListPath, d.flatListPath) 838} 839 840// TODO(b/158059172): remove minSdkVersion allowlist 841var minSdkVersionAllowlist = func(apiMap map[string]int) map[string]ApiLevel { 842 list := make(map[string]ApiLevel, len(apiMap)) 843 for name, finalApiInt := range apiMap { 844 list[name] = uncheckedFinalApiLevel(finalApiInt) 845 } 846 return list 847}(map[string]int{ 848 "android.net.ipsec.ike": 30, 849 "androidx.annotation_annotation-nodeps": 29, 850 "androidx.arch.core_core-common-nodeps": 29, 851 "androidx.collection_collection-nodeps": 29, 852 "androidx.collection_collection-ktx-nodeps": 30, 853 "androidx.concurrent_concurrent-futures-nodeps": 30, 854 "androidx.lifecycle_lifecycle-common-java8-nodeps": 30, 855 "androidx.lifecycle_lifecycle-common-nodeps": 29, 856 "androidx.room_room-common-nodeps": 30, 857 "androidx-constraintlayout_constraintlayout-solver-nodeps": 29, 858 "apache-commons-compress": 29, 859 "bouncycastle_ike_digests": 30, 860 "brotli-java": 29, 861 "captiveportal-lib": 28, 862 "error_prone_annotations": 30, 863 "flatbuffer_headers": 30, 864 "framework-permission": 30, 865 "gemmlowp_headers": 30, 866 "guava-listenablefuture-prebuilt-jar": 30, 867 "ike-internals": 30, 868 "kotlinx-coroutines-android": 28, 869 "kotlinx-coroutines-android-nodeps": 30, 870 "kotlinx-coroutines-core": 28, 871 "kotlinx-coroutines-core-nodeps": 30, 872 "libbrotli": 30, 873 "libcrypto_static": 30, 874 "libeigen": 30, 875 "liblz4": 30, 876 "libmdnssd": 30, 877 "libneuralnetworks_common": 30, 878 "libneuralnetworks_headers": 30, 879 "libneuralnetworks": 30, 880 "libprocpartition": 30, 881 "libprotobuf-java-lite": 30, 882 "libprotoutil": 30, 883 "libtextclassifier_hash_headers": 30, 884 "libtextclassifier_hash_static": 30, 885 "libtflite_kernel_utils": 30, 886 "libwatchdog": 29, 887 "libzstd": 30, 888 "metrics-constants-protos": 28, 889 "net-utils-framework-common": 29, 890 "permissioncontroller-statsd": 28, 891 "philox_random_headers": 30, 892 "philox_random": 30, 893 "service-permission": 30, 894 "tensorflow_headers": 30, 895 "xz-java": 29, 896}) 897 898// Function called while walking an APEX's payload dependencies. 899// 900// Return true if the `to` module should be visited, false otherwise. 901type PayloadDepsCallback func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool 902type WalkPayloadDepsFunc func(ctx ModuleContext, do PayloadDepsCallback) 903 904// ModuleWithMinSdkVersionCheck represents a module that implements min_sdk_version checks 905type ModuleWithMinSdkVersionCheck interface { 906 Module 907 MinSdkVersion(ctx EarlyModuleContext) SdkSpec 908 CheckMinSdkVersion(ctx ModuleContext) 909} 910 911// CheckMinSdkVersion checks if every dependency of an updatable module sets min_sdk_version 912// accordingly 913func CheckMinSdkVersion(ctx ModuleContext, minSdkVersion ApiLevel, walk WalkPayloadDepsFunc) { 914 // do not enforce min_sdk_version for host 915 if ctx.Host() { 916 return 917 } 918 919 // do not enforce for coverage build 920 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") || ctx.DeviceConfig().NativeCoverageEnabled() || ctx.DeviceConfig().ClangCoverageEnabled() { 921 return 922 } 923 924 // do not enforce deps.min_sdk_version if APEX/APK doesn't set min_sdk_version 925 if minSdkVersion.IsNone() { 926 return 927 } 928 929 walk(ctx, func(ctx ModuleContext, from blueprint.Module, to ApexModule, externalDep bool) bool { 930 if externalDep { 931 // external deps are outside the payload boundary, which is "stable" 932 // interface. We don't have to check min_sdk_version for external 933 // dependencies. 934 return false 935 } 936 if am, ok := from.(DepIsInSameApex); ok && !am.DepIsInSameApex(ctx, to) { 937 return false 938 } 939 if m, ok := to.(ModuleWithMinSdkVersionCheck); ok { 940 // This dependency performs its own min_sdk_version check, just make sure it sets min_sdk_version 941 // to trigger the check. 942 if !m.MinSdkVersion(ctx).Specified() { 943 ctx.OtherModuleErrorf(m, "must set min_sdk_version") 944 } 945 return false 946 } 947 if err := to.ShouldSupportSdkVersion(ctx, minSdkVersion); err != nil { 948 toName := ctx.OtherModuleName(to) 949 if ver, ok := minSdkVersionAllowlist[toName]; !ok || ver.GreaterThan(minSdkVersion) { 950 ctx.OtherModuleErrorf(to, "should support min_sdk_version(%v) for %q: %v."+ 951 "\n\nDependency path: %s\n\n"+ 952 "Consider adding 'min_sdk_version: %q' to %q", 953 minSdkVersion, ctx.ModuleName(), err.Error(), 954 ctx.GetPathString(false), 955 minSdkVersion, toName) 956 return false 957 } 958 } 959 return true 960 }) 961} 962