From 67a6d702d1f1a7110d56d8fe33209a17d794476a Mon Sep 17 00:00:00 2001 From: Bob Badour Date: Mon, 16 May 2022 12:28:06 -0700 Subject: [PATCH] Allow deferred module build action generation. Provide a means to generate build actions for modules from within the later singleton context. Allows modules to depend on the metadata for arbitrary modules without causing dependency cycles. Care needs to be taken to establish all metadata during the normal module GenerateBuildAction to avoid synchronization issues and only use read-only access to modules from the singleton context. Bug: 213388645 Change-Id: I82ed218926b1d8fbe2edde6d55c4bf40ea0d8618 --- singleton_ctx.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/singleton_ctx.go b/singleton_ctx.go index 455f6fc..d579b8e 100644 --- a/singleton_ctx.go +++ b/singleton_ctx.go @@ -157,6 +157,10 @@ type SingletonContext interface { // Fs returns a pathtools.Filesystem that can be used to interact with files. Using the Filesystem interface allows // the singleton to be used in build system tests that run against a mock filesystem. Fs() pathtools.FileSystem + + // ModuleVariantsFromName returns the list of module variants named `name` in the same namespace as `referer`. + // Allows generating build actions for `referer` based on the metadata for `name` deferred until the singleton context. + ModuleVariantsFromName(referer Module, name string) []Module } var _ SingletonContext = (*singletonContext)(nil) @@ -369,3 +373,26 @@ func (s *singletonContext) GlobWithDeps(pattern string, func (s *singletonContext) Fs() pathtools.FileSystem { return s.context.fs } + +func (s *singletonContext) ModuleVariantsFromName(referer Module, name string) []Module { + c := s.context + + refererInfo := c.moduleInfo[referer] + if refererInfo == nil { + s.ModuleErrorf(referer, "could not find module %q", referer.Name()) + return nil + } + + moduleGroup, exists := c.nameInterface.ModuleFromName(name, refererInfo.namespace()) + if !exists { + return nil + } + result := make([]Module, 0, len(moduleGroup.modules)) + for _, module := range moduleGroup.modules { + moduleInfo := module.module() + if moduleInfo != nil { + result = append(result, moduleInfo.logicModule) + } + } + return result +}