about summary refs log tree commit diff
path: root/nix/buildLisp/default.nix
blob: fe43c895f416ca1c8a54110a6f2166aef5b98068 (plain) (blame)
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
# buildLisp provides Nix functions to build Common Lisp packages,
# targeting SBCL.
#
# buildLisp is designed to enforce conventions and do away with the
# free-for-all of existing Lisp build systems.

{ pkgs ? import <nixpkgs> {}, ... }:

let
  inherit (builtins) map elemAt match filter;
  inherit (pkgs) lib runCommandNoCC makeWrapper writeText writeShellScriptBin sbcl;

  #
  # Internal helper definitions
  #

  # 'genLoadLisp' generates Lisp code that instructs SBCL to load all
  # the provided Lisp libraries.
  genLoadLisp = deps: lib.concatStringsSep "\n"
    (map (lib: "(load \"${lib}/${lib.lispName}.fasl\")") (allDeps deps));

  # 'genCompileLisp' generates a Lisp file that instructs SBCL to
  # compile the provided list of Lisp source files to $out.
  genCompileLisp = srcs: deps: writeText "compile.lisp" ''
    ;; This file compiles the specified sources into the Nix build
    ;; directory, creating one FASL file for each source.
    (require 'sb-posix)

    ${genLoadLisp deps}

    (defun nix-compile-lisp (file srcfile)
      (let ((outfile (make-pathname :type "fasl"
                                    :directory (or (sb-posix:getenv "NIX_BUILD_TOP")
                                                   (error "not running in a Nix build"))
                                    :name (substitute #\- #\/ srcfile))))
        (multiple-value-bind (_outfile _warnings-p failure-p)
            (compile-file srcfile :output-file outfile)
          (if failure-p (sb-posix:exit 1)
              (progn
                ;; For the case of multiple files belonging to the same
                ;; library being compiled, load them in order:
                (load outfile)

                ;; Write them to the FASL list in the same order:
                (format file "cat ~a~%" (namestring outfile)))))))

    (let ((*compile-verbose* t)
          ;; FASL files are compiled into the working directory of the
          ;; build and *then* moved to the correct out location.
          (pwd (sb-posix:getcwd)))

      (with-open-file (file "cat_fasls"
                            :direction :output
                            :if-does-not-exist :create)

        ;; These forms were inserted by the Nix build:
        ${
          lib.concatStringsSep "\n" (map (src: "(nix-compile-lisp file \"${src}\")") srcs)
        }
        ))
  '';

  # 'genTestLisp' generates a Lisp file that loads all sources and deps and
  # executes expression
  genTestLisp = name: srcs: deps: expression: writeText "${name}.lisp" ''
    ;; Dependencies
    ${genLoadLisp deps}

    ;; Sources
    ${lib.concatStringsSep "\n" (map (src: "(load \"${src}\")") srcs)}

    ;; Test expression
    (unless ${expression}
      (exit :code 1))
  '';

  # 'dependsOn' determines whether Lisp library 'b' depends on 'a'.
  dependsOn = a: b: builtins.elem a b.lispDeps;

  # 'allDeps' flattens the list of dependencies (and their
  # dependencies) into one ordered list of unique deps.
  allDeps = deps: (lib.toposort dependsOn (lib.unique (
    lib.flatten (deps ++ (map (d: d.lispDeps) deps))
  ))).result;

  # 'allNative' extracts all native dependencies of a dependency list
  # to ensure that library load paths are set correctly during all
  # compilations and program assembly.
  allNative = native: deps: lib.unique (
    lib.flatten (native ++ (map (d: d.lispNativeDeps) deps))
  );

  # 'genDumpLisp' generates a Lisp file that instructs SBCL to dump
  # the currently loaded image as an executable to $out/bin/$name.
  #
  # TODO(tazjin): Compression is currently unsupported because the
  # SBCL in nixpkgs is, by default, not compiled with zlib support.
  genDumpLisp = name: main: deps: writeText "dump.lisp" ''
    (require 'sb-posix)

    ${genLoadLisp deps}

    (let* ((bindir (concatenate 'string (sb-posix:getenv "out") "/bin"))
           (outpath (make-pathname :name "${name}"
                                   :directory bindir)))
      (save-lisp-and-die outpath
                         :executable t
                         :toplevel (function ${main})
                         :purify t))
    ;;
  '';

  # Add an `overrideLisp` attribute to a function result that works
  # similar to `overrideAttrs`, but is used specifically for the
  # arguments passed to Lisp builders.
  makeOverridable = f: orig: (f orig) // {
    overrideLisp = new: makeOverridable f (orig // (new orig));
  };

  # 'testSuite' builds a Common Lisp test suite that loads all of srcs and deps,
  # and then executes expression to check its result
  testSuite = { name, expression, srcs, deps ? [], native ? [] }:
    let
      lispNativeDeps = allNative native deps;
      lispDeps = allDeps deps;
    in runCommandNoCC name {
      LD_LIBRARY_PATH = lib.makeLibraryPath lispNativeDeps;
      LANG = "C.UTF-8";
    } ''
      echo "Running test suite ${name}"

      ${sbcl}/bin/sbcl --script ${genTestLisp name srcs deps expression} \
        | tee $out

      echo "Test suite ${name} succeeded"
    '';

  #
  # Public API functions
  #

  # 'library' builds a list of Common Lisp files into a single FASL
  # which can then be loaded into SBCL.
  library =
    { name
    , srcs
    , deps ? []
    , native ? []
    , tests ? null
    }:
    let
      lispNativeDeps = (allNative native deps);
      lispDeps = allDeps deps;
      testDrv = if ! isNull tests
        then testSuite {
          name = tests.name or "${name}-test";
          srcs = srcs ++ (tests.srcs or []);
          deps = deps ++ (tests.deps or []);
          expression = tests.expression;
        }
        else null;
    in lib.fix (self: runCommandNoCC "${name}-cllib" {
      LD_LIBRARY_PATH = lib.makeLibraryPath lispNativeDeps;
      LANG = "C.UTF-8";
    } ''
      ${if ! isNull testDrv
        then "echo 'Test ${testDrv} succeeded'"
        else "echo 'No tests run'"}
      ${sbcl}/bin/sbcl --script ${genCompileLisp srcs lispDeps}

      echo "Compilation finished, assembling FASL files"

      # FASL files can be combined by simply concatenating them
      # together, but it needs to be in the compilation order.
      mkdir $out

      chmod +x cat_fasls
      ./cat_fasls > $out/${name}.fasl
    '' // {
      inherit lispNativeDeps lispDeps;
      lispName = name;
      lispBinary = false;
      tests = testDrv;
      sbcl = sbclWith [ self ];
    });

  # 'program' creates an executable containing a dumped image of the
  # specified sources and dependencies.
  program =
    { name
    , main ? "${name}:main"
    , srcs
    , deps ? []
    , native ? []
    , tests ? null
    }:
    let
      lispDeps = allDeps deps;
      libPath = lib.makeLibraryPath (allNative native lispDeps);
      selfLib = library {
        inherit name srcs native;
        deps = lispDeps;
      };
      testDrv = if ! isNull tests
        then testSuite {
          name = tests.name or "${name}-test";
          srcs =
            (
              srcs ++ (tests.srcs or []));
          deps = deps ++ (tests.deps or []);
          expression = tests.expression;
        }
        else null;
    in lib.fix (self: runCommandNoCC "${name}" {
      nativeBuildInputs = [ makeWrapper ];
      LD_LIBRARY_PATH = libPath;
      LANG = "C.UTF-8";
    } ''
      ${if ! isNull testDrv
        then "echo 'Test ${testDrv} succeeded'"
        else ""}
      mkdir -p $out/bin

      ${sbcl}/bin/sbcl --script ${
        genDumpLisp name main ([ selfLib ] ++ lispDeps)
      }

      wrapProgram $out/bin/${name} --prefix LD_LIBRARY_PATH : "${libPath}"
    '' // {
      lispName = name;
      lispDeps = [ selfLib ] ++ (tests.deps or []);
      lispNativeDeps = native;
      lispBinary = true;
      tests = testDrv;
      sbcl = sbclWith [ self ];
    });

  # 'bundled' creates a "library" that calls 'require' on a built-in
  # package, such as any of SBCL's sb-* packages.
  bundled = name: (makeOverridable library) {
    inherit name;
    srcs = lib.singleton (builtins.toFile "${name}.lisp" "(require '${name})");
  };

  # 'sbclWith' creates an image with the specified libraries /
  # programs loaded.
  sbclWith = deps:
  let lispDeps = filter (d: !d.lispBinary) (allDeps deps);
  in writeShellScriptBin "sbcl" ''
    export LD_LIBRARY_PATH="${lib.makeLibraryPath (allNative [] lispDeps)}"
    export LANG="C.UTF-8"
    exec ${sbcl}/bin/sbcl ${lib.optionalString (deps != []) "--load ${writeText "load.lisp" (genLoadLisp lispDeps)}"} $@
  '';
in {
  library = makeOverridable library;
  program = makeOverridable program;
  sbclWith = makeOverridable sbclWith;
  bundled = makeOverridable bundled;
}