Fix genrule tool dependencies when a prebuilt tool is preferred.

Since the genrule tool dep mutator runs after the prebuilt mutators,
this adds a helper function android.PrebuiltGetPreferred to resolve the
source or prebuilt as appropriate.

Test: m SOONG_CONFIG_art_module_source_build=false droid
  in internal
Bug: 214292395
Change-Id: I1a208fd048b998f9f19ad1f45d8389decda2cb9e
This commit is contained in:
Martin Stjernholm
2022-01-12 23:18:30 +00:00
parent ed4900f9d9
commit dbd814d44e
3 changed files with 155 additions and 0 deletions

View File

@@ -309,6 +309,54 @@ func GetEmbeddedPrebuilt(module Module) *Prebuilt {
return nil
}
// PrebuiltGetPreferred returns the module that is preferred for the given
// module. That is either the module itself or the prebuilt counterpart that has
// taken its place. The given module must be a direct dependency of the current
// context module, and it must be the source module if both source and prebuilt
// exist.
//
// This function is for use on dependencies after PrebuiltPostDepsMutator has
// run - any dependency that is registered before that will already reference
// the right module. This function is only safe to call after all mutators that
// may call CreateVariations, e.g. in GenerateAndroidBuildActions.
func PrebuiltGetPreferred(ctx BaseModuleContext, module Module) Module {
if !module.IsReplacedByPrebuilt() {
return module
}
if IsModulePrebuilt(module) {
// If we're given a prebuilt then assume there's no source module around.
return module
}
sourceModDepFound := false
var prebuiltMod Module
ctx.WalkDeps(func(child, parent Module) bool {
if prebuiltMod != nil {
return false
}
if parent == ctx.Module() {
// First level: Only recurse if the module is found as a direct dependency.
sourceModDepFound = child == module
return sourceModDepFound
}
// Second level: Follow PrebuiltDepTag to the prebuilt.
if t := ctx.OtherModuleDependencyTag(child); t == PrebuiltDepTag {
prebuiltMod = child
}
return false
})
if prebuiltMod == nil {
if !sourceModDepFound {
panic(fmt.Errorf("Failed to find source module as a direct dependency: %s", module))
} else {
panic(fmt.Errorf("Failed to find prebuilt for source module: %s", module))
}
}
return prebuiltMod
}
func RegisterPrebuiltsPreArchMutators(ctx RegisterMutatorsContext) {
ctx.BottomUp("prebuilt_rename", PrebuiltRenameMutator).Parallel()
}