Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d106efe7 authored by Ivan Lozano's avatar Ivan Lozano
Browse files

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
parent c5b9abba
Loading
Loading
Loading
Loading
+18 −3
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import (

	"android/soong/android"
	"android/soong/bazel"
	"android/soong/cc"

	"github.com/google/blueprint/proptools"
)
@@ -59,6 +60,9 @@ 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 {
@@ -66,6 +70,7 @@ type protobufDecorator struct {

	Properties       ProtobufProperties
	protoNames       []string
	additionalCrates []string
	grpcNames        []string

	grpcProtoFlags 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))
+52 −0
Original line number Diff line number Diff line
@@ -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 {
+59 −18
Original line number Diff line number Diff line
@@ -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,9 +1600,10 @@ func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
	}

	// rustlibs
	if deps.Rustlibs != nil && !mod.compiler.Disabled() {
		autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
	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)
@@ -1595,7 +1624,19 @@ func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
					}
				}
			}
		} 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 {