summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-06-28 09:59:33 +0800
committerGitHub <[email protected]>2023-06-28 09:59:33 +0800
commit822e96e570844bdd27633db244514dda4caa3ace (patch)
tree85e8182dea28aee9fbe13735b2d71338242ac4e9
parentc744ae0fc7674825a12a4a1196a387030053dc0f (diff)
parent6e0b9f7e44c862d9ed5c6d675d353bca8b481896 (diff)
Merge pull request #3886 from ivanallen/master
support grpc_cpp_plugin #3148
-rw-r--r--tests/projects/c++/protobuf_grpc_cpp_plugin/src/main.cpp11
-rw-r--r--tests/projects/c++/protobuf_grpc_cpp_plugin/src/subdir/test2.proto8
-rw-r--r--tests/projects/c++/protobuf_grpc_cpp_plugin/src/test.proto22
-rw-r--r--tests/projects/c++/protobuf_grpc_cpp_plugin/xmake.lua14
-rw-r--r--xmake/rules/protobuf/proto.lua53
5 files changed, 102 insertions, 6 deletions
diff --git a/tests/projects/c++/protobuf_grpc_cpp_plugin/src/main.cpp b/tests/projects/c++/protobuf_grpc_cpp_plugin/src/main.cpp
new file mode 100644
index 000000000..dc06458ae
--- /dev/null
+++ b/tests/projects/c++/protobuf_grpc_cpp_plugin/src/main.cpp
@@ -0,0 +1,11 @@
+#include <iostream>
+#include "test.pb.h"
+#include "subdir/test2.pb.h"
+
+using namespace std;
+
+int main(int argc, char** argv)
+{
+ cout << "hello world!" << endl;
+ return 0;
+}
diff --git a/tests/projects/c++/protobuf_grpc_cpp_plugin/src/subdir/test2.proto b/tests/projects/c++/protobuf_grpc_cpp_plugin/src/subdir/test2.proto
new file mode 100644
index 000000000..811cdac3e
--- /dev/null
+++ b/tests/projects/c++/protobuf_grpc_cpp_plugin/src/subdir/test2.proto
@@ -0,0 +1,8 @@
+syntax = "proto3";
+package test2;
+message TestCase2 {
+ string name2 = 4;
+}
+message Test2 {
+ repeated TestCase2 case2 = 1;
+}
diff --git a/tests/projects/c++/protobuf_grpc_cpp_plugin/src/test.proto b/tests/projects/c++/protobuf_grpc_cpp_plugin/src/test.proto
new file mode 100644
index 000000000..d486ef316
--- /dev/null
+++ b/tests/projects/c++/protobuf_grpc_cpp_plugin/src/test.proto
@@ -0,0 +1,22 @@
+syntax = "proto3";
+import "subdir/test2.proto";
+package test;
+message TestCase {
+ string name = 4;
+}
+message Test {
+ repeated TestCase case = 1;
+ repeated test2.TestCase2 case2 = 2;
+}
+
+message EchoRequest {
+ string data = 1;
+}
+
+message EchoResponse {
+ string data = 1;
+}
+
+service EchoServer {
+ rpc Echo(EchoRequest) returns (EchoResponse);
+}
diff --git a/tests/projects/c++/protobuf_grpc_cpp_plugin/xmake.lua b/tests/projects/c++/protobuf_grpc_cpp_plugin/xmake.lua
new file mode 100644
index 000000000..a38d19d6c
--- /dev/null
+++ b/tests/projects/c++/protobuf_grpc_cpp_plugin/xmake.lua
@@ -0,0 +1,14 @@
+add_rules("mode.debug", "mode.release")
+add_requires("protobuf-cpp")
+add_requires("grpc", {system = false})
+
+target("test")
+ set_kind("binary")
+ set_languages("c++17")
+ add_packages("protobuf-cpp")
+ add_packages("grpc")
+ add_rules("protobuf.cpp")
+ add_files("src/*.cpp")
+ add_files("src/test.proto", {proto_rootdir = "src", proto_grpc_cpp_plugin = true})
+ add_files("src/subdir/test2.proto", {proto_rootdir = "src"})
+
diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua
index 1d30c7bd6..349864e9e 100644
--- a/xmake/rules/protobuf/proto.lua
+++ b/xmake/rules/protobuf/proto.lua
@@ -49,6 +49,13 @@ function _get_protoc(target, sourcekind)
return assert(target:data(sourcekind == "cxx" and "protobuf.protoc" or "protobuf.protoc-c"), "protoc not found!")
end
+-- get grpc_cpp_plugin
+function _get_grpc_cpp_plugin(target, sourcekind)
+ assert(sourcekind == "cxx", "grpc_cpp_plugin only support c++")
+ local grpc_cpp_plugin = find_tool("grpc_cpp_plugin", {norun = true})
+ return assert(grpc_cpp_plugin and grpc_cpp_plugin.program, "grpc_cpp_plugin not found!")
+end
+
-- we need add some configs to export includedirs to other targets in on_load
-- @see https://github.com/xmake-io/xmake/issues/2256
function load(target, sourcekind)
@@ -92,6 +99,7 @@ function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind)
local prefixdir
local autogendir
local public
+ local grpc_cpp_plugin
local fileconfig = target:fileconfig(sourcefile_proto)
if fileconfig then
public = fileconfig.proto_public
@@ -99,12 +107,22 @@ function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind)
-- custom autogen directory to access the generated header files
-- @see https://github.com/xmake-io/xmake/issues/3678
autogendir = fileconfig.proto_autogendir
+ grpc_cpp_plugin = fileconfig.proto_grpc_cpp_plugin
end
local rootdir = autogendir and autogendir or path.join(target:autogendir(), "rules", "protobuf")
local filename = path.basename(sourcefile_proto) .. ".pb" .. (sourcekind == "cxx" and ".cc" or "-c.c")
local sourcefile_cx = target:autogenfile(sourcefile_proto, {rootdir = rootdir, filename = filename})
local sourcefile_dir = prefixdir and path.join(rootdir, prefixdir) or path.directory(sourcefile_cx)
+ local grpc_cpp_plugin_bin
+ local filename_grpc
+ local sourcefile_cx_grpc
+ if grpc_cpp_plugin then
+ grpc_cpp_plugin_bin = _get_grpc_cpp_plugin(target, sourcekind)
+ filename_grpc = path.basename(sourcefile_proto) .. ".grpc.pb.cc"
+ sourcefile_cx_grpc = target:autogenfile(sourcefile_proto, {rootdir = rootdir, filename = filename_grpc})
+ end
+
-- add includedirs
target:add("includedirs", sourcefile_dir, {public = public})
@@ -112,18 +130,41 @@ function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind)
local objectfile = target:objectfile(sourcefile_cx)
table.insert(target:objectfiles(), objectfile)
+ local objectfile_grpc
+ if grpc_cpp_plugin then
+ objectfile_grpc = target:objectfile(sourcefile_cx_grpc)
+ table.insert(target:objectfiles(), objectfile_grpc)
+ end
+
+ local protoc_args = {
+ path(sourcefile_proto),
+ path(prefixdir and prefixdir or path.directory(sourcefile_proto), function (p) return "-I" .. p end),
+ path(sourcefile_dir, function (p) return (sourcekind == "cxx" and "--cpp_out=" or "--c_out=") .. p end)
+ }
+
+ if grpc_cpp_plugin then
+ table.insert(protoc_args, "--plugin=protoc-gen-grpc=" .. grpc_cpp_plugin_bin)
+ table.insert(protoc_args, path(sourcefile_dir, function (p) return ("--grpc_out=") .. p end))
+ end
+
-- add commands
batchcmds:mkdir(sourcefile_dir)
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.proto %s", sourcefile_proto)
- batchcmds:vrunv(protoc, {path(sourcefile_proto),
- path(prefixdir and prefixdir or path.directory(sourcefile_proto), function (p) return "-I" .. p end),
- path(sourcefile_dir, function (p) return (sourcekind == "cxx" and "--cpp_out=" or "--c_out=") .. p end)})
- batchcmds:compile(sourcefile_cx, objectfile, {configs = {includedirs = sourcefile_dir, languages = (sourcekind == "cxx" and "c++11")}})
-
+ batchcmds:vrunv(protoc, protoc_args)
+ batchcmds:compile(sourcefile_cx, objectfile, {configs = {includedirs = sourcefile_dir}})
+ if grpc_cpp_plugin then
+ batchcmds:compile(sourcefile_cx_grpc, objectfile_grpc, {configs = {includedirs = sourcefile_dir}})
+ end
+
-- add deps
+ local depmtime = os.mtime(objectfile)
batchcmds:add_depfiles(sourcefile_proto)
- batchcmds:set_depmtime(os.mtime(objectfile))
batchcmds:set_depcache(target:dependfile(objectfile))
+ if grpc_cpp_plugin then
+ batchcmds:set_depmtime(math.max(os.mtime(objectfile_grpc), depmtime))
+ else
+ batchcmds:set_depmtime(depmtime)
+ end
end
-- build batch jobs