Generate module targets per-variant
Previously, the module target was only generated for the final varinat,
but it visited all its other variants to get output files from them.
This was mostly fine, except for that now there's ctx.Device() and
ctx.Target().HostCross checks in it. For a module with both device and
host variants, only one of these checks would apply to all variants
of the device, leading to missing -target or -host phonies.
Instead, just create the phonies on all variants. We'll rely on the
fact that phonies are merged/deduped, so saying `m foo` will still
build all variants of foo. But now `m foo-host` or `m foo-target`
will correctly only build the host or target variants.
Fixes: 400503755
Test: atest CtsCompilationTestCases:android.compilation.cts.CompilationTest on a soong-only build
Change-Id: I4896a6cb0461b373ec1853809fa862f9b852096c
diff --git a/android/module.go b/android/module.go
index 405573c..7786f13 100644
--- a/android/module.go
+++ b/android/module.go
@@ -1654,38 +1654,10 @@
}
+// generateModuleTarget generates phony targets so that you can do `m <module-name>`.
+// It will be run on every variant of the module, so it relies on the fact that phony targets
+// are deduped to merge all the deps from different variants together.
func (m *ModuleBase) generateModuleTarget(ctx *moduleContext) {
- var allInstalledFiles InstallPaths
- var allCheckbuildTargets Paths
- var alloutputFiles Paths
- ctx.VisitAllModuleVariantProxies(func(module ModuleProxy) {
- var checkbuildTarget Path
- var uncheckedModule bool
- var skipAndroidMkProcessing bool
- if EqualModules(m.module, module) {
- allInstalledFiles = append(allInstalledFiles, ctx.installFiles...)
- checkbuildTarget = ctx.checkbuildTarget
- uncheckedModule = ctx.uncheckedModule
- skipAndroidMkProcessing = shouldSkipAndroidMkProcessing(ctx, m)
- } else {
- info := OtherModuleProviderOrDefault(ctx, module, InstallFilesProvider)
- allInstalledFiles = append(allInstalledFiles, info.InstallFiles...)
- checkbuildTarget = info.CheckbuildTarget
- uncheckedModule = info.UncheckedModule
- skipAndroidMkProcessing = OtherModuleProviderOrDefault(ctx, module, CommonModuleInfoKey).SkipAndroidMkProcessing
- }
- if outputFiles, err := outputFilesForModule(ctx, module, ""); err == nil {
- alloutputFiles = append(alloutputFiles, outputFiles...)
- }
- // A module's -checkbuild phony targets should
- // not be created if the module is not exported to make.
- // Those could depend on the build target and fail to compile
- // for the current build target.
- if (!ctx.Config().KatiEnabled() || !skipAndroidMkProcessing) && !uncheckedModule && checkbuildTarget != nil {
- allCheckbuildTargets = append(allCheckbuildTargets, checkbuildTarget)
- }
- })
-
var namespacePrefix string
nameSpace := ctx.Namespace().Path
if nameSpace != "." {
@@ -1693,24 +1665,28 @@
}
var deps Paths
- var info FinalModuleBuildTargetsInfo
+ var info ModuleBuildTargetsInfo
- if len(allInstalledFiles) > 0 {
+ if len(ctx.installFiles) > 0 {
name := namespacePrefix + ctx.ModuleName() + "-install"
- ctx.Phony(name, allInstalledFiles.Paths()...)
+ ctx.Phony(name, ctx.installFiles.Paths()...)
info.InstallTarget = PathForPhony(ctx, name)
deps = append(deps, info.InstallTarget)
}
- if len(allCheckbuildTargets) > 0 {
+ // A module's -checkbuild phony targets should
+ // not be created if the module is not exported to make.
+ // Those could depend on the build target and fail to compile
+ // for the current build target.
+ if (!ctx.Config().KatiEnabled() || !shouldSkipAndroidMkProcessing(ctx, m)) && !ctx.uncheckedModule && ctx.checkbuildTarget != nil {
name := namespacePrefix + ctx.ModuleName() + "-checkbuild"
- ctx.Phony(name, allCheckbuildTargets...)
+ ctx.Phony(name, ctx.checkbuildTarget)
deps = append(deps, PathForPhony(ctx, name))
}
- if len(alloutputFiles) > 0 {
+ if outputFiles, err := outputFilesForModule(ctx, ctx.Module(), ""); err == nil && len(outputFiles) > 0 {
name := namespacePrefix + ctx.ModuleName() + "-outputs"
- ctx.Phony(name, alloutputFiles...)
+ ctx.Phony(name, outputFiles...)
deps = append(deps, PathForPhony(ctx, name))
}
@@ -1734,7 +1710,7 @@
}
info.BlueprintDir = ctx.ModuleDir()
- SetProvider(ctx, FinalModuleBuildTargetsProvider, info)
+ SetProvider(ctx, ModuleBuildTargetsProvider, info)
}
}
@@ -1876,15 +1852,15 @@
var SourceFilesInfoProvider = blueprint.NewProvider[SourceFilesInfo]()
-// FinalModuleBuildTargetsInfo is used by buildTargetSingleton to create checkbuild and
-// per-directory build targets. Only set on the final variant of each module
-type FinalModuleBuildTargetsInfo struct {
+// ModuleBuildTargetsInfo is used by buildTargetSingleton to create checkbuild and
+// per-directory build targets.
+type ModuleBuildTargetsInfo struct {
InstallTarget WritablePath
CheckbuildTarget WritablePath
BlueprintDir string
}
-var FinalModuleBuildTargetsProvider = blueprint.NewProvider[FinalModuleBuildTargetsInfo]()
+var ModuleBuildTargetsProvider = blueprint.NewProvider[ModuleBuildTargetsInfo]()
type CommonModuleInfo struct {
Enabled bool
@@ -2151,14 +2127,19 @@
}
if sourceFileProducer, ok := m.module.(SourceFileProducer); ok {
+ srcs := sourceFileProducer.Srcs()
+ for _, src := range srcs {
+ if src == nil {
+ ctx.ModuleErrorf("SourceFileProducer cannot return nil srcs")
+ return
+ }
+ }
SetProvider(ctx, SourceFilesInfoProvider, SourceFilesInfo{Srcs: sourceFileProducer.Srcs()})
}
- if ctx.IsFinalModule(m.module) {
- m.generateModuleTarget(ctx)
- if ctx.Failed() {
- return
- }
+ m.generateModuleTarget(ctx)
+ if ctx.Failed() {
+ return
}
ctx.TransitiveInstallFiles = depset.New[InstallPath](depset.TOPOLOGICAL, ctx.installFiles, dependencyInstallFiles)
@@ -3098,7 +3079,7 @@
modulesInDir := make(map[string]Paths)
ctx.VisitAllModuleProxies(func(module ModuleProxy) {
- info := OtherModuleProviderOrDefault(ctx, module, FinalModuleBuildTargetsProvider)
+ info := OtherModuleProviderOrDefault(ctx, module, ModuleBuildTargetsProvider)
if info.CheckbuildTarget != nil {
checkbuildDeps = append(checkbuildDeps, info.CheckbuildTarget)
diff --git a/android/module_context.go b/android/module_context.go
index fb62e67..0a23a74 100644
--- a/android/module_context.go
+++ b/android/module_context.go
@@ -456,6 +456,11 @@
}
func (m *moduleContext) Phony(name string, deps ...Path) {
+ for _, dep := range deps {
+ if dep == nil {
+ panic("Phony dep cannot be nil")
+ }
+ }
m.phonies[name] = append(m.phonies[name], deps...)
}
@@ -748,6 +753,9 @@
m.packageFile(fullInstallPath, srcPath, executable, m.requiresFullInstall())
if checkbuild {
+ if srcPath == nil {
+ panic("srcPath cannot be nil")
+ }
m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
}
@@ -879,6 +887,11 @@
// CheckbuildFile specifies the output files that should be built by checkbuild.
func (m *moduleContext) CheckbuildFile(srcPaths ...Path) {
+ for _, srcPath := range srcPaths {
+ if srcPath == nil {
+ panic("CheckbuildFile() files cannot be nil")
+ }
+ }
m.checkbuildFiles = append(m.checkbuildFiles, srcPaths...)
}
diff --git a/android/prebuilt_test.go b/android/prebuilt_test.go
index 27a68fb..d31fc4f 100644
--- a/android/prebuilt_test.go
+++ b/android/prebuilt_test.go
@@ -551,6 +551,9 @@
}
func (s *sourceModule) Srcs() Paths {
+ if s.src == nil {
+ return nil
+ }
return Paths{s.src}
}
diff --git a/android/test_suites_test.go b/android/test_suites_test.go
index dda9329..03aa424 100644
--- a/android/test_suites_test.go
+++ b/android/test_suites_test.go
@@ -108,7 +108,8 @@
func (f *fake_module) GenerateAndroidBuildActions(ctx ModuleContext) {
for _, output := range f.props.Outputs {
- ctx.InstallFile(pathForTestCases(ctx), output, nil)
+ f := PathForModuleOut(ctx, output)
+ ctx.InstallFile(pathForTestCases(ctx), output, f)
}
SetProvider(ctx, TestSuiteInfoProvider, TestSuiteInfo{
diff --git a/filesystem/avb_add_hash_footer.go b/filesystem/avb_add_hash_footer.go
index c1e03cb..327a41f 100644
--- a/filesystem/avb_add_hash_footer.go
+++ b/filesystem/avb_add_hash_footer.go
@@ -210,6 +210,9 @@
// Implements android.SourceFileProducer
func (a *avbAddHashFooter) Srcs() android.Paths {
+ if a.output == nil {
+ return nil
+ }
return append(android.Paths{}, a.output)
}