rust: Import protos from dependent rust_protobuf

rust_protobuf were unable to import protos from other rust_protobuf
modules. This CL adds support for that. rust_protobuf modules which are
listed in rustlibs will have their modules imported into the generated
protobuf stub. Additionally, rust_protobuf modules which define
"exported_include_dirs" will export those include paths to dependent
rust_protobuf modules.

Bug: 301266700
Test: m rust
Change-Id: I132edffa4d77e0ac80a7ac934f873374c8e94c1b
This commit is contained in:
Ivan Lozano 2023-09-21 23:30:26 -04:00
parent c5b9abba30
commit d106efe76d
3 changed files with 131 additions and 23 deletions

View file

@ -20,6 +20,7 @@ import (
"android/soong/android" "android/soong/android"
"android/soong/bazel" "android/soong/bazel"
"android/soong/cc"
"github.com/google/blueprint/proptools" "github.com/google/blueprint/proptools"
) )
@ -59,14 +60,18 @@ type ProtobufProperties struct {
// Use protobuf version 3.x. This will be deleted once we migrate all current users // Use protobuf version 3.x. This will be deleted once we migrate all current users
// of protobuf off of 2.x. // of protobuf off of 2.x.
Use_protobuf3 *bool Use_protobuf3 *bool
// List of exported include paths containing proto files for dependent rust_protobuf modules.
Exported_include_dirs []string
} }
type protobufDecorator struct { type protobufDecorator struct {
*BaseSourceProvider *BaseSourceProvider
Properties ProtobufProperties Properties ProtobufProperties
protoNames []string protoNames []string
grpcNames []string additionalCrates []string
grpcNames []string
grpcProtoFlags android.ProtoFlags grpcProtoFlags android.ProtoFlags
protoFlags android.ProtoFlags protoFlags android.ProtoFlags
@ -184,6 +189,10 @@ func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps)
// stemFile must be first here as the first path in BaseSourceProvider.OutputFiles is the library entry-point. // stemFile must be first here as the first path in BaseSourceProvider.OutputFiles is the library entry-point.
proto.BaseSourceProvider.OutputFiles = append(android.Paths{stemFile}, outputs.Paths()...) proto.BaseSourceProvider.OutputFiles = append(android.Paths{stemFile}, outputs.Paths()...)
ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{
IncludeDirs: android.PathsForModuleSrc(ctx, proto.Properties.Exported_include_dirs),
})
// mod_stem.rs is the entry-point for our library modules, so this is what we return. // mod_stem.rs is the entry-point for our library modules, so this is what we return.
return stemFile return stemFile
} }
@ -192,10 +201,16 @@ func (proto *protobufDecorator) genModFileContents() string {
lines := []string{ lines := []string{
"// @Soong generated Source", "// @Soong generated Source",
} }
for _, protoName := range proto.protoNames { for _, protoName := range proto.protoNames {
lines = append(lines, fmt.Sprintf("pub mod %s;", protoName)) lines = append(lines, fmt.Sprintf("pub mod %s;", protoName))
} }
for _, crate := range proto.additionalCrates {
lines = append(lines, fmt.Sprintf("pub use %s::*;", crate))
}
for _, grpcName := range proto.grpcNames { for _, grpcName := range proto.grpcNames {
lines = append(lines, fmt.Sprintf("pub mod %s;", grpcName)) lines = append(lines, fmt.Sprintf("pub mod %s;", grpcName))
lines = append(lines, fmt.Sprintf("pub mod %s%s;", grpcName, grpcSuffix)) lines = append(lines, fmt.Sprintf("pub mod %s%s;", grpcName, grpcSuffix))

View file

@ -118,6 +118,58 @@ func TestRustProtobuf3(t *testing.T) {
} }
} }
func TestRustProtobufInclude(t *testing.T) {
ctx := testRust(t, `
rust_protobuf {
name: "librust_proto",
protos: ["proto.proto"],
crate_name: "rust_proto",
source_stem: "proto",
use_protobuf3: true,
rustlibs: ["librust_exported_proto", "libfoo"],
}
rust_protobuf {
name: "librust_exported_proto",
protos: ["proto.proto"],
crate_name: "rust_exported_proto",
source_stem: "exported_proto",
use_protobuf3: true,
exported_include_dirs: ["proto"]
}
rust_library {
name: "libfoo",
crate_name: "foo",
srcs: ["foo.rs"],
}
`)
// Check that librust_exported_proto is added as additional crate to generate source.
librust_proto := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").Module().(*Module).sourceProvider.(*protobufDecorator)
if !android.InList("rust_exported_proto", librust_proto.additionalCrates) {
t.Errorf("librust_proto should have librust_exported_proto included as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates)
}
// Make sure the default crates aren't being included.
if android.InList("std", librust_proto.additionalCrates) {
t.Errorf("librust_proto should not have included libstd as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates)
}
if android.InList("protobuf", librust_proto.additionalCrates) {
t.Errorf("librust_proto should not have included libprotobuf as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates)
}
// And make sure that non-protobuf crates aren't getting included either.
if android.InList("foo", librust_proto.additionalCrates) {
t.Errorf("librust_proto should not have included libfoo as an additional crate for generated source, instead got: %#v", librust_proto.additionalCrates)
}
// Check librust_proto args includes -Iproto
librust_proto_rule := ctx.ModuleForTests("librust_proto", "android_arm64_armv8-a_source").Output("proto.rs")
cmd := librust_proto_rule.RuleParams.Command
if w := "-Iproto"; !strings.Contains(cmd, w) {
t.Errorf("expected %q in %q", w, cmd)
}
}
func TestRustGrpc(t *testing.T) { func TestRustGrpc(t *testing.T) {
ctx := testRust(t, ` ctx := testRust(t, `
rust_protobuf { rust_protobuf {

View file

@ -267,6 +267,15 @@ func (mod *Module) Dylib() bool {
return false return false
} }
func (mod *Module) Source() bool {
if mod.compiler != nil {
if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
return library.source()
}
}
return false
}
func (mod *Module) RlibStd() bool { func (mod *Module) RlibStd() bool {
if mod.compiler != nil { if mod.compiler != nil {
if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() { if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
@ -1156,6 +1165,13 @@ func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.L
return cc.MakeLibName(ctx, c, dep, depName) return cc.MakeLibName(ctx, c, dep, depName)
} }
func collectIncludedProtos(mod *Module, dep *Module) {
if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
}
}
}
func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
var depPaths PathDeps var depPaths PathDeps
@ -1268,6 +1284,11 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
case procMacroDepTag: case procMacroDepTag:
directProcMacroDeps = append(directProcMacroDeps, rustDep) directProcMacroDeps = append(directProcMacroDeps, rustDep)
mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName) mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
case sourceDepTag:
if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
collectIncludedProtos(mod, rustDep)
}
} }
transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs) transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
@ -1308,7 +1329,14 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
lib.exportLinkDirs(linkDir) lib.exportLinkDirs(linkDir)
} }
} }
if depTag == sourceDepTag {
if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
}
}
}
} else if ccDep, ok := dep.(cc.LinkableInterface); ok { } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
//Handle C dependencies //Handle C dependencies
makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName) makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
@ -1572,30 +1600,43 @@ func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
} }
// rustlibs // rustlibs
if deps.Rustlibs != nil && !mod.compiler.Disabled() { if deps.Rustlibs != nil {
autoDep := mod.compiler.(autoDeppable).autoDep(ctx) if !mod.compiler.Disabled() {
for _, lib := range deps.Rustlibs { for _, lib := range deps.Rustlibs {
if autoDep.depTag == rlibDepTag { autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
// Handle the rlib deptag case if autoDep.depTag == rlibDepTag {
addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations) // Handle the rlib deptag case
} else {
// autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
// Check for the existence of the dylib deptag variant. Select it if available,
// otherwise select the rlib variant.
autoDepVariations := append(commonDepVariations,
blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
} else {
// If there's no dylib dependency available, try to add the rlib dependency instead.
addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations) addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
} else {
// autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
// Check for the existence of the dylib deptag variant. Select it if available,
// otherwise select the rlib variant.
autoDepVariations := append(commonDepVariations,
blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
} else {
// If there's no dylib dependency available, try to add the rlib dependency instead.
addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
}
}
}
} else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
for _, lib := range deps.Rustlibs {
replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
srcProviderVariations := append(commonDepVariations,
blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
if actx.OtherModuleDependencyVariantExists(srcProviderVariations, replacementLib) {
actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
} }
} }
} }
} }
// stdlibs // stdlibs
if deps.Stdlibs != nil { if deps.Stdlibs != nil {
if mod.compiler.stdLinkage(ctx) == RlibLinkage { if mod.compiler.stdLinkage(ctx) == RlibLinkage {