diff --git a/rust/protobuf.go b/rust/protobuf.go index c3aa8dc64..2982efdf2 100644 --- a/rust/protobuf.go +++ b/rust/protobuf.go @@ -20,6 +20,7 @@ import ( "android/soong/android" "android/soong/bazel" + "android/soong/cc" "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 // of protobuf off of 2.x. Use_protobuf3 *bool + + // List of exported include paths containing proto files for dependent rust_protobuf modules. + Exported_include_dirs []string } type protobufDecorator struct { *BaseSourceProvider - Properties ProtobufProperties - protoNames []string - grpcNames []string + Properties ProtobufProperties + protoNames []string + additionalCrates []string + grpcNames []string grpcProtoFlags 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. 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. return stemFile } @@ -192,10 +201,16 @@ func (proto *protobufDecorator) genModFileContents() string { lines := []string{ "// @Soong generated Source", } + for _, protoName := range proto.protoNames { 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 { lines = append(lines, fmt.Sprintf("pub mod %s;", grpcName)) lines = append(lines, fmt.Sprintf("pub mod %s%s;", grpcName, grpcSuffix)) diff --git a/rust/protobuf_test.go b/rust/protobuf_test.go index b723f3f1a..9dca0290f 100644 --- a/rust/protobuf_test.go +++ b/rust/protobuf_test.go @@ -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) { ctx := testRust(t, ` rust_protobuf { diff --git a/rust/rust.go b/rust/rust.go index 250e39857..49a7ff353 100644 --- a/rust/rust.go +++ b/rust/rust.go @@ -267,6 +267,15 @@ func (mod *Module) Dylib() bool { 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 { if mod.compiler != nil { 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) } +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 { var depPaths PathDeps @@ -1268,6 +1284,11 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { case procMacroDepTag: directProcMacroDeps = append(directProcMacroDeps, rustDep) mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName) + + case sourceDepTag: + if _, ok := mod.sourceProvider.(*protobufDecorator); ok { + collectIncludedProtos(mod, rustDep) + } } transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs) @@ -1308,7 +1329,14 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { 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 { //Handle C dependencies makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName) @@ -1572,30 +1600,43 @@ func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) { } // rustlibs - if deps.Rustlibs != nil && !mod.compiler.Disabled() { - autoDep := mod.compiler.(autoDeppable).autoDep(ctx) - for _, lib := range deps.Rustlibs { - if autoDep.depTag == rlibDepTag { - // Handle the rlib deptag case - 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. + if deps.Rustlibs != nil { + if !mod.compiler.Disabled() { + for _, lib := range deps.Rustlibs { + autoDep := mod.compiler.(autoDeppable).autoDep(ctx) + if autoDep.depTag == rlibDepTag { + // Handle the rlib deptag case 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 if deps.Stdlibs != nil { if mod.compiler.stdLinkage(ctx) == RlibLinkage {