Hash :
b0ab5bb5
Author :
Thomas de Grivel
Date :
2022-12-04T18:50:53
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
;; cl-unix-cybernetics
;; Copyright 2013-2022 Thomas de Grivel <thodg@kmx.io>
;;
;; Permission is hereby granted to use this software granted
;; the above copyright notice and this permission paragraph
;; are included in all copies and substantial portions of this
;; software.
;;
;; THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
;; PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
;; AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
;; THIS SOFTWARE.
(in-package :common-lisp-user)
(defvar *system-directory*
(make-hash-table))
(defun system-directory (system)
(or #1=(gethash system *system-directory*)
(let* ((sys (typecase system (asdf:system system)
(t (asdf:find-system system))))
(asd (asdf:system-source-file sys)))
(setf #1#
(make-pathname :name nil :type nil :defaults asd)))))
(defun system-file (system &rest parts)
(let ((str (apply #'concatenate 'string parts)))
(merge-pathnames str (system-directory system))))
(asdf:load-system :alexandria)
(defun namestring* (x)
(etypecase x
(null "")
(pathname (namestring x))
(string x)))
(defparameter *dir* (namestring* (system-file :cl-unix-cybernetics "")))
(defgeneric collect-sources (x))
(defmethod collect-sources ((x symbol))
(collect-sources (asdf:find-system x)))
(defmethod collect-sources ((x string))
(collect-sources (asdf:find-system x)))
(defun sort-components (list)
(declare (type list list))
(let (components roots)
(declare (type list components roots))
(labels ((map-dependencies (comp fn)
(declare (type asdf:component comp))
(dolist (id (asdf:component-sideway-dependencies comp))
(let ((dep (find id list :test #'string=
:key #'asdf:component-name)))
(when dep
(funcall (the function fn) dep)))))
(dfs (comp)
(declare (type asdf:component comp))
(map-dependencies comp #'dfs)
(pushnew comp components)))
(dolist (comp list)
(declare (type asdf:component comp))
(pushnew comp roots))
(dolist (comp list)
(declare (type asdf:component comp))
(map-dependencies comp (lambda (dep)
(setf roots (delete dep roots)))))
(dolist (comp roots)
(dfs comp)))
(nreverse components)))
(defmethod collect-sources ((x asdf:parent-component))
(let ((children (sort-components (asdf:component-children x))))
(mapcan #'collect-sources children)))
(defmethod collect-sources ((req asdf:require-system))
(list `(require ,(string-upcase (asdf:component-name req)))))
(defun strip-common-lisp-directory (dir)
(declare (type list dir))
(let ((pos (position "common-lisp" dir :test #'string=)))
(if pos
(nthcdr pos dir)
dir)))
(defun dependency-path (src)
(let* ((cl-unix-cybernetics-dir *dir*)
(path (pathname src))
(dir (pathname-directory path))
(name (pathname-name path))
(type (pathname-type path)))
(with-output-to-string (out)
(write-string cl-unix-cybernetics-dir out)
(write-string "build/" out)
(let ((dir (strip-common-lisp-directory dir)))
(dolist (d (rest dir))
(write-string d out)
(write-char #\- out)))
(write-string name out)
(write-char #\. out)
(write-string type out))))
(defun copy-dependency (src)
(let ((dep (dependency-path src)))
(alexandria:copy-file src dep)
(enough-namestring dep *dir*)))
(defmethod collect-sources ((x asdf:cl-source-file))
(let* ((src (asdf:component-pathname x))
(dep (copy-dependency src)))
(list `(compile-lisp ,dep))))
(defmethod collect-sources ((x asdf:file-component))
(list `(quote ,(asdf:component-pathname x))))
(defmethod collect-sources :around ((x asdf:component))
(let ((if-feature (asdf::component-if-feature x)))
(etypecase if-feature
(null
(call-next-method))
(symbol
(when (find (the symbol if-feature) *features*)
(call-next-method)))
(cons
(cond ((string-equal 'not (first if-feature))
(unless (find (the symbol (second if-feature)) *features*)
(call-next-method)))
(t (error "Bad if-feature")))))))
#+nil (collect-sources :cl-unix-cybernetics)
(defun write-system-build-file (system sbf)
(format t "~&~A~%" sbf) (force-output)
(with-open-file (out sbf :direction :output
:element-type 'character
:if-exists :supersede
:if-does-not-exist :create
:external-format :utf-8)
(declare (type stream out))
(format out "~&;; ~A" (asdf:component-name system))
(dolist (src (collect-sources system))
(print src out))))
(defun system-build-file (system)
(let* ((asd (asdf:system-source-file system))
(name (substitute #\- #\/ (asdf:component-name system)))
(sbf (concatenate 'string "build/" name ".lisp")))
(unless (and (probe-file sbf)
(<= (file-write-date asd)
(file-write-date sbf)))
(write-system-build-file system sbf))
sbf))
(defun system-and-dependencies (name)
(let (dependencies)
(labels ((dfs (name)
(let ((sys (asdf:find-system name)))
(when (and sys (not (find sys dependencies)))
(locally (declare (type asdf:system sys))
(format t "~& ~A" sys) (force-output)
(map 'nil #'dfs (asdf:system-depends-on sys))
(push sys dependencies))))))
(dfs name)
(nreverse dependencies))))
(defun write-build-systems-file (system)
(unless (typep system 'asdf:system)
(setq system (asdf:find-system system)))
(let* ((path (system-file system "build/systems.lisp")))
(print path) (force-output)
(ensure-directories-exist path)
(with-open-file (out path :direction :output
:element-type 'character
:external-format :utf-8
:if-exists :supersede
:if-does-not-exist :create)
(declare (type stream out))
(dolist (sys (system-and-dependencies system))
(let* ((build-file (system-build-file sys))
(load-form `(load ,build-file)))
(format t "~& ~A~%" sys) (force-output)
(print load-form out)))
(fresh-line out)
(force-output out))))
(write-build-systems-file :cl-unix-cybernetics)