Skip to content

Commit 9d2196c

Browse files
ObserverOfTimeamaanq
authored andcommitted
build(zig): add optional wasmtime dependency
And support compiling a shared library
1 parent cad2d03 commit 9d2196c

File tree

3 files changed

+177
-22
lines changed

3 files changed

+177
-22
lines changed

build.zig

Lines changed: 109 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,116 @@
11
const std = @import("std");
22

3-
pub fn build(b: *std.Build) void {
4-
var lib = b.addStaticLibrary(.{
5-
.name = "tree-sitter",
6-
.target = b.standardTargetOptions(.{}),
7-
.optimize = b.standardOptimizeOption(.{}),
3+
pub fn build(b: *std.Build) !void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
const wasm = b.option(bool, "enable-wasm", "Enable Wasm support") orelse false;
8+
const shared = b.option(bool, "build-shared", "Build a shared library") orelse false;
9+
const amalgamated = b.option(bool, "amalgamated", "Build using an amalgamated source") orelse false;
10+
11+
const lib: *std.Build.Step.Compile = if (!shared) b.addStaticLibrary(.{
12+
.name = "tree-sitter",
13+
.target = target,
14+
.optimize = optimize,
15+
.link_libc = true,
16+
}) else b.addSharedLibrary(.{
17+
.name = "tree-sitter",
18+
.pic = true,
19+
.target = target,
20+
.optimize = optimize,
21+
.link_libc = true,
22+
});
23+
24+
if (amalgamated) {
25+
lib.addCSourceFile(.{
26+
.file = b.path("lib/src/lib.c"),
27+
.flags = &.{"-std=c11"},
28+
});
29+
} else {
30+
lib.addCSourceFiles(.{
31+
.root = b.path("lib/src"),
32+
.files = try findSourceFiles(b),
33+
.flags = &.{"-std=c11"},
834
});
35+
}
36+
37+
lib.addIncludePath(b.path("lib/include"));
38+
lib.addIncludePath(b.path("lib/src"));
39+
lib.addIncludePath(b.path("lib/src/wasm"));
40+
41+
lib.root_module.addCMacro("_POSIX_C_SOURCE", "200112L");
42+
lib.root_module.addCMacro("_DEFAULT_SOURCE", "");
43+
44+
if (wasm) {
45+
if (b.lazyDependency(wasmtimeDep(target.result), .{})) |wasmtime| {
46+
lib.root_module.addCMacro("TREE_SITTER_FEATURE_WASM", "");
47+
lib.addSystemIncludePath(wasmtime.path("include"));
48+
lib.addLibraryPath(wasmtime.path("lib"));
49+
lib.linkSystemLibrary("wasmtime");
50+
}
51+
}
52+
53+
lib.installHeadersDirectory(b.path("lib/include"), ".", .{});
54+
55+
b.installArtifact(lib);
56+
}
57+
58+
fn wasmtimeDep(target: std.Target) []const u8 {
59+
const arch = target.cpu.arch;
60+
const os = target.os.tag;
61+
const abi = target.abi;
62+
return switch (os) {
63+
.linux => switch (arch) {
64+
.x86_64 => switch (abi) {
65+
.gnu => "wasmtime_c_api_x86_64_linux",
66+
.musl => "wasmtime_c_api_x86_64_musl",
67+
.android => "wasmtime_c_api_x86_64_android",
68+
else => null
69+
},
70+
.aarch64 => switch (abi) {
71+
.gnu => "wasmtime_c_api_aarch64_linux",
72+
.android => "wasmtime_c_api_aarch64_android",
73+
else => null
74+
},
75+
.s390x => "wasmtime_c_api_s390x_linux",
76+
.riscv64 => "wasmtime_c_api_riscv64gc_linux",
77+
else => null
78+
},
79+
.windows => switch (arch) {
80+
.x86_64 => switch (abi) {
81+
.gnu => "wasmtime_c_api_x86_64_mingw",
82+
.msvc => "wasmtime_c_api_x86_64_windows",
83+
else => null
84+
},
85+
else => null
86+
},
87+
.macos => switch (arch) {
88+
.x86_64 => "wasmtime_c_api_x86_64_macos",
89+
.aarch64 => "wasmtime_c_api_aarch64_macos",
90+
else => null
91+
},
92+
else => null
93+
} orelse std.debug.panic(
94+
"Unsupported target for wasmtime: {s}-{s}-{s}",
95+
.{ @tagName(arch), @tagName(os), @tagName(abi) }
96+
);
97+
}
98+
99+
fn findSourceFiles(b: *std.Build) ![]const []const u8 {
100+
var sources = std.ArrayList([]const u8).init(b.allocator);
9101

10-
lib.linkLibC();
11-
lib.addCSourceFile(.{ .file = b.path("lib/src/lib.c"), .flags = &.{"-std=c11"} });
12-
lib.addIncludePath(b.path("lib/include"));
13-
lib.addIncludePath(b.path("lib/src"));
14-
lib.root_module.addCMacro("_POSIX_C_SOURCE", "200112L");
15-
lib.root_module.addCMacro("_DEFAULT_SOURCE", "");
102+
var dir = try std.fs.cwd().openDir("lib/src", .{ .iterate = true });
103+
var iter = dir.iterate();
104+
defer dir.close();
16105

17-
lib.installHeadersDirectory(b.path("lib/include"), ".", .{});
106+
while (try iter.next()) |entry| {
107+
if (entry.kind != .file) continue;
108+
const file = entry.name;
109+
const ext = std.fs.path.extension(file);
110+
if (std.mem.eql(u8, ext, ".c") and !std.mem.eql(u8, file, "lib.c")) {
111+
try sources.append(b.dupe(file));
112+
}
113+
}
18114

19-
b.installArtifact(lib);
115+
return sources.items;
20116
}

build.zig.zon

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,69 @@
11
.{
2-
.name = "tree-sitter",
3-
.version = "0.25.0",
4-
.paths = .{
5-
"build.zig",
6-
"build.zig.zon",
7-
"lib/src",
8-
"lib/include",
2+
.name = "tree-sitter",
3+
.version = "0.25.0",
4+
.paths = .{
5+
"build.zig",
6+
"build.zig.zon",
7+
"lib/src",
8+
"lib/include",
9+
"README.md",
10+
"LICENSE",
11+
},
12+
.dependencies = .{
13+
.wasmtime_c_api_aarch64_android = .{
14+
.url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-aarch64-android-c-api.tar.xz",
15+
.hash = "1220ef4fe7e1ad2cd3d317dc994d41d826e0fc8d0de3f6baee80c6eef1b6fef28cd0",
16+
.lazy = true,
917
},
18+
.wasmtime_c_api_aarch64_linux = .{
19+
.url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-aarch64-linux-c-api.tar.xz",
20+
.hash = "12207760e18e793ee0a10b2de12bddfc7ca619e2f2a7ff89306af7f72253bb165221",
21+
.lazy = true,
22+
},
23+
.wasmtime_c_api_aarch64_macos = .{
24+
.url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-aarch64-macos-c-api.tar.xz",
25+
.hash = "1220e1f559d4a60ccb80e4c561c2d7dd6450348c2891c570fb40e9139f2ffd2dfc4c",
26+
.lazy = true,
27+
},
28+
.wasmtime_c_api_riscv64gc_linux = .{
29+
.url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-riscv64gc-linux-c-api.tar.xz",
30+
.hash = "1220e6f848e1ed5e5ec30b311c5d61a3b7a8e54f33498c1da92b34a1d54d35932387",
31+
.lazy = true,
32+
},
33+
.wasmtime_c_api_s390x_linux = .{
34+
.url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-s390x-linux-c-api.tar.xz",
35+
.hash = "12206996df721084acbf1deb6640b98b99758ffa4aaadcf1b986b9c5f449eaf1c2b1",
36+
.lazy = true,
37+
},
38+
.wasmtime_c_api_x86_64_android = .{
39+
.url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-android-c-api.tar.xz",
40+
.hash = "12203593149a0462a0682e68e6d18c16b549dd38a838d166734614547caf2314afad",
41+
.lazy = true,
42+
},
43+
.wasmtime_c_api_x86_64_linux = .{
44+
.url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-linux-c-api.tar.xz",
45+
.hash = "1220b36e34597f10c7c95f60cf9459bac313dd39e90a55e11d52147049f5c3f36941",
46+
.lazy = true,
47+
},
48+
.wasmtime_c_api_x86_64_macos = .{
49+
.url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-macos-c-api.tar.xz",
50+
.hash = "12204f986e7eed25f9839945d35dd3b547c08e282dd31f18554e5385f544ae701c2b",
51+
.lazy = true,
52+
},
53+
.wasmtime_c_api_x86_64_mingw = .{
54+
.url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-mingw-c-api.zip",
55+
.hash = "12203cff700bfbe02ca9abeba193728c2cd6f1f3ff25d503f82f85860b1cacaef9d6",
56+
.lazy = true,
57+
},
58+
.wasmtime_c_api_x86_64_musl = .{
59+
.url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-musl-c-api.tar.xz",
60+
.hash = "1220cf429d85f886e12cbb8b44e6a8a288b4a67162b3935525c9480ebbeab8919a96",
61+
.lazy = true,
62+
},
63+
.wasmtime_c_api_x86_64_windows = .{
64+
.url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-windows-c-api.zip",
65+
.hash = "1220073844a35f563949faa0d2d6cf02c173830047826d7baeda23fe4f2f8e149f54",
66+
.lazy = true,
67+
},
68+
}
1069
}

xtask/src/bump.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ fn update_zig(next_version: &Version) -> Result<()> {
291291
let zig = zig
292292
.lines()
293293
.map(|line| {
294-
if line.starts_with(" .version") {
295-
format!(" .version = \"{next_version}\",")
294+
if line.starts_with(" .version") {
295+
format!(" .version = \"{next_version}\",")
296296
} else {
297297
line.to_string()
298298
}

0 commit comments

Comments
 (0)