summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-07-01 22:35:50 +0800
committerruki <[email protected]>2019-07-01 09:35:01 +0800
commita40920e82e60b7a87731ba60b848b9bafa8cf937 (patch)
tree772b961a8ac0c107142ae0f743b73b332afa2140
parentff067fda68bf431a4d46ce0149d425c7665aa2e7 (diff)
add protobuf rules
-rw-r--r--tests/projects/c++/protobuf/src/main.cpp1
-rw-r--r--tests/projects/c++/protobuf/xmake.lua13
-rw-r--r--tests/projects/c/protobuf/src/main.c7
-rw-r--r--tests/projects/c/protobuf/src/test.proto8
-rw-r--r--tests/projects/c/protobuf/xmake.lua20
-rw-r--r--xmake/actions/require/impl/action/patch.lua2
-rw-r--r--xmake/rules/protobuf/proto.lua28
-rw-r--r--xmake/rules/protobuf/xmake.lua28
8 files changed, 72 insertions, 35 deletions
diff --git a/tests/projects/c++/protobuf/src/main.cpp b/tests/projects/c++/protobuf/src/main.cpp
index db2361659..1e7830c17 100644
--- a/tests/projects/c++/protobuf/src/main.cpp
+++ b/tests/projects/c++/protobuf/src/main.cpp
@@ -1,4 +1,5 @@
#include <iostream>
+#include "test.pb.h"
using namespace std;
diff --git a/tests/projects/c++/protobuf/xmake.lua b/tests/projects/c++/protobuf/xmake.lua
index b340a8cd4..da8f1ee16 100644
--- a/tests/projects/c++/protobuf/xmake.lua
+++ b/tests/projects/c++/protobuf/xmake.lua
@@ -3,7 +3,7 @@
add_rules("mode.debug", "mode.release")
-- add protobuf
-add_requires("protobuf-cpp", "protoc")
+add_requires("protobuf-cpp")
-- add target
target("console_c++")
@@ -11,10 +11,13 @@ target("console_c++")
-- set kind
set_kind("binary")
- -- add rules and packages
- add_rules("protobuf.cpp")
- add_packages("protobuf-cpp", "protoc")
+ -- set languages
+ set_languages("c++11")
+
+ -- add packages
+ add_packages("protobuf-cpp")
-- add files
- add_files("src/*.cpp", "src/*.proto")
+ add_files("src/*.cpp")
+ add_files("src/*.proto", {rules = "protobuf.cpp"})
diff --git a/tests/projects/c/protobuf/src/main.c b/tests/projects/c/protobuf/src/main.c
new file mode 100644
index 000000000..1f0458b66
--- /dev/null
+++ b/tests/projects/c/protobuf/src/main.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+#include "test.pb-c.h"
+
+int main(int argc, char** argv)
+{
+ return 0;
+}
diff --git a/tests/projects/c/protobuf/src/test.proto b/tests/projects/c/protobuf/src/test.proto
new file mode 100644
index 000000000..0f129ea13
--- /dev/null
+++ b/tests/projects/c/protobuf/src/test.proto
@@ -0,0 +1,8 @@
+syntax = "proto3";
+package test;
+message TestCase {
+ string name = 4;
+}
+message Test {
+ repeated TestCase case = 1;
+}
diff --git a/tests/projects/c/protobuf/xmake.lua b/tests/projects/c/protobuf/xmake.lua
new file mode 100644
index 000000000..151acf5b4
--- /dev/null
+++ b/tests/projects/c/protobuf/xmake.lua
@@ -0,0 +1,20 @@
+
+-- add rules: debug and release
+add_rules("mode.debug", "mode.release")
+
+-- add protobuf
+add_requires("protobuf-c")
+
+-- add target
+target("console_c")
+
+ -- set kind
+ set_kind("binary")
+
+ -- add packages
+ add_packages("protobuf-c")
+
+ -- add files
+ add_files("src/*.c")
+ add_files("src/*.proto", {rules = "protobuf.c"})
+
diff --git a/xmake/actions/require/impl/action/patch.lua b/xmake/actions/require/impl/action/patch.lua
index bab50f9c5..cde52ec12 100644
--- a/xmake/actions/require/impl/action/patch.lua
+++ b/xmake/actions/require/impl/action/patch.lua
@@ -30,7 +30,7 @@ function _patch(package, patch_url, patch_hash)
vprint("patching %s to %s-%s ..", patch_url, package:name(), package:version_str())
-- get the patch file
- local patch_file = path.join(os.tmpdir(), "patches", package:name(), package:version_str(), path.filename(patch_url))
+ local patch_file = path.join(os.tmpdir(), "patches", package:name(), package:version_str(), (path.filename(patch_url):gsub("%?.+$", "")))
-- the package file have been downloaded?
local cached = true
diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua
index cc00d3b35..41ae6a388 100644
--- a/xmake/rules/protobuf/proto.lua
+++ b/xmake/rules/protobuf/proto.lua
@@ -24,17 +24,39 @@ import("core.theme.theme")
import("core.project.config")
import("core.project.depend")
import("core.tool.compiler")
+import("lib.detect.find_tool")
-- build protobuf file
function main(target, sourcekind, sourcefile_proto, opt)
+ -- find protoc
+ local protoc = target:data("protobuf.protoc")
+ if not protoc and sourcekind == "cxx" then
+ protoc = find_tool("protoc")
+ if protoc and protoc.program then
+ target:data_set("protobuf.protoc", protoc.program)
+ end
+ end
+
+ -- find protoc-c
+ local protoc_c = target:data("protobuf.protoc-c")
+ if not protoc_c and sourcekind == "cc" then
+ protoc_c = find_tool("protoc-c") or protoc
+ if protoc_c and protoc_c.program then
+ target:data_set("protobuf.protoc-c", protoc_c.program)
+ end
+ end
+
-- get protoc
- local protoc = assert(target:data("protobuf.protoc"), "protoc not found!")
+ protoc = assert(target:data(sourcekind == "cxx" and "protobuf.protoc" or "protobuf.protoc-c"), "protoc not found!")
-- get c/c++ source file for protobuf
- local sourcefile_cx = path.join(target:autogendir(), "rules", "protobuf", "src", path.basename(sourcefile_proto) .. ".pb" .. (sourcekind == "cxx" and ".cc" or ".c"))
+ local sourcefile_cx = path.join(target:autogendir(), "rules", "protobuf", path.basename(sourcefile_proto) .. ".pb" .. (sourcekind == "cxx" and ".cc" or "-c.c"))
local sourcefile_dir = path.directory(sourcefile_cx)
+ -- add includedirs
+ target:add("includedirs", sourcefile_dir)
+
-- get object file
local objectfile = target:objectfile(sourcefile_cx)
@@ -75,7 +97,7 @@ function main(target, sourcekind, sourcefile_proto, opt)
end
-- compile protobuf
- os.vrunv(protoc, {sourcefile_proto, (sourcekind == "cxx" and "--cpp_out=" or "--c_out=") .. sourcefile_dir})
+ os.vrunv(protoc, {sourcefile_proto, "-I" .. os.args(path.directory(sourcefile_proto)), (sourcekind == "cxx" and "--cpp_out=" or "--c_out=") .. sourcefile_dir})
-- trace
if option.get("verbose") then
diff --git a/xmake/rules/protobuf/xmake.lua b/xmake/rules/protobuf/xmake.lua
index 3907a4c2d..1447e2b05 100644
--- a/xmake/rules/protobuf/xmake.lua
+++ b/xmake/rules/protobuf/xmake.lua
@@ -18,35 +18,14 @@
-- @file xmake.lua
--
-
--- define rule: protobuf-cpp
-rule("protobuf.env")
-
- -- load protoc
- before_load(function (target)
- import("lib.detect.find_tool")
- local protoc = target:data("protobuf.protoc")
- if not protoc then
- protoc = find_tool("protoc")
- if protoc and protoc.program then
- target:data_set("protobuf.protoc", protoc.program)
- else
- raise("protoc not found!")
- end
- end
- end)
-
-- define rule: protobuf.cpp
rule("protobuf.cpp")
- -- add deps
- add_deps("protobuf.env")
-
-- set extension
set_extensions(".proto")
-- build protobuf file
- on_build_file(function (target, sourcefile_proto, opt)
+ before_build_file(function (target, sourcefile_proto, opt)
import("proto")(target, "cxx", sourcefile_proto, opt)
end)
@@ -54,13 +33,10 @@ rule("protobuf.cpp")
-- define rule: protobuf.c
rule("protobuf.c")
- -- add deps
- add_deps("protobuf.env")
-
-- set extension
set_extensions(".proto")
-- build protobuf file
- on_build_file(function (target, sourcefile_proto, opt)
+ before_build_file(function (target, sourcefile_proto, opt)
import("proto")(target, "cc", sourcefile_proto, opt)
end)