fork/helix

git clone https://git.julia.blue/fork/helix

Erin Kimnix: replace `runCommandNoCC` with `runCommand` (#3992)bdc7b352

23.05
3.4 KiB121 linesraw
1{
2  stdenv,
3  lib,
4  runCommandLocal,
5  runCommand,
6  yj,
7  includeGrammarIf ? _: true,
8  ...
9}: let
10  # HACK: nix < 2.6 has a bug in the toml parser, so we convert to JSON
11  # before parsing
12  languages-json = runCommandLocal "languages-toml-to-json" {} ''
13    ${yj}/bin/yj -t < ${./languages.toml} > $out
14  '';
15  languagesConfig =
16    if lib.versionAtLeast builtins.nixVersion "2.6.0"
17    then builtins.fromTOML (builtins.readFile ./languages.toml)
18    else builtins.fromJSON (builtins.readFile (builtins.toPath languages-json));
19  isGitGrammar = grammar:
20    builtins.hasAttr "source" grammar
21    && builtins.hasAttr "git" grammar.source
22    && builtins.hasAttr "rev" grammar.source;
23  isGitHubGrammar = grammar: lib.hasPrefix "https://github.com" grammar.source.git;
24  toGitHubFetcher = url: let
25    match = builtins.match "https://github\.com/([^/]*)/([^/]*)/?" url;
26  in {
27    owner = builtins.elemAt match 0;
28    repo = builtins.elemAt match 1;
29  };
30  gitGrammars = builtins.filter isGitGrammar languagesConfig.grammar;
31  buildGrammar = grammar: let
32    gh = toGitHubFetcher grammar.source.git;
33    sourceGit = builtins.fetchTree {
34      type = "git";
35      url = grammar.source.git;
36      rev = grammar.source.rev;
37      ref = grammar.source.ref or "HEAD";
38      shallow = true;
39    };
40    sourceGitHub = builtins.fetchTree {
41      type = "github";
42      owner = gh.owner;
43      repo = gh.repo;
44      inherit (grammar.source) rev;
45    };
46    source =
47      if isGitHubGrammar grammar
48      then sourceGitHub
49      else sourceGit;
50  in
51    stdenv.mkDerivation rec {
52      # see https://github.com/NixOS/nixpkgs/blob/fbdd1a7c0bc29af5325e0d7dd70e804a972eb465/pkgs/development/tools/parsing/tree-sitter/grammar.nix
53
54      pname = "helix-tree-sitter-${grammar.name}";
55      version = grammar.source.rev;
56
57      src =
58        if builtins.hasAttr "subpath" grammar.source
59        then "${source}/${grammar.source.subpath}"
60        else source;
61
62      dontUnpack = true;
63      dontConfigure = true;
64
65      FLAGS = [
66        "-I${src}/src"
67        "-g"
68        "-O3"
69        "-fPIC"
70        "-fno-exceptions"
71        "-Wl,-z,relro,-z,now"
72      ];
73
74      NAME = grammar.name;
75
76      buildPhase = ''
77        runHook preBuild
78
79        if [[ -e "$src/src/scanner.cc" ]]; then
80          $CXX -c "$src/src/scanner.cc" -o scanner.o $FLAGS
81        elif [[ -e "$src/src/scanner.c" ]]; then
82          $CC -c "$src/src/scanner.c" -o scanner.o $FLAGS
83        fi
84
85        $CC -c "$src/src/parser.c" -o parser.o $FLAGS
86        $CXX -shared -o $NAME.so *.o
87
88        ls -al
89
90        runHook postBuild
91      '';
92
93      installPhase = ''
94        runHook preInstall
95        mkdir $out
96        mv $NAME.so $out/
97        runHook postInstall
98      '';
99
100      # Strip failed on darwin: strip: error: symbols referenced by indirect symbol table entries that can't be stripped
101      fixupPhase = lib.optionalString stdenv.isLinux ''
102        runHook preFixup
103        $STRIP $out/$NAME.so
104        runHook postFixup
105      '';
106    };
107  grammarsToBuild = builtins.filter includeGrammarIf gitGrammars;
108  builtGrammars =
109    builtins.map (grammar: {
110      inherit (grammar) name;
111      artifact = buildGrammar grammar;
112    })
113    grammarsToBuild;
114  grammarLinks =
115    builtins.map (grammar: "ln -s ${grammar.artifact}/${grammar.name}.so $out/${grammar.name}.so")
116    builtGrammars;
117in
118  runCommand "consolidated-helix-grammars" {} ''
119    mkdir -p $out
120    ${builtins.concatStringsSep "\n" grammarLinks}
121  ''