summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-10 16:08:30 +0800
committerruki <[email protected]>2022-04-10 16:08:47 +0800
commitfd58f4c2842b2b484b4807d7b16e0ee569926a88 (patch)
tree51f6f7b1f7d0d916d92b3a67bece1e23561209db
parent78d8781400335645567eeedd1f59da15046b44d0 (diff)
improve proto rules
-rw-r--r--xmake/rules/protobuf/proto.lua31
-rw-r--r--xmake/rules/protobuf/xmake.lua14
2 files changed, 41 insertions, 4 deletions
diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua
index 88a00c989..56d3bcdec 100644
--- a/xmake/rules/protobuf/proto.lua
+++ b/xmake/rules/protobuf/proto.lua
@@ -49,6 +49,37 @@ function _get_protoc(target, sourcekind)
return assert(target:data(sourcekind == "cxx" and "protobuf.protoc" or "protobuf.protoc-c"), "protoc 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)
+
+ -- get the first sourcefile
+ local sourcefile_proto
+ local sourcebatch = target:sourcebatches()[sourcekind == "cxx" and "protobuf.cpp" or "protobuf.c"]
+ if sourcebatch then
+ sourcefile_proto = sourcebatch.sourcefiles[1]
+ end
+ if not sourcefile_proto then
+ return
+ end
+
+ -- get c/c++ source file for protobuf
+ local prefixdir
+ local public
+ local fileconfig = target:fileconfig(sourcefile_proto)
+ if fileconfig then
+ public = fileconfig.proto_public
+ prefixdir = fileconfig.proto_rootdir
+ end
+ 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 = prefixdir and path.join(rootdir, prefixdir) or path.directory(sourcefile_cx)
+
+ -- add includedirs
+ target:add("includedirs", sourcefile_dir, {public = public})
+end
+
-- generate build commands
function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind)
diff --git a/xmake/rules/protobuf/xmake.lua b/xmake/rules/protobuf/xmake.lua
index ac210de8c..31446eb8e 100644
--- a/xmake/rules/protobuf/xmake.lua
+++ b/xmake/rules/protobuf/xmake.lua
@@ -21,22 +21,28 @@
-- define rule: protobuf.cpp
rule("protobuf.cpp")
set_extensions(".proto")
+ on_load(function(target)
+ import("proto").load(target, "cxx")
+ end)
before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt)
- return import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cxx")
+ import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cxx")
end)
before_build_files(function (target, batchjobs, sourcebatch, opt)
- return import("proto").build_batchjobs(target, batchjobs, sourcebatch, opt, "cxx")
+ import("proto").build_batchjobs(target, batchjobs, sourcebatch, opt, "cxx")
end, {batch = true})
-- define rule: protobuf.c
rule("protobuf.c")
set_extensions(".proto")
+ on_load(function(target)
+ import("proto").load(target, "cc")
+ end)
before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt)
- return import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cc")
+ import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cc")
end)
before_build_files(function (target, batchjobs, sourcebatch, opt)
- return import("proto").build_batchjobs(target, batchjobs, sourcebatch, opt, "cc")
+ import("proto").build_batchjobs(target, batchjobs, sourcebatch, opt, "cc")
end, {batch = true})