summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-06-12 22:34:48 +0800
committerruki <[email protected]>2020-06-12 09:28:51 +0800
commitcf1df0402b2fdcb1c6d8d4f35385f153a036dc24 (patch)
tree353270c8ba9680160686ca26a4fa54b7e909a08a
parent2daaee9087bf3a503831d8dfb141baa328cbac43 (diff)
improve protobuf
-rw-r--r--tests/projects/c++/protobuf/xmake.lua2
-rw-r--r--tests/projects/c/protobuf/src/test.proto3
-rw-r--r--tests/projects/c/protobuf/xmake.lua4
-rw-r--r--xmake/core/project/target.lua74
-rw-r--r--xmake/rules/protobuf/proto.lua4
5 files changed, 47 insertions, 40 deletions
diff --git a/tests/projects/c++/protobuf/xmake.lua b/tests/projects/c++/protobuf/xmake.lua
index da8f1ee16..ee65ee71f 100644
--- a/tests/projects/c++/protobuf/xmake.lua
+++ b/tests/projects/c++/protobuf/xmake.lua
@@ -6,7 +6,7 @@ add_rules("mode.debug", "mode.release")
add_requires("protobuf-cpp")
-- add target
-target("console_c++")
+target("test")
-- set kind
set_kind("binary")
diff --git a/tests/projects/c/protobuf/src/test.proto b/tests/projects/c/protobuf/src/test.proto
index f22385950..0f580b87a 100644
--- a/tests/projects/c/protobuf/src/test.proto
+++ b/tests/projects/c/protobuf/src/test.proto
@@ -1,9 +1,10 @@
syntax = "proto3";
-import subdir/test2.proto
+import "subdir/test2.proto";
package test;
message TestCase {
string name = 4;
}
message Test {
repeated TestCase case = 1;
+ repeated test2.TestCase2 case2 = 2;
}
diff --git a/tests/projects/c/protobuf/xmake.lua b/tests/projects/c/protobuf/xmake.lua
index 151acf5b4..b32af856c 100644
--- a/tests/projects/c/protobuf/xmake.lua
+++ b/tests/projects/c/protobuf/xmake.lua
@@ -6,7 +6,7 @@ add_rules("mode.debug", "mode.release")
add_requires("protobuf-c")
-- add target
-target("console_c")
+target("test")
-- set kind
set_kind("binary")
@@ -16,5 +16,5 @@ target("console_c")
-- add files
add_files("src/*.c")
- add_files("src/*.proto", {rules = "protobuf.c"})
+ add_files("src/**.proto", {rules = "protobuf.c"})
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index 1538d7930..af9e194e9 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -802,6 +802,43 @@ function _instance:autogendir(opt)
return autogendir
end
+-- get the autogen file path from the given source file path
+function _instance:autogenfile(sourcefile, opt)
+
+ -- get relative directory in the autogen directory
+ local relativedir = nil
+ local origindir = path.directory(path.absolute(sourcefile))
+ local autogendir = path.absolute(self:autogendir())
+ if origindir:startswith(autogendir) then
+ relativedir = path.join("gens", path.relative(origindir, autogendir))
+ end
+
+ -- get relative directory in the source directory
+ if not relativedir then
+ relativedir = path.directory(sourcefile)
+ end
+
+ -- translate path
+ --
+ -- e.g.
+ --
+ -- src/xxx.c
+ -- project/xmake.lua
+ -- build/.objs
+ -- build/.gens
+ --
+ -- objectfile: project/build/.objs/xxxx/../../xxx.c will be out of range for objectdir
+ -- autogenfile: project/build/.gens/xxxx/../../xxx.c will be out of range for autogendir
+ --
+ -- we need replace '..' to '__' in this case
+ --
+ if path.is_absolute(relativedir) and os.host() == "windows" then
+ relativedir = relativedir:gsub(":[\\/]*", '\\') -- replace C:\xxx\ => C\xxx\
+ end
+ relativedir = relativedir:gsub("%.%.", "__")
+ return path.join((opt and opt.rootdir) and opt.rootdir or self:autogendir(), relativedir, (opt and opt.filename) and opt.filename or path.filename(sourcefile))
+end
+
-- get the target kind
function _instance:targetkind()
return self:get("kind") or "phony"
@@ -1081,41 +1118,8 @@ function _instance:sourcefiles()
end
-- get object file from source file
-function _instance:objectfile(sourcefile, sourcekind)
-
- -- get relative directory in the autogen directory
- local relativedir = nil
- local origindir = path.directory(path.absolute(sourcefile))
- local autogendir = path.absolute(self:autogendir())
- if origindir:startswith(autogendir) then
- relativedir = path.join("gens", path.relative(origindir, autogendir))
- end
-
- -- get relative directory in the source directory
- if not relativedir then
- relativedir = path.directory(sourcefile)
- end
-
- -- translate path
- --
- -- e.g.
- --
- -- src/xxx.c
- -- project/xmake.lua
- -- build/.objs
- --
- -- objectfile: project/build/.objs/xxxx/../../xxx.c will be out of range for objectdir
- --
- -- we need replace '..' to '__' in this case
- --
- if path.is_absolute(relativedir) and os.host() == "windows" then
- relativedir = relativedir:gsub(":[\\/]*", '\\') -- replace C:\xxx\ => C\xxx\
- end
- relativedir = relativedir:gsub("%.%.", "__")
-
- -- make object file
- -- full file name(not base) to avoid name-clash of object file
- return path.join(self:objectdir(), relativedir, target.filename(path.filename(sourcefile), "object"))
+function _instance:objectfile(sourcefile)
+ return self:autogenfile(sourcefile, {rootdir = self:objectdir(), filename = target.filename(path.filename(sourcefile), "object")})
end
-- get the object files
diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua
index 24c482186..b7ad10eae 100644
--- a/xmake/rules/protobuf/proto.lua
+++ b/xmake/rules/protobuf/proto.lua
@@ -52,7 +52,9 @@ function main(target, sourcekind, sourcefile_proto, opt)
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", path.basename(sourcefile_proto) .. ".pb" .. (sourcekind == "cxx" and ".cc" or "-c.c"))
+ local rootdir = 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 = path.directory(sourcefile_cx)
-- add includedirs