2020-04-24 23:36:52 +00:00
|
|
|
{ stdenv
|
|
|
|
, tree-sitter
|
2021-06-28 23:13:55 +00:00
|
|
|
, lib
|
2020-12-25 13:55:36 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
# Build a parser grammar and put the resulting shared object in `$out/parser`
|
|
|
|
|
|
|
|
{
|
|
|
|
# language name
|
|
|
|
language
|
|
|
|
# version of tree-sitter
|
2020-04-24 23:36:52 +00:00
|
|
|
, version
|
2020-12-25 13:55:36 +00:00
|
|
|
# source for the language grammar
|
2020-04-24 23:36:52 +00:00
|
|
|
, source
|
2021-03-16 09:55:35 +00:00
|
|
|
, location ? null
|
2020-04-24 23:36:52 +00:00
|
|
|
}:
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
stdenv.mkDerivation rec {
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-01-05 17:05:55 +00:00
|
|
|
pname = "${language}-grammar";
|
2020-04-24 23:36:52 +00:00
|
|
|
inherit version;
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
src = if location == null then source else "${source}/${location}";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
buildInputs = [ tree-sitter ];
|
|
|
|
|
|
|
|
dontUnpack = true;
|
2021-07-21 07:28:18 +00:00
|
|
|
dontConfigure = true;
|
|
|
|
|
2021-12-06 16:07:01 +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-04-24 23:36:52 +00:00
|
|
|
buildPhase = ''
|
|
|
|
runHook preBuild
|
2021-12-06 16:07:01 +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-03-09 03:18:52 +00:00
|
|
|
fi
|
2021-12-06 16:07:01 +00:00
|
|
|
$CC -c "$src/src/parser.c" -o parser.o $CFLAGS
|
|
|
|
$CXX -shared -o parser *.o
|
2020-04-24 23:36:52 +00:00
|
|
|
runHook postBuild
|
|
|
|
'';
|
2021-12-06 16:07:01 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
installPhase = ''
|
|
|
|
runHook preInstall
|
|
|
|
mkdir $out
|
|
|
|
mv parser $out/
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
2021-12-06 16:07:01 +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 ''
|
|
|
|
runHook preFixup
|
|
|
|
$STRIP $out/parser
|
|
|
|
runHook postFixup
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|