summaryrefslogtreecommitdiff
path: root/Omni/Bild/Builder.nix
blob: 455efce8de5be0d937bff9297d9c81509fa99140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
This is the library of nix builders. Some rules to follow:
- Keep this code as minimal as possible. I'd rather write Haskell than Nix,
  wouldn't you?
- Try to reuse as much upstream Nix as possible.
*/
{
  analysisJSON,
  bild,
}:
with bild; let
  analysis = builtins.fromJSON analysisJSON;

  # common bash functions for the builder
  commonBash = builtins.toFile "common.bash" ''
    # Check that a command succeeds, fail and log if not.
    function check {
        $@ || { echo "fail:  $name:  $3"; exit 1; }
      }
  '';

  build = _: target: let
    name = target.out;
    root = builtins.getEnv "CODEROOT";
    mainModule = target.mainModule;
    compileLine =
      lib.strings.concatStringsSep " "
      ([target.compiler] ++ target.compilerFlags);

    allSources = target.srcs ++ [target.quapath];

    isEmpty = x: x == null || x == [];

    skip = ["_" ".direnv"];

    # Normalize paths by removing leading "./"
    normalize = p: lib.strings.removePrefix "./" p;

    # Given a list of path parts, produce all cumulative prefixes:
    # ["a","b","c"] -> ["a","a/b","a/b/c"]
    dirPrefixes = parts:
      if parts == []
      then []
      else let
        hd = lib.lists.head parts;
        tl = lib.lists.tail parts;
        rest = dirPrefixes tl;
      in
        [hd] ++ (lib.lists.map (r: "${hd}/${r}") rest);

    # Normalize all source file paths (relative to root)
    allSourcesRel = lib.lists.map normalize allSources;

    # Allowed directories are the ancestors of all source files, plus the repo root ""
    allowedDirs = lib.lists.unique (
      [""]
      ++ lib.lists.concatMap
      (p: let
        parts = lib.strings.splitString "/" p;
      in
        dirPrefixes (lib.lists.init parts))
      allSourcesRel
    );

    filter = file: type:
      if lib.lists.elem (builtins.baseNameOf file) skip
      then false
      else if type == "directory"
      then let
        rel = lib.strings.removePrefix "${root}/" file;
        rel' = normalize rel;
      in
        lib.lists.elem rel' allowedDirs
      else if type == "regular"
      then let
        rel = lib.strings.removePrefix "${root}/" file;
        rel' = normalize rel;
      in
        lib.lists.elem rel' allSourcesRel
      else false;

    # remove empty directories, leftover from the src filter
    preBuild = "find . -type d -empty -delete";

    src = lib.sources.cleanSourceWith {
      inherit filter;
      src = lib.sources.cleanSource root;
    };

    langdeps_ =
      if isEmpty target.langdeps
      then []
      else
        lib.attrsets.attrVals target.langdeps (lib.attrsets.getAttrFromPath
          (lib.strings.splitString "." target.packageSet)
          bild);

    sysdeps_ =
      if isEmpty target.sysdeps
      then []
      else lib.attrsets.attrVals target.sysdeps pkgs;

    rundeps_ =
      if isEmpty target.rundeps
      then []
      else lib.attrsets.attrVals target.rundeps pkgs;

    CODEROOT = ".";

    builders = {
      base = stdenv.mkDerivation rec {
        inherit name src CODEROOT preBuild;
        buildInputs = langdeps_ ++ sysdeps_;
        installPhase = "install -D ${name} $out/bin/${name}";
        buildPhase = compileLine;
      };

      haskell =
        if (target.hsGraph or null) == null
        then
          # Monolithic build (fallback for TH/cycles)
          stdenv.mkDerivation rec {
            inherit name src CODEROOT preBuild;
            nativeBuildInputs = [makeWrapper];
            buildInputs =
              sysdeps_
              ++ [
                (haskell.ghcWith (p: (lib.attrsets.attrVals target.langdeps p)))
              ];
            buildPhase = compileLine;
            installPhase = ''
              install -D ${name} $out/bin/${name}
              wrapProgram $out/bin/${name} \
                --prefix PATH : ${lib.makeBinPath rundeps_}
            '';
          }
        else
          # Per-module incremental build
          let
            graph = target.hsGraph;
            ghcPkg = haskell.ghcWith (p: (lib.attrsets.attrVals target.langdeps p));

            # Helper to sanitize module names for Nix attr names
            sanitize = builtins.replaceStrings ["."] ["_"];

            # Create source filter for a single module
            mkModuleSrc = modulePath: let
              moduleFiles = [modulePath];
              moduleAllSources = moduleFiles;
              moduleAllSourcesRel = lib.lists.map normalize moduleAllSources;
              moduleAllowedDirs = lib.lists.unique (
                [""]
                ++ lib.lists.concatMap
                (p: let
                  parts = lib.strings.splitString "/" p;
                in
                  dirPrefixes (lib.lists.init parts))
                moduleAllSourcesRel
              );
              moduleFilter = file: type:
                if lib.lists.elem (builtins.baseNameOf file) skip
                then false
                else if type == "directory"
                then let
                  rel = lib.strings.removePrefix "${root}/" file;
                  rel' = normalize rel;
                in
                  lib.lists.elem rel' moduleAllowedDirs
                else if type == "regular"
                then let
                  rel = lib.strings.removePrefix "${root}/" file;
                  rel' = normalize rel;
                in
                  lib.lists.elem rel' moduleAllSourcesRel
                else false;
            in
              lib.sources.cleanSourceWith {
                filter = moduleFilter;
                src = lib.sources.cleanSource root;
              };

            # Build one module derivation
            mkModuleDrv = modName: node: depDrvs:
              stdenv.mkDerivation {
                name = "hs-mod-${sanitize modName}";
                src = mkModuleSrc node.nodePath;
                inherit CODEROOT;
                nativeBuildInputs = [];
                buildInputs = sysdeps_ ++ depDrvs;
                builder = "${stdenv.shell}";
                args = [
                  "-c"
                  (let
                    copyDeps =
                      lib.strings.concatMapStringsSep "\n" (d: ''
                        cp -rL ${d}/hidir/. hidir/ 2>/dev/null || true
                      '')
                      depDrvs;
                  in ''
                    set -eu
                    unpackPhase
                    cd source
                    mkdir -p hidir odir
                    ${copyDeps}
                    chmod -R +w hidir || true
                    ${ghcPkg}/bin/ghc -c \
                      -Wall -Werror -haddock -Winvalid-haddock \
                      -i. -ihidir \
                      -odir odir -hidir hidir \
                      ${node.nodePath}
                    mkdir -p $out/hidir $out/odir
                    cp -r hidir/* $out/hidir/ || true
                    cp -r odir/* $out/odir/ || true
                  '')
                ];
              };

            # Recursive attrset of all module derivations
            modules = lib.fix (self:
              lib.mapAttrs
              (modName: node:
                mkModuleDrv modName node (map (dep: builtins.getAttr dep self) node.nodeImports))
              graph.graphModules);
          in
            # Final link derivation
            stdenv.mkDerivation rec {
              inherit name CODEROOT src;
              nativeBuildInputs = [makeWrapper];
              dontConfigure = true;
              buildPhase = let
                pkgFlags = lib.strings.concatMapStringsSep " " (p: "-package ${p}") target.langdeps;
                copyHiFiles = lib.strings.concatMapStringsSep "\n" (drv: "cp -rL ${drv}/hidir/. . 2>/dev/null || true") (lib.attrsets.attrValues modules);
              in ''
                set -eu
                ${copyHiFiles}
                chmod -R +w . || true
                ${ghcPkg}/bin/ghc --make \
                  ${target.quapath} \
                  -i. \
                  ${pkgFlags} \
                  -threaded \
                  -o ${name} \
                  ${lib.optionalString (target.mainModule != "Main") "-main-is ${target.mainModule}"}
              '';
              installPhase = ''
                install -D ${name} $out/bin/${name}
                ${lib.optionalString (rundeps_ != []) ''
                  wrapProgram $out/bin/${name} \
                    --prefix PATH : ${lib.makeBinPath rundeps_}
                ''}
              '';
            };

      c = stdenv.mkDerivation rec {
        inherit name src CODEROOT preBuild;
        buildInputs = langdeps_ ++ sysdeps_;
        installPhase = "install -D ${name} $out/bin/${name}";
        buildPhase = lib.strings.concatStringsSep " " [
          compileLine
          (
            if isEmpty langdeps_
            then ""
            else "$(pkg-config --cflags ${
              lib.strings.concatStringsSep " " target.langdeps
            })"
          )
          (
            if isEmpty sysdeps_
            then ""
            else "$(pkg-config --libs ${
              lib.strings.concatStringsSep " " target.sysdeps
            })"
          )
        ];
      };

      python = python.buildPythonApplication rec {
        inherit name src CODEROOT;
        nativeBuildInputs = [makeWrapper];
        propagatedBuildInputs = langdeps_ ++ sysdeps_ ++ rundeps_;
        buildInputs = sysdeps_;
        nativeCheckInputs = [pkgs.ruff python.packages.mypy];
        checkPhase = ''
          . ${commonBash}
          cp ${../../pyproject.toml} ./pyproject.toml
          check ruff format --exclude 'setup.py' --check .
          # ignore EXE here to support run.sh shebangs
          check ruff check \
            --ignore EXE \
            --exclude 'setup.py' \
            --exclude '__init__.py' \
            .
          touch ./py.typed
          check python -m mypy \
            --explicit-package-bases \
            --no-color-output \
            --exclude 'setup\.py$' \
            .
        '';
        installCheck = ''
          . ${commonBash}
          check python -m ${mainModule} test
        '';
        preBuild = ''
          # remove empty directories, leftover from the src filter
          find . -type d -empty -delete
          # initialize remaining dirs as python modules
          find . -type d -exec touch {}/__init__.py \;
          # generate a minimal setup.py
          cat > setup.py << EOF
          from setuptools import find_packages, setup
          setup(
              name="${name}",
              entry_points={"console_scripts":["${name} = ${mainModule}:main"]},
              version="0.0.0",
              url="https://git.bensima.com/omni.git",
              author="dev",
              author_email="dev@bensima.com",
              description="nil",
              packages=find_packages(),
              install_requires=[],
          )
          EOF
        '';
        pythonImportsCheck = [mainModule]; # sanity check
      };
    };
  in
    builders.${target.builder};
  # the bild caller gives us the Analysis type, which is a hashmap, but i need to
  # return a single drv, so just take the first one for now. ideally i would only
  # pass Target, one at a time, (perhaps parallelized in haskell land) and then i
  # wouldn't need all of this let nesting
in
  builtins.head (lib.attrsets.mapAttrsToList build analysis)