Add support for protoc plugins

Add a proto.plugin property to allow specifying a custom protoc
plugin to generate the code.

Fixes: 70706119
Test: m am StreamingProtoTest
Change-Id: I1ecdd346284b42bbcc8297019d98d2cd564eb94c
This commit is contained in:
Colin Cross
2019-03-28 19:30:56 -07:00
parent 19878da6a0
commit fe17f6f0e8
16 changed files with 208 additions and 115 deletions

View File

@@ -260,7 +260,6 @@ type builderFlags struct {
stripAddGnuDebuglink bool
stripUseGnuStrip bool
protoDeps android.Paths
proto android.ProtoFlags
protoC bool
protoOptionsFile bool

View File

@@ -163,7 +163,6 @@ type Flags struct {
GroupStaticLibs bool
proto android.ProtoFlags
protoDeps android.Paths
protoC bool // Whether to use C instead of C++
protoOptionsFile bool // Whether to look for a .options file next to the .proto
}
@@ -1594,6 +1593,10 @@ func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
return
}
if depTag == android.ProtoPluginDepTag {
return
}
if dep.Target().Os != ctx.Os() {
ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
return

View File

@@ -54,6 +54,7 @@ func TestMain(m *testing.M) {
func createTestContext(t *testing.T, config android.Config, bp string, os android.OsType) *android.TestContext {
ctx := android.NewTestArchContext()
ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(BinaryFactory))
ctx.RegisterModuleType("cc_binary_host", android.ModuleFactoryAdaptor(binaryHostFactory))
ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(LibraryFactory))
ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(LibrarySharedFactory))
ctx.RegisterModuleType("cc_library_static", android.ModuleFactoryAdaptor(LibraryStaticFactory))

View File

@@ -221,6 +221,7 @@ func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
android.ProtoDeps(ctx, &compiler.Proto)
if compiler.hasSrcExt(".proto") {
deps = protoDeps(ctx, deps, &compiler.Proto, Bool(compiler.Properties.Proto.Static))
}

View File

@@ -39,7 +39,7 @@ func genProto(ctx android.ModuleContext, protoFile android.Path, flags builderFl
headerFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.h"))
}
protoDeps := flags.protoDeps
protoDeps := flags.proto.Deps
if flags.protoOptionsFile {
optionsFile := pathtools.ReplaceExtension(protoFile.String(), "options")
optionsPath := android.PathForSource(ctx, optionsFile)
@@ -59,53 +59,55 @@ func genProto(ctx android.ModuleContext, protoFile android.Path, flags builderFl
return ccFile, headerFile
}
func protoDeps(ctx BaseModuleContext, deps Deps, p *android.ProtoProperties, static bool) Deps {
func protoDeps(ctx DepsContext, deps Deps, p *android.ProtoProperties, static bool) Deps {
var lib string
switch String(p.Proto.Type) {
case "full":
if ctx.useSdk() {
lib = "libprotobuf-cpp-full-ndk"
if String(p.Proto.Plugin) == "" {
switch String(p.Proto.Type) {
case "full":
if ctx.useSdk() {
lib = "libprotobuf-cpp-full-ndk"
static = true
} else {
lib = "libprotobuf-cpp-full"
}
case "lite", "":
if ctx.useSdk() {
lib = "libprotobuf-cpp-lite-ndk"
static = true
} else {
lib = "libprotobuf-cpp-lite"
}
case "nanopb-c":
lib = "libprotobuf-c-nano"
static = true
} else {
lib = "libprotobuf-cpp-full"
}
case "lite", "":
if ctx.useSdk() {
lib = "libprotobuf-cpp-lite-ndk"
case "nanopb-c-enable_malloc":
lib = "libprotobuf-c-nano-enable_malloc"
static = true
} else {
lib = "libprotobuf-cpp-lite"
case "nanopb-c-16bit":
lib = "libprotobuf-c-nano-16bit"
static = true
case "nanopb-c-enable_malloc-16bit":
lib = "libprotobuf-c-nano-enable_malloc-16bit"
static = true
case "nanopb-c-32bit":
lib = "libprotobuf-c-nano-32bit"
static = true
case "nanopb-c-enable_malloc-32bit":
lib = "libprotobuf-c-nano-enable_malloc-32bit"
static = true
default:
ctx.PropertyErrorf("proto.type", "unknown proto type %q",
String(p.Proto.Type))
}
case "nanopb-c":
lib = "libprotobuf-c-nano"
static = true
case "nanopb-c-enable_malloc":
lib = "libprotobuf-c-nano-enable_malloc"
static = true
case "nanopb-c-16bit":
lib = "libprotobuf-c-nano-16bit"
static = true
case "nanopb-c-enable_malloc-16bit":
lib = "libprotobuf-c-nano-enable_malloc-16bit"
static = true
case "nanopb-c-32bit":
lib = "libprotobuf-c-nano-32bit"
static = true
case "nanopb-c-enable_malloc-32bit":
lib = "libprotobuf-c-nano-enable_malloc-32bit"
static = true
default:
ctx.PropertyErrorf("proto.type", "unknown proto type %q",
String(p.Proto.Type))
}
if static {
deps.StaticLibs = append(deps.StaticLibs, lib)
deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib)
} else {
deps.SharedLibs = append(deps.SharedLibs, lib)
deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib)
if static {
deps.StaticLibs = append(deps.StaticLibs, lib)
deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib)
} else {
deps.SharedLibs = append(deps.SharedLibs, lib)
deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib)
}
}
return deps
@@ -120,33 +122,35 @@ func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flag
}
flags.GlobalFlags = append(flags.GlobalFlags, "-I"+flags.proto.Dir.String())
var plugin string
if String(p.Proto.Plugin) == "" {
var plugin string
switch String(p.Proto.Type) {
case "nanopb-c", "nanopb-c-enable_malloc", "nanopb-c-16bit", "nanopb-c-enable_malloc-16bit", "nanopb-c-32bit", "nanopb-c-enable_malloc-32bit":
flags.protoC = true
flags.protoOptionsFile = true
flags.proto.OutTypeFlag = "--nanopb_out"
plugin = "protoc-gen-nanopb"
case "full":
flags.proto.OutTypeFlag = "--cpp_out"
case "lite":
flags.proto.OutTypeFlag = "--cpp_out"
flags.proto.OutParams = append(flags.proto.OutParams, "lite")
case "":
// TODO(b/119714316): this should be equivalent to "lite" in
// order to match protoDeps, but some modules are depending on
// this behavior
flags.proto.OutTypeFlag = "--cpp_out"
default:
ctx.PropertyErrorf("proto.type", "unknown proto type %q",
String(p.Proto.Type))
}
switch String(p.Proto.Type) {
case "nanopb-c", "nanopb-c-enable_malloc", "nanopb-c-16bit", "nanopb-c-enable_malloc-16bit", "nanopb-c-32bit", "nanopb-c-enable_malloc-32bit":
flags.protoC = true
flags.protoOptionsFile = true
flags.proto.OutTypeFlag = "--nanopb_out"
plugin = "protoc-gen-nanopb"
case "full":
flags.proto.OutTypeFlag = "--cpp_out"
case "lite":
flags.proto.OutTypeFlag = "--cpp_out"
flags.proto.OutParams = append(flags.proto.OutParams, "lite")
case "":
// TODO(b/119714316): this should be equivalent to "lite" in
// order to match protoDeps, but some modules are depending on
// this behavior
flags.proto.OutTypeFlag = "--cpp_out"
default:
ctx.PropertyErrorf("proto.type", "unknown proto type %q",
String(p.Proto.Type))
}
if plugin != "" {
path := ctx.Config().HostToolPath(ctx, plugin)
flags.protoDeps = append(flags.protoDeps, path)
flags.proto.Flags = append(flags.proto.Flags, "--plugin="+path.String())
if plugin != "" {
path := ctx.Config().HostToolPath(ctx, plugin)
flags.proto.Deps = append(flags.proto.Deps, path)
flags.proto.Flags = append(flags.proto.Flags, "--plugin="+path.String())
}
}
return flags

View File

@@ -17,6 +17,8 @@ package cc
import (
"strings"
"testing"
"android/soong/android"
)
func TestProto(t *testing.T) {
@@ -33,4 +35,37 @@ func TestProto(t *testing.T) {
t.Errorf("expected '--cpp_out' in %q", cmd)
}
})
t.Run("plugin", func(t *testing.T) {
ctx := testCc(t, `
cc_binary_host {
name: "protoc-gen-foobar",
stl: "none",
}
cc_library_shared {
name: "libfoo",
srcs: ["a.proto"],
proto: {
plugin: "foobar",
},
}`)
buildOS := android.BuildOs.String()
proto := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_core_shared").Output("proto/a.pb.cc")
foobar := ctx.ModuleForTests("protoc-gen-foobar", buildOS+"_x86_64")
cmd := proto.RuleParams.Command
if w := "--foobar_out="; !strings.Contains(cmd, w) {
t.Errorf("expected %q in %q", w, cmd)
}
foobarPath := foobar.Module().(android.HostToolProvider).HostToolPath().String()
if w := "--plugin=protoc-gen-foobar=" + foobarPath; !strings.Contains(cmd, w) {
t.Errorf("expected %q in %q", w, cmd)
}
})
}

View File

@@ -84,7 +84,6 @@ func flagsToBuilderFlags(in Flags) builderFlags {
groupStaticLibs: in.GroupStaticLibs,
protoDeps: in.protoDeps,
proto: in.proto,
protoC: in.protoC,
protoOptionsFile: in.protoOptionsFile,