tree-sitter: add optional wasm support

Add a wasmSupport override that builds libtree-sitter against wasmtime_36 while leaving the default derivation unchanged.
This commit is contained in:
Austin Horstman
2026-06-22 07:46:35 -05:00
parent 905b7c6884
commit a4ad2cc8d6
+35
View File
@@ -18,8 +18,11 @@
substitute,
installShellFiles,
buildPackages,
cmake,
wasmtime_36,
enableShared ? !stdenv.hostPlatform.isStatic,
enableStatic ? stdenv.hostPlatform.isStatic,
wasmSupport ? false,
webUISupport ? false,
}:
@@ -124,9 +127,14 @@ rustPlatform.buildRustPackage (finalAttrs: {
cargoHash = "sha256-9FeWnWWPUWmMF15Psmul8GxGv2JceHWc2WZPmOr81gw=";
cargoBuildFeatures = lib.optionals wasmSupport [ "wasm" ];
buildInputs = [
installShellFiles
]
++ lib.optionals wasmSupport [
wasmtime_36
]
++ lib.optionals webUISupport [
openssl
];
@@ -134,6 +142,9 @@ rustPlatform.buildRustPackage (finalAttrs: {
rustPlatform.bindgenHook
which
]
++ lib.optionals wasmSupport [
cmake
]
++ lib.optionals webUISupport [
emscripten
pkg-config
@@ -164,6 +175,15 @@ rustPlatform.buildRustPackage (finalAttrs: {
sed -i '/^install:/,/^[^[:space:]]/ { /$(SOEXT/d; }' ./Makefile
'';
# The Makefile install can't enable the wasm feature.
cmakeFlags = lib.optionals wasmSupport [
(lib.cmakeBool "TREE_SITTER_FEATURE_WASM" true)
(lib.cmakeFeature "WASMTIME_INCLUDE_DIR" "${lib.getDev wasmtime_36}/include")
(lib.cmakeFeature "WASMTIME_LIBRARY" "${lib.getLib wasmtime_36}/lib/libwasmtime${stdenv.hostPlatform.extensions.sharedLibrary}")
(lib.cmakeFeature "CMAKE_INSTALL_INCLUDEDIR" "include")
(lib.cmakeFeature "CMAKE_INSTALL_LIBDIR" "lib")
];
# Compile web assembly with emscripten. The --debug flag prevents us from
# minifying the JavaScript; passing it allows us to side-step more Node
# JS dependencies for installation.
@@ -175,6 +195,11 @@ rustPlatform.buildRustPackage (finalAttrs: {
postInstall = ''
PREFIX=$out make install
''
+ lib.optionalString wasmSupport ''
cmake --install $cmakeBuildDir
''
+ ''
${lib.optionalString (!enableShared) "rm -f $out/lib/*.so{,.*}"}
${lib.optionalString (!enableStatic) "rm -f $out/lib/*.a"}
@@ -196,6 +221,16 @@ rustPlatform.buildRustPackage (finalAttrs: {
# test result: FAILED. 120 passed; 13 failed; 0 ignored; 0 measured; 0 filtered out
doCheck = false;
# CMake builds libtree-sitter with wasm support; cargo still builds the CLI.
${if wasmSupport then "configurePhase" else null} = ''
cmakeConfigurePhase
cd ..
'';
${if wasmSupport then "postBuild" else null} = ''
cmake --build $cmakeBuildDir
'';
passthru = {
inherit
grammars