From c582108917ba7b3a67b0d15b6d2f37f58e814441 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Mon, 21 Apr 2025 17:52:24 +0800 Subject: [PATCH] fix SEGV issue & merge xmake --- compiler/capnpc-c.c | 25 +++++++++---------- xmake.lua | 58 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 xmake.lua diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index af15071..9f4a77d 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -73,6 +73,7 @@ struct string_list { }; typedef struct { + struct capn capn; struct str HDR; struct str SRC; struct capn_segment g_valseg; @@ -1118,7 +1119,7 @@ static void encode_member(capnp_ctx_t *ctx, struct str *func, struct field *f, static void decode_member(capnp_ctx_t *ctx, struct str *func, struct field *f, const char *tab, const char *var, const char *var2) { struct Type list_type; - struct node * n = NULL; + struct node *n = NULL; if (f->v.t.which == Type__void) { return; @@ -1242,7 +1243,7 @@ void mk_struct_ptr_encoder(capnp_ctx_t *ctx, struct node *n) { } mapname = (char *)get_mapname(n->n.annotations); - + if (mapname == NULL) { sprintf(buf, "struct %s_", n->name.str); } else { @@ -1306,7 +1307,7 @@ void mk_struct_ptr_decoder(capnp_ctx_t *ctx, struct node *n) { } mapname = (char *)get_mapname(n->n.annotations); - + if (mapname == NULL) { sprintf(buf, "struct %s_", n->name.str); } else { @@ -2110,18 +2111,17 @@ static void declare_codec(capnp_ctx_t *ctx, struct node *file_node) { } } int ctx_init(capnp_ctx_t *ctx, FILE *fp) { - struct capn capn; struct capn_segment *current_seg = NULL; int total_len = 0; int i; struct node *n; memset(ctx, 0x0, sizeof(*ctx)); - if (capn_init_fp(&capn, fp, 0) < 0) { + if (capn_init_fp(&(ctx->capn), fp, 0) < 0) { return -1; } - current_seg = capn.seglist; + current_seg = ctx->capn.seglist; while (current_seg != NULL) { total_len += current_seg->len; current_seg = current_seg->next; @@ -2130,7 +2130,7 @@ int ctx_init(capnp_ctx_t *ctx, FILE *fp) { ctx->g_valseg.data = calloc(1, total_len); ctx->g_valseg.cap = total_len; - ctx->root.p = capn_getp(capn_root(&capn), 0, 1); + ctx->root.p = capn_getp(capn_root(&(ctx->capn)), 0, 1); read_CodeGeneratorRequest(&(ctx->req), ctx->root); for (i = 0; i < capn_len(ctx->req.nodes); i++) { @@ -2518,13 +2518,14 @@ int main(int argc, char *argv[]) { } } else { fp = stdin; -#if defined(_WIN32) - if (_setmode(_fileno(fp), _O_BINARY) == -1) { - fail(-1, "fail to set stdin to binary mode\n"); - } -#endif } +#if defined(_WIN32) + if (_setmode(_fileno(fp), _O_BINARY) == -1) { + fail(-1, "fail to set stdin to binary mode\n"); + } +#endif + ctx_init(&ctx, fp); ctx_resolve_names(&ctx); diff --git a/xmake.lua b/xmake.lua new file mode 100644 index 0000000..5c5bbf4 --- /dev/null +++ b/xmake.lua @@ -0,0 +1,58 @@ +add_rules("mode.debug", "mode.release") + +includes("@builtin/xpack") + +add_requires("gtest") + +option("trace") + set_default(false) + set_showmenu(true) + add_defines("DEBUG") + +target("CapnC_Runtime") + set_kind("static") + add_rules("utils.install.pkgconfig_importfiles") + add_files("lib/*.c") + add_headerfiles("lib/capnp_c.h") + add_options("trace") + +target("capnpc-c") + set_kind("binary") + add_files("compiler/*.c") + add_includedirs("lib", "compiler") + add_installfiles("compiler/c.capnp", "compiler/c++.capnp","compiler/schema.capnp",{ prefixdir = "share/schema"}) + add_deps("CapnC_Runtime") + add_options("trace") + +rule("capnproto.c") + set_extensions(".capnp") + on_build_file(function (target, sourcefile,opt) + os.setenv("PATH", string.format("%s:%s", target:targetdir(), os.getenv("PATH"))) + os.runv("capnp", {"compile", "-oc", "-Icompiler", sourcefile}) + end) + + +target("test") + set_kind("binary") + add_packages("gtest") + add_files("tests/*.c", "tests/*.cpp") + add_deps("CapnC_Runtime", "capnpc-c") + add_includedirs("compiler", "lib") + add_options("trace") + +target("book") + set_kind("binary") + set_policy("build.across_targets_in_parallel", false) + add_packages("capnpc-c") + add_rules("capnproto.c") + add_files("examples/book/book.capnp") + add_files("examples/book/*.c") + add_includedirs("compiler","lib","examples/book") + add_deps("CapnC_Runtime", "capnpc-c") + +xpack("capnpc-c") + set_formats("targz") + set_title("c-capnproto") + set_licensefile("COPYING") + add_targets("capnpc-c", "CapnC_Runtime") + \ No newline at end of file