summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-07-08 23:58:35 +0800
committerGitHub <[email protected]>2021-07-08 23:58:35 +0800
commit2ffa320d71f61fae814b24a477b743cc06292b04 (patch)
tree629f73ab45cc3b93d3e33da36f356534b5e3e850
parent28a463f8df23b65949a5cac73de4894fa7ec2785 (diff)
parent47600ee26c6fe6644b0152749edfc24f55fbd409 (diff)
Merge pull request #1495 from wsw0108/rule-capnproto
Add rule 'capnproto.cpp'
-rw-r--r--tests/projects/c++/capnproto/proto/message.capnp8
-rw-r--r--tests/projects/c++/capnproto/src/main.cc7
-rw-r--r--tests/projects/c++/capnproto/xmake.lua9
-rw-r--r--xmake/rules/capnproto/capnp.lua93
-rw-r--r--xmake/rules/capnproto/xmake.lua26
5 files changed, 143 insertions, 0 deletions
diff --git a/tests/projects/c++/capnproto/proto/message.capnp b/tests/projects/c++/capnproto/proto/message.capnp
new file mode 100644
index 000000000..7109294ed
--- /dev/null
+++ b/tests/projects/c++/capnproto/proto/message.capnp
@@ -0,0 +1,8 @@
+@0xd30600b3651feef7;
+
+using Cxx = import "/capnp/c++.capnp";
+$Cxx.namespace("test::proto");
+
+struct Message {
+ text @0 :Text;
+}
diff --git a/tests/projects/c++/capnproto/src/main.cc b/tests/projects/c++/capnproto/src/main.cc
new file mode 100644
index 000000000..8145c6cba
--- /dev/null
+++ b/tests/projects/c++/capnproto/src/main.cc
@@ -0,0 +1,7 @@
+#include <capnp/message.h>
+#include "message.capnp.h"
+
+int main() {
+ capnp::MallocMessageBuilder builder;
+ // test::proto::Message msg;
+}
diff --git a/tests/projects/c++/capnproto/xmake.lua b/tests/projects/c++/capnproto/xmake.lua
new file mode 100644
index 000000000..d4859dabb
--- /dev/null
+++ b/tests/projects/c++/capnproto/xmake.lua
@@ -0,0 +1,9 @@
+add_rules("mode.debug", "mode.release")
+add_requires("capnproto")
+
+target("test")
+ set_kind("binary")
+ set_languages("c++14")
+ add_packages("capnproto")
+ add_files("src/**.cc")
+ add_files("proto/*.capnp", {rules = "capnproto.cpp", capnp_rootdir = "proto"})
diff --git a/xmake/rules/capnproto/capnp.lua b/xmake/rules/capnproto/capnp.lua
new file mode 100644
index 000000000..2bd25c0e2
--- /dev/null
+++ b/xmake/rules/capnproto/capnp.lua
@@ -0,0 +1,93 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author wsw0108
+-- @file capnp.lua
+--
+
+-- imports
+import("lib.detect.find_tool")
+
+-- get capnp
+function _get_capnp(target)
+
+ -- find capnp
+ local capnp = target:data("capnproto.capnp")
+ if not capnp then
+ capnp = find_tool("capnp")
+ if capnp and capnp.program then
+ target:data_set("capnproto.capnp", capnp.program)
+ end
+ end
+
+ -- get capnp
+ return assert(target:data("capnproto.capnp"), "capnp not found!")
+end
+
+-- generate build commands
+function buildcmd(target, batchcmds, sourcefile_capnp, opt)
+
+ -- get capnp
+ local capnp = _get_capnp(target)
+
+ -- get c/c++ source file for capnproto
+ local prefixdir
+ local public
+ local fileconfig = target:fileconfig(sourcefile_capnp)
+ if fileconfig then
+ public = fileconfig.capnp_public
+ prefixdir = fileconfig.capnp_rootdir
+ end
+ local rootdir = path.join(target:autogendir(), "rules", "capnproto")
+ local filename = path.basename(sourcefile_capnp) .. ".capnp.c++"
+ local sourcefile_cx = target:autogenfile(sourcefile_capnp, {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})
+
+ -- add objectfile
+ local objectfile = target:objectfile(sourcefile_cx)
+ table.insert(target:objectfiles(), objectfile)
+
+ -- add commands
+ batchcmds:mkdir(sourcefile_dir)
+ batchcmds:show_progress(opt.progress, "${color.build.object}compiling.capnp %s", sourcefile_capnp)
+ local capnproto = target:pkg("capnproto")
+ local includes = capnproto:get("sysincludedirs")
+ local argv = {"compile"}
+ for _, value in ipairs(includes) do
+ table.insert(argv, "-I" .. value)
+ end
+ table.insert(argv, "-I" .. (prefixdir and prefixdir or path.directory(sourcefile_capnp)))
+ if prefixdir then
+ table.insert(argv, "--src-prefix=" .. prefixdir)
+ end
+ table.insert(argv, "-o")
+ table.insert(argv, "c++:" .. sourcefile_dir)
+ table.insert(argv, sourcefile_capnp)
+ batchcmds:vrunv(capnp, argv)
+ local configs = {includedirs = sourcefile_dir, languages = "c++14"}
+ if package:is_plat("windows") then
+ configs.cxflags = "/TP"
+ end
+ batchcmds:compile(sourcefile_cx, objectfile, {sourcekind = "cxx", configs = configs})
+
+ -- add deps
+ batchcmds:add_depfiles(sourcefile_capnp)
+ batchcmds:set_depmtime(os.mtime(objectfile))
+ batchcmds:set_depcache(target:dependfile(objectfile))
+end
diff --git a/xmake/rules/capnproto/xmake.lua b/xmake/rules/capnproto/xmake.lua
new file mode 100644
index 000000000..651c5bd47
--- /dev/null
+++ b/xmake/rules/capnproto/xmake.lua
@@ -0,0 +1,26 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author wsw0108
+-- @file xmake.lua
+--
+
+-- define rule: capnproto.cpp
+rule("capnproto.cpp")
+ set_extensions(".capnp")
+ before_buildcmd_file(function (target, batchcmds, sourcefile_capnp, opt)
+ return import("capnp").buildcmd(target, batchcmds, sourcefile_capnp, opt)
+ end)