2020-01-08 03:00:54 +01:00
|
|
|
# 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.
|
|
|
|
|
2020-02-21 13:47:29 +01:00
|
|
|
{ pkgs ? import <nixpkgs> {}, ... }:
|
2020-01-08 03:00:54 +01:00
|
|
|
|
|
|
|
let
|
2020-01-17 17:41:57 +01:00
|
|
|
inherit (builtins) map elemAt match filter;
|
2020-02-21 13:47:29 +01:00
|
|
|
inherit (pkgs) lib runCommandNoCC makeWrapper writeText writeShellScriptBin sbcl;
|
2020-01-08 03:00:54 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Internal helper definitions
|
|
|
|
#
|
|
|
|
|
2020-01-08 22:39:06 +01:00
|
|
|
# '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));
|
|
|
|
|
2020-01-08 03:00:54 +01:00
|
|
|
# 'genCompileLisp' generates a Lisp file that instructs SBCL to
|
|
|
|
# compile the provided list of Lisp source files to $out.
|
2020-01-08 22:39:06 +01:00
|
|
|
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}
|
|
|
|
|
2020-01-09 00:57:34 +01:00
|
|
|
(defun nix-compile-lisp (file srcfile)
|
2020-01-08 22:39:06 +01:00
|
|
|
(let ((outfile (make-pathname :type "fasl"
|
|
|
|
:directory (or (sb-posix:getenv "NIX_BUILD_TOP")
|
|
|
|
(error "not running in a Nix build"))
|
2020-07-06 18:40:31 +02:00
|
|
|
:name (substitute #\- #\/ srcfile))))
|
2020-01-08 22:39:06 +01:00
|
|
|
(multiple-value-bind (_outfile _warnings-p failure-p)
|
|
|
|
(compile-file srcfile :output-file outfile)
|
2020-01-09 00:57:34 +01:00
|
|
|
(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)))))))
|
2020-01-08 22:39:06 +01:00
|
|
|
|
|
|
|
(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)))
|
|
|
|
|
2020-01-09 00:57:34 +01:00
|
|
|
(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)
|
|
|
|
}
|
|
|
|
))
|
2020-01-08 22:39:06 +01:00
|
|
|
'';
|
2020-01-08 03:00:54 +01:00
|
|
|
|
2020-07-27 03:11:32 +02:00
|
|
|
# '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))
|
|
|
|
'';
|
|
|
|
|
2020-01-09 01:46:20 +01:00
|
|
|
# 'dependsOn' determines whether Lisp library 'b' depends on 'a'.
|
|
|
|
dependsOn = a: b: builtins.elem a b.lispDeps;
|
|
|
|
|
2020-01-08 19:40:53 +01:00
|
|
|
# 'allDeps' flattens the list of dependencies (and their
|
2020-01-09 01:46:20 +01:00
|
|
|
# dependencies) into one ordered list of unique deps.
|
|
|
|
allDeps = deps: (lib.toposort dependsOn (lib.unique (
|
|
|
|
lib.flatten (deps ++ (map (d: d.lispDeps) deps))
|
|
|
|
))).result;
|
2020-01-08 19:40:53 +01:00
|
|
|
|
2020-01-09 03:57:02 +01:00
|
|
|
# '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))
|
|
|
|
);
|
|
|
|
|
2020-01-08 22:39:26 +01:00
|
|
|
# 'genDumpLisp' generates a Lisp file that instructs SBCL to dump
|
|
|
|
# the currently loaded image as an executable to $out/bin/$name.
|
2020-01-08 03:00:54 +01:00
|
|
|
#
|
2020-01-08 22:39:26 +01:00
|
|
|
# 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))
|
|
|
|
;;
|
|
|
|
'';
|
2020-01-08 03:00:54 +01:00
|
|
|
|
|
|
|
# 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));
|
|
|
|
};
|
|
|
|
|
2020-07-27 03:11:32 +02:00
|
|
|
# '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"
|
|
|
|
'';
|
|
|
|
|
2020-01-08 22:39:26 +01:00
|
|
|
#
|
|
|
|
# Public API functions
|
|
|
|
#
|
|
|
|
|
2020-01-08 03:00:54 +01:00
|
|
|
# 'library' builds a list of Common Lisp files into a single FASL
|
|
|
|
# which can then be loaded into SBCL.
|
2020-07-27 03:11:32 +02:00
|
|
|
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 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;
|
|
|
|
};
|
2020-01-08 03:00:54 +01:00
|
|
|
|
|
|
|
# 'program' creates an executable containing a dumped image of the
|
|
|
|
# specified sources and dependencies.
|
2020-07-27 03:11:32 +02:00
|
|
|
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 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;
|
2020-01-09 03:57:02 +01:00
|
|
|
};
|
2020-01-08 03:00:54 +01:00
|
|
|
|
2020-01-27 00:58:31 +01:00
|
|
|
# '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})");
|
|
|
|
};
|
|
|
|
|
2020-01-08 03:00:54 +01:00
|
|
|
# 'sbclWith' creates an image with the specified libraries /
|
|
|
|
# programs loaded.
|
2020-01-17 17:41:57 +01:00
|
|
|
sbclWith = deps:
|
|
|
|
let lispDeps = filter (d: !d.lispBinary) (allDeps deps);
|
|
|
|
in writeShellScriptBin "sbcl" ''
|
2020-01-09 03:57:02 +01:00
|
|
|
export LD_LIBRARY_PATH=${lib.makeLibraryPath (allNative [] lispDeps)};
|
2020-01-24 14:30:13 +01:00
|
|
|
exec ${sbcl}/bin/sbcl ${lib.optionalString (deps != []) "--load ${writeText "load.lisp" (genLoadLisp lispDeps)}"} $@
|
2020-01-08 20:38:29 +01:00
|
|
|
'';
|
2020-01-08 03:00:54 +01:00
|
|
|
in {
|
|
|
|
library = makeOverridable library;
|
|
|
|
program = makeOverridable program;
|
|
|
|
sbclWith = makeOverridable sbclWith;
|
2020-01-27 00:58:31 +01:00
|
|
|
bundled = makeOverridable bundled;
|
2020-01-08 03:00:54 +01:00
|
|
|
}
|