feat(nix): add cross compilation support

This commit is contained in:
Amaan Qureshi 2025-09-04 04:00:48 -04:00 committed by Amaan Qureshi
parent d287acfcc0
commit 17854168d9
3 changed files with 123 additions and 68 deletions

View file

@ -6,31 +6,25 @@
lib,
src,
version,
crossTargets,
...
}:
let
nativeBuildInputs = [
pkgs.pkg-config
pkgs.nodejs_22
];
buildInputs = [
pkgs.openssl
pkgs.installShellFiles
];
in
{
packages = {
cli = pkgs.rustPlatform.buildRustPackage {
inherit
src
version
nativeBuildInputs
buildInputs
;
buildCliFor =
targetPkgs:
let
isCross = targetPkgs.stdenv.hostPlatform != pkgs.stdenv.buildPlatform;
in
targetPkgs.rustPlatform.buildRustPackage {
inherit src version;
pname = "tree-sitter-cli";
nativeBuildInputs = [
pkgs.pkg-config
pkgs.nodejs_22
]
++ lib.optionals (!isCross) [ pkgs.installShellFiles ];
cargoLock.lockFile = ../../Cargo.lock;
preBuild = ''
@ -40,20 +34,17 @@
chmod -R u+w test/fixtures
'';
preCheck = ''
export HOME=$TMPDIR
'';
preCheck = "export HOME=$TMPDIR";
doCheck = !isCross;
doCheck = true;
postInstall = ''
installShellCompletion --cmd tree-sitter \
postInstall = lib.optionalString (!isCross) ''
installShellCompletion --cmd tree-sitter \
--bash <($out/bin/tree-sitter complete --shell bash) \
--zsh <($out/bin/tree-sitter complete --shell zsh) \
--zsh <($out/bin/tree-sitter complete --shell zsh) \
--fish <($out/bin/tree-sitter complete --shell fish)
'';
meta = {
meta = with lib; {
description = "Tree-sitter CLI - A tool for developing, testing, and using Tree-sitter parsers";
longDescription = ''
Tree-sitter is a parser generator tool and an incremental parsing library.
@ -63,13 +54,21 @@
'';
homepage = "https://tree-sitter.github.io/tree-sitter";
changelog = "https://github.com/tree-sitter/tree-sitter/releases/tag/v${version}";
license = lib.licenses.mit;
maintainers = [ lib.maintainers.amaanq ];
platforms = lib.platforms.all;
license = licenses.mit;
maintainers = [ maintainers.amaanq ];
platforms = platforms.all;
mainProgram = "tree-sitter";
};
};
crossPackages = lib.mapAttrs (name: targetPkgs: buildCliFor targetPkgs) crossTargets;
in
{
packages = {
cli = buildCliFor pkgs;
}
// (lib.mapAttrs' (name: pkg: lib.nameValuePair "cli-${name}" pkg) crossPackages)
// {
rust-fmt =
pkgs.runCommand "rust-fmt-check"
{

View file

@ -91,10 +91,31 @@
};
testGrammars = lib.mapAttrs (name: spec: fetchGrammar name spec.rev spec.sha256) grammarSpecs;
crossTargets = {
aarch64-linux = pkgs.pkgsCross.aarch64-multiplatform;
armv7l-linux = pkgs.pkgsCross.armv7l-hf-multiplatform;
x86_64-linux = pkgs.pkgsCross.gnu64;
i686-linux = pkgs.pkgsCross.gnu32;
loongarch64 = pkgs.pkgsCross.loongarch64-linux;
mips = pkgs.pkgsCross.mips-linux-gnu;
mips64 = pkgs.pkgsCross.mips64-linux-gnuabi64;
musl64 = pkgs.pkgsCross.musl64;
powerpc64-linux = pkgs.pkgsCross.ppc64;
riscv32 = pkgs.pkgsCross.riscv32;
riscv64 = pkgs.pkgsCross.riscv64;
s390x = pkgs.pkgsCross.s390x;
x86_64-windows = pkgs.pkgsCross.mingwW64;
}
// (lib.optionalAttrs pkgs.stdenv.isDarwin {
x86_64-darwin = pkgs.pkgsCross.x86_64-darwin;
aarch64-darwin = pkgs.pkgsCross.aarch64-darwin;
});
in
{
_module.args = {
inherit src version;
inherit src version crossTargets;
};
packages = {
@ -241,6 +262,27 @@
echo " nix build .#web-tree-sitter - Build WASM bindings"
echo " nix build .#docs - Build documentation"
echo ""
echo "Cross-compilation:"
echo " Build for other platforms using .#cli-<target> for the CLI,"
echo " and .#lib-<target> for the library (e.g. nix build .#cli-aarch64-linux)."
echo ""
echo " Available targets:"
echo " aarch64-linux - ARM64 Linux"
echo " armv7l-linux - ARMv7 Linux"
echo " x86_64-linux - x86_64 Linux"
echo " i686-linux - i686 Linux"
echo " loongarch64 - LoongArch64 Linux"
echo " mips - MIPS Linux"
echo " mips64 - MIPS64 Linux"
echo " musl64 - x86_64 MUSL Linux"
echo " powerpc64-linux - PowerPC64 Linux"
echo " riscv32 - RISC-V 32-bit Linux"
echo " riscv64 - RISC-V 64-bit Linux"
echo " s390x - s390x Linux"
echo " x86_64-windows - x86_64 Windows"
echo " x86_64-darwin - x86_64 macOS (Darwin only)"
echo " aarch64-darwin - ARM64 macOS (Darwin only)"
echo ""
echo "Apps:"
echo " nix run .#cli - Run tree-sitter CLI"
echo " nix run .#docs - Serve docs locally"

View file

@ -5,50 +5,64 @@
lib,
src,
version,
crossTargets,
...
}:
{
packages.lib = pkgs.stdenv.mkDerivation {
inherit src version;
let
buildLibFor =
targetPkgs:
let
isCross = targetPkgs.stdenv.hostPlatform != pkgs.stdenv.buildPlatform;
in
targetPkgs.stdenv.mkDerivation {
inherit src version;
pname = "tree-sitter";
pname = "tree-sitter";
nativeBuildInputs = [
pkgs.cmake
pkgs.pkg-config
];
nativeBuildInputs = [
targetPkgs.cmake
targetPkgs.pkg-config
];
sourceRoot = "source/lib";
sourceRoot = "source/lib";
cmakeFlags = [
"-DBUILD_SHARED_LIBS=ON"
"-DCMAKE_INSTALL_LIBDIR=lib"
"-DCMAKE_INSTALL_INCLUDEDIR=include"
"-DTREE_SITTER_FEATURE_WASM=OFF"
];
cmakeFlags = [
"-DBUILD_SHARED_LIBS=ON"
"-DCMAKE_INSTALL_LIBDIR=lib"
"-DCMAKE_INSTALL_INCLUDEDIR=include"
"-DTREE_SITTER_FEATURE_WASM=OFF"
];
enableParallelBuilding = true;
enableParallelBuilding = true;
postInstall = ''
mkdir -p $out/{lib/pkgconfig,share/tree-sitter}
substituteInPlace $out/lib/pkgconfig/tree-sitter.pc \
--replace-fail "\''${prefix}" "$out" 2>/dev/null
'';
meta = {
description = "Tree-sitter incremental parsing library";
longDescription = ''
Tree-sitter is a parser generator tool and an incremental parsing library.
It can build a concrete syntax tree for a source file and efficiently update
the syntax tree as the source file is edited. This package provides the core
C library that can be used to parse source code using Tree-sitter grammars.
postInstall = ''
mkdir -p $out/{lib/pkgconfig,share/tree-sitter}
substituteInPlace $out/lib/pkgconfig/tree-sitter.pc \
--replace-fail "\''${prefix}" "$out" 2>/dev/null
'';
homepage = "https://tree-sitter.github.io/tree-sitter";
changelog = "https://github.com/tree-sitter/tree-sitter/releases/tag/v${version}";
license = lib.licenses.mit;
maintainers = [ lib.maintainers.amaanq ];
platforms = lib.platforms.all;
meta = {
description = "Tree-sitter incremental parsing library";
longDescription = ''
Tree-sitter is a parser generator tool and an incremental parsing library.
It can build a concrete syntax tree for a source file and efficiently update
the syntax tree as the source file is edited. This package provides the core
C library that can be used to parse source code using Tree-sitter grammars.
'';
homepage = "https://tree-sitter.github.io/tree-sitter";
changelog = "https://github.com/tree-sitter/tree-sitter/releases/tag/v${version}";
license = lib.licenses.mit;
maintainers = [ lib.maintainers.amaanq ];
platforms = lib.platforms.all;
};
};
};
crossPackages = lib.mapAttrs (name: targetPkgs: buildLibFor targetPkgs) crossTargets;
in
{
packages = {
lib = buildLibFor pkgs;
}
// (lib.mapAttrs' (name: pkg: lib.nameValuePair "lib-${name}" pkg) crossPackages);
};
}