sbcl-build: Improve error handling and generally tidy up.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 1 Apr 2013 15:25:51 +0000 (16:25 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 1 Apr 2013 16:39:22 +0000 (17:39 +0100)
In particular, the polyglot rune is now a bit less awful, and we have
pleasant error messages rather than vast stack traces of doom.

sbcl-build

index b5170f1..b97cd7a 100755 (executable)
@@ -1,23 +1,35 @@
 #! /bin/sh -e
-#| -*-lisp-*-
-exec sbcl --script "$0" "$0" "$@"
-|#
+":"; exec sbcl --script "$0" "$@" # -*-lisp-*-
 
 (require 'sb-executable)
 
 (let* ((args (cdr *posix-argv*))
-       (prog (pop args))
+       (prog (pathname-name *load-pathname*))
        (cmd (pop args)))
-  (cond
-    ((string= cmd "build")
-     (let ((rc 0))
-       (dolist (file args)
-        (unless (compile-file file :verbose nil :print nil)
-          (setf rc 1)))
-       (quit :unix-status rc)))
-    ((string= cmd "link")
-     (let ((out (pop args)))
-       (sb-executable:make-executable out args)))
-    (t
-     (format *error-output* "~A: unknown command `~A'~%" prog cmd)
-     (quit :unix-status 1))))
+  (labels ((moan (message &rest arguments)
+            (format *error-output* "~A: ~?~%" prog message arguments))
+          (die (message &rest arguments)
+            (apply #'moan message arguments)
+            (exit :code 1)))
+    (cond
+      ((not cmd)
+       (format *error-output*
+              "Usage: ~A build SOURCE SOURCE ...~%~
+            ~0~       ~:*~A link OUTPUT FASL FASL ...~%"
+              prog)
+       (exit :code 1))
+      ((string= cmd "build")
+       (let ((rc 0))
+        (dolist (file args)
+          (unless (handler-case (compile-file file :verbose nil :print nil)
+                    (error (cond)
+                      (moan "failed to compile `~A':~%~8T~A" file cond)))
+            (setf rc 1)))
+        (exit :code rc)))
+      ((string= cmd "link")
+       (let ((out (pop args)))
+        (handler-case (sb-executable:make-executable out args)
+          (error (cond)
+            (die "failed to make executable:~%~8T~A" cond)))))
+      (t
+       (die "unknown command `~A'" cmd)))))