From 9d2196cdbd4ac4e00654278fbdfb0dbbefd970dc Mon Sep 17 00:00:00 2001 From: ObserverOfTime Date: Fri, 4 Oct 2024 19:11:30 +0300 Subject: [PATCH] build(zig): add optional wasmtime dependency And support compiling a shared library --- build.zig | 122 +++++++++++++++++++++++++++++++++++++++++----- build.zig.zon | 73 ++++++++++++++++++++++++--- xtask/src/bump.rs | 4 +- 3 files changed, 177 insertions(+), 22 deletions(-) diff --git a/build.zig b/build.zig index da577bae..7b30966d 100644 --- a/build.zig +++ b/build.zig @@ -1,20 +1,116 @@ const std = @import("std"); -pub fn build(b: *std.Build) void { - var lib = b.addStaticLibrary(.{ - .name = "tree-sitter", - .target = b.standardTargetOptions(.{}), - .optimize = b.standardOptimizeOption(.{}), +pub fn build(b: *std.Build) !void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const wasm = b.option(bool, "enable-wasm", "Enable Wasm support") orelse false; + const shared = b.option(bool, "build-shared", "Build a shared library") orelse false; + const amalgamated = b.option(bool, "amalgamated", "Build using an amalgamated source") orelse false; + + const lib: *std.Build.Step.Compile = if (!shared) b.addStaticLibrary(.{ + .name = "tree-sitter", + .target = target, + .optimize = optimize, + .link_libc = true, + }) else b.addSharedLibrary(.{ + .name = "tree-sitter", + .pic = true, + .target = target, + .optimize = optimize, + .link_libc = true, + }); + + if (amalgamated) { + lib.addCSourceFile(.{ + .file = b.path("lib/src/lib.c"), + .flags = &.{"-std=c11"}, }); + } else { + lib.addCSourceFiles(.{ + .root = b.path("lib/src"), + .files = try findSourceFiles(b), + .flags = &.{"-std=c11"}, + }); + } - lib.linkLibC(); - lib.addCSourceFile(.{ .file = b.path("lib/src/lib.c"), .flags = &.{"-std=c11"} }); - lib.addIncludePath(b.path("lib/include")); - lib.addIncludePath(b.path("lib/src")); - lib.root_module.addCMacro("_POSIX_C_SOURCE", "200112L"); - lib.root_module.addCMacro("_DEFAULT_SOURCE", ""); + lib.addIncludePath(b.path("lib/include")); + lib.addIncludePath(b.path("lib/src")); + lib.addIncludePath(b.path("lib/src/wasm")); - lib.installHeadersDirectory(b.path("lib/include"), ".", .{}); + lib.root_module.addCMacro("_POSIX_C_SOURCE", "200112L"); + lib.root_module.addCMacro("_DEFAULT_SOURCE", ""); - b.installArtifact(lib); + if (wasm) { + if (b.lazyDependency(wasmtimeDep(target.result), .{})) |wasmtime| { + lib.root_module.addCMacro("TREE_SITTER_FEATURE_WASM", ""); + lib.addSystemIncludePath(wasmtime.path("include")); + lib.addLibraryPath(wasmtime.path("lib")); + lib.linkSystemLibrary("wasmtime"); + } + } + + lib.installHeadersDirectory(b.path("lib/include"), ".", .{}); + + b.installArtifact(lib); +} + +fn wasmtimeDep(target: std.Target) []const u8 { + const arch = target.cpu.arch; + const os = target.os.tag; + const abi = target.abi; + return switch (os) { + .linux => switch (arch) { + .x86_64 => switch (abi) { + .gnu => "wasmtime_c_api_x86_64_linux", + .musl => "wasmtime_c_api_x86_64_musl", + .android => "wasmtime_c_api_x86_64_android", + else => null + }, + .aarch64 => switch (abi) { + .gnu => "wasmtime_c_api_aarch64_linux", + .android => "wasmtime_c_api_aarch64_android", + else => null + }, + .s390x => "wasmtime_c_api_s390x_linux", + .riscv64 => "wasmtime_c_api_riscv64gc_linux", + else => null + }, + .windows => switch (arch) { + .x86_64 => switch (abi) { + .gnu => "wasmtime_c_api_x86_64_mingw", + .msvc => "wasmtime_c_api_x86_64_windows", + else => null + }, + else => null + }, + .macos => switch (arch) { + .x86_64 => "wasmtime_c_api_x86_64_macos", + .aarch64 => "wasmtime_c_api_aarch64_macos", + else => null + }, + else => null + } orelse std.debug.panic( + "Unsupported target for wasmtime: {s}-{s}-{s}", + .{ @tagName(arch), @tagName(os), @tagName(abi) } + ); +} + +fn findSourceFiles(b: *std.Build) ![]const []const u8 { + var sources = std.ArrayList([]const u8).init(b.allocator); + + var dir = try std.fs.cwd().openDir("lib/src", .{ .iterate = true }); + var iter = dir.iterate(); + defer dir.close(); + + while (try iter.next()) |entry| { + if (entry.kind != .file) continue; + const file = entry.name; + const ext = std.fs.path.extension(file); + if (std.mem.eql(u8, ext, ".c") and !std.mem.eql(u8, file, "lib.c")) { + try sources.append(b.dupe(file)); + } + } + + return sources.items; } diff --git a/build.zig.zon b/build.zig.zon index 7f1f87fd..eaf58c64 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,10 +1,69 @@ .{ - .name = "tree-sitter", - .version = "0.25.0", - .paths = .{ - "build.zig", - "build.zig.zon", - "lib/src", - "lib/include", + .name = "tree-sitter", + .version = "0.25.0", + .paths = .{ + "build.zig", + "build.zig.zon", + "lib/src", + "lib/include", + "README.md", + "LICENSE", + }, + .dependencies = .{ + .wasmtime_c_api_aarch64_android = .{ + .url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-aarch64-android-c-api.tar.xz", + .hash = "1220ef4fe7e1ad2cd3d317dc994d41d826e0fc8d0de3f6baee80c6eef1b6fef28cd0", + .lazy = true, }, + .wasmtime_c_api_aarch64_linux = .{ + .url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-aarch64-linux-c-api.tar.xz", + .hash = "12207760e18e793ee0a10b2de12bddfc7ca619e2f2a7ff89306af7f72253bb165221", + .lazy = true, + }, + .wasmtime_c_api_aarch64_macos = .{ + .url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-aarch64-macos-c-api.tar.xz", + .hash = "1220e1f559d4a60ccb80e4c561c2d7dd6450348c2891c570fb40e9139f2ffd2dfc4c", + .lazy = true, + }, + .wasmtime_c_api_riscv64gc_linux = .{ + .url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-riscv64gc-linux-c-api.tar.xz", + .hash = "1220e6f848e1ed5e5ec30b311c5d61a3b7a8e54f33498c1da92b34a1d54d35932387", + .lazy = true, + }, + .wasmtime_c_api_s390x_linux = .{ + .url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-s390x-linux-c-api.tar.xz", + .hash = "12206996df721084acbf1deb6640b98b99758ffa4aaadcf1b986b9c5f449eaf1c2b1", + .lazy = true, + }, + .wasmtime_c_api_x86_64_android = .{ + .url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-android-c-api.tar.xz", + .hash = "12203593149a0462a0682e68e6d18c16b549dd38a838d166734614547caf2314afad", + .lazy = true, + }, + .wasmtime_c_api_x86_64_linux = .{ + .url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-linux-c-api.tar.xz", + .hash = "1220b36e34597f10c7c95f60cf9459bac313dd39e90a55e11d52147049f5c3f36941", + .lazy = true, + }, + .wasmtime_c_api_x86_64_macos = .{ + .url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-macos-c-api.tar.xz", + .hash = "12204f986e7eed25f9839945d35dd3b547c08e282dd31f18554e5385f544ae701c2b", + .lazy = true, + }, + .wasmtime_c_api_x86_64_mingw = .{ + .url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-mingw-c-api.zip", + .hash = "12203cff700bfbe02ca9abeba193728c2cd6f1f3ff25d503f82f85860b1cacaef9d6", + .lazy = true, + }, + .wasmtime_c_api_x86_64_musl = .{ + .url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-musl-c-api.tar.xz", + .hash = "1220cf429d85f886e12cbb8b44e6a8a288b4a67162b3935525c9480ebbeab8919a96", + .lazy = true, + }, + .wasmtime_c_api_x86_64_windows = .{ + .url = "https://github.com/bytecodealliance/wasmtime/releases/download/v25.0.1/wasmtime-v25.0.1-x86_64-windows-c-api.zip", + .hash = "1220073844a35f563949faa0d2d6cf02c173830047826d7baeda23fe4f2f8e149f54", + .lazy = true, + }, + } } diff --git a/xtask/src/bump.rs b/xtask/src/bump.rs index 2f7c74f1..671793cf 100644 --- a/xtask/src/bump.rs +++ b/xtask/src/bump.rs @@ -291,8 +291,8 @@ fn update_zig(next_version: &Version) -> Result<()> { let zig = zig .lines() .map(|line| { - if line.starts_with(" .version") { - format!(" .version = \"{next_version}\",") + if line.starts_with(" .version") { + format!(" .version = \"{next_version}\",") } else { line.to_string() }