Update outputFilesForModuleFromProvider

This CL includes following changes:
1. Added the ability to differentiate the cases that module never
   sets OutputFilesProvider and that module sets the provider with
   a nil value.
2. Updated GenerateTaggedDistFiles to use outputFilesForModuleFromProvider.
3. Updated on cc module to use OutputFilesProvider.

Test: CI
Bug: 339477385
Change-Id: Ib5663a947315f6a90a81b7f073cf8dd22fbb1e05
This commit is contained in:
mrziwang
2024-06-18 12:43:41 -07:00
parent 94a6824c99
commit abdb293492
6 changed files with 68 additions and 72 deletions

View File

@@ -1234,17 +1234,28 @@ func (m *ModuleBase) GenerateTaggedDistFiles(ctx BaseModuleContext) TaggedDistFi
// the special tag name which represents that. // the special tag name which represents that.
tag := proptools.StringDefault(dist.Tag, DefaultDistTag) tag := proptools.StringDefault(dist.Tag, DefaultDistTag)
distFileForTagFromProvider, err := outputFilesForModuleFromProvider(ctx, m.module, tag)
if err != OutputFilesProviderNotSet {
if err != nil && tag != DefaultDistTag {
ctx.PropertyErrorf("dist.tag", "%s", err.Error())
} else {
distFiles = distFiles.addPathsForTag(tag, distFileForTagFromProvider...)
continue
}
}
// if the tagged dist file cannot be obtained from OutputFilesProvider,
// fall back to use OutputFileProducer
// TODO: remove this part after OutputFilesProvider fully replaces OutputFileProducer
if outputFileProducer, ok := m.module.(OutputFileProducer); ok { if outputFileProducer, ok := m.module.(OutputFileProducer); ok {
// Call the OutputFiles(tag) method to get the paths associated with the tag. // Call the OutputFiles(tag) method to get the paths associated with the tag.
distFilesForTag, err := outputFileProducer.OutputFiles(tag) distFilesForTag, err := outputFileProducer.OutputFiles(tag)
// If the tag was not supported and is not DefaultDistTag then it is an error. // If the tag was not supported and is not DefaultDistTag then it is an error.
// Failing to find paths for DefaultDistTag is not an error. It just means // Failing to find paths for DefaultDistTag is not an error. It just means
// that the module type requires the legacy behavior. // that the module type requires the legacy behavior.
if err != nil && tag != DefaultDistTag { if err != nil && tag != DefaultDistTag {
ctx.PropertyErrorf("dist.tag", "%s", err.Error()) ctx.PropertyErrorf("dist.tag", "%s", err.Error())
} }
distFiles = distFiles.addPathsForTag(tag, distFilesForTag...) distFiles = distFiles.addPathsForTag(tag, distFilesForTag...)
} else if tag != DefaultDistTag { } else if tag != DefaultDistTag {
// If the tag was specified then it is an error if the module does not // If the tag was specified then it is an error if the module does not
@@ -2513,7 +2524,7 @@ func OutputFileForModule(ctx PathContext, module blueprint.Module, tag string) P
func outputFilesForModule(ctx PathContext, module blueprint.Module, tag string) (Paths, error) { func outputFilesForModule(ctx PathContext, module blueprint.Module, tag string) (Paths, error) {
outputFilesFromProvider, err := outputFilesForModuleFromProvider(ctx, module, tag) outputFilesFromProvider, err := outputFilesForModuleFromProvider(ctx, module, tag)
if outputFilesFromProvider != nil || err != nil { if outputFilesFromProvider != nil || err != OutputFilesProviderNotSet {
return outputFilesFromProvider, err return outputFilesFromProvider, err
} }
if outputFileProducer, ok := module.(OutputFileProducer); ok { if outputFileProducer, ok := module.(OutputFileProducer); ok {
@@ -2541,38 +2552,38 @@ func outputFilesForModule(ctx PathContext, module blueprint.Module, tag string)
// reading OutputFilesProvider before GenerateBuildActions is finished. // reading OutputFilesProvider before GenerateBuildActions is finished.
// If a module doesn't have the OutputFilesProvider, nil is returned. // If a module doesn't have the OutputFilesProvider, nil is returned.
func outputFilesForModuleFromProvider(ctx PathContext, module blueprint.Module, tag string) (Paths, error) { func outputFilesForModuleFromProvider(ctx PathContext, module blueprint.Module, tag string) (Paths, error) {
var outputFilesProvider OutputFilesInfo var outputFiles OutputFilesInfo
fromProperty := false
if mctx, isMctx := ctx.(ModuleContext); isMctx { if mctx, isMctx := ctx.(ModuleContext); isMctx {
if mctx.Module() != module { if mctx.Module() != module {
outputFilesProvider, _ = OtherModuleProvider(mctx, module, OutputFilesProvider) outputFiles, _ = OtherModuleProvider(mctx, module, OutputFilesProvider)
} else { } else {
if tag == "" { outputFiles = mctx.Module().base().outputFiles
return mctx.Module().base().outputFiles.DefaultOutputFiles, nil fromProperty = true
} else if taggedOutputFiles, hasTag := mctx.Module().base().outputFiles.TaggedOutputFiles[tag]; hasTag {
return taggedOutputFiles, nil
} else {
return nil, fmt.Errorf("unsupported tag %q for module getting its own output files", tag)
}
} }
} else if cta, isCta := ctx.(*singletonContextAdaptor); isCta { } else if cta, isCta := ctx.(*singletonContextAdaptor); isCta {
providerData, _ := cta.moduleProvider(module, OutputFilesProvider) providerData, _ := cta.moduleProvider(module, OutputFilesProvider)
outputFilesProvider, _ = providerData.(OutputFilesInfo) outputFiles, _ = providerData.(OutputFilesInfo)
} }
// TODO: Add a check for skipped context // TODO: Add a check for skipped context
if !outputFilesProvider.isEmpty() { if outputFiles.isEmpty() {
// TODO: Add a check for param module not having OutputFilesProvider set
return nil, OutputFilesProviderNotSet
}
if tag == "" { if tag == "" {
return outputFilesProvider.DefaultOutputFiles, nil return outputFiles.DefaultOutputFiles, nil
} else if taggedOutputFiles, hasTag := outputFilesProvider.TaggedOutputFiles[tag]; hasTag { } else if taggedOutputFiles, hasTag := outputFiles.TaggedOutputFiles[tag]; hasTag {
return taggedOutputFiles, nil return taggedOutputFiles, nil
} else {
if fromProperty {
return nil, fmt.Errorf("unsupported tag %q for module getting its own output files", tag)
} else { } else {
return nil, fmt.Errorf("unsupported module reference tag %q", tag) return nil, fmt.Errorf("unsupported module reference tag %q", tag)
} }
} }
// TODO: Add a check for param module not having OutputFilesProvider set
return nil, nil
} }
func (o OutputFilesInfo) isEmpty() bool { func (o OutputFilesInfo) isEmpty() bool {
@@ -2589,6 +2600,9 @@ type OutputFilesInfo struct {
var OutputFilesProvider = blueprint.NewProvider[OutputFilesInfo]() var OutputFilesProvider = blueprint.NewProvider[OutputFilesInfo]()
// This is used to mark the case where OutputFilesProvider is not set on some modules.
var OutputFilesProviderNotSet = fmt.Errorf("No output files from provider")
// Modules can implement HostToolProvider and return a valid OptionalPath from HostToolPath() to // Modules can implement HostToolProvider and return a valid OptionalPath from HostToolPath() to
// specify that they can be used as a tool by a genrule module. // specify that they can be used as a tool by a genrule module.
type HostToolProvider interface { type HostToolProvider interface {

View File

@@ -1025,9 +1025,12 @@ func (m TestingModule) VariablesForTestsRelativeToTop() map[string]string {
// otherwise returns the result of calling Paths.RelativeToTop // otherwise returns the result of calling Paths.RelativeToTop
// on the returned Paths. // on the returned Paths.
func (m TestingModule) OutputFiles(t *testing.T, tag string) Paths { func (m TestingModule) OutputFiles(t *testing.T, tag string) Paths {
// TODO: add non-empty-string tag case and remove OutputFileProducer part // TODO: remove OutputFileProducer part
if tag == "" && m.module.base().outputFiles.DefaultOutputFiles != nil { outputFiles := m.Module().base().outputFiles
return m.module.base().outputFiles.DefaultOutputFiles.RelativeToTop() if tag == "" && outputFiles.DefaultOutputFiles != nil {
return outputFiles.DefaultOutputFiles.RelativeToTop()
} else if taggedOutputFiles, hasTag := outputFiles.TaggedOutputFiles[tag]; hasTag {
return taggedOutputFiles
} }
producer, ok := m.module.(OutputFileProducer) producer, ok := m.module.(OutputFileProducer)

View File

@@ -451,7 +451,7 @@ func (binary *binaryDecorator) unstrippedOutputFilePath() android.Path {
} }
func (binary *binaryDecorator) strippedAllOutputFilePath() android.Path { func (binary *binaryDecorator) strippedAllOutputFilePath() android.Path {
panic("Not implemented.") return nil
} }
func (binary *binaryDecorator) setSymlinkList(ctx ModuleContext) { func (binary *binaryDecorator) setSymlinkList(ctx ModuleContext) {

View File

@@ -2119,10 +2119,23 @@ func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
if c.Properties.IsSdkVariant && c.Properties.SdkAndPlatformVariantVisibleToMake { if c.Properties.IsSdkVariant && c.Properties.SdkAndPlatformVariantVisibleToMake {
moduleInfoJSON.Uninstallable = true moduleInfoJSON.Uninstallable = true
} }
} }
buildComplianceMetadataInfo(ctx, c, deps) buildComplianceMetadataInfo(ctx, c, deps)
c.setOutputFiles(ctx)
}
func (c *Module) setOutputFiles(ctx ModuleContext) {
if c.outputFile.Valid() {
ctx.SetOutputFiles(android.Paths{c.outputFile.Path()}, "")
} else {
ctx.SetOutputFiles(android.Paths{}, "")
}
if c.linker != nil {
ctx.SetOutputFiles(android.PathsIfNonNil(c.linker.unstrippedOutputFilePath()), "unstripped")
ctx.SetOutputFiles(android.PathsIfNonNil(c.linker.strippedAllOutputFilePath()), "stripped_all")
}
} }
func buildComplianceMetadataInfo(ctx ModuleContext, c *Module, deps PathDeps) { func buildComplianceMetadataInfo(ctx ModuleContext, c *Module, deps PathDeps) {
@@ -3615,28 +3628,6 @@ func (c *Module) IntermPathForModuleOut() android.OptionalPath {
return c.outputFile return c.outputFile
} }
func (c *Module) OutputFiles(tag string) (android.Paths, error) {
switch tag {
case "":
if c.outputFile.Valid() {
return android.Paths{c.outputFile.Path()}, nil
}
return android.Paths{}, nil
case "unstripped":
if c.linker != nil {
return android.PathsIfNonNil(c.linker.unstrippedOutputFilePath()), nil
}
return nil, nil
case "stripped_all":
if c.linker != nil {
return android.PathsIfNonNil(c.linker.strippedAllOutputFilePath()), nil
}
return nil, nil
default:
return nil, fmt.Errorf("unsupported module reference tag %q", tag)
}
}
func (c *Module) static() bool { func (c *Module) static() bool {
if static, ok := c.linker.(interface { if static, ok := c.linker.(interface {
static() bool static() bool

View File

@@ -300,13 +300,9 @@ func TestDataLibs(t *testing.T) {
config := TestConfig(t.TempDir(), android.Android, nil, bp, nil) config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
ctx := testCcWithConfig(t, config) ctx := testCcWithConfig(t, config)
module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module() testingModule := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon")
testBinary := module.(*Module).linker.(*testBinary) testBinary := testingModule.Module().(*Module).linker.(*testBinary)
outputFiles, err := module.(android.OutputFileProducer).OutputFiles("") outputFiles := testingModule.OutputFiles(t, "")
if err != nil {
t.Errorf("Expected cc_test to produce output files, error: %s", err)
return
}
if len(outputFiles) != 1 { if len(outputFiles) != 1 {
t.Errorf("expected exactly one output file. output files: [%s]", outputFiles) t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
return return
@@ -356,12 +352,10 @@ func TestDataLibsRelativeInstallPath(t *testing.T) {
config := TestConfig(t.TempDir(), android.Android, nil, bp, nil) config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
ctx := testCcWithConfig(t, config) ctx := testCcWithConfig(t, config)
module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module() testingModule := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon")
module := testingModule.Module()
testBinary := module.(*Module).linker.(*testBinary) testBinary := module.(*Module).linker.(*testBinary)
outputFiles, err := module.(android.OutputFileProducer).OutputFiles("") outputFiles := testingModule.OutputFiles(t, "")
if err != nil {
t.Fatalf("Expected cc_test to produce output files, error: %s", err)
}
if len(outputFiles) != 1 { if len(outputFiles) != 1 {
t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles) t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
} }
@@ -1407,12 +1401,10 @@ func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
config := TestConfig(t.TempDir(), android.Android, nil, bp, nil) config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
ctx := testCcWithConfig(t, config) ctx := testCcWithConfig(t, config)
module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module() testingModule := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon")
module := testingModule.Module()
testBinary := module.(*Module).linker.(*testBinary) testBinary := module.(*Module).linker.(*testBinary)
outputFiles, err := module.(android.OutputFileProducer).OutputFiles("") outputFiles := testingModule.OutputFiles(t, "")
if err != nil {
t.Fatalf("Expected cc_test to produce output files, error: %s", err)
}
if len(outputFiles) != 1 { if len(outputFiles) != 1 {
t.Errorf("expected exactly one output file. output files: [%s]", outputFiles) t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
} }
@@ -3118,12 +3110,8 @@ func TestStrippedAllOutputFile(t *testing.T) {
` `
config := TestConfig(t.TempDir(), android.Android, nil, bp, nil) config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
ctx := testCcWithConfig(t, config) ctx := testCcWithConfig(t, config)
module := ctx.ModuleForTests("test_lib", "android_arm_armv7-a-neon_shared").Module() testingModule := ctx.ModuleForTests("test_lib", "android_arm_armv7-a-neon_shared")
outputFile, err := module.(android.OutputFileProducer).OutputFiles("stripped_all") outputFile := testingModule.OutputFiles(t, "stripped_all")
if err != nil {
t.Errorf("Expected cc_library to produce output files, error: %s", err)
return
}
if !strings.HasSuffix(outputFile.Strings()[0], "/stripped_all/test_lib.so") { if !strings.HasSuffix(outputFile.Strings()[0], "/stripped_all/test_lib.so") {
t.Errorf("Unexpected output file: %s", outputFile.Strings()[0]) t.Errorf("Unexpected output file: %s", outputFile.Strings()[0])
return return

View File

@@ -220,7 +220,7 @@ func (object *objectLinker) unstrippedOutputFilePath() android.Path {
} }
func (object *objectLinker) strippedAllOutputFilePath() android.Path { func (object *objectLinker) strippedAllOutputFilePath() android.Path {
panic("Not implemented.") return nil
} }
func (object *objectLinker) nativeCoverage() bool { func (object *objectLinker) nativeCoverage() bool {