2022-06-06 22:24:52 +03:00
|
|
|
{
|
|
|
|
stdenv,
|
|
|
|
lib,
|
|
|
|
runCommandLocal,
|
2022-09-27 16:01:58 +03:00
|
|
|
runCommand,
|
2022-06-06 22:24:52 +03:00
|
|
|
yj,
|
2022-07-27 20:52:07 +03:00
|
|
|
includeGrammarIf ? _: true,
|
|
|
|
...
|
2022-06-06 22:24:52 +03:00
|
|
|
}: let
|
2022-02-15 06:50:08 +03:00
|
|
|
# HACK: nix < 2.6 has a bug in the toml parser, so we convert to JSON
|
|
|
|
# before parsing
|
2022-06-06 22:24:52 +03:00
|
|
|
languages-json = runCommandLocal "languages-toml-to-json" {} ''
|
2022-02-15 06:50:08 +03:00
|
|
|
${yj}/bin/yj -t < ${./languages.toml} > $out
|
|
|
|
'';
|
2022-06-06 22:24:52 +03:00
|
|
|
languagesConfig =
|
|
|
|
if lib.versionAtLeast builtins.nixVersion "2.6.0"
|
|
|
|
then builtins.fromTOML (builtins.readFile ./languages.toml)
|
|
|
|
else builtins.fromJSON (builtins.readFile (builtins.toPath languages-json));
|
|
|
|
isGitGrammar = grammar:
|
|
|
|
builtins.hasAttr "source" grammar
|
|
|
|
&& builtins.hasAttr "git" grammar.source
|
|
|
|
&& builtins.hasAttr "rev" grammar.source;
|
2022-03-29 00:50:18 +03:00
|
|
|
isGitHubGrammar = grammar: lib.hasPrefix "https://github.com" grammar.source.git;
|
|
|
|
toGitHubFetcher = url: let
|
|
|
|
match = builtins.match "https://github\.com/([^/]*)/([^/]*)/?" url;
|
|
|
|
in {
|
|
|
|
owner = builtins.elemAt match 0;
|
|
|
|
repo = builtins.elemAt match 1;
|
|
|
|
};
|
2022-02-15 06:50:08 +03:00
|
|
|
gitGrammars = builtins.filter isGitGrammar languagesConfig.grammar;
|
2022-06-06 22:24:52 +03:00
|
|
|
buildGrammar = grammar: let
|
|
|
|
gh = toGitHubFetcher grammar.source.git;
|
|
|
|
sourceGit = builtins.fetchTree {
|
|
|
|
type = "git";
|
|
|
|
url = grammar.source.git;
|
|
|
|
rev = grammar.source.rev;
|
|
|
|
ref = grammar.source.ref or "HEAD";
|
|
|
|
shallow = true;
|
|
|
|
};
|
|
|
|
sourceGitHub = builtins.fetchTree {
|
|
|
|
type = "github";
|
|
|
|
owner = gh.owner;
|
|
|
|
repo = gh.repo;
|
|
|
|
inherit (grammar.source) rev;
|
|
|
|
};
|
|
|
|
source =
|
|
|
|
if isGitHubGrammar grammar
|
|
|
|
then sourceGitHub
|
|
|
|
else sourceGit;
|
|
|
|
in
|
|
|
|
stdenv.mkDerivation rec {
|
2022-02-15 06:50:08 +03:00
|
|
|
# see https://github.com/NixOS/nixpkgs/blob/fbdd1a7c0bc29af5325e0d7dd70e804a972eb465/pkgs/development/tools/parsing/tree-sitter/grammar.nix
|
|
|
|
|
|
|
|
pname = "helix-tree-sitter-${grammar.name}";
|
|
|
|
version = grammar.source.rev;
|
|
|
|
|
2022-06-06 22:24:52 +03:00
|
|
|
src =
|
|
|
|
if builtins.hasAttr "subpath" grammar.source
|
|
|
|
then "${source}/${grammar.source.subpath}"
|
|
|
|
else source;
|
2022-02-15 06:50:08 +03:00
|
|
|
|
|
|
|
dontUnpack = true;
|
|
|
|
dontConfigure = true;
|
|
|
|
|
|
|
|
FLAGS = [
|
|
|
|
"-I${src}/src"
|
|
|
|
"-g"
|
|
|
|
"-O3"
|
|
|
|
"-fPIC"
|
|
|
|
"-fno-exceptions"
|
|
|
|
"-Wl,-z,relro,-z,now"
|
|
|
|
];
|
|
|
|
|
|
|
|
NAME = grammar.name;
|
|
|
|
|
|
|
|
buildPhase = ''
|
|
|
|
runHook preBuild
|
|
|
|
|
|
|
|
if [[ -e "$src/src/scanner.cc" ]]; then
|
|
|
|
$CXX -c "$src/src/scanner.cc" -o scanner.o $FLAGS
|
|
|
|
elif [[ -e "$src/src/scanner.c" ]]; then
|
|
|
|
$CC -c "$src/src/scanner.c" -o scanner.o $FLAGS
|
|
|
|
fi
|
|
|
|
|
|
|
|
$CC -c "$src/src/parser.c" -o parser.o $FLAGS
|
|
|
|
$CXX -shared -o $NAME.so *.o
|
|
|
|
|
|
|
|
ls -al
|
|
|
|
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = ''
|
|
|
|
runHook preInstall
|
|
|
|
mkdir $out
|
|
|
|
mv $NAME.so $out/
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
|
|
|
# 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/$NAME.so
|
|
|
|
runHook postFixup
|
|
|
|
'';
|
|
|
|
};
|
2022-07-27 20:52:07 +03:00
|
|
|
grammarsToBuild = builtins.filter includeGrammarIf gitGrammars;
|
2022-06-06 22:24:52 +03:00
|
|
|
builtGrammars =
|
|
|
|
builtins.map (grammar: {
|
|
|
|
inherit (grammar) name;
|
|
|
|
artifact = buildGrammar grammar;
|
|
|
|
})
|
2022-07-27 20:52:07 +03:00
|
|
|
grammarsToBuild;
|
2022-06-06 22:24:52 +03:00
|
|
|
grammarLinks =
|
|
|
|
builtins.map (grammar: "ln -s ${grammar.artifact}/${grammar.name}.so $out/${grammar.name}.so")
|
2022-02-15 06:50:08 +03:00
|
|
|
builtGrammars;
|
2022-06-06 22:24:52 +03:00
|
|
|
in
|
2022-09-27 16:01:58 +03:00
|
|
|
runCommand "consolidated-helix-grammars" {} ''
|
2022-06-06 22:24:52 +03:00
|
|
|
mkdir -p $out
|
|
|
|
${builtins.concatStringsSep "\n" grammarLinks}
|
|
|
|
''
|