<commit_before>;; FIXME: arrange packages so that this can be moved in ASDF some time later?

(in-package #:cffi-toolchain)

(defun static-ops-enabled-p ()
  (ensure-toolchain-parameters)
  (and (or *linkkit-start* *linkkit-end*) t))

(defclass static-runtime-op (monolithic-bundle-op link-op selfward-operation)
  ((selfward-operation :initform 'monolithic-lib-op :allocation :class))
  (:documentation "Create a Lisp runtime linkable library for the system and its dependencies."))

(defmethod output-files ((o static-runtime-op) (s system))
  #-(or ecl mkcl)
  (list (subpathname (component-pathname s)
                     (strcat (coerce-name s) "-runtime")
                     :type (bundle-pathname-type :program))))

(defmethod perform ((o static-runtime-op) (s system))
  (link-lisp-executable
   (output-file o s)
   (link-all-library (first (input-files o s)))))

(defclass static-image-op (image-op)
  (#-(or ecl mkcl) (selfward-operation :initform '(load-op static-runtime-op) :allocation :class)
   #+(or ecl mkcl)
   (gather-operation :initform 'compile-op :allocation :class)
   #+(or ecl mkcl)
   (gather-type :initform :object :allocation :class))
  (:documentation "Create a statically linked standalone image for the system."))

(defclass static-program-op (program-op static-image-op)
  (#-(or ecl mkcl) (selfward-operation :initform '(load-op static-runtime-op) :allocation :class)
   #+(or ecl mkcl)
   (gather-operation :initform 'compile-op :allocation :class)
   #+(or ecl mkcl)
   (gather-type :initform :object :allocation :class))
  (:documentation "Create a statically linked standalone executable for the system."))

;; Problem? Its output may conflict with the program-op output :-/

#-(or ecl mkcl)
(defmethod perform ((o static-image-op) (s system))
  #-(or clisp sbcl) (error "Not implemented yet")
  #+(or clisp sbcl)
  (let* ((name (coerce-name s))
         (runtime (output-file 'static-runtime-op s))
         (image
           #+clisp (implementation-file "base/lispinit.mem")
           #+sbcl (subpathname (lisp-implementation-directory) "sbcl.core"))
         (output (output-file o s))
         (child-op (if (typep o 'program-op) 'program-op 'image-op)))
    (with-temporary-output (tmp output)
      (apply 'invoke runtime
             #+clisp "-M" #+sbcl "--core" image
             `(#+clisp ,@'("--silent" "-ansi" "-norc" "-x")
               #+sbcl ,@'("--noinform" "--non-interactive" "--no-sysinit" "--no-userinit" "--eval")
               ,(with-safe-io-syntax (:package :asdf)
                  (let ((*print-pretty* nil)
                        (*print-case* :downcase))
                    (format
                     ;; This clever staging allows to put things in a single form,<commit_msg>Make static-link ASDF support more robust<commit_after>;; FIXME: arrange packages so that this can be moved in ASDF some time later?

(in-package #:cffi-toolchain)

(defun static-ops-enabled-p ()
  (ensure-toolchain-parameters)
  (and (or *linkkit-start* *linkkit-end*) t))

(defclass static-runtime-op (monolithic-bundle-op link-op selfward-operation) ()
  (:documentation "Create a Lisp runtime linkable library for the system and its dependencies."))
(defmethod bundle-type ((o static-runtime-op)) :program)
(defmethod selfward-operation ((o static-runtime-op)) 'monolithic-lib-op)

(defmethod output-files ((o static-runtime-op) (s system))
  #-(or ecl mkcl)
  (list (subpathname (component-pathname s)
                     (strcat (coerce-name s) "-runtime")
                     :type (bundle-pathname-type :program))))

(defmethod perform ((o static-runtime-op) (s system))
  (link-lisp-executable
   (output-file o s)
   (link-all-library (first (input-files o s)))))

(defclass static-image-op (image-op) ()
  (:documentation "Create a statically linked standalone image for the system."))
#-(or ecl mkcl) (defmethod selfward-operation ((o static-image-op)) '(load-op static-runtime-op))
#+(or ecl mkcl) (defmethod gather-operation ((o static-image-op)) 'compile-op)
#+(or ecl mkcl) (defmethod gather-operation ((o static-image-op)) :object)

(defclass static-program-op (program-op static-image-op) ()
  (:documentation "Create a statically linked standalone executable for the system."))

;; Problem? Its output may conflict with the program-op output :-/

#-(or ecl mkcl)
(defmethod perform ((o static-image-op) (s system))
  #-(or clisp sbcl) (error "Not implemented yet")
  #+(or clisp sbcl)
  (let* ((name (coerce-name s))
         (runtime (output-file 'static-runtime-op s))
         (image
           #+clisp (implementation-file "base/lispinit.mem")
           #+sbcl (subpathname (lisp-implementation-directory) "sbcl.core"))
         (output (output-file o s))
         (child-op (if (typep o 'program-op) 'program-op 'image-op)))
    (with-temporary-output (tmp output)
      (apply 'invoke runtime
             #+clisp "-M" #+sbcl "--core" image
             `(#+clisp ,@'("--silent" "-ansi" "-norc" "-x")
               #+sbcl ,@'("--noinform" "--non-interactive" "--no-sysinit" "--no-userinit" "--eval")
               ,(with-safe-io-syntax (:package :asdf)
                  (let ((*print-pretty* nil)
                        (*print-case* :downcase))
                    (format
                     ;; This clever staging allows to put things in a single form,
{'fraction_non_alphanumeric': 0.15401911186059583, 'fraction_numerical': 0.0, 'mean_word_length': 2.9865571321882003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2821537', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1678, 'n_words': 498}