From 2e73beed761d719182c5fbe65da3e6def590627f Mon Sep 17 00:00:00 2001 From: Nikutsuki Date: Tue, 16 Jun 2026 14:31:43 +0200 Subject: [PATCH 1/2] add zig build support --- .gitignore | 4 ++++ build.zig | 47 +++++++++++++++++++++++++++++++++++++++++++++++ build.zig.zon | 13 +++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 build.zig create mode 100644 build.zig.zon diff --git a/.gitignore b/.gitignore index 308fcab..8dae0c7 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,7 @@ dist/ *.tar.gz *.tgz *.zip + +# Zig artifacts +zig-out/ +.zig-cache/ \ No newline at end of file diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..d52041a --- /dev/null +++ b/build.zig @@ -0,0 +1,47 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const shared = b.option(bool, "build-shared", "Build a shared library") orelse true; + const reuse_alloc = b.option(bool, "reuse-allocator", "Reuse the library allocator") orelse false; + + const library_name = "tree-sitter-cpp"; + + const grammar = b.createModule(.{ + .target = target, + .optimize = optimize, + .link_libc = true, + .pic = if (shared) true else null, + }); + const lib: *std.Build.Step.Compile = b.addLibrary(.{ + .name = library_name, + .linkage = if (shared) .dynamic else .static, + .root_module = grammar, + }); + + grammar.addCSourceFiles(.{ + .files = &.{ "src/parser.c", "src/scanner.c" }, + .flags = &.{"-std=c11"}, + }); + + if (reuse_alloc) { + grammar.addCMacro("TREE_SITTER_REUSE_ALLOCATOR", ""); + } + if (optimize == .Debug) { + grammar.addCMacro("TREE_SITTER_DEBUG", ""); + } + + grammar.addIncludePath(b.path("src")); + + b.installArtifact(lib); + b.installFile("src/node-types.json", "node-types.json"); + + b.installDirectory(.{ + .source_dir = b.path("queries"), + .install_dir = .prefix, + .install_subdir = "queries", + .include_extensions = &.{"scm"}, + }); +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..631a6e3 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,13 @@ +.{ + .name = .tree_sitter_cpp, + .fingerprint = 0xb8f4bd40e27ed859, + .minimum_zig_version = "0.16.0", + .version = "0.23.4", + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + "queries", + "LICENSE", + }, +} From a95b63deb1dc5b90dbd4a459053892994a07ccc1 Mon Sep 17 00:00:00 2001 From: Nikutsuki Date: Fri, 26 Jun 2026 21:01:35 +0200 Subject: [PATCH 2/2] add zig bindings and build --- .gitignore | 14 ++++++++++---- bindings/zig/root.zig | 5 +++++ bindings/zig/test.zig | 16 ++++++++++++++++ build.zig | 20 ++++++++++++++++++++ build.zig.zon | 9 +++++++++ 5 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 bindings/zig/root.zig create mode 100644 bindings/zig/test.zig diff --git a/.gitignore b/.gitignore index 8dae0c7..87a0c80 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,16 @@ # Rust artifacts target/ +Cargo.lock # Node artifacts build/ prebuilds/ node_modules/ +package-lock.json # Swift artifacts .build/ +Package.resolved # Go artifacts _obj/ @@ -25,6 +28,13 @@ dist/ *.dylib *.dll *.pc +*.exp +*.lib + +# Zig artifacts +.zig-cache/ +zig-cache/ +zig-out/ # Example dirs /examples/*/ @@ -38,7 +48,3 @@ dist/ *.tar.gz *.tgz *.zip - -# Zig artifacts -zig-out/ -.zig-cache/ \ No newline at end of file diff --git a/bindings/zig/root.zig b/bindings/zig/root.zig new file mode 100644 index 0000000..e4b82bf --- /dev/null +++ b/bindings/zig/root.zig @@ -0,0 +1,5 @@ +extern fn tree_sitter_cpp() callconv(.c) *const anyopaque; + +pub fn language() *const anyopaque { + return tree_sitter_cpp(); +} diff --git a/bindings/zig/test.zig b/bindings/zig/test.zig new file mode 100644 index 0000000..bbae4f6 --- /dev/null +++ b/bindings/zig/test.zig @@ -0,0 +1,16 @@ +const testing = @import("std").testing; + +const ts = @import("tree-sitter"); +const root = @import("tree-sitter-cpp"); +const Parser = ts.Parser; + +test "can load grammar" { + const parser = Parser.create(); + defer parser.destroy(); + + const lang: *const ts.Language = @ptrCast(root.language()); + defer lang.destroy(); + + try testing.expectEqual(void{}, parser.setLanguage(lang)); + try testing.expectEqual(lang, parser.getLanguage()); +} diff --git a/build.zig b/build.zig index d52041a..a1bf7a9 100644 --- a/build.zig +++ b/build.zig @@ -44,4 +44,24 @@ pub fn build(b: *std.Build) void { .install_subdir = "queries", .include_extensions = &.{"scm"}, }); + + const module = b.addModule(library_name, .{ + .root_source_file = b.path("bindings/zig/root.zig"), + .target = target, + .optimize = optimize, + }); + module.linkLibrary(lib); + + const tests = b.addTest(.{ + .root_module = b.createModule(.{ + .root_source_file = b.path("bindings/zig/test.zig"), + .target = target, + .optimize = optimize, + }), + }); + tests.root_module.addImport(library_name, module); + + const run_tests = b.addRunArtifact(tests); + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_tests.step); } diff --git a/build.zig.zon b/build.zig.zon index 631a6e3..ba13cd3 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,11 +3,20 @@ .fingerprint = 0xb8f4bd40e27ed859, .minimum_zig_version = "0.16.0", .version = "0.23.4", + .dependencies = .{ + .tree_sitter = .{ + .url = "git+https://github.com/tree-sitter/zig-tree-sitter#b4b72c903e69998fc88e27e154a5e3cc9166551b", + .hash = "tree_sitter-0.25.0-8heIf51vAQConvVIgvm-9mVIbqh7yabZYqPXfOpS3YoG", + .lazy = true, + }, + }, .paths = .{ "build.zig", "build.zig.zon", + "bindings/zig", "src", "queries", "LICENSE", }, } +