1
1
const std = @import ("std" );
2
+ const ts = @import ("tree_sitter" );
3
+
4
+ const wasm_url = "https://github.com/tree-sitter/tree-sitter-c/releases/download/v0.24.1/tree-sitter-c.wasm" ;
2
5
3
6
pub fn build (b : * std.Build ) ! void {
4
7
const target = b .standardTargetOptions (.{});
5
8
const optimize = b .standardOptimizeOption (.{});
6
9
7
- const core = b .dependency ("tree_sitter" , .{
10
+ const options = b .addOptions ();
11
+ const enable_wasm = b .option (bool , "enable-wasm" , "Enable Wasm support" ) orelse false ;
12
+ options .addOption (bool , "enable_wasm" , enable_wasm );
13
+
14
+ const core = b .dependencyFromBuildZig (ts , .{
8
15
.target = target ,
9
16
.optimize = optimize ,
17
+ .amalgamated = true ,
18
+ .@"build-shared" = false ,
19
+ .@"enable-wasm" = enable_wasm ,
10
20
});
11
21
const core_lib = core .artifact ("tree-sitter" );
22
+ const wasmtime = if (enable_wasm ) core .builder .lazyDependency (ts .wasmtimeDep (target .result ), .{}) else null ;
12
23
13
24
const module = b .addModule ("tree_sitter" , .{
14
25
.root_source_file = b .path ("src/root.zig" ),
15
26
.target = target ,
16
27
.optimize = optimize ,
17
28
});
18
29
module .linkLibrary (core_lib );
30
+ module .addOptions ("build" , options );
31
+ if (wasmtime ) | dep | {
32
+ module .addLibraryPath (dep .path ("lib" ));
33
+ module .linkSystemLibrary ("wasmtime" , .{
34
+ .preferred_link_mode = .static ,
35
+ });
36
+ }
19
37
20
38
const docs = b .addObject (.{
21
39
.name = "tree_sitter" ,
22
40
.root_source_file = b .path ("src/root.zig" ),
23
41
.target = target ,
24
42
.optimize = .Debug ,
25
43
});
44
+ docs .root_module .addOptions ("build" , options );
26
45
27
46
const install_docs = b .addInstallDirectory (.{
28
47
.source_dir = docs .getEmittedDocs (),
@@ -39,9 +58,31 @@ pub fn build(b: *std.Build) !void {
39
58
.optimize = optimize ,
40
59
});
41
60
tests .linkLibrary (core_lib );
61
+ tests .root_module .addOptions ("build" , options );
62
+ if (wasmtime ) | dep | {
63
+ tests .root_module .addLibraryPath (dep .path ("lib" ));
64
+ tests .root_module .linkSystemLibrary ("wasmtime" , .{
65
+ .preferred_link_mode = .static ,
66
+ });
67
+ tests .root_module .linkSystemLibrary ("unwind" , .{});
68
+ if (target .result .os .tag == .windows ) {
69
+ if (target .result .abi != .msvc ) {
70
+ tests .root_module .linkSystemLibrary ("unwind" , .{});
71
+ tests .root_module .linkSystemLibrary ("advapi32" , .{});
72
+ tests .root_module .linkSystemLibrary ("bcrypt" , .{});
73
+ tests .root_module .linkSystemLibrary ("ntdll" , .{});
74
+ tests .root_module .linkSystemLibrary ("ole32" , .{});
75
+ tests .root_module .linkSystemLibrary ("shell32" , .{});
76
+ tests .root_module .linkSystemLibrary ("userenv" , .{});
77
+ tests .root_module .linkSystemLibrary ("ws2_32" , .{});
78
+ } else {
79
+ const fail = b .addFail ("FIXME: cannot build with enable-wasm for MSVC" );
80
+ tests .step .dependOn (& fail .step );
81
+ }
82
+ }
83
+ }
42
84
43
85
const run_tests = b .addRunArtifact (tests );
44
-
45
86
const test_step = b .step ("test" , "Run unit tests" );
46
87
test_step .dependOn (& run_tests .step );
47
88
@@ -55,6 +96,19 @@ pub fn build(b: *std.Build) !void {
55
96
.optimize = optimize ,
56
97
}) orelse continue ;
57
98
tests .linkLibrary (dep .artifact ("tree-sitter-c" ));
99
+
100
+ if (enable_wasm ) {
101
+ // FIXME: prevent the file from being downloaded multiple times
102
+ std .log .info ("Downloading {s}\n " , .{ wasm_url });
103
+ const run_curl = b .addSystemCommand (&.{"curl" , "-LSsf" , wasm_url , "-o" });
104
+ const wasm_file = run_curl .addOutputFileArg ("tree-sitter-c.wasm" );
105
+ run_curl .expectStdErrEqual ("" );
106
+ tests .step .dependOn (& run_curl .step );
107
+ tests .root_module .addAnonymousImport ("tree-sitter-c.wasm" , .{
108
+ .root_source_file = wasm_file ,
109
+ });
110
+ }
111
+
58
112
break ;
59
113
}
60
114
}
0 commit comments