SRRAT: use MRAT reader functions instead of CADDAR, etc.
[maxima.git] / archive / src / make.lisp
blob84037a83584f503d31d71ebb49347e9a5692f9dd
1 ;;; -*- Mode: Lisp; Package: MAKE; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;; Copyright William F. Schelter 1989.
4 ;; The author expressly permits copying and alteration of this file,
5 ;; provided any modifications are clearly labeled, and this notice is
6 ;; preserved. The author provides no warranty and this software is
7 ;; provided on an 'as is' basis.
9 (or (find-package "MAKE") (make-package "MAKE" :use '("LISP") #+akcl :external #+akcl 11 #+akcl :internal #+akcl 79))
11 (in-package "MAKE")
13 (export '(make system-load system-compile))
14 (provide "MAKE")
15 ;;; ******* Description of Make Facility ************
16 ;; We provide a simple MAKE facility to allow
17 ;;compiling and loading of a tree of files
18 ;;If the tree is '(a b (d e g h) i)
19 ;; a will be loaded before b is compiled,
20 ;; b will be loaded before d, e, g, h are compiled
21 ;; d e g h will be loaded before i is compiled.
23 ;; A record is kept of write dates of loaded compiled files, and a file
24 ;;won't be reloaded if it is the same version (unless a force flag is t).
26 ;;Thus if you do (make :uinfor) twice in a row, the second one would not
27 ;;load anything. NOTE: If you change a, and a macro in it would affect
28 ;;b, b still will not be recompiled. You must choose the :recompile t
29 ;;option, to force the recompiling if you change macro files.
30 ;;Alternately you may specify dependency information (see :depends below).
33 ;;****** Sample file which when loaded causes system ALGEBRA
34 ;; to be compiled and loaded ******
36 ;;(require "MAKE")
37 ;;(use-package "MAKE")
38 ;;(setf (get :algebra :make) '(a b (d e) l))
39 ;;(setf (get :algebra :source-path) "/usr2/wfs/algebra/foo.lisp")
40 ;;(setf (get :algebra :object-path) "/usr2/wfs/algebra/o/foo.o")
41 ;;(make :algebra :compile t)
43 ;; More complex systems may need to do some special operations
44 ;;at certain points of the make.
45 ;;the tree of files may contain some keywords which have special meaning.
46 ;;eg. '(a b (:progn (gbc) (if make::*compile*
47 ;; (format t "A and B finally compiled")))
48 ;; (:load-source h i)
49 ;; (d e) l)
51 ;;then during the load and compile phases the function (gbc) will be
52 ;;called after a and b have been acted on, and during the compile phase
53 ;;the message about "A and B finally.." will be printed.
54 ;;the lisp files h and i will be loaded after merging the paths with
55 ;;the source directory. This feature is extensible: see the definitions
56 ;;of :load-source and :progn.
58 ;; The keyword feature is extensible, and you may specify what
59 ;;happens during the load or compile phase for your favorite keyword.
60 ;;To do this look at the definition of :progn, and :load-source
61 ;;in the source for make.
64 ;;Dependency feature:
66 ;; This make NEVER loads or compiles files in an order different from
67 ;;that specified by the tree. It will omit loading files which are
68 ;;loaded and up to date, but if two files are out of date, the first (in
69 ;;the printed representation of the tree), will always be loaded before
70 ;;the second. A consequence of this is that circular dependencies can
71 ;;never occur.
73 ;; If the :make tree contains (a b c d (:depends (c d) (a b))) then c
74 ;;and d depend on a and b, so that if a or b need recompilation then c
75 ;;and d will also be recompiled. Thus the general form of a :depends
76 ;;clause is (:depends later earlier) where LATER and EARLIER are either
77 ;;a single file or a list of files. Read it as LATER depends on EARLIER.
78 ;;A declaration of a (:depends (c) (d)) would have no effect, since the
79 ;;order in the tree already rules out such a dependence.
81 ;; An easy way of specifying a linear dependence is by using :serial.
82 ;;The tree (a (:serial b c d) e) is completely equivalent to the tree
83 ;;(a b c d e (:depends c b)(:depends d (b c))), but with a long list of
84 ;;serial files, it is inconvenient to specify them in the
85 ;;latter representation.
87 ;;A common case is a set of macros whose dependence is serial followed by a set
88 ;;of files whose order is unimportant. A conventient way of building that
89 ;;tree is
91 ;;(let ((macros '(a b c d))
92 ;; (files '(c d e f g)))
93 ;; `((:serial ,@ macros)
94 ;; ,files
95 ;; (:depends ,files ,macros)))
97 ;; The depends clause may occur anywhere within the tree, since
98 ;;an initial pass collects all dependency information.
100 ;; Make takes a SHOW keyword argument. It is almost impossible to simulate
101 ;;all the possible features of make, for show. Nonetheless, it is good
102 ;;to get an idea of the compiling and loading sequence for a new system.
103 ;;As a byproduct, you could use the output, as a simple sequence of calls
104 ;;to compile-file and load, to do the required work, when make is not around
105 ;;to help.
108 ;;***** Definitions ********
109 (defvar *files-loaded* nil)
110 (defvar *show-files-loaded* nil) ;only for show option
111 (defvar *load* nil "Will be non nil inside load-files")
112 (defvar *compile* nil "Bound by compile-files to t")
113 (defvar *depends* nil)
114 (defvar *depends-new* nil)
115 (defvar *force* nil)
116 (defvar *when-compile* nil "Each compile-file evals things in this list and sets it to nil")
117 #+kcl(defvar *system-p* nil)
118 (defvar *compile-file-function* 'make-compile-file)
119 (defvar *load-function* 'make-load-file)
120 (defvar show nil)
121 (defvar *cflags* #-kcl nil
122 #+kcl '(:system-p *system-p*))
125 ;;this is the main entry point
127 (defun make (system &key recompile compile batch object-path source-path
128 show proclaims
129 &aux files *depends* *when-compile*
130 *show-files-loaded*
131 #+akcl (*load-fn-too* proclaims)
135 "SYSTEM is a tree of files, or a symbol with :make property. It
136 loads all file files in system. If COMPILE it will try to compile
137 files with newer source versions than object versions, before loading.
138 If RECOMPILE it will recompile all files. This is equivalent to deleting all
139 objects and using :compile t. SOURCE-PATH is merged with the name given
140 in the files list, when looking for a file to compile. OBJECT-PATH is
141 merged with the name in the files list, when looking for a file to
142 load. If SYSTEM is a symbol, then a null OBJECT-PATH would be set to
143 the :object-path property of SYSTEM. Similarly for :source-path"
145 (declare (special object-path source-path show)) batch
146 (cond ((symbolp system)
147 (or object-path (setf object-path (get system :object-path)))
148 (or source-path (setf source-path (get system :source-path)))
149 (setf files (get system :make))
150 (or files
151 (if (get system :files)
152 (error "Use :make property, :files property is obssolet{!")))
154 (t (setf files system)))
155 #+akcl (when proclaims (compiler::emit-fn t) (compiler::setup-sys-proclaims))
156 (let (#+lispm ( si::inhibit-fdefine-warnings
157 (if batch :just-warn si::inhibit-fdefine-warnings)))
158 (let ((*depends* (if (or compile recompile) (get-depends system)))
159 *depends-new*)
160 (dolist (v files)
161 (when (or compile recompile)
162 (compile-files v recompile))
163 (load-files v recompile)))
164 #+akcl
165 (if proclaims (compiler::write-sys-proclaims))
168 (defun system-load (system-name &rest names)
169 "If :infor is a system, (system-load :uinfor joe betty) will load
170 joe and betty from the object-path for :uinfor"
171 (load-files names t (get system-name :object-path)))
173 (defun system-compile (system-name &rest names)
175 "If :iunfor is a system, (system-compile :uinfor joe) will in the
176 source path for joe and compile him into the object path for :uinfor"
177 (compile-files names t :source-path
178 (get system-name :source-path) :object-path
179 (get system-name :object-path)))
181 (defun get-depends (system-name &aux result)
182 (dolist (v (get system-name :make))
183 (cond ((atom v) )
184 ((eq (car v) :serial)
185 (do ((w (reverse (cdr v))(cdr w)))
186 ((null (cdr w)))
187 (push (list (car w) (cdr w)) result)))
188 ((eq (car v) :depends)
189 (push (cdr v) result ))))
190 result)
192 #+kcl
193 (setq si::*default-time-zone* 6)
195 (defun print-date (&optional(stream *standard-output*)
196 (time (get-universal-time)))
197 (multiple-value-bind (sec min hr day mon yr wkday)
198 (decode-universal-time time)
199 (format stream "~a ~a ~a ~d:~2,'0d:~2,'0d ~a"
200 (nth wkday '( "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun"))
201 (nth (1- mon) '("Jan" "Feb" "Mar" "Apr" "May" "Jun"
202 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"))
204 hr min sec yr)))
206 ;;This is an awfully roundabout downcase, but some machines
207 ;;like symbolics swap cases on the pathname, so we have to do an extra
208 ;;swap!!
209 (defun lowcase (na &aux (*print-case* :downcase))
210 (pathname-name (pathname (format nil "~a" na))))
212 (defun our-merge (name path &optional ign ) ign
213 #+lispm (setq name (string-upcase (string name)))
214 (make-pathname :name (string name)
215 :type (pathname-type path)
216 :version (pathname-version path)
217 :host (pathname-host path)
218 :directory (pathname-directory path)))
221 #+kcl
222 (setf (get :link 'load)
223 #'(lambda (path to-link)
224 (declare (special object-path))
225 (si::faslink (our-merge (lowcase path) object-path)
226 to-link)))
228 (setf (get :link 'compile)
229 #'(lambda (path to-link)
230 to-link
231 (compile-files path *force*)))
233 (setf (get :progn 'load)
234 #'(lambda (&rest args)
235 (eval (cons 'progn args))))
237 (setf (get :progn 'compile) (get :progn 'load))
239 (setf (get :load-source 'load)
240 #'(lambda (&rest args)
241 (declare (special source-path))
242 (load-files args *force* source-path)))
244 (setf (get :load-source-when-compile 'compile)
245 (get :load-source 'load))
247 ;;should nott use :lisp anymore
248 (setf (get :lisp 'load)
249 #'(lambda (x) (error "please replace :lisp by :load-source")))
251 (setf (get :serial 'load) #'(lambda (&rest l)(load-files l)))
252 (setf (get :serial 'compile)
253 #'(lambda (&rest l)
254 (dolist (v l)
255 (compile-files v)
256 (load-files v))))
259 (defun load-files (files &optional (*force* *force*) (object-path object-path)
260 &aux path tem (*load* t))
261 (declare (special object-path source-path *force* show))
262 (cond ((atom files)
263 (setq path (object files))
264 (cond (show
265 (unless (member path *show-files-loaded* :test 'equalp)
266 (push path *show-files-loaded*)
267 (format t "~%(LOAD ~s)" (namestring path))))
268 ((null *load-function*))
269 ((or *force*
270 (or (not (setq tem
271 (member path *files-loaded*
272 :test 'equalp :key 'car)))
273 (> (file-write-date path) (cdr (car tem)))))
274 (funcall *load-function* files)
275 (push (cons path (file-write-date path)) *files-loaded*))))
276 ((keywordp (car files))
277 (let ((fun (get (car files) 'load)))
278 (cond (fun (apply fun (cdr files))))))
279 (t (dolist (v files) (load-files v *force* object-path)))))
282 (defun file-date (file)
283 (if (probe-file file) (or (file-write-date file) 0) 0))
285 (defun source (file)
286 (declare (special source-path))
287 (our-merge (lowcase file) source-path))
289 (defun object (file)
290 (declare (special object-path))
291 (our-merge (lowcase file) object-path))
294 ;;for lisp machines, and others where checking date is slow, this
295 ;;we should try to cache some dates, and then remove them as we do
296 ;;things like compile files...
298 (defun file-out-dated (file)
299 (let ((obj-date (file-date (object file))))
300 (or (<= obj-date (file-date (source file)))
301 (dolist (v *depends*)
302 (cond ((or (and (consp (car v))
303 (member file (car v)))
304 (eq (car v) file))
305 (dolist (w (if (consp (second v))
306 (second v) (cdr v)))
307 (cond ((or (<= obj-date (file-date (source w)))
308 (member w *depends-new*))
309 (return-from file-out-dated t))))))))))
312 (defun make-compile-file ( l)
313 (format t "~&Begin compile ~a at ~a~%" l (print-date nil))
314 (dolist (v *when-compile*) (eval v))
315 (setq *when-compile* nil)
316 ;;Franz excl needs pathnames quoted, and some other lisp
317 ;;would not allow an apply here. Sad.
318 (eval `(compile-file ',(source l) :output-file ',(object l)
319 ,@ *cflags*))
320 (format t "~&End compile ~a at ~a~%" l (print-date nil))
324 (defvar *load-fn-too* nil)
325 (defun make-load-file (l)
326 (let ((na (object l)))
327 (load na)
328 (if (and *load-fn-too*
329 (probe-file
330 (setq na
331 (our-merge (lowcase l) (merge-pathnames "foo.fn" na)))))
332 (load na))
337 ;;these are versions which don't really compile or load files, but
338 ;;do create a new "compiled file" and "fake load" to test date mechanism.
339 #+debug
340 (defun make-compile-file (file)
341 (format t "~%Fake Compile ~a" (namestring (source file)))
342 (dolist (v *when-compile*) (eval v)) (setq *when-compile* nil)
343 (with-open-file (st (object file) :direction :output)
344 (format st "(print (list 'hi))")))
345 #+debug
346 (defun make-load-file (l)
347 (format t "~%Fake loading ~a" (namestring(object l))))
352 (defun compile-files (files &optional (*force* *force*)
353 &key (source-path source-path)
354 (object-path object-path)
355 &aux
356 (*compile* t) )
357 (declare (special object-path source-path *force* show))
358 (cond ((atom files)
359 (when (or *force* (file-out-dated files))
360 (push files *depends-new*)
361 (cond
362 (show
363 (format t "~%(COMPILE-FILE ~s)" (namestring (source files))))
365 (and *compile-file-function*
366 (funcall *compile-file-function* files))
367 ))))
368 ((keywordp (car files))
369 (let ((fun (get (car files) 'compile)))
370 (if fun (apply fun (cdr files)))))
371 (t (dolist (v files) (compile-files v *force*)))))
373 ;;Return the files for SYSTEM
375 (defun system-files (system &aux *files*)
376 (declare (special *files*))
377 (let ((sys (get system :make)))
378 (get-files1 sys))
379 (nreverse *files*))
382 (defun get-files1 (sys)
383 (declare (special *files*))
384 (cond ((and sys (atom sys) )(pushnew sys *files*))
385 ((eq (car sys) :serial) (get-files1 (cdr sys)))
386 ((keywordp (car sys)))
387 (t (dolist (v sys) (get-files1 v)))))