save.lisp 1.9 KB

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