From e50fe61accf04dfd4aa431ca7256d67c423e7378 Mon Sep 17 00:00:00 2001 From: Jaewoong Jung Date: Thu, 11 Jun 2020 10:37:51 -0700 Subject: [PATCH] Make override modules compatible with prebuilts. Prebuilt's prefer flag doesn't work against override modules because override modules are sort of virtual modules that delegate all the work to their base modules. Therefore, even if a prebuilt module suppresses installation of its src-counterpart override module, the actual build actions are still performed in the base module. This change fixes it by filtering out override modules that are being replaced by prebuilts. Test: prebuilt_test.go Fixes: 152155285 Change-Id: I859b35c0629b2b6258dd1ec5e020ba2c77ff9612 Merged-In: I859b35c0629b2b6258dd1ec5e020ba2c77ff9612 --- android/override_module.go | 16 +++++++- android/prebuilt_test.go | 78 ++++++++++++++++++++++++++++++++++---- 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/android/override_module.go b/android/override_module.go index 9f5127dc3..7e58890d7 100644 --- a/android/override_module.go +++ b/android/override_module.go @@ -208,7 +208,21 @@ var overrideBaseDepTag overrideBaseDependencyTag // next phase. func overrideModuleDepsMutator(ctx BottomUpMutatorContext) { if module, ok := ctx.Module().(OverrideModule); ok { - ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base) + // Skip this overriding module if there's a prebuilt module that overrides it with prefer flag. + overriddenByPrebuilt := false + ctx.VisitDirectDepsWithTag(PrebuiltDepTag, func(dep Module) { + prebuilt, ok := dep.(PrebuiltInterface) + if !ok { + panic("PrebuiltDepTag leads to a non-prebuilt module " + dep.Name()) + } + if prebuilt.Prebuilt().UsePrebuilt() { + overriddenByPrebuilt = true + return + } + }) + if !overriddenByPrebuilt { + ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base) + } } } diff --git a/android/prebuilt_test.go b/android/prebuilt_test.go index 8ff5c4034..e8c5121f0 100644 --- a/android/prebuilt_test.go +++ b/android/prebuilt_test.go @@ -60,7 +60,7 @@ var prebuiltsTests = []struct { source { name: "bar", } - + prebuilt { name: "bar", prefer: false, @@ -74,7 +74,7 @@ var prebuiltsTests = []struct { source { name: "bar", } - + prebuilt { name: "bar", prefer: true, @@ -88,7 +88,7 @@ var prebuiltsTests = []struct { source { name: "bar", } - + prebuilt { name: "bar", prefer: false, @@ -101,7 +101,7 @@ var prebuiltsTests = []struct { source { name: "bar", } - + prebuilt { name: "bar", prefer: true, @@ -122,6 +122,44 @@ var prebuiltsTests = []struct { }`, prebuilt: true, }, + { + name: "prebuilt override not preferred", + modules: ` + source { + name: "baz", + } + + override_source { + name: "bar", + base: "baz", + } + + prebuilt { + name: "bar", + prefer: false, + srcs: ["prebuilt_file"], + }`, + prebuilt: false, + }, + { + name: "prebuilt override preferred", + modules: ` + source { + name: "baz", + } + + override_source { + name: "bar", + base: "baz", + } + + prebuilt { + name: "bar", + prefer: true, + srcs: ["prebuilt_file"], + }`, + prebuilt: true, + }, } func TestPrebuilts(t *testing.T) { @@ -213,8 +251,10 @@ func TestPrebuilts(t *testing.T) { func registerTestPrebuiltBuildComponents(ctx RegistrationContext) { ctx.RegisterModuleType("prebuilt", newPrebuiltModule) ctx.RegisterModuleType("source", newSourceModule) + ctx.RegisterModuleType("override_source", newOverrideSourceModule) RegisterPrebuiltMutators(ctx) + ctx.PostDepsMutators(RegisterOverridePostDepsMutators) } type prebuiltModule struct { @@ -257,11 +297,15 @@ func (p *prebuiltModule) OutputFiles(tag string) (Paths, error) { } } +type sourceModuleProperties struct { + Deps []string `android:"path"` +} + type sourceModule struct { ModuleBase - properties struct { - Deps []string `android:"path"` - } + OverridableModuleBase + + properties sourceModuleProperties dependsOnSourceModule, dependsOnPrebuiltModule bool deps Paths src Path @@ -271,10 +315,11 @@ func newSourceModule() Module { m := &sourceModule{} m.AddProperties(&m.properties) InitAndroidModule(m) + InitOverridableModule(m, nil) return m } -func (s *sourceModule) DepsMutator(ctx BottomUpMutatorContext) { +func (s *sourceModule) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) { // s.properties.Deps are annotated with android:path, so they are // automatically added to the dependency by pathDeps mutator } @@ -287,3 +332,20 @@ func (s *sourceModule) GenerateAndroidBuildActions(ctx ModuleContext) { func (s *sourceModule) Srcs() Paths { return Paths{s.src} } + +type overrideSourceModule struct { + ModuleBase + OverrideModuleBase +} + +func (o *overrideSourceModule) GenerateAndroidBuildActions(_ ModuleContext) { +} + +func newOverrideSourceModule() Module { + m := &overrideSourceModule{} + m.AddProperties(&sourceModuleProperties{}) + + InitAndroidModule(m) + InitOverrideModule(m) + return m +}