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
This commit is contained in:
Colin Cross 2019-08-06 13:59:50 -07:00
parent 7c7c11445b
commit 5e70805690
2 changed files with 52 additions and 0 deletions

View File

@ -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
}

View File

@ -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