Skip to content

Commit

Permalink
Merge changes from topic "transition-mutator-config" into main
Browse files Browse the repository at this point in the history
* changes:
  Add ArchModuleContext to TransitionMutator contexts
  Split ArchMutatorContext out of BaseMutatorContext
  • Loading branch information
colincross authored and Gerrit Code Review committed Jan 24, 2024
2 parents 5373efc + 4aa3e0a commit c50999e
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 151 deletions.
1 change: 1 addition & 0 deletions android/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bootstrap_go_package {
"api_levels.go",
"arch.go",
"arch_list.go",
"arch_module_context.go",
"base_module_context.go",
"buildinfo_prop.go",
"config.go",
Expand Down
83 changes: 83 additions & 0 deletions android/arch_module_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2024 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package android

// ArchModuleContext can be embedded in other contexts to provide information about the module set by
// the archMutator.
type ArchModuleContext interface {
Target() Target
TargetPrimary() bool

// The additional arch specific targets (e.g. 32/64 bit) that this module variant is
// responsible for creating.
MultiTargets() []Target
Arch() Arch
Os() OsType
Host() bool
Device() bool
Darwin() bool
Windows() bool
PrimaryArch() bool
}

type archModuleContext struct {
// TODO: these should eventually go through a (possibly cached) provider like any other configuration instead
// of being special cased.
os OsType
target Target
targetPrimary bool
multiTargets []Target
primaryArch bool
}

func (a *archModuleContext) Target() Target {
return a.target
}

func (a *archModuleContext) TargetPrimary() bool {
return a.targetPrimary
}

func (a *archModuleContext) MultiTargets() []Target {
return a.multiTargets
}

func (a *archModuleContext) Arch() Arch {
return a.target.Arch
}

func (a *archModuleContext) Os() OsType {
return a.os
}

func (a *archModuleContext) Host() bool {
return a.os.Class == Host
}

func (a *archModuleContext) Device() bool {
return a.os.Class == Device
}

func (a *archModuleContext) Darwin() bool {
return a.os == Darwin
}

func (a *archModuleContext) Windows() bool {
return a.os == Windows
}

func (b *archModuleContext) PrimaryArch() bool {
return b.primaryArch
}
63 changes: 2 additions & 61 deletions android/base_module_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
// instead of a blueprint.Module, plus some extra methods that return Android-specific information
// about the current module.
type BaseModuleContext interface {
ArchModuleContext
EarlyModuleContext

blueprintBaseModuleContext() blueprint.BaseModuleContext
Expand Down Expand Up @@ -213,29 +214,12 @@ type BaseModuleContext interface {
// getMissingDependencies returns the list of missing dependencies.
// Calling this function prevents adding new dependencies.
getMissingDependencies() []string

Target() Target
TargetPrimary() bool

// The additional arch specific targets (e.g. 32/64 bit) that this module variant is
// responsible for creating.
MultiTargets() []Target
Arch() Arch
Os() OsType
Host() bool
Device() bool
Darwin() bool
Windows() bool
PrimaryArch() bool
}

type baseModuleContext struct {
bp blueprint.BaseModuleContext
earlyModuleContext
os OsType
target Target
multiTargets []Target
targetPrimary bool
archModuleContext

walkPath []Module
tagPath []blueprint.DependencyTag
Expand Down Expand Up @@ -580,46 +564,3 @@ func (b *baseModuleContext) GetPathString(skipFirst bool) string {
}
return sb.String()
}

func (b *baseModuleContext) Target() Target {
return b.target
}

func (b *baseModuleContext) TargetPrimary() bool {
return b.targetPrimary
}

func (b *baseModuleContext) MultiTargets() []Target {
return b.multiTargets
}

func (b *baseModuleContext) Arch() Arch {
return b.target.Arch
}

func (b *baseModuleContext) Os() OsType {
return b.os
}

func (b *baseModuleContext) Host() bool {
return b.os.Class == Host
}

func (b *baseModuleContext) Device() bool {
return b.os.Class == Device
}

func (b *baseModuleContext) Darwin() bool {
return b.os == Darwin
}

func (b *baseModuleContext) Windows() bool {
return b.os == Windows
}

func (b *baseModuleContext) PrimaryArch() bool {
if len(b.config.Targets[b.target.Os]) <= 1 {
return true
}
return b.target.Arch.ArchType == b.config.Targets[b.target.Os][0].Arch.ArchType
}
25 changes: 21 additions & 4 deletions android/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -1641,14 +1641,31 @@ func (m *ModuleBase) earlyModuleContextFactory(ctx blueprint.EarlyModuleContext)
func (m *ModuleBase) baseModuleContextFactory(ctx blueprint.BaseModuleContext) baseModuleContext {
return baseModuleContext{
bp: ctx,
archModuleContext: m.archModuleContextFactory(ctx),
earlyModuleContext: m.earlyModuleContextFactory(ctx),
os: m.commonProperties.CompileOS,
target: m.commonProperties.CompileTarget,
targetPrimary: m.commonProperties.CompilePrimary,
multiTargets: m.commonProperties.CompileMultiTargets,
}
}

func (m *ModuleBase) archModuleContextFactory(ctx blueprint.IncomingTransitionContext) archModuleContext {
config := ctx.Config().(Config)
target := m.Target()
primaryArch := false
if len(config.Targets[target.Os]) <= 1 {
primaryArch = true
} else {
primaryArch = target.Arch.ArchType == config.Targets[target.Os][0].Arch.ArchType
}

return archModuleContext{
os: m.commonProperties.CompileOS,
target: m.commonProperties.CompileTarget,
targetPrimary: m.commonProperties.CompilePrimary,
multiTargets: m.commonProperties.CompileMultiTargets,
primaryArch: primaryArch,
}

}

func (m *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) {
ctx := &moduleContext{
module: m.module,
Expand Down
61 changes: 47 additions & 14 deletions android/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,25 +365,36 @@ func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.Bot
}

type IncomingTransitionContext interface {
ArchModuleContext

// Module returns the target of the dependency edge for which the transition
// is being computed
Module() Module

// Config returns the configuration for the build.
Config() Config

DeviceConfig() DeviceConfig
}

type OutgoingTransitionContext interface {
ArchModuleContext

// Module returns the target of the dependency edge for which the transition
// is being computed
Module() Module

// DepTag() Returns the dependency tag through which this dependency is
// reached
DepTag() blueprint.DependencyTag

// Config returns the configuration for the build.
Config() Config

DeviceConfig() DeviceConfig
}

// Transition mutators implement a top-down mechanism where a module tells its
// TransitionMutator implements a top-down mechanism where a module tells its
// direct dependencies what variation they should be built in but the dependency
// has the final say.
//
Expand Down Expand Up @@ -448,18 +459,18 @@ type TransitionMutator interface {
// called on.
Split(ctx BaseModuleContext) []string

// Called on a module to determine which variation it wants from its direct
// dependencies. The dependency itself can override this decision. This method
// should not mutate the module itself.
// OutgoingTransition is called on a module to determine which variation it wants
// from its direct dependencies. The dependency itself can override this decision.
// This method should not mutate the module itself.
OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string

// Called on a module to determine which variation it should be in based on
// the variation modules that depend on it want. This gives the module a final
// say about its own variations. This method should not mutate the module
// IncomingTransition is called on a module to determine which variation it should
// be in based on the variation modules that depend on it want. This gives the module
// a final say about its own variations. This method should not mutate the module
// itself.
IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string

// Called after a module was split into multiple variations on each variation.
// Mutate is called after a module was split into multiple variations on each variation.
// It should not split the module any further but adding new dependencies is
// fine. Unlike all the other methods on TransitionMutator, this method is
// allowed to mutate the module.
Expand All @@ -481,6 +492,7 @@ func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []stri
}

type outgoingTransitionContextImpl struct {
archModuleContext
bp blueprint.OutgoingTransitionContext
}

Expand All @@ -492,15 +504,28 @@ func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag {
return c.bp.DepTag()
}

func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
if _, ok := ctx.Module().(Module); ok {
return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation)
func (c *outgoingTransitionContextImpl) Config() Config {
return c.bp.Config().(Config)
}

func (c *outgoingTransitionContextImpl) DeviceConfig() DeviceConfig {
return DeviceConfig{c.bp.Config().(Config).deviceConfig}
}

func (a *androidTransitionMutator) OutgoingTransition(bpctx blueprint.OutgoingTransitionContext, sourceVariation string) string {
if m, ok := bpctx.Module().(Module); ok {
ctx := &outgoingTransitionContextImpl{
archModuleContext: m.base().archModuleContextFactory(bpctx),
bp: bpctx,
}
return a.mutator.OutgoingTransition(ctx, sourceVariation)
} else {
return ""
}
}

type incomingTransitionContextImpl struct {
archModuleContext
bp blueprint.IncomingTransitionContext
}

Expand All @@ -512,9 +537,17 @@ func (c *incomingTransitionContextImpl) Config() Config {
return c.bp.Config().(Config)
}

func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string {
if _, ok := ctx.Module().(Module); ok {
return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation)
func (c *incomingTransitionContextImpl) DeviceConfig() DeviceConfig {
return DeviceConfig{c.bp.Config().(Config).deviceConfig}
}

func (a *androidTransitionMutator) IncomingTransition(bpctx blueprint.IncomingTransitionContext, incomingVariation string) string {
if m, ok := bpctx.Module().(Module); ok {
ctx := &incomingTransitionContextImpl{
archModuleContext: m.base().archModuleContextFactory(bpctx),
bp: bpctx,
}
return a.mutator.IncomingTransition(ctx, incomingVariation)
} else {
return ""
}
Expand Down
Loading

0 comments on commit c50999e

Please sign in to comment.