From 3d313212052b415695de4a0737d4dad6c24bd6e4 Mon Sep 17 00:00:00 2001 From: oxalica Date: Sat, 30 Oct 2021 00:52:37 +0800 Subject: [PATCH] tree-sitter: use CXX to compile C++, optimize and strip --- .../tools/parsing/tree-sitter/grammar.nix | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/pkgs/development/tools/parsing/tree-sitter/grammar.nix b/pkgs/development/tools/parsing/tree-sitter/grammar.nix index d4782b37b6a..d0265a584b3 100644 --- a/pkgs/development/tools/parsing/tree-sitter/grammar.nix +++ b/pkgs/development/tools/parsing/tree-sitter/grammar.nix @@ -1,6 +1,5 @@ { stdenv , tree-sitter -, libcxx , lib }: @@ -16,42 +15,46 @@ , location ? null }: -stdenv.mkDerivation { +stdenv.mkDerivation rec { pname = "${language}-grammar"; inherit version; - src = - if location == null - then - source - else - "${source}/${location}" - ; + src = 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 ]; dontUnpack = 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 = '' runHook preBuild - scanner_cc="$src/src/scanner.cc" - if [ ! -f "$scanner_cc" ]; then - scanner_cc="" - fi - scanner_c="$src/src/scanner.c" - if [ ! -f "$scanner_c" ]; then - scanner_c="" + 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 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 ''; + installPhase = '' runHook preInstall mkdir $out mv parser $out/ runHook postInstall ''; + + # Auto strip cannot detect files missing extension. + fixupPhase = '' + runHook preFixup + strip -s $out/parser + runHook postFixup + ''; }