Skip to content

Commit 2ffa320

Browse files
authored
Merge pull request #1495 from wsw0108/rule-capnproto
Add rule 'capnproto.cpp'
2 parents 28a463f + 47600ee commit 2ffa320

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@0xd30600b3651feef7;
2+
3+
using Cxx = import "/capnp/c++.capnp";
4+
$Cxx.namespace("test::proto");
5+
6+
struct Message {
7+
text @0 :Text;
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <capnp/message.h>
2+
#include "message.capnp.h"
3+
4+
int main() {
5+
capnp::MallocMessageBuilder builder;
6+
// test::proto::Message msg;
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
add_rules("mode.debug", "mode.release")
2+
add_requires("capnproto")
3+
4+
target("test")
5+
set_kind("binary")
6+
set_languages("c++14")
7+
add_packages("capnproto")
8+
add_files("src/**.cc")
9+
add_files("proto/*.capnp", {rules = "capnproto.cpp", capnp_rootdir = "proto"})

xmake/rules/capnproto/capnp.lua

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
--!A cross-platform build utility based on Lua
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
--
15+
-- Copyright (C) 2015-present, TBOOX Open Source Group.
16+
--
17+
-- @author wsw0108
18+
-- @file capnp.lua
19+
--
20+
21+
-- imports
22+
import("lib.detect.find_tool")
23+
24+
-- get capnp
25+
function _get_capnp(target)
26+
27+
-- find capnp
28+
local capnp = target:data("capnproto.capnp")
29+
if not capnp then
30+
capnp = find_tool("capnp")
31+
if capnp and capnp.program then
32+
target:data_set("capnproto.capnp", capnp.program)
33+
end
34+
end
35+
36+
-- get capnp
37+
return assert(target:data("capnproto.capnp"), "capnp not found!")
38+
end
39+
40+
-- generate build commands
41+
function buildcmd(target, batchcmds, sourcefile_capnp, opt)
42+
43+
-- get capnp
44+
local capnp = _get_capnp(target)
45+
46+
-- get c/c++ source file for capnproto
47+
local prefixdir
48+
local public
49+
local fileconfig = target:fileconfig(sourcefile_capnp)
50+
if fileconfig then
51+
public = fileconfig.capnp_public
52+
prefixdir = fileconfig.capnp_rootdir
53+
end
54+
local rootdir = path.join(target:autogendir(), "rules", "capnproto")
55+
local filename = path.basename(sourcefile_capnp) .. ".capnp.c++"
56+
local sourcefile_cx = target:autogenfile(sourcefile_capnp, {rootdir = rootdir, filename = filename})
57+
local sourcefile_dir = prefixdir and path.join(rootdir, prefixdir) or path.directory(sourcefile_cx)
58+
59+
-- add includedirs
60+
target:add("includedirs", sourcefile_dir, {public = public})
61+
62+
-- add objectfile
63+
local objectfile = target:objectfile(sourcefile_cx)
64+
table.insert(target:objectfiles(), objectfile)
65+
66+
-- add commands
67+
batchcmds:mkdir(sourcefile_dir)
68+
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.capnp %s", sourcefile_capnp)
69+
local capnproto = target:pkg("capnproto")
70+
local includes = capnproto:get("sysincludedirs")
71+
local argv = {"compile"}
72+
for _, value in ipairs(includes) do
73+
table.insert(argv, "-I" .. value)
74+
end
75+
table.insert(argv, "-I" .. (prefixdir and prefixdir or path.directory(sourcefile_capnp)))
76+
if prefixdir then
77+
table.insert(argv, "--src-prefix=" .. prefixdir)
78+
end
79+
table.insert(argv, "-o")
80+
table.insert(argv, "c++:" .. sourcefile_dir)
81+
table.insert(argv, sourcefile_capnp)
82+
batchcmds:vrunv(capnp, argv)
83+
local configs = {includedirs = sourcefile_dir, languages = "c++14"}
84+
if package:is_plat("windows") then
85+
configs.cxflags = "/TP"
86+
end
87+
batchcmds:compile(sourcefile_cx, objectfile, {sourcekind = "cxx", configs = configs})
88+
89+
-- add deps
90+
batchcmds:add_depfiles(sourcefile_capnp)
91+
batchcmds:set_depmtime(os.mtime(objectfile))
92+
batchcmds:set_depcache(target:dependfile(objectfile))
93+
end

xmake/rules/capnproto/xmake.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--!A cross-platform build utility based on Lua
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
--
15+
-- Copyright (C) 2015-present, TBOOX Open Source Group.
16+
--
17+
-- @author wsw0108
18+
-- @file xmake.lua
19+
--
20+
21+
-- define rule: capnproto.cpp
22+
rule("capnproto.cpp")
23+
set_extensions(".capnp")
24+
before_buildcmd_file(function (target, batchcmds, sourcefile_capnp, opt)
25+
return import("capnp").buildcmd(target, batchcmds, sourcefile_capnp, opt)
26+
end)

0 commit comments

Comments
 (0)