apex.go 37 KB

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