mutator.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. // Copyright 2015 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. "android/soong/bazel"
  17. "android/soong/ui/metrics/bp2build_metrics_proto"
  18. "github.com/google/blueprint"
  19. )
  20. // Phases:
  21. // run Pre-arch mutators
  22. // run archMutator
  23. // run Pre-deps mutators
  24. // run depsMutator
  25. // run PostDeps mutators
  26. // run FinalDeps mutators (CreateVariations disallowed in this phase)
  27. // continue on to GenerateAndroidBuildActions
  28. // RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing.
  29. func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
  30. bp2buildMutators := append(preArchMutators, registerBp2buildConversionMutator)
  31. registerMutatorsForBazelConversion(ctx, bp2buildMutators)
  32. }
  33. // RegisterMutatorsForApiBazelConversion is an alternate registration pipeline for api_bp2build
  34. // This pipeline restricts generation of Bazel targets to Soong modules that contribute APIs
  35. func RegisterMutatorsForApiBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) {
  36. bp2buildMutators := append(preArchMutators, registerApiBp2buildConversionMutator)
  37. registerMutatorsForBazelConversion(ctx, bp2buildMutators)
  38. }
  39. func registerMutatorsForBazelConversion(ctx *Context, bp2buildMutators []RegisterMutatorFunc) {
  40. mctx := &registerMutatorsContext{
  41. bazelConversionMode: true,
  42. }
  43. allMutators := append([]RegisterMutatorFunc{
  44. RegisterNamespaceMutator,
  45. RegisterDefaultsPreArchMutators,
  46. // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should
  47. // evaluate the impact on conversion.
  48. RegisterPrebuiltsPreArchMutators,
  49. },
  50. bp2buildMutators...)
  51. // Register bp2build mutators
  52. for _, f := range allMutators {
  53. f(mctx)
  54. }
  55. mctx.mutators.registerAll(ctx)
  56. }
  57. // collateGloballyRegisteredMutators constructs the list of mutators that have been registered
  58. // with the InitRegistrationContext and will be used at runtime.
  59. func collateGloballyRegisteredMutators() sortableComponents {
  60. // ensure mixed builds mutator is the last mutator
  61. finalDeps = append(finalDeps, registerMixedBuildsMutator)
  62. return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps)
  63. }
  64. // collateRegisteredMutators constructs a single list of mutators from the separate lists.
  65. func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents {
  66. mctx := &registerMutatorsContext{}
  67. register := func(funcs []RegisterMutatorFunc) {
  68. for _, f := range funcs {
  69. f(mctx)
  70. }
  71. }
  72. register(preArch)
  73. register(preDeps)
  74. register([]RegisterMutatorFunc{registerDepsMutator})
  75. register(postDeps)
  76. mctx.finalPhase = true
  77. register(finalDeps)
  78. return mctx.mutators
  79. }
  80. type registerMutatorsContext struct {
  81. mutators sortableComponents
  82. finalPhase bool
  83. bazelConversionMode bool
  84. }
  85. type RegisterMutatorsContext interface {
  86. TopDown(name string, m TopDownMutator) MutatorHandle
  87. BottomUp(name string, m BottomUpMutator) MutatorHandle
  88. BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle
  89. Transition(name string, m TransitionMutator)
  90. }
  91. type RegisterMutatorFunc func(RegisterMutatorsContext)
  92. var preArch = []RegisterMutatorFunc{
  93. RegisterNamespaceMutator,
  94. // Check the visibility rules are valid.
  95. //
  96. // This must run after the package renamer mutators so that any issues found during
  97. // validation of the package's default_visibility property are reported using the
  98. // correct package name and not the synthetic name.
  99. //
  100. // This must also be run before defaults mutators as the rules for validation are
  101. // different before checking the rules than they are afterwards. e.g.
  102. // visibility: ["//visibility:private", "//visibility:public"]
  103. // would be invalid if specified in a module definition but is valid if it results
  104. // from something like this:
  105. //
  106. // defaults {
  107. // name: "defaults",
  108. // // Be inaccessible outside a package by default.
  109. // visibility: ["//visibility:private"]
  110. // }
  111. //
  112. // defaultable_module {
  113. // name: "defaultable_module",
  114. // defaults: ["defaults"],
  115. // // Override the default.
  116. // visibility: ["//visibility:public"]
  117. // }
  118. //
  119. RegisterVisibilityRuleChecker,
  120. // Record the default_applicable_licenses for each package.
  121. //
  122. // This must run before the defaults so that defaults modules can pick up the package default.
  123. RegisterLicensesPackageMapper,
  124. // Apply properties from defaults modules to the referencing modules.
  125. //
  126. // Any mutators that are added before this will not see any modules created by
  127. // a DefaultableHook.
  128. RegisterDefaultsPreArchMutators,
  129. // Add dependencies on any components so that any component references can be
  130. // resolved within the deps mutator.
  131. //
  132. // Must be run after defaults so it can be used to create dependencies on the
  133. // component modules that are creating in a DefaultableHook.
  134. //
  135. // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are
  136. // renamed. That is so that if a module creates components using a prebuilt module
  137. // type that any dependencies (which must use prebuilt_ prefixes) are resolved to
  138. // the prebuilt module and not the source module.
  139. RegisterComponentsMutator,
  140. // Create an association between prebuilt modules and their corresponding source
  141. // modules (if any).
  142. //
  143. // Must be run after defaults mutators to ensure that any modules created by
  144. // a DefaultableHook can be either a prebuilt or a source module with a matching
  145. // prebuilt.
  146. RegisterPrebuiltsPreArchMutators,
  147. // Gather the licenses properties for all modules for use during expansion and enforcement.
  148. //
  149. // This must come after the defaults mutators to ensure that any licenses supplied
  150. // in a defaults module has been successfully applied before the rules are gathered.
  151. RegisterLicensesPropertyGatherer,
  152. // Gather the visibility rules for all modules for us during visibility enforcement.
  153. //
  154. // This must come after the defaults mutators to ensure that any visibility supplied
  155. // in a defaults module has been successfully applied before the rules are gathered.
  156. RegisterVisibilityRuleGatherer,
  157. }
  158. func registerArchMutator(ctx RegisterMutatorsContext) {
  159. ctx.BottomUpBlueprint("os", osMutator).Parallel()
  160. ctx.BottomUp("image", imageMutator).Parallel()
  161. ctx.BottomUpBlueprint("arch", archMutator).Parallel()
  162. }
  163. var preDeps = []RegisterMutatorFunc{
  164. registerArchMutator,
  165. }
  166. var postDeps = []RegisterMutatorFunc{
  167. registerPathDepsMutator,
  168. RegisterPrebuiltsPostDepsMutators,
  169. RegisterVisibilityRuleEnforcer,
  170. RegisterLicensesDependencyChecker,
  171. registerNeverallowMutator,
  172. RegisterOverridePostDepsMutators,
  173. }
  174. var finalDeps = []RegisterMutatorFunc{}
  175. func PreArchMutators(f RegisterMutatorFunc) {
  176. preArch = append(preArch, f)
  177. }
  178. func PreDepsMutators(f RegisterMutatorFunc) {
  179. preDeps = append(preDeps, f)
  180. }
  181. func PostDepsMutators(f RegisterMutatorFunc) {
  182. postDeps = append(postDeps, f)
  183. }
  184. func FinalDepsMutators(f RegisterMutatorFunc) {
  185. finalDeps = append(finalDeps, f)
  186. }
  187. var bp2buildPreArchMutators = []RegisterMutatorFunc{}
  188. // A minimal context for Bp2build conversion
  189. type Bp2buildMutatorContext interface {
  190. BazelConversionPathContext
  191. CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
  192. }
  193. // PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
  194. // into Bazel BUILD targets that should run prior to deps and conversion.
  195. func PreArchBp2BuildMutators(f RegisterMutatorFunc) {
  196. bp2buildPreArchMutators = append(bp2buildPreArchMutators, f)
  197. }
  198. type BaseMutatorContext interface {
  199. BaseModuleContext
  200. // MutatorName returns the name that this mutator was registered with.
  201. MutatorName() string
  202. // Rename all variants of a module. The new name is not visible to calls to ModuleName,
  203. // AddDependency or OtherModuleName until after this mutator pass is complete.
  204. Rename(name string)
  205. }
  206. type TopDownMutator func(TopDownMutatorContext)
  207. type TopDownMutatorContext interface {
  208. BaseMutatorContext
  209. // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies
  210. // the specified property structs to it as if the properties were set in a blueprint file.
  211. CreateModule(ModuleFactory, ...interface{}) Module
  212. // CreateBazelTargetModule creates a BazelTargetModule by calling the
  213. // factory method, just like in CreateModule, but also requires
  214. // BazelTargetModuleProperties containing additional metadata for the
  215. // bp2build codegenerator.
  216. CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{})
  217. // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the
  218. // factory method, just like in CreateModule, but also requires
  219. // BazelTargetModuleProperties containing additional metadata for the
  220. // bp2build codegenerator. The generated target is restricted to only be buildable for certain
  221. // platforms, as dictated by a given bool attribute: the target will not be buildable in
  222. // any platform for which this bool attribute is false.
  223. CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute)
  224. // MarkBp2buildUnconvertible registers the current module as "unconvertible to bp2build" for the
  225. // given reason.
  226. MarkBp2buildUnconvertible(reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string)
  227. // CreateBazelTargetAliasInDir creates an alias definition in `dir` directory.
  228. // This function can be used to create alias definitions in a directory that is different
  229. // from the directory of the visited Soong module.
  230. CreateBazelTargetAliasInDir(dir string, name string, actual bazel.Label)
  231. // CreateBazelConfigSetting creates a config_setting in <dir>/BUILD.bazel
  232. // build/bazel has several static config_setting(s) that are used in Bazel builds.
  233. // This function can be used to createa additional config_setting(s) based on the build graph
  234. // (e.g. a config_setting specific to an apex variant)
  235. CreateBazelConfigSetting(csa bazel.ConfigSettingAttributes, ca CommonAttributes, dir string)
  236. }
  237. type topDownMutatorContext struct {
  238. bp blueprint.TopDownMutatorContext
  239. baseModuleContext
  240. }
  241. type BottomUpMutator func(BottomUpMutatorContext)
  242. type BottomUpMutatorContext interface {
  243. BaseMutatorContext
  244. // AddDependency adds a dependency to the given module. It returns a slice of modules for each
  245. // dependency (some entries may be nil).
  246. //
  247. // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
  248. // new dependencies have had the current mutator called on them. If the mutator is not
  249. // parallel this method does not affect the ordering of the current mutator pass, but will
  250. // be ordered correctly for all future mutator passes.
  251. AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module
  252. // AddReverseDependency adds a dependency from the destination to the given module.
  253. // Does not affect the ordering of the current mutator pass, but will be ordered
  254. // correctly for all future mutator passes. All reverse dependencies for a destination module are
  255. // collected until the end of the mutator pass, sorted by name, and then appended to the destination
  256. // module's dependency list.
  257. AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string)
  258. // CreateVariations splits a module into multiple variants, one for each name in the variationNames
  259. // parameter. It returns a list of new modules in the same order as the variationNames
  260. // list.
  261. //
  262. // If any of the dependencies of the module being operated on were already split
  263. // by calling CreateVariations with the same name, the dependency will automatically
  264. // be updated to point the matching variant.
  265. //
  266. // If a module is split, and then a module depending on the first module is not split
  267. // when the Mutator is later called on it, the dependency of the depending module will
  268. // automatically be updated to point to the first variant.
  269. CreateVariations(...string) []Module
  270. // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames
  271. // parameter. It returns a list of new modules in the same order as the variantNames
  272. // list.
  273. //
  274. // Local variations do not affect automatic dependency resolution - dependencies added
  275. // to the split module via deps or DynamicDependerModule must exactly match a variant
  276. // that contains all the non-local variations.
  277. CreateLocalVariations(...string) []Module
  278. // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation
  279. // with given name. This function ignores the default variation set by SetDefaultDependencyVariation.
  280. SetDependencyVariation(string)
  281. // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected
  282. // during the subsequent calls on Create*Variations* functions. To reset, set it to nil.
  283. SetDefaultDependencyVariation(*string)
  284. // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations
  285. // argument to select which variant of the dependency to use. It returns a slice of modules for
  286. // each dependency (some entries may be nil). A variant of the dependency must exist that matches
  287. // all the non-local variations of the current module, plus the variations argument.
  288. //
  289. // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
  290. // new dependencies have had the current mutator called on them. If the mutator is not
  291. // parallel this method does not affect the ordering of the current mutator pass, but will
  292. // be ordered correctly for all future mutator passes.
  293. AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module
  294. // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the
  295. // variations argument to select which variant of the dependency to use. It returns a slice of
  296. // modules for each dependency (some entries may be nil). A variant of the dependency must
  297. // exist that matches the variations argument, but may also have other variations.
  298. // For any unspecified variation the first variant will be used.
  299. //
  300. // Unlike AddVariationDependencies, the variations of the current module are ignored - the
  301. // dependency only needs to match the supplied variations.
  302. //
  303. // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the
  304. // new dependencies have had the current mutator called on them. If the mutator is not
  305. // parallel this method does not affect the ordering of the current mutator pass, but will
  306. // be ordered correctly for all future mutator passes.
  307. AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module
  308. // AddInterVariantDependency adds a dependency between two variants of the same module. Variants are always
  309. // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change
  310. // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps,
  311. // WalkDeps, etc.
  312. AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module)
  313. // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
  314. // specified name with the current variant of this module. Replacements don't take effect until
  315. // after the mutator pass is finished.
  316. ReplaceDependencies(string)
  317. // ReplaceDependencies replaces all dependencies on the identical variant of the module with the
  318. // specified name with the current variant of this module as long as the supplied predicate returns
  319. // true.
  320. //
  321. // Replacements don't take effect until after the mutator pass is finished.
  322. ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate)
  323. // AliasVariation takes a variationName that was passed to CreateVariations for this module,
  324. // and creates an alias from the current variant (before the mutator has run) to the new
  325. // variant. The alias will be valid until the next time a mutator calls CreateVariations or
  326. // CreateLocalVariations on this module without also calling AliasVariation. The alias can
  327. // be used to add dependencies on the newly created variant using the variant map from
  328. // before CreateVariations was run.
  329. AliasVariation(variationName string)
  330. // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this
  331. // module, and creates an alias from a new fromVariationName variant the toVariationName
  332. // variant. The alias will be valid until the next time a mutator calls CreateVariations or
  333. // CreateLocalVariations on this module without also calling AliasVariation. The alias can
  334. // be used to add dependencies on the toVariationName variant using the fromVariationName
  335. // variant.
  336. CreateAliasVariation(fromVariationName, toVariationName string)
  337. // SetVariationProvider sets the value for a provider for the given newly created variant of
  338. // the current module, i.e. one of the Modules returned by CreateVariations.. It panics if
  339. // not called during the appropriate mutator or GenerateBuildActions pass for the provider,
  340. // if the value is not of the appropriate type, or if the module is not a newly created
  341. // variant of the current module. The value should not be modified after being passed to
  342. // SetVariationProvider.
  343. SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{})
  344. }
  345. type bottomUpMutatorContext struct {
  346. bp blueprint.BottomUpMutatorContext
  347. baseModuleContext
  348. finalPhase bool
  349. }
  350. func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module,
  351. finalPhase, bazelConversionMode bool) BottomUpMutatorContext {
  352. moduleContext := a.base().baseModuleContextFactory(ctx)
  353. moduleContext.bazelConversionMode = bazelConversionMode
  354. return &bottomUpMutatorContext{
  355. bp: ctx,
  356. baseModuleContext: moduleContext,
  357. finalPhase: finalPhase,
  358. }
  359. }
  360. func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle {
  361. finalPhase := x.finalPhase
  362. bazelConversionMode := x.bazelConversionMode
  363. f := func(ctx blueprint.BottomUpMutatorContext) {
  364. if a, ok := ctx.Module().(Module); ok {
  365. m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode))
  366. }
  367. }
  368. mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f}
  369. x.mutators = append(x.mutators, mutator)
  370. return mutator
  371. }
  372. func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle {
  373. mutator := &mutator{name: name, bottomUpMutator: m}
  374. x.mutators = append(x.mutators, mutator)
  375. return mutator
  376. }
  377. type IncomingTransitionContext interface {
  378. // Module returns the target of the dependency edge for which the transition
  379. // is being computed
  380. Module() Module
  381. // Config returns the configuration for the build.
  382. Config() Config
  383. }
  384. type OutgoingTransitionContext interface {
  385. // Module returns the target of the dependency edge for which the transition
  386. // is being computed
  387. Module() Module
  388. // DepTag() Returns the dependency tag through which this dependency is
  389. // reached
  390. DepTag() blueprint.DependencyTag
  391. }
  392. // Transition mutators implement a top-down mechanism where a module tells its
  393. // direct dependencies what variation they should be built in but the dependency
  394. // has the final say.
  395. //
  396. // When implementing a transition mutator, one needs to implement four methods:
  397. // - Split() that tells what variations a module has by itself
  398. // - OutgoingTransition() where a module tells what it wants from its
  399. // dependency
  400. // - IncomingTransition() where a module has the final say about its own
  401. // variation
  402. // - Mutate() that changes the state of a module depending on its variation
  403. //
  404. // That the effective variation of module B when depended on by module A is the
  405. // composition the outgoing transition of module A and the incoming transition
  406. // of module B.
  407. //
  408. // the outgoing transition should not take the properties of the dependency into
  409. // account, only those of the module that depends on it. For this reason, the
  410. // dependency is not even passed into it as an argument. Likewise, the incoming
  411. // transition should not take the properties of the depending module into
  412. // account and is thus not informed about it. This makes for a nice
  413. // decomposition of the decision logic.
  414. //
  415. // A given transition mutator only affects its own variation; other variations
  416. // stay unchanged along the dependency edges.
  417. //
  418. // Soong makes sure that all modules are created in the desired variations and
  419. // that dependency edges are set up correctly. This ensures that "missing
  420. // variation" errors do not happen and allows for more flexible changes in the
  421. // value of the variation among dependency edges (as oppposed to bottom-up
  422. // mutators where if module A in variation X depends on module B and module B
  423. // has that variation X, A must depend on variation X of B)
  424. //
  425. // The limited power of the context objects passed to individual mutators
  426. // methods also makes it more difficult to shoot oneself in the foot. Complete
  427. // safety is not guaranteed because no one prevents individual transition
  428. // mutators from mutating modules in illegal ways and for e.g. Split() or
  429. // Mutate() to run their own visitations of the transitive dependency of the
  430. // module and both of these are bad ideas, but it's better than no guardrails at
  431. // all.
  432. //
  433. // This model is pretty close to Bazel's configuration transitions. The mapping
  434. // between concepts in Soong and Bazel is as follows:
  435. // - Module == configured target
  436. // - Variant == configuration
  437. // - Variation name == configuration flag
  438. // - Variation == configuration flag value
  439. // - Outgoing transition == attribute transition
  440. // - Incoming transition == rule transition
  441. //
  442. // The Split() method does not have a Bazel equivalent and Bazel split
  443. // transitions do not have a Soong equivalent.
  444. //
  445. // Mutate() does not make sense in Bazel due to the different models of the
  446. // two systems: when creating new variations, Soong clones the old module and
  447. // thus some way is needed to change it state whereas Bazel creates each
  448. // configuration of a given configured target anew.
  449. type TransitionMutator interface {
  450. // Split returns the set of variations that should be created for a module no
  451. // matter who depends on it. Used when Make depends on a particular variation
  452. // or when the module knows its variations just based on information given to
  453. // it in the Blueprint file. This method should not mutate the module it is
  454. // called on.
  455. Split(ctx BaseModuleContext) []string
  456. // Called on a module to determine which variation it wants from its direct
  457. // dependencies. The dependency itself can override this decision. This method
  458. // should not mutate the module itself.
  459. OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string
  460. // Called on a module to determine which variation it should be in based on
  461. // the variation modules that depend on it want. This gives the module a final
  462. // say about its own variations. This method should not mutate the module
  463. // itself.
  464. IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string
  465. // Called after a module was split into multiple variations on each variation.
  466. // It should not split the module any further but adding new dependencies is
  467. // fine. Unlike all the other methods on TransitionMutator, this method is
  468. // allowed to mutate the module.
  469. Mutate(ctx BottomUpMutatorContext, variation string)
  470. }
  471. type androidTransitionMutator struct {
  472. finalPhase bool
  473. bazelConversionMode bool
  474. mutator TransitionMutator
  475. }
  476. func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string {
  477. if m, ok := ctx.Module().(Module); ok {
  478. moduleContext := m.base().baseModuleContextFactory(ctx)
  479. moduleContext.bazelConversionMode = a.bazelConversionMode
  480. return a.mutator.Split(&moduleContext)
  481. } else {
  482. return []string{""}
  483. }
  484. }
  485. type outgoingTransitionContextImpl struct {
  486. bp blueprint.OutgoingTransitionContext
  487. }
  488. func (c *outgoingTransitionContextImpl) Module() Module {
  489. return c.bp.Module().(Module)
  490. }
  491. func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
  492. return c.bp.DepTag()
  493. }
  494. func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
  495. if _, ok := ctx.Module().(Module); ok {
  496. return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation)
  497. } else {
  498. return ""
  499. }
  500. }
  501. type incomingTransitionContextImpl struct {
  502. bp blueprint.IncomingTransitionContext
  503. }
  504. func (c *incomingTransitionContextImpl) Module() Module {
  505. return c.bp.Module().(Module)
  506. }
  507. func (c *incomingTransitionContextImpl) Config() Config {
  508. return c.bp.Config().(Config)
  509. }
  510. func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string {
  511. if _, ok := ctx.Module().(Module); ok {
  512. return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation)
  513. } else {
  514. return ""
  515. }
  516. }
  517. func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) {
  518. if am, ok := ctx.Module().(Module); ok {
  519. a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase, a.bazelConversionMode), variation)
  520. }
  521. }
  522. func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) {
  523. atm := &androidTransitionMutator{
  524. finalPhase: x.finalPhase,
  525. bazelConversionMode: x.bazelConversionMode,
  526. mutator: m,
  527. }
  528. mutator := &mutator{
  529. name: name,
  530. transitionMutator: atm}
  531. x.mutators = append(x.mutators, mutator)
  532. }
  533. func (x *registerMutatorsContext) mutatorName(name string) string {
  534. if x.bazelConversionMode {
  535. return name + "_bp2build"
  536. }
  537. return name
  538. }
  539. func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle {
  540. f := func(ctx blueprint.TopDownMutatorContext) {
  541. if a, ok := ctx.Module().(Module); ok {
  542. moduleContext := a.base().baseModuleContextFactory(ctx)
  543. moduleContext.bazelConversionMode = x.bazelConversionMode
  544. actx := &topDownMutatorContext{
  545. bp: ctx,
  546. baseModuleContext: moduleContext,
  547. }
  548. m(actx)
  549. }
  550. }
  551. mutator := &mutator{name: x.mutatorName(name), topDownMutator: f}
  552. x.mutators = append(x.mutators, mutator)
  553. return mutator
  554. }
  555. func (mutator *mutator) componentName() string {
  556. return mutator.name
  557. }
  558. func (mutator *mutator) register(ctx *Context) {
  559. blueprintCtx := ctx.Context
  560. var handle blueprint.MutatorHandle
  561. if mutator.bottomUpMutator != nil {
  562. handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator)
  563. } else if mutator.topDownMutator != nil {
  564. handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator)
  565. } else if mutator.transitionMutator != nil {
  566. blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator)
  567. }
  568. if mutator.parallel {
  569. handle.Parallel()
  570. }
  571. }
  572. type MutatorHandle interface {
  573. Parallel() MutatorHandle
  574. }
  575. func (mutator *mutator) Parallel() MutatorHandle {
  576. mutator.parallel = true
  577. return mutator
  578. }
  579. func RegisterComponentsMutator(ctx RegisterMutatorsContext) {
  580. ctx.BottomUp("component-deps", componentDepsMutator).Parallel()
  581. }
  582. // A special mutator that runs just prior to the deps mutator to allow the dependencies
  583. // on component modules to be added so that they can depend directly on a prebuilt
  584. // module.
  585. func componentDepsMutator(ctx BottomUpMutatorContext) {
  586. if m := ctx.Module(); m.Enabled() {
  587. m.ComponentDepsMutator(ctx)
  588. }
  589. }
  590. func depsMutator(ctx BottomUpMutatorContext) {
  591. if m := ctx.Module(); m.Enabled() {
  592. m.DepsMutator(ctx)
  593. }
  594. }
  595. func registerDepsMutator(ctx RegisterMutatorsContext) {
  596. ctx.BottomUp("deps", depsMutator).Parallel()
  597. }
  598. func registerDepsMutatorBp2Build(ctx RegisterMutatorsContext) {
  599. // TODO(b/179313531): Consider a separate mutator that only runs depsMutator for modules that are
  600. // being converted to build targets.
  601. ctx.BottomUp("deps", depsMutator).Parallel()
  602. }
  603. func (t *topDownMutatorContext) CreateBazelTargetModule(
  604. bazelProps bazel.BazelTargetModuleProperties,
  605. commonAttrs CommonAttributes,
  606. attrs interface{}) {
  607. t.createBazelTargetModule(bazelProps, commonAttrs, attrs, bazel.BoolAttribute{})
  608. }
  609. func (t *topDownMutatorContext) CreateBazelTargetModuleWithRestrictions(
  610. bazelProps bazel.BazelTargetModuleProperties,
  611. commonAttrs CommonAttributes,
  612. attrs interface{},
  613. enabledProperty bazel.BoolAttribute) {
  614. t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty)
  615. }
  616. func (t *topDownMutatorContext) MarkBp2buildUnconvertible(
  617. reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string) {
  618. mod := t.Module()
  619. mod.base().setBp2buildUnconvertible(reasonType, detail)
  620. }
  621. var (
  622. bazelAliasModuleProperties = bazel.BazelTargetModuleProperties{
  623. Rule_class: "alias",
  624. }
  625. )
  626. type bazelAliasAttributes struct {
  627. Actual *bazel.LabelAttribute
  628. }
  629. func (t *topDownMutatorContext) CreateBazelTargetAliasInDir(
  630. dir string,
  631. name string,
  632. actual bazel.Label) {
  633. mod := t.Module()
  634. attrs := &bazelAliasAttributes{
  635. Actual: bazel.MakeLabelAttribute(actual.Label),
  636. }
  637. info := bp2buildInfo{
  638. Dir: dir,
  639. BazelProps: bazelAliasModuleProperties,
  640. CommonAttrs: CommonAttributes{Name: name},
  641. ConstraintAttrs: constraintAttributes{},
  642. Attrs: attrs,
  643. }
  644. mod.base().addBp2buildInfo(info)
  645. }
  646. func (t *topDownMutatorContext) CreateBazelConfigSetting(
  647. csa bazel.ConfigSettingAttributes,
  648. ca CommonAttributes,
  649. dir string) {
  650. mod := t.Module()
  651. info := bp2buildInfo{
  652. Dir: dir,
  653. BazelProps: bazel.BazelTargetModuleProperties{
  654. Rule_class: "config_setting",
  655. },
  656. CommonAttrs: ca,
  657. ConstraintAttrs: constraintAttributes{},
  658. Attrs: &csa,
  659. }
  660. mod.base().addBp2buildInfo(info)
  661. }
  662. // ApexAvailableTags converts the apex_available property value of an ApexModule
  663. // module and returns it as a list of keyed tags.
  664. func ApexAvailableTags(mod Module) bazel.StringListAttribute {
  665. attr := bazel.StringListAttribute{}
  666. // Transform specific attributes into tags.
  667. if am, ok := mod.(ApexModule); ok {
  668. // TODO(b/218841706): hidl_interface has the apex_available prop, but it's
  669. // defined directly as a prop and not via ApexModule, so this doesn't
  670. // pick those props up.
  671. apexAvailable := am.apexModuleBase().ApexAvailable()
  672. // If a user does not specify apex_available in Android.bp, then soong provides a default.
  673. // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
  674. if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
  675. apexAvailable = []string{}
  676. }
  677. attr.Value = ConvertApexAvailableToTags(apexAvailable)
  678. }
  679. return attr
  680. }
  681. func ApexAvailableTagsWithoutTestApexes(ctx BaseModuleContext, mod Module) bazel.StringListAttribute {
  682. attr := bazel.StringListAttribute{}
  683. if am, ok := mod.(ApexModule); ok {
  684. apexAvailableWithoutTestApexes := removeTestApexes(ctx, am.apexModuleBase().ApexAvailable())
  685. // If a user does not specify apex_available in Android.bp, then soong provides a default.
  686. // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files.
  687. if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 {
  688. apexAvailableWithoutTestApexes = []string{}
  689. }
  690. attr.Value = ConvertApexAvailableToTags(apexAvailableWithoutTestApexes)
  691. }
  692. return attr
  693. }
  694. func removeTestApexes(ctx BaseModuleContext, apex_available []string) []string {
  695. testApexes := []string{}
  696. for _, aa := range apex_available {
  697. // ignore the wildcards
  698. if InList(aa, AvailableToRecognziedWildcards) {
  699. continue
  700. }
  701. mod, _ := ctx.ModuleFromName(aa)
  702. if apex, ok := mod.(ApexTestInterface); ok && apex.IsTestApex() {
  703. testApexes = append(testApexes, aa)
  704. }
  705. }
  706. return RemoveListFromList(CopyOf(apex_available), testApexes)
  707. }
  708. func ConvertApexAvailableToTags(apexAvailable []string) []string {
  709. if len(apexAvailable) == 0 {
  710. // We need nil specifically to make bp2build not add the tags property at all,
  711. // instead of adding it with an empty list
  712. return nil
  713. }
  714. result := make([]string, 0, len(apexAvailable))
  715. for _, a := range apexAvailable {
  716. result = append(result, "apex_available="+a)
  717. }
  718. return result
  719. }
  720. // ConvertApexAvailableToTagsWithoutTestApexes converts a list of apex names to a list of bazel tags
  721. // This function drops any test apexes from the input.
  722. func ConvertApexAvailableToTagsWithoutTestApexes(ctx BaseModuleContext, apexAvailable []string) []string {
  723. noTestApexes := removeTestApexes(ctx, apexAvailable)
  724. return ConvertApexAvailableToTags(noTestApexes)
  725. }
  726. func (t *topDownMutatorContext) createBazelTargetModule(
  727. bazelProps bazel.BazelTargetModuleProperties,
  728. commonAttrs CommonAttributes,
  729. attrs interface{},
  730. enabledProperty bazel.BoolAttribute) {
  731. constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty)
  732. mod := t.Module()
  733. info := bp2buildInfo{
  734. Dir: t.OtherModuleDir(mod),
  735. BazelProps: bazelProps,
  736. CommonAttrs: commonAttrs,
  737. ConstraintAttrs: constraintAttributes,
  738. Attrs: attrs,
  739. }
  740. mod.base().addBp2buildInfo(info)
  741. }
  742. // android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that
  743. // has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid
  744. // ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every
  745. // non-overridden method has to be forwarded. There are fewer non-overridden methods, so use the latter. The following
  746. // methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext.
  747. func (t *topDownMutatorContext) MutatorName() string {
  748. return t.bp.MutatorName()
  749. }
  750. func (t *topDownMutatorContext) Rename(name string) {
  751. t.bp.Rename(name)
  752. t.Module().base().commonProperties.DebugName = name
  753. }
  754. func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module {
  755. return t.bp.CreateModule(factory, name, props...)
  756. }
  757. func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module {
  758. return createModule(t, factory, "_topDownMutatorModule", props...)
  759. }
  760. func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module {
  761. module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module)
  762. return module
  763. }
  764. func (b *bottomUpMutatorContext) MutatorName() string {
  765. return b.bp.MutatorName()
  766. }
  767. func (b *bottomUpMutatorContext) Rename(name string) {
  768. b.bp.Rename(name)
  769. b.Module().base().commonProperties.DebugName = name
  770. }
  771. func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module {
  772. if b.baseModuleContext.checkedMissingDeps() {
  773. panic("Adding deps not allowed after checking for missing deps")
  774. }
  775. return b.bp.AddDependency(module, tag, name...)
  776. }
  777. func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) {
  778. if b.baseModuleContext.checkedMissingDeps() {
  779. panic("Adding deps not allowed after checking for missing deps")
  780. }
  781. b.bp.AddReverseDependency(module, tag, name)
  782. }
  783. func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module {
  784. if b.finalPhase {
  785. panic("CreateVariations not allowed in FinalDepsMutators")
  786. }
  787. modules := b.bp.CreateVariations(variations...)
  788. aModules := make([]Module, len(modules))
  789. for i := range variations {
  790. aModules[i] = modules[i].(Module)
  791. base := aModules[i].base()
  792. base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
  793. base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
  794. }
  795. return aModules
  796. }
  797. func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module {
  798. if b.finalPhase {
  799. panic("CreateLocalVariations not allowed in FinalDepsMutators")
  800. }
  801. modules := b.bp.CreateLocalVariations(variations...)
  802. aModules := make([]Module, len(modules))
  803. for i := range variations {
  804. aModules[i] = modules[i].(Module)
  805. base := aModules[i].base()
  806. base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName())
  807. base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i])
  808. }
  809. return aModules
  810. }
  811. func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) {
  812. b.bp.SetDependencyVariation(variation)
  813. }
  814. func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) {
  815. b.bp.SetDefaultDependencyVariation(variation)
  816. }
  817. func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag,
  818. names ...string) []blueprint.Module {
  819. if b.baseModuleContext.checkedMissingDeps() {
  820. panic("Adding deps not allowed after checking for missing deps")
  821. }
  822. return b.bp.AddVariationDependencies(variations, tag, names...)
  823. }
  824. func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation,
  825. tag blueprint.DependencyTag, names ...string) []blueprint.Module {
  826. if b.baseModuleContext.checkedMissingDeps() {
  827. panic("Adding deps not allowed after checking for missing deps")
  828. }
  829. return b.bp.AddFarVariationDependencies(variations, tag, names...)
  830. }
  831. func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) {
  832. b.bp.AddInterVariantDependency(tag, from, to)
  833. }
  834. func (b *bottomUpMutatorContext) ReplaceDependencies(name string) {
  835. if b.baseModuleContext.checkedMissingDeps() {
  836. panic("Adding deps not allowed after checking for missing deps")
  837. }
  838. b.bp.ReplaceDependencies(name)
  839. }
  840. func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) {
  841. if b.baseModuleContext.checkedMissingDeps() {
  842. panic("Adding deps not allowed after checking for missing deps")
  843. }
  844. b.bp.ReplaceDependenciesIf(name, predicate)
  845. }
  846. func (b *bottomUpMutatorContext) AliasVariation(variationName string) {
  847. b.bp.AliasVariation(variationName)
  848. }
  849. func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) {
  850. b.bp.CreateAliasVariation(fromVariationName, toVariationName)
  851. }
  852. func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) {
  853. b.bp.SetVariationProvider(module, provider, value)
  854. }