From 0523b8f1faec114e3c3eeefe02aa3a6988ffb8b9 Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Mon, 3 Mar 2025 15:11:32 -0800 Subject: 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 --- android/module.go | 79 +++++++++++++++++++++---------------------------------- 1 file changed, 30 insertions(+), 49 deletions(-) (limited to 'android/module.go') diff --git a/android/module.go b/android/module.go index 405573c1b..7786f13ee 100644 --- a/android/module.go +++ b/android/module.go @@ -1654,38 +1654,10 @@ func (m *ModuleBase) generateVariantTarget(ctx *moduleContext) { } +// generateModuleTarget generates phony targets so that you can do `m `. +// 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 @@ func (m *ModuleBase) generateModuleTarget(ctx *moduleContext) { } 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 @@ func (m *ModuleBase) generateModuleTarget(ctx *moduleContext) { } info.BlueprintDir = ctx.ModuleDir() - SetProvider(ctx, FinalModuleBuildTargetsProvider, info) + SetProvider(ctx, ModuleBuildTargetsProvider, info) } } @@ -1876,15 +1852,15 @@ type SourceFilesInfo struct { 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 @@ func (m *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext) } 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 @@ func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) { 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) -- cgit v1.2.3-59-g8ed1b