2020-02-29 23:21:36 +00:00
|
|
|
{ stdenv
|
|
|
|
, tree-sitter
|
2021-05-31 15:20:22 +00:00
|
|
|
, lib
|
2020-12-13 05:54:25 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
# Build a parser grammar and put the resulting shared object in `$out/parser`
|
|
|
|
|
|
|
|
{
|
|
|
|
# language name
|
|
|
|
language
|
|
|
|
# version of tree-sitter
|
2020-02-29 23:21:36 +00:00
|
|
|
, version
|
2020-12-13 05:54:25 +00:00
|
|
|
# source for the language grammar
|
2020-02-29 23:21:36 +00:00
|
|
|
, source
|
2021-03-03 22:15:27 +00:00
|
|
|
, location ? null
|
2020-02-29 23:21:36 +00:00
|
|
|
}:
|
|
|
|
|
2021-10-29 16:52:37 +00:00
|
|
|
stdenv.mkDerivation rec {
|
2020-02-29 23:21:36 +00:00
|
|
|
|
2020-12-24 01:52:32 +00:00
|
|
|
pname = "${language}-grammar";
|
2020-02-29 23:21:36 +00:00
|
|
|
inherit version;
|
|
|
|
|
2021-10-29 16:52:37 +00:00
|
|
|
src = if location == null then source else "${source}/${location}";
|
2020-02-29 23:21:36 +00:00
|
|
|
|
|
|
|
buildInputs = [ tree-sitter ];
|
|
|
|
|
|
|
|
dontUnpack = true;
|
2021-07-18 21:42:48 +00:00
|
|
|
dontConfigure = true;
|
|
|
|
|
2021-10-29 16:52:37 +00:00
|
|
|
CFLAGS = [ "-I${src}/src" "-O2" ];
|
|
|
|
CXXFLAGS = [ "-I${src}/src" "-O2" ];
|
|
|
|
|
|
|
|
# When both scanner.{c,cc} exist, we should not link both since they may be the same but in
|
|
|
|
# different languages. Just randomly prefer C++ if that happens.
|
2020-02-29 23:21:36 +00:00
|
|
|
buildPhase = ''
|
|
|
|
runHook preBuild
|
2021-10-29 16:52:37 +00:00
|
|
|
if [[ -e "$src/src/scanner.cc" ]]; then
|
|
|
|
$CXX -c "$src/src/scanner.cc" -o scanner.o $CXXFLAGS
|
|
|
|
elif [[ -e "$src/src/scanner.c" ]]; then
|
|
|
|
$CC -c "$src/src/scanner.c" -o scanner.o $CFLAGS
|
2021-01-07 05:13:23 +00:00
|
|
|
fi
|
2021-10-29 16:52:37 +00:00
|
|
|
$CC -c "$src/src/parser.c" -o parser.o $CFLAGS
|
|
|
|
$CXX -shared -o parser *.o
|
2020-02-29 23:21:36 +00:00
|
|
|
runHook postBuild
|
|
|
|
'';
|
2021-10-29 16:52:37 +00:00
|
|
|
|
2020-02-29 23:21:36 +00:00
|
|
|
installPhase = ''
|
|
|
|
runHook preInstall
|
|
|
|
mkdir $out
|
|
|
|
mv parser $out/
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
2021-10-29 16:52:37 +00:00
|
|
|
|
2021-11-08 04:23:56 +00:00
|
|
|
# Strip failed on darwin: strip: error: symbols referenced by indirect symbol table entries that can't be stripped
|
|
|
|
fixupPhase = lib.optionalString stdenv.isLinux ''
|
2021-10-29 16:52:37 +00:00
|
|
|
runHook preFixup
|
2021-11-08 04:23:56 +00:00
|
|
|
$STRIP $out/parser
|
2021-10-29 16:52:37 +00:00
|
|
|
runHook postFixup
|
|
|
|
'';
|
2020-02-29 23:21:36 +00:00
|
|
|
}
|