SBCL core saving

SBCL cores are a cool way to dump your lisp into a standalone executable. The only awkward thing with SBCL’s core handling is that the image exists after you save it. The manual suggested forking to work around that issue, so that’s what I did. I thought I’d put it here for future reference, maybe someone else finds it helpful too. Here’s the core-saving function I’m using:

(defun main ()
  (format t "Hello, world!~%")
  #+sbcl
  (loop for index = 0 then (1+ index) for arg in sb-ext:*posix-argv* do
       (format t "arg ~a: ~a~%" index arg)))

(defun save-core (core-fn)
  (progn
    #+sbcl
    (let ((fork-result (sb-posix:fork)))
      (case fork-result
	(-1 (error "fork failed"))
	(0 (sb-ext:save-lisp-and-die core-fn :toplevel #'main :executable t))
	(otherwise (sb-posix:wait)))
      (format t "stand-alone core ~a saved" core-fn))
    #-sbcl
    (error "not available on this lisp")
    (values)))

This is a convenient thing to have when you’re trying to get that command-line front end just right, as you can dump lisp cores and try them out from the shell in no time at all:

CL-USER> (save-core #p"/tmp/core2")
stand-alone core /tmp/core2 saved

We can now switch to the shell and run the core’s main function:

leonardo:~ dep$ /tmp/core2 a b c
Hello, world!
arg 0: /tmp/core2
arg 1: a
arg 2: b
arg 3: c
Advertisements

3 thoughts on “SBCL core saving

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s