tree-sitter: use CXX to compile C++, optimize and strip

main
oxalica 3 years ago committed by Profpatsch
parent 639d86d5be
commit 3d31321205
  1. 39
      pkgs/development/tools/parsing/tree-sitter/grammar.nix

@ -1,6 +1,5 @@
{ stdenv { stdenv
, tree-sitter , tree-sitter
, libcxx
, lib , lib
}: }:
@ -16,42 +15,46 @@
, location ? null , location ? null
}: }:
stdenv.mkDerivation { stdenv.mkDerivation rec {
pname = "${language}-grammar"; pname = "${language}-grammar";
inherit version; inherit version;
src = src = if location == null then source else "${source}/${location}";
if location == null
then
source
else
"${source}/${location}"
;
NIX_CFLAGS_COMPILE = lib.optionalString stdenv.isDarwin "-I${lib.getDev libcxx}/include/c++/v1";
buildInputs = [ tree-sitter ]; buildInputs = [ tree-sitter ];
dontUnpack = true; dontUnpack = true;
dontConfigure = true; dontConfigure = true;
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.
buildPhase = '' buildPhase = ''
runHook preBuild runHook preBuild
scanner_cc="$src/src/scanner.cc" if [[ -e "$src/src/scanner.cc" ]]; then
if [ ! -f "$scanner_cc" ]; then $CXX -c "$src/src/scanner.cc" -o scanner.o $CXXFLAGS
scanner_cc="" elif [[ -e "$src/src/scanner.c" ]]; then
fi $CC -c "$src/src/scanner.c" -o scanner.o $CFLAGS
scanner_c="$src/src/scanner.c"
if [ ! -f "$scanner_c" ]; then
scanner_c=""
fi fi
$CC -I$src/src/ -shared -o parser -Os $src/src/parser.c $scanner_cc $scanner_c -lstdc++ $CC -c "$src/src/parser.c" -o parser.o $CFLAGS
$CXX -shared -o parser *.o
runHook postBuild runHook postBuild
''; '';
installPhase = '' installPhase = ''
runHook preInstall runHook preInstall
mkdir $out mkdir $out
mv parser $out/ mv parser $out/
runHook postInstall runHook postInstall
''; '';
# Auto strip cannot detect files missing extension.
fixupPhase = ''
runHook preFixup
strip -s $out/parser
runHook postFixup
'';
} }

Loading…
Cancel
Save