Convert depsToPaths to use ModuleProxy for both cc and rust.
Bug: 377723687
Test: Unit tests and compare the ninja and mk files generated.
Change-Id: Id465f293c3615fc803b34c990f19b4386ebece1c
diff --git a/rust/rust.go b/rust/rust.go
index 246670f..ba6e293 100644
--- a/rust/rust.go
+++ b/rust/rust.go
@@ -34,6 +34,35 @@
var pctx = android.NewPackageContext("android/soong/rust")
+type LibraryInfo struct {
+ Rlib bool
+ Dylib bool
+}
+
+type CompilerInfo struct {
+ StdLinkageForDevice RustLinkage
+ StdLinkageForNonDevice RustLinkage
+ NoStdlibs bool
+ LibraryInfo *LibraryInfo
+}
+
+type ProtobufDecoratorInfo struct{}
+
+type SourceProviderInfo struct {
+ ProtobufDecoratorInfo *ProtobufDecoratorInfo
+}
+
+type RustInfo struct {
+ AndroidMkSuffix string
+ RustSubName string
+ TransitiveAndroidMkSharedLibs depset.DepSet[string]
+ CompilerInfo *CompilerInfo
+ SnapshotInfo *cc.SnapshotInfo
+ SourceProviderInfo *SourceProviderInfo
+}
+
+var RustInfoProvider = blueprint.NewProvider[*RustInfo]()
+
func init() {
android.RegisterModuleType("rust_defaults", defaultsFactory)
android.PreDepsMutators(registerPreDepsMutators)
@@ -996,9 +1025,44 @@
ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
}
- android.SetProvider(ctx, cc.LinkableInfoKey, cc.LinkableInfo{
- StaticExecutable: mod.StaticExecutable(),
- })
+ linkableInfo := cc.CreateCommonLinkableInfo(mod)
+ linkableInfo.Static = mod.Static()
+ linkableInfo.Shared = mod.Shared()
+ linkableInfo.CrateName = mod.CrateName()
+ linkableInfo.ExportedCrateLinkDirs = mod.ExportedCrateLinkDirs()
+ android.SetProvider(ctx, cc.LinkableInfoProvider, linkableInfo)
+
+ rustInfo := &RustInfo{
+ AndroidMkSuffix: mod.AndroidMkSuffix(),
+ RustSubName: mod.Properties.RustSubName,
+ TransitiveAndroidMkSharedLibs: mod.transitiveAndroidMkSharedLibs,
+ }
+ if mod.compiler != nil {
+ rustInfo.CompilerInfo = &CompilerInfo{
+ NoStdlibs: mod.compiler.noStdlibs(),
+ StdLinkageForDevice: mod.compiler.stdLinkage(true),
+ StdLinkageForNonDevice: mod.compiler.stdLinkage(false),
+ }
+ if lib, ok := mod.compiler.(libraryInterface); ok {
+ rustInfo.CompilerInfo.LibraryInfo = &LibraryInfo{
+ Dylib: lib.dylib(),
+ Rlib: lib.rlib(),
+ }
+ }
+ if lib, ok := mod.compiler.(cc.SnapshotInterface); ok {
+ rustInfo.SnapshotInfo = &cc.SnapshotInfo{
+ SnapshotAndroidMkSuffix: lib.SnapshotAndroidMkSuffix(),
+ }
+ }
+ }
+ if mod.sourceProvider != nil {
+ if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
+ rustInfo.SourceProviderInfo = &SourceProviderInfo{
+ ProtobufDecoratorInfo: &ProtobufDecoratorInfo{},
+ }
+ }
+ }
+ android.SetProvider(ctx, RustInfoProvider, rustInfo)
mod.setOutputFiles(ctx)
@@ -1175,21 +1239,21 @@
return nil
}
-func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
- if rustDep, ok := dep.(*Module); ok {
+func rustMakeLibName(rustInfo *RustInfo, linkableInfo *cc.LinkableInfo, commonInfo *android.CommonModuleInfo, depName string) string {
+ if rustInfo != nil {
// Use base module name for snapshots when exporting to Makefile.
- if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
- baseName := rustDep.BaseModuleName()
- return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
+ if rustInfo.SnapshotInfo != nil {
+ baseName := linkableInfo.BaseModuleName
+ return baseName + rustInfo.SnapshotInfo.SnapshotAndroidMkSuffix + rustInfo.AndroidMkSuffix
}
}
- return cc.MakeLibName(ctx, c, dep, depName)
+ return cc.MakeLibName(nil, linkableInfo, commonInfo, depName)
}
-func collectIncludedProtos(mod *Module, dep *Module) {
+func collectIncludedProtos(mod *Module, rustInfo *RustInfo, linkableInfo *cc.LinkableInfo) {
if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
- if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
- protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
+ if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
+ protoMod.additionalCrates = append(protoMod.additionalCrates, linkableInfo.CrateName)
}
}
}
@@ -1197,13 +1261,13 @@
func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
var depPaths PathDeps
- directRlibDeps := []*Module{}
- directDylibDeps := []*Module{}
- directProcMacroDeps := []*Module{}
+ directRlibDeps := []*cc.LinkableInfo{}
+ directDylibDeps := []*cc.LinkableInfo{}
+ directProcMacroDeps := []*cc.LinkableInfo{}
directSharedLibDeps := []cc.SharedLibraryInfo{}
- directStaticLibDeps := [](cc.LinkableInterface){}
- directSrcProvidersDeps := []*Module{}
- directSrcDeps := [](android.SourceFileProducer){}
+ directStaticLibDeps := [](*cc.LinkableInfo){}
+ directSrcProvidersDeps := []*android.ModuleProxy{}
+ directSrcDeps := []android.SourceFilesInfo{}
// For the dependency from platform to apex, use the latest stubs
mod.apexSdkVersion = android.FutureApiLevel
@@ -1224,7 +1288,7 @@
var transitiveAndroidMkSharedLibs []depset.DepSet[string]
var directAndroidMkSharedLibs []string
- ctx.VisitDirectDeps(func(dep android.Module) {
+ ctx.VisitDirectDepsProxy(func(dep android.ModuleProxy) {
depName := ctx.OtherModuleName(dep)
depTag := ctx.OtherModuleDependencyTag(dep)
modStdLinkage := mod.compiler.stdLinkage(ctx.Device())
@@ -1237,18 +1301,22 @@
return
}
- if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
+ rustInfo, hasRustInfo := android.OtherModuleProvider(ctx, dep, RustInfoProvider)
+ ccInfo, _ := android.OtherModuleProvider(ctx, dep, cc.CcInfoProvider)
+ linkableInfo, hasLinkableInfo := android.OtherModuleProvider(ctx, dep, cc.LinkableInfoProvider)
+ commonInfo := android.OtherModuleProviderOrDefault(ctx, dep, android.CommonModuleInfoKey)
+ if hasRustInfo && !linkableInfo.Static && !linkableInfo.Shared {
//Handle Rust Modules
- makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
+ makeLibName := rustMakeLibName(rustInfo, linkableInfo, &commonInfo, depName+rustInfo.RustSubName)
switch {
case depTag == dylibDepTag:
- dylib, ok := rustDep.compiler.(libraryInterface)
- if !ok || !dylib.dylib() {
+ dylib := rustInfo.CompilerInfo.LibraryInfo
+ if dylib == nil || !dylib.Dylib {
ctx.ModuleErrorf("mod %q not an dylib library", depName)
return
}
- directDylibDeps = append(directDylibDeps, rustDep)
+ directDylibDeps = append(directDylibDeps, linkableInfo)
mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
@@ -1257,8 +1325,11 @@
depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
}
- if !rustDep.compiler.noStdlibs() {
- rustDepStdLinkage := rustDep.compiler.stdLinkage(ctx.Device())
+ if !rustInfo.CompilerInfo.NoStdlibs {
+ rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
+ if ctx.Device() {
+ rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
+ }
if rustDepStdLinkage != modStdLinkage {
ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
return
@@ -1266,27 +1337,30 @@
}
case depTag == rlibDepTag:
- rlib, ok := rustDep.compiler.(libraryInterface)
- if !ok || !rlib.rlib() {
+ rlib := rustInfo.CompilerInfo.LibraryInfo
+ if rlib == nil || !rlib.Rlib {
ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
return
}
- directRlibDeps = append(directRlibDeps, rustDep)
+ directRlibDeps = append(directRlibDeps, linkableInfo)
mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
// rust_ffi rlibs may export include dirs, so collect those here.
exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
- depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
+ depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
// rlibs are not installed, so don't add the output file to directImplementationDeps
if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
}
- if !rustDep.compiler.noStdlibs() {
- rustDepStdLinkage := rustDep.compiler.stdLinkage(ctx.Device())
+ if !rustInfo.CompilerInfo.NoStdlibs {
+ rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
+ if ctx.Device() {
+ rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
+ }
if rustDepStdLinkage != modStdLinkage {
ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
return
@@ -1294,14 +1368,14 @@
}
case depTag == procMacroDepTag:
- directProcMacroDeps = append(directProcMacroDeps, rustDep)
+ directProcMacroDeps = append(directProcMacroDeps, linkableInfo)
mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
// proc_macro link dirs need to be exported, so collect those here.
- depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
+ depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
case depTag == sourceDepTag:
if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
- collectIncludedProtos(mod, rustDep)
+ collectIncludedProtos(mod, rustInfo, linkableInfo)
}
case cc.IsStaticDepTag(depTag):
// Rust FFI rlibs should not be declared in a Rust modules
@@ -1314,7 +1388,7 @@
}
- transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
+ transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustInfo.TransitiveAndroidMkSharedLibs)
if android.IsSourceDepTagWithOutputTag(depTag, "") {
// Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
@@ -1326,14 +1400,14 @@
helper = "device module defined?"
}
- if dep.Target().Os != ctx.Os() {
+ if commonInfo.Target.Os != ctx.Os() {
ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
return
- } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
+ } else if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
return
}
- directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
+ directSrcProvidersDeps = append(directSrcProvidersDeps, &dep)
}
exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
@@ -1345,7 +1419,7 @@
}
if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
- linkFile := rustDep.UnstrippedOutputFile()
+ linkFile := linkableInfo.UnstrippedOutputFile
linkDir := linkPathFromFilePath(linkFile)
if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
lib.exportLinkDirs(linkDir)
@@ -1354,26 +1428,26 @@
if depTag == sourceDepTag {
if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
- if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
+ if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
}
}
}
- } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
+ } else if hasLinkableInfo {
//Handle C dependencies
- makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
- if _, ok := ccDep.(*Module); !ok {
- if ccDep.Module().Target().Os != ctx.Os() {
+ makeLibName := cc.MakeLibName(ccInfo, linkableInfo, &commonInfo, depName)
+ if !hasRustInfo {
+ if commonInfo.Target.Os != ctx.Os() {
ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
return
}
- if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
+ if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
return
}
}
- linkObject := ccDep.OutputFile()
+ linkObject := linkableInfo.OutputFile
if !linkObject.Valid() {
if !ctx.Config().AllowMissingDependencies() {
ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
@@ -1413,7 +1487,7 @@
depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
- directStaticLibDeps = append(directStaticLibDeps, ccDep)
+ directStaticLibDeps = append(directStaticLibDeps, linkableInfo)
// Record baseLibName for snapshots.
mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
@@ -1484,7 +1558,7 @@
}
}
- if srcDep, ok := dep.(android.SourceFileProducer); ok {
+ if srcDep, ok := android.OtherModuleProvider(ctx, dep, android.SourceFilesInfoKey); ok {
if android.IsSourceDepTagWithOutputTag(depTag, "") {
// These are usually genrules which don't have per-target variants.
directSrcDeps = append(directSrcDeps, srcDep)
@@ -1497,32 +1571,32 @@
var rlibDepFiles RustLibraries
aliases := mod.compiler.Aliases()
for _, dep := range directRlibDeps {
- crateName := dep.CrateName()
+ crateName := dep.CrateName
if alias, aliased := aliases[crateName]; aliased {
crateName = alias
}
- rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
+ rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
}
var dylibDepFiles RustLibraries
for _, dep := range directDylibDeps {
- crateName := dep.CrateName()
+ crateName := dep.CrateName
if alias, aliased := aliases[crateName]; aliased {
crateName = alias
}
- dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
+ dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
}
var procMacroDepFiles RustLibraries
for _, dep := range directProcMacroDeps {
- crateName := dep.CrateName()
+ crateName := dep.CrateName
if alias, aliased := aliases[crateName]; aliased {
crateName = alias
}
- procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
+ procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
}
var staticLibDepFiles android.Paths
for _, dep := range directStaticLibDeps {
- staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
+ staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile.Path())
}
var sharedLibFiles android.Paths
@@ -1538,11 +1612,11 @@
var srcProviderDepFiles android.Paths
for _, dep := range directSrcProvidersDeps {
- srcs := android.OutputFilesForModule(ctx, dep, "")
+ srcs := android.OutputFilesForModule(ctx, *dep, "")
srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
}
for _, dep := range directSrcDeps {
- srcs := dep.Srcs()
+ srcs := dep.Srcs
srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
}