From 2e73beed761d719182c5fbe65da3e6def590627f Mon Sep 17 00:00:00 2001 From: Nikutsuki Date: Tue, 16 Jun 2026 14:31:43 +0200 Subject: [PATCH] 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", + }, +}