From 5e70805690188d4c9e7fe1272614d7d894061b2e Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Tue, 6 Aug 2019 13:59:50 -0700 Subject: [PATCH] Add helper functions for getting output files from modules Add OutputFileForModule and OutputFilesForModules to get paths from OutputFileProducers. Bug: 122332855 Test: m checkbuild Change-Id: I2e9aeeb55987742077250390cdb804ba22e79442 --- android/module.go | 43 +++++++++++++++++++++++++++++++++++++++++++ android/paths.go | 9 +++++++++ 2 files changed, 52 insertions(+) diff --git a/android/module.go b/android/module.go index ffcbf329f..a14e57556 100644 --- a/android/module.go +++ b/android/module.go @@ -1803,6 +1803,49 @@ type OutputFileProducer interface { OutputFiles(tag string) (Paths, error) } +// OutputFilesForModule returns the paths from an OutputFileProducer with the given tag. On error, including if the +// module produced zero paths, it reports errors to the ctx and returns nil. +func OutputFilesForModule(ctx PathContext, module blueprint.Module, tag string) Paths { + paths, err := outputFilesForModule(ctx, module, tag) + if err != nil { + reportPathError(ctx, err) + return nil + } + return paths +} + +// OutputFileForModule returns the path from an OutputFileProducer with the given tag. On error, including if the +// module produced zero or multiple paths, it reports errors to the ctx and returns nil. +func OutputFileForModule(ctx PathContext, module blueprint.Module, tag string) Path { + paths, err := outputFilesForModule(ctx, module, tag) + if err != nil { + reportPathError(ctx, err) + return nil + } + if len(paths) > 1 { + reportPathErrorf(ctx, "got multiple output files from module %q, expected exactly one", + pathContextName(ctx, module)) + return nil + } + return paths[0] +} + +func outputFilesForModule(ctx PathContext, module blueprint.Module, tag string) (Paths, error) { + if outputFileProducer, ok := module.(OutputFileProducer); ok { + paths, err := outputFileProducer.OutputFiles(tag) + if err != nil { + return nil, fmt.Errorf("failed to get output file from module %q: %s", + pathContextName(ctx, module), err.Error()) + } + if len(paths) == 0 { + return nil, fmt.Errorf("failed to get output files from module %q", pathContextName(ctx, module)) + } + return paths, nil + } else { + return nil, fmt.Errorf("module %q is not an OutputFileProducer", pathContextName(ctx, module)) + } +} + type HostToolProvider interface { HostToolPath() OptionalPath } diff --git a/android/paths.go b/android/paths.go index a04dc6bf2..d181b75b4 100644 --- a/android/paths.go +++ b/android/paths.go @@ -89,6 +89,15 @@ func reportPathErrorf(ctx PathContext, format string, args ...interface{}) { } } +func pathContextName(ctx PathContext, module blueprint.Module) string { + if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok { + return x.ModuleName(module) + } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok { + return x.OtherModuleName(module) + } + return "unknown" +} + type Path interface { // Returns the path in string form String() string