save.lisp
1	;;; Stub that takes lisp source file as an argument and generates an executable
2 ;;; for it. Assumes entry point is main.
3
4 (ql:quickload "unix-opts")
5
6 (defun file-test (filename)
7 (if (probe-file filename) filename (print "Couldn't find filename")))
8
9 (defun get-fn-str (filename)
10 (if (stringp filename) filename))
11
12 (opts:define-opts
13 (:name :help
14 :description "Print this text"
15 :short #\h
16 :long "help")
17 (:name :output-file
18 :description "File to write executable to"
19 :short #\f
20 :long "output-file"
21 :arg-parser #'get-fn-str)
22 (:name :entry-point
23 :description "Entry point for saved binary"
24 :short #\e
25 :long "entry-point"
26 :arg-parser #'read-from-string)
27 (:name :input-file
28 :description "File to load and save"
29 :short #\l
30 :long "load"
31 :arg-parser #'file-test))
32
33 ;; See: https://github.com/libre-man/unix-opts/blob/master/example/example.lisp
34 (defmacro when-option ((options opt) &body body)
35 `(let ((it (getf ,options ,opt)))
36 (when it
37 ,@body)))
38
39 ;;(defun saver (filename entry-point)
40 ;; (sb-ext:save-lisp-and-die filename :toplevel (function entry-point) :executable t :compression t))
41 (defmacro saver (filename entry-point)
42 `(sb-ext:save-lisp-and-die ,filename :toplevel ,entry-point :executable t :compression t))
43
44 (defun display-help ()
45 (progn
46 (opts:describe
47 :prefix "save.bin - Load and then save lisp files. Script over sb-ext:save-lisp-and-die"
48 :usage-of "save.bin"
49 :args "[FREE-ARGS]")
50 (quit)))
51
52 (defun builder ()
53 (if (> 2 (length sb-ext:*posix-argv*)) (display-help))
54 ;;; Get and process args
55 (let ((matches (opts:get-opts)))
56 (progn
57 (format t "~a ~%" matches)
58 (when-option (matches :help)
59 (display-help))
60 ;; Load program
61 (load (getf matches :input-file))
62 (print "Loaded file")
63 ;; Save program
64 (saver (getf matches :output-file) (getf matches :entry-point)))))