Debian Codegraph @

src/util/system.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;
5 ;;; **********************************************************************
6 ;;; (c) Copyright G. Attardi, 1993. All rights reserved.
7 ;;; **********************************************************************
8 ;;;
9 ;;; A simple minded System Builder Tool.
10 ;;;
11
12 ;;; ----------------------------------------------------------------------
13 ;;; Use:
14 ;;;
15 ;;; (defsystem name
16 ;;;
17 ;;; :modules
18 ;;; -----------------------------------------------------------------
19 ;;; file | load | compile | files which force
20 ;;; | environment | environment | recompilations of
21 ;;; -----------------------------------------------------------------
22 ;;; `(,@patches
23 ;;; (pkg ,(car patches) ,(car patches) ())
24 ;;; (macros (pkg macros) (pkg macros) ())
25 ;;; (low (pkg walk) (pkg macros) (macros))
26 ;;; (,xxx-low (low) (macros low) (low))
27 ;;; (boot (,xxx) (macros low ,xxx) (low ,xxx))
28 ;;; (last t t (boot)))
29 ;;;
30 ;;; :source-directory '("./")
31 ;;; :source-extension "./"
32 ;;; :fasl-directory "/usr/src/o/"
33 ;;; :fasl-extension "o"
34 ;;; :library-directory "./")
35 ;;;
36 ;;; ----------------------------------------------------------------------
37
38 (defpackage "SBT"
39 (:use "CL")
40 (:export defsystem
41 build-system
42 compile-system
43 load-system
44 build-ecl))
45
46 (in-package "SBT")
47
48 (defmacro defsystem (name &key modules
49 (source-directory '("./"))
50 (source-extension "lsp")
51 (fasl-directory "./")
52 (fasl-extension "o")
53 (library-directory "./"))
54 `(defparameter ,name ; rather then defvar
55 (make-system :NAME ',name
56 :MODULES ,modules
57 :SOURCE-DIRECTORY ,(if (consp source-directory) source-directory (list 'quote (list source-directory)))
58 :SOURCE-EXTENSION ,source-extension
59 :FASL-DIRECTORY ,fasl-directory
60 :FASL-EXTENSION ,fasl-extension
61 :LIBRARY-DIRECTORY ,library-directory)))
62
63 ;;; ----------------------------------------------------------------------
64
65 (defstruct (system (:TYPE vector) :NAMED)
66 name
67 modules
68 (source-directory '("./"))
69 (source-extension "lsp")
70 (fasl-directory "./")
71 (fasl-extension "o")
72 (library-directory "./"))
73
74 (defun make-source-pathname (name system)
75 (let ((name (string-downcase name))
76 (extension (system-source-extension system)))
77 (dolist (i (system-source-directory system))
78 (let ((pathname (make-pathname :name name :type extension
79 :defaults i)))
80 (let ((ok (probe-file pathname)))
81 (when ok (return-from make-source-pathname ok)))))
82 (error "sbt::make-source-pathname: source file not found")))
83
84 (defun make-binary-pathname (name system)
85 (make-pathname :name (string-downcase name)
86 :type (system-fasl-extension system)
87 :defaults (system-fasl-directory system)))
88
89 (defun make-library-pathname (system target)
90 (let* ((name (string-downcase (system-name system)))
91 (directory (system-library-directory system))
92 (output-name (merge-pathnames name directory)))
93 (compile-file-pathname output-name :type target)))
94
95 ;;; ----------------------------------------------------------------------
96 ;;; Operations on modules
97 ;;;
98
99 (defstruct (module (:TYPE vector) :NAMED
100 (:CONSTRUCTOR make-module (name))
101 ; (:PRINT-FUNCTION
102 ; (lambda (m s d)
103 ; (declare (ignore d))
104 ; (format s "#<Module ~A>" (module-name m))))
105 )
106 name
107 load-env
108 comp-env
109 recomp-reasons)
110
111 (defun make-modules (system-description)
112 (let ((modules ()))
113 (labels ((get-module (name)
114 (or (find name modules :KEY #'module-name)
115 (progn (setq modules (cons (make-module name) modules))
116 (first modules))))
117 (parse-spec (spec)
118 (if (eq spec 't)
119 (reverse (cdr modules))
120 (mapcar #'get-module spec))))
121 (dolist (file system-description)
122 (let* ((name (first file))
123 (module (get-module name)))
124 (setf (module-load-env module) (parse-spec (second file))
125 (module-comp-env module) (parse-spec (third file))
126 (module-recomp-reasons module) (parse-spec (fourth file))))))
127 (reverse modules)))
128
129 (defun make-transformations (system filter make-transform)
130 (let ((transforms (list nil)))
131 (dolist (m (make-modules (system-modules system)))
132 (when (funcall filter system m transforms)
133 (funcall make-transform m transforms)))
134 (nreverse (cdr transforms))))
135
136 (defun make-compile-transformation (module transforms)
137 (unless (dolist (trans transforms)
138 (and (eq (first trans) :COMPILE)
139 (eq (second trans) module)
140 (return trans)))
141 (dolist (c (module-comp-env module))
142 (make-load-transformation c transforms))
143 (push `(:COMPILE ,module) (cdr transforms))))
144
145 (defun make-load-transformation (module transforms)
146 (unless (dolist (trans transforms)
147 (when (eq (second trans) module)
148 (case (first trans)
149 (:COMPILE (return nil))
150 (:LOAD (return trans)))))
151 (dolist (l (module-load-env module))
152 (make-load-transformation l transforms))
153 (push `(:LOAD ,module) (cdr transforms))))
154
155
156 (defun make-load-without-dependencies-transformation (module transforms)
157 (unless (dolist (trans transforms)
158 (and (eq (first trans) :LOAD)
159 (eq (second trans) module)
160 (return trans)))
161 (push `(:LOAD ,module) (cdr transforms))))
162
163 (defun compile-filter (system module transforms)
164 (or (dolist (r (module-recomp-reasons module))
165 (when (dolist (transform transforms)
166 (when (and (eq (first transform) :COMPILE)
167 (eq (second transform) r))
168 (return t)))
169 (return t)))
170 (null (probe-file (make-binary-pathname (module-name module) system)))
171 (> (file-write-date (make-source-pathname (module-name module) system))
172 (file-write-date (make-binary-pathname (module-name module) system)))))
173
174 (defun sbt-compile-file (&rest a)
175 (apply #'compiler::compile-file a))
176
177 (defun operate-on-system (system mode &optional arg print-only
178 &aux (si::*init-function-prefix*
179 (string-upcase (system-name system))))
180 (let (transformations)
181 (flet ((load-module (m s)
182 (let ((name (module-name m)))
183 #-dlopen
184 (if print-only
185 (format t "~&Loading source of ~A..." name)
186 (load (make-source-pathname name s)))
187 #+dlopen
188 (if (or (eq mode :source)
189 (dolist (trans transformations)
190 (and (eq (first trans) :compile)
191 (eq (second trans) m)
192 ; Is this ok?
193 (return nil))))
194 (if print-only
195 (format t "~&Loading source of ~A..." name)
196 (load (make-source-pathname name s)))
197 (if print-only
198 (format t "~&Loading binary of ~A..." name)
199 (load (make-binary-pathname name s))))))
200
201 (compile-module (m s)
202 (format t "~&Compiling ~A..." (module-name m))
203 (unless print-only
204 (let ((name (module-name m)))
205 (sbt-compile-file (make-source-pathname name s)
206 :OUTPUT-FILE (make-binary-pathname name s)))))
207
208 (true (&rest ignore) (declare (ignore ignore)) 't))
209
210 (setq transformations
211 (ecase mode
212 ((:STATIC-LIBRARY :LIBRARY :SHARED-LIBRARY :FASL)
213 (let* ((transforms (make-transformations system
214 #'true
215 #'make-load-transformation))
216 (objects (mapcar #'(lambda (x) (make-binary-pathname (module-name (cadr x)) system))
217 (remove-if-not #'(lambda (x) (eq (car x) :LOAD))
218 transforms)))
219 (library (make-library-pathname system mode)))
220 (operate-on-system system :COMPILE)
221 (c::builder mode library :lisp-files objects))
222 nil)
223 (:COMPILE
224 (make-transformations system
225 #'compile-filter
226 #'make-compile-transformation))
227 (:RECOMPILE
228 (make-transformations system
229 #'true
230 #'make-compile-transformation))
231 (:QUERY-COMPILE
232 (make-transformations system
233 #'(lambda (s m transforms)
234 (or (compile-filter s m transforms)
235 (y-or-n-p "Compile ~A?"
236 (module-name m))))
237 #'make-compile-transformation))
238 (:COMPILE-FROM
239 (make-transformations system
240 #'(lambda (s m transforms)
241 (or (member (module-name m) arg)
242 (compile-filter s m transforms)))
243 #'make-compile-transformation))
244 ((:LOAD :SOURCE)
245 (make-transformations system
246 #'true
247 #'make-load-transformation))
248 (:QUERY-LOAD
249 (make-transformations system
250 #'(lambda (s m transforms)
251 (declare (ignore s transforms))
252 (y-or-n-p "Load ~A?" (module-name m)))
253 #'make-load-without-dependencies-transformation))))
254
255 (dolist (transform transformations)
256 (ecase (first transform)
257 (:COMPILE (compile-module (second transform) system))
258 (:LOAD (load-module (second transform) system)))))))
259
260
261 (defun compile-system (system &optional m)
262 (cond ((null m) (operate-on-system system :COMPILE))
263 ((eq m 't) (operate-on-system system :RECOMPILE))
264 ((eq m :PRINT) (operate-on-system system :COMPILE () t))
265 ((eq m :QUERY) (operate-on-system system :QUERY-COMPILE))
266 ((symbolp m) (operate-on-system system :COMPILE-FROM (list m)))
267 ((listp m) (operate-on-system system :COMPILE-FROM m))))
268
269 (defun load-system (system &optional mode)
270 (case mode
271 ((NIL) (operate-on-system system :LOAD))
272 (:SOURCE (operate-on-system system :SOURCE))
273 (:QUERY-LOAD (operate-on-system system :QUERY-LOAD))))
274
275
276 ;;;----------------------------------------------------------------------
277 ;;; User interface
278
279 (defmacro build-system (system &optional op mode)
280 (case op
281 (:LOAD
282 `(load-system ,system ,(case mode
283 (:QUERY :QUERY-LOAD)
284 (:SOURCE :SOURCE))))
285 (:COMPILE
286 `(compile-system ,system ,(case mode
287 (:QUERY :QUERY-COMPILE)
288 (:FORCE :RECOMPILE))))
289 (:PRINT
290 `(compile-system ,system :PRINT))
291 (otherwise
292 `(load-system ,system))))