source: tags/0.00.8/j/build-abcl.lisp

Last change on this file was 9861, checked in by piso, 19 years ago

OpenMCL support (Byran O'Connor).

File size: 22.9 KB
Line 
1;;; build-abcl.lisp
2
3#+abcl
4(require 'format)
5
6(defpackage build-abcl
7  (:use "COMMON-LISP")
8  (:export #:build-abcl #:make-dist)
9  #+abcl (:import-from #:extensions #:run-shell-command #:probe-directory)
10  #+allegro (:import-from #:excl #:probe-directory)
11  #+clisp (:import-from #:ext #:probe-directory)
12  )
13
14(in-package #:build-abcl)
15
16;; Platform detection.
17
18(defun platform ()
19  #-clisp
20  (let ((software-type (software-type)))
21    (cond ((search "Linux" software-type)
22           :linux)
23          ((or (search "Mac OS X" software-type) ; abcl
24               (search "Darwin" software-type))  ; sbcl
25           :darwin)
26          ((search "Windows" software-type)
27           :windows)
28          (t
29           :unknown)))
30  #+clisp
31  (cond ((member :win32 *features*)
32         :windows)
33        ((zerop (ext:run-shell-command "uname | grep -i darwin" :output nil))
34         :darwin)
35        ((zerop (ext:run-shell-command "uname | grep -i linux" :output nil))
36         :linux)
37        (t
38         :unknown)))
39
40(defparameter *platform* (platform))
41
42(defparameter *file-separator-char*
43  (if (eq *platform* :windows) #\\ #\/))
44
45(defparameter *path-separator-char*
46  (if (eq *platform* :windows) #\; #\:))
47
48(defmacro with-current-directory ((directory) &body body)
49  `(let ((*default-pathname-defaults* ,directory)
50         #+clisp
51         (old-directory (ext:cd)))
52     #+clisp
53     (ext:cd ,directory)
54     (unwind-protect
55         (progn ,@body)
56       #+clisp
57       (ext:cd old-directory)
58       )))
59
60#+sbcl
61(defun run-shell-command (command &key directory (output *standard-output*))
62  (when directory
63    (setf command (concatenate 'string
64                               "\\cd \""
65                               (namestring (pathname directory))
66                               "\" && "
67                               command)))
68  (sb-impl::process-exit-code
69   (sb-ext:run-program
70    "/bin/sh"
71    (list  "-c" command)
72    :input nil :output output)))
73
74#+cmu
75(defun run-shell-command (command &key directory (output *standard-output*))
76  (when directory
77    (setf command (concatenate 'string
78                               "\\cd \""
79                               (namestring (pathname directory))
80                               "\" && "
81                               command)))
82  (ext::process-exit-code
83   (ext:run-program
84    "/bin/sh"
85    (list  "-c" command)
86    :input nil :output output)))
87
88#+clisp
89(defun run-shell-command (command &key directory (output *standard-output*))
90  (declare (ignore output)) ;; FIXME
91  (let (status old-directory)
92    (when directory
93      (setf old-directory (ext:cd))
94      (ext:cd directory))
95    (unwind-protect
96        (setf status (ext:run-shell-command command))
97      (when old-directory
98        (ext:cd old-directory)))
99    (cond ((numberp status)
100           status)
101          ((eq status t)
102           0)
103          (t
104           -1))))
105
106#+lispworks
107(defun run-shell-command (command &key directory (output *standard-output*))
108  (when directory
109    (unless (eq *platform* :windows)
110      (setf command (concatenate 'string
111                                 "\\cd \""
112                                 (namestring (pathname directory))
113                                 "\" && "
114                                 command))))
115  (system:call-system-showing-output
116   command
117   :shell-type "/bin/sh"
118   :output-stream output))
119
120#+allegro
121(defun run-shell-command (command &key directory (output *standard-output*))
122  (excl:run-shell-command command :directory directory :input nil :output output))
123
124#+openmcl
125(defun run-shell-command (command &key directory (output *standard-output*))
126  (when directory
127    (setf command (concatenate 'string
128                               "\\cd \""
129                               (namestring (pathname directory))
130                               "\" && "
131                               command)))
132  (multiple-value-bind (status exitcode)
133      (ccl:external-process-status
134       (ccl:run-program
135        "/bin/sh"
136        (list  "-c" command)
137        :wait t :input nil :output output))
138    (declare (ignore status))
139    exitcode))
140
141#+(or sbcl cmu lispworks)
142(defun probe-directory (pathspec)
143  (let* ((truename (probe-file pathspec)) ; TRUENAME is a pathname.
144         (namestring (and truename (namestring truename)))) ; NAMESTRING is a string.
145    (and namestring
146         (> (length namestring) 0)
147         (eql (char namestring (1- (length namestring))) *file-separator-char*)
148         truename)))
149
150(defparameter *build-root*
151  (make-pathname :device (pathname-device *load-truename*)
152                 :directory (pathname-directory *load-truename*)))
153
154(defparameter *customizations-file*
155  (merge-pathnames "customizations.lisp" *build-root*))
156
157(defparameter *abcl-dir*
158  (merge-pathnames "src/org/armedbear/lisp/" *build-root*))
159
160(defparameter *jdk*           nil)
161(defparameter *java-compiler* nil)
162(defparameter *javac-options* nil)
163(defparameter *jikes-options* nil)
164(defparameter *jar*           nil)
165
166(defvar *classpath*)
167(defvar *java*)
168(defvar *java-compiler-options*)
169(defvar *java-compiler-command-line-prefix*)
170
171(defun initialize-build ()
172  (setf *jdk*           nil
173        *java-compiler* nil
174        *javac-options* nil
175        *jikes-options* nil
176        *jar*           nil)
177  (load *customizations-file*)
178  (setf *java* (probe-file (merge-pathnames (if (eq *platform* :windows)
179                                                "bin\\java.exe"
180                                                "bin/java")
181                                            *jdk*)))
182  (unless *java*
183    (error "Can't find Java executable."))
184  (unless *java-compiler*
185    (setf *java-compiler* (merge-pathnames (if (eq *platform* :windows)
186                                               "bin/javac.exe"
187                                               "bin/javac")
188                                           *jdk*)))
189  (unless *jar*
190    (setf *jar* (merge-pathnames (if (eq *platform* :windows)
191                                     "bin/jar.exe"
192                                     "bin/jar")
193                                 *jdk*)))
194  (let ((classpath-components (list (merge-pathnames "src" *build-root*)
195                                    (if (eq *platform* :darwin)
196                                        #p"/System/Library/Frameworks/JavaVM.framework/Classes/classes.jar"
197                                        (merge-pathnames "jre/lib/rt.jar" *jdk*)))))
198    (setf *classpath*
199          (with-output-to-string (s)
200            (do* ((components classpath-components (cdr components))
201                  (component (car components) (car components)))
202                 ((null components))
203              (princ (safe-namestring component) s)
204              (unless (null (cdr components))
205                (write-char *path-separator-char* s))))))
206  (let ((prefix (concatenate 'string
207                             (safe-namestring *java-compiler*)
208                             " -classpath " *classpath*)))
209    (setf *java-compiler-options*
210          (if (string-equal (pathname-name (pathname *java-compiler*)) "jikes")
211              *jikes-options*
212              *javac-options*))
213    (setf prefix
214          (if *java-compiler-options*
215              (concatenate 'string prefix " " *java-compiler-options* " ")
216              (concatenate 'string prefix " ")))
217    (setf *java-compiler-command-line-prefix* prefix)))
218
219(defun substitute-in-string (string substitutions-alist)
220  (dolist (entry substitutions-alist)
221    (let ((index (search (car entry) string :test #'string=)))
222      (when index
223        (setf string (concatenate 'string
224                                  (subseq string 0 index)
225                                  (cdr entry)
226                                  (subseq string (+ index (length (car entry)))))))))
227  string)
228
229(defun copy-with-substitutions (source-file target-file substitutions-alist)
230  (with-open-file (in source-file :direction :input)
231    (with-open-file (out target-file :direction :output :if-exists :supersede)
232      (loop
233        (let ((string (read-line in nil)))
234          (when (null string)
235            (return))
236          (write-line (substitute-in-string string substitutions-alist) out))))))
237
238(defun build-javac-command-line (source-file)
239  (concatenate 'string
240               *java-compiler-command-line-prefix*
241               (namestring source-file)))
242
243(defun java-compile-file (source-file)
244  (let ((cmdline (build-javac-command-line source-file)))
245    (zerop (run-shell-command cmdline :directory *abcl-dir*))))
246
247(defun make-classes (force batch)
248  (let* ((source-files
249          (append (with-current-directory (*abcl-dir*)
250                    (directory "*.java"))
251                  (with-current-directory ((merge-pathnames "java/awt/" *abcl-dir*))
252                    (directory "*.java"))))
253         (to-do ()))
254    (if force
255        (setf to-do source-files)
256        (dolist (source-file source-files)
257          (let ((class-file (merge-pathnames (make-pathname :type "class"
258                                                            :defaults source-file))))
259            (when (or (null (probe-file class-file))
260                      (>= (file-write-date source-file)
261                          (file-write-date class-file)))
262              (push source-file to-do)))))
263    (format t "~&JDK: ~A~%" *jdk*)
264    (format t "Java compiler: ~A~%" *java-compiler*)
265    (format t "Compiler options: ~A~%~%" (if *java-compiler-options* *java-compiler-options* ""))
266    (finish-output)
267    (cond ((null to-do)
268           (format t "Classes are up to date.~%")
269           (finish-output)
270           t)
271          (t
272           (cond (batch
273                  (let* ((dir (pathname-directory *abcl-dir*))
274                         (cmdline (with-output-to-string (s)
275                                    (princ *java-compiler-command-line-prefix* s)
276                                    (dolist (source-file to-do)
277                                      (princ
278                                       (if (equal (pathname-directory source-file) dir)
279                                           (file-namestring source-file)
280                                           (namestring source-file))
281                                       s)
282                                      (princ #\space s))))
283                         (status (run-shell-command cmdline :directory *abcl-dir*)))
284                    (zerop status)))
285                 (t
286                  (dolist (source-file to-do t)
287                    (unless (java-compile-file source-file)
288                      (format t "Build failed.~%")
289                      (return nil)))))))))
290
291(defun make-jar ()
292  (let ((*default-pathname-defaults* *build-root*)
293        (jar-namestring (namestring *jar*)))
294    (when (position #\space jar-namestring)
295      (setf jar-namestring (concatenate 'string "\"" jar-namestring "\"")))
296    (let ((substitutions-alist (acons "@JAR@" jar-namestring nil))
297          (source-file (if (eq *platform* :windows) "make-jar.bat.in" "make-jar.in"))
298          (target-file (if (eq *platform* :windows) "make-jar.bat"    "make-jar"))
299          (command     (if (eq *platform* :windows) "make-jar.bat"    "sh make-jar")))
300      (copy-with-substitutions source-file target-file substitutions-alist)
301      (let ((status (run-shell-command command :directory *build-root*)))
302        (unless (zerop status)
303          (format t "~A returned ~S~%" command status))
304        status))))
305
306(defun do-compile-system (&key (zip t))
307  (terpri)
308  (finish-output)
309  (let* ((java-namestring (safe-namestring *java*))
310         status)
311    (cond ((eq *platform* :windows)
312           (with-open-file (stream
313                            (merge-pathnames "compile-system.bat" *build-root*)
314                            :direction :output
315                            :if-exists :supersede)
316             (princ java-namestring stream)
317             (write-string " -cp " stream)
318             (princ "src" stream)
319             (write-char #\space stream)
320             (write-string
321              (if zip
322                 "org.armedbear.lisp.Main --eval \"(compile-system :zip t :quit t)\""
323                 "org.armedbear.lisp.Main --eval \"(compile-system :zip nil :quit t)\"")
324              stream)
325             (terpri stream))
326           (setf status
327                 (run-shell-command "compile-system.bat"
328                                    :directory *build-root*)))
329          (t ; Linux
330           (let ((cmdline
331                  (with-output-to-string (s)
332                    (princ java-namestring s)
333                    (write-string " -cp " s)
334                    (princ "src" s)
335                    (write-char #\space s)
336                    (write-string
337                     (if zip
338                         "org.armedbear.lisp.Main --eval \"(compile-system :zip t :quit t)\""
339                         "org.armedbear.lisp.Main --eval \"(compile-system :zip nil :quit t)\"")
340                     s))))
341             (setf status
342                   (run-shell-command cmdline
343                                      :directory *build-root*)))))
344    status))
345
346(defun make-libabcl ()
347  (and (let* ((javah-namestring (namestring (probe-file (merge-pathnames "bin/javah" *jdk*))))
348              (command
349               (format nil "~A -o org/armedbear/lisp/native.h org.armedbear.lisp.Native"
350                       javah-namestring))
351              (status
352               (run-shell-command command :directory (merge-pathnames "src/" *build-root*))))
353         (unless (zerop status)
354           (format t "~A returned ~S~%" command status))
355         (zerop status))
356       (let* ((jdk-namestring (namestring *jdk*))
357              (command
358               (format nil "gcc -shared -o libabcl.so -O -D_REENTRANT -fpic -I~Ainclude -I~Ainclude/~A native.c"
359                       jdk-namestring jdk-namestring
360                       (cond ((eq *platform* :linux)
361                              "linux")
362                             ((search "SunOS" (software-type))
363                              "solaris"))))
364              (status
365               (run-shell-command command :directory *abcl-dir*)))
366         (unless (zerop status)
367           (format t "~A returned ~S~%" command status))
368         (zerop status))))
369
370;; abcl/abcl.bat
371(defun make-launch-script ()
372  (cond ((eq *platform* :windows)
373         (with-open-file (s
374                          (merge-pathnames "abcl.bat" *build-root*)
375                          :direction :output
376                          :if-exists :supersede)
377           (format s "~A -cp ~A;~A org.armedbear.lisp.Main %1 %2 %3 %4 %5 %6 %7 %8 %9~%"
378                   (safe-namestring *java*)
379                   (safe-namestring (merge-pathnames "src" *build-root*))
380                   (safe-namestring (merge-pathnames "abcl.jar" *build-root*)))))
381        (t
382         ;; Use the -Xmx256M flag on non-Windows platforms so that the default
383         ;; launch script can be used to build sbcl.
384         (let ((pathname (merge-pathnames "abcl" *build-root*)))
385           (with-open-file (s pathname :direction :output :if-exists :supersede)
386             (if (eq *platform* :linux)
387                 ;; On Linux, set java.library.path for libabcl.so.
388                 (format s "#!/bin/sh~%exec ~A -Xmx256M -Xrs -Djava.library.path=~A -cp ~A:~A org.armedbear.lisp.Main \"$@\"~%"
389                         (safe-namestring *java*)
390                         (safe-namestring *abcl-dir*)
391                         (safe-namestring (merge-pathnames "src" *build-root*))
392                         (safe-namestring (merge-pathnames "abcl.jar" *build-root*)))
393                 ;; Not Linux.
394                 (format s "#!/bin/sh~%exec ~A -Xmx256M -cp ~A:~A org.armedbear.lisp.Main \"$@\"~%"
395                         (safe-namestring *java*)
396                         (safe-namestring (merge-pathnames "src" *build-root*))
397                         (safe-namestring (merge-pathnames "abcl.jar" *build-root*)))))
398           (run-shell-command (format nil "chmod +x ~A" (safe-namestring pathname))
399                              :directory *build-root*)))))
400
401(defun build-stamp ()
402  (multiple-value-bind
403      (second minute hour date month year day daylight-p zone)
404      (decode-universal-time (get-universal-time))
405    (declare (ignore daylight-p))
406    (setf day (nth day '("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun")))
407    (setf month (nth (1- month) '("Jan" "Feb" "Mar" "Apr" "May" "Jun"
408                                  "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")))
409    (setf zone (* zone 100)) ;; FIXME
410    (format nil "~A ~A ~D ~D ~2,'0D:~2,'0D:~2,'0D -~4,'0D"
411            day month date year hour minute second zone)))
412
413(defun make-build-stamp ()
414  (with-open-file (s
415                   (merge-pathnames (make-pathname :name "build"
416                                                   :defaults *abcl-dir*))
417                   :direction :output
418                   :if-exists :supersede)
419    (format s "~A" (build-stamp))))
420
421(defun delete-files (pathnames)
422  (dolist (pathname pathnames)
423    (let ((truename (probe-file pathname)))
424      (when truename
425        (delete-file truename)))))
426
427(defun clean ()
428  (with-current-directory (*abcl-dir*)
429    (delete-files (directory "*.class"))
430    (delete-files (directory "*.abcl"))
431    (delete-files (directory "*.cls"))
432    (delete-files '("native.h" "libabcl.so" "build")))
433  (with-current-directory ((merge-pathnames "java/awt/" *abcl-dir*))
434    (delete-files (directory "*.class"))))
435
436(defun safe-namestring (pathname)
437  (let ((string (namestring pathname)))
438    (when (position #\space string)
439      (setf string (concatenate 'string "\"" string "\"")))
440    string))
441
442(defun build-abcl (&key force
443                        (batch t)
444                        compile-system
445                        jar
446                        clean
447                        libabcl
448                        full)
449  (let ((start (get-internal-real-time)))
450
451    #+lispworks
452    (when (eq *platform* :windows)
453      (setf batch nil))
454
455    (initialize-build)
456    (format t "~&Platform: ~A~%"
457            (case *platform*
458              (:windows "Windows")
459              (:linux   "Linux")
460              (:darwin  "Mac OS X")
461              (t        (software-type))))
462    (finish-output)
463    ;; clean
464    (when clean
465      (clean))
466    ;; classes
467    (unless (make-classes force batch)
468      (format t "Build failed.~%")
469      (return-from build-abcl nil))
470    ;; COMPILE-SYSTEM
471    (when (or full compile-system)
472      (let* ((zip    (if (or full jar) nil t))
473             (status (do-compile-system :zip zip)))
474        (unless (zerop status)
475          (format t "Build failed.~%")
476          (return-from build-abcl nil))))
477    ;; abcl.jar
478    (when (or full jar)
479      (let ((status (make-jar)))
480        (unless (zerop status)
481          (format t "Build failed.~%")
482          (return-from build-abcl nil))))
483    ;; libabcl.so
484    (when (and (or full libabcl)
485               (or (eq *platform* :linux)
486                   (search "SunOS" (software-type))))
487      ;; A failure here is not fatal.
488      (make-libabcl))
489    ;; abcl/abcl.bat
490    (make-launch-script)
491    (make-build-stamp)
492    (let ((end (get-internal-real-time)))
493      (format t "Build completed successfully in ~A seconds.~%"
494              (/ (float (- end start)) internal-time-units-per-second)))
495    t))
496
497(defun build-abcl-executable ()
498  (let* ((*default-pathname-defaults* *abcl-dir*)
499         (source-files (directory "*.java"))
500         (cmdline (with-output-to-string (s)
501                    (princ "gcj -O2 " s)
502                    (dolist (source-file source-files)
503                      (unless (string= (pathname-name source-file) "ControlC")
504                        (princ (pathname-name source-file) s)
505                        (princ ".java" s)
506                        (princ #\space s)))
507                    (princ "--main=org.armedbear.lisp.Main -o lisp" s)))
508         (result (run-shell-command cmdline :directory *abcl-dir*)))
509    (zerop result)))
510
511(defvar *copy-verbose* nil)
512
513(defun copy-file (source target)
514  (when *copy-verbose*
515    (format t "~A -> ~A~%" source target))
516  (let ((buffer (make-array 4096 :element-type '(unsigned-byte 8))))
517    (with-open-file (in source :direction :input :element-type '(unsigned-byte 8))
518      (with-open-file (out target :direction :output :element-type '(unsigned-byte 8)
519                           :if-exists :supersede)
520        (loop
521          (let ((end (read-sequence buffer in)))
522            (when (zerop end)
523              (return))
524            (write-sequence buffer out :end end)))))))
525
526(defun copy-files (files source-dir target-dir)
527  (dolist (file files)
528    (copy-file (merge-pathnames file source-dir)
529               (merge-pathnames file target-dir))))
530
531(defun make-dist-dir (version-string)
532  (unless (eq *platform* :linux)
533    (error "MAKE-DIST is only supported on Linux."))
534  (let ((target-root (pathname (concatenate 'string "/var/tmp/" version-string "/"))))
535    (when (probe-directory target-root)
536      (error "Target directory ~S already exists." target-root))
537    (ensure-directories-exist
538     (merge-pathnames "src/org/armedbear/lisp/java/awt/" target-root))
539    (let* ((source-dir *build-root*)
540           (target-dir target-root)
541           (files (list "README"
542                        "COPYING"
543                        "build-abcl.lisp"
544                        "customizations.lisp"
545                        "make-jar.bat.in"
546                        "make-jar.in")))
547      (copy-files files source-dir target-dir))
548    (let* ((source-dir (merge-pathnames "src/" *build-root*))
549           (target-dir (merge-pathnames "src/" target-root))
550           (files (list "manifest-abcl")))
551      (copy-files files source-dir target-dir))
552    (let* ((source-dir *abcl-dir*)
553           (target-dir (merge-pathnames "src/org/armedbear/lisp/" target-root))
554           (*default-pathname-defaults* source-dir)
555           (files (mapcar #'file-namestring (append (directory "*.java")
556                                                    (directory "*.lisp")
557                                                    (list "LICENSE" "native.c")))))
558      (copy-files files source-dir target-dir))
559    (let* ((source-dir (merge-pathnames "java/awt/" *abcl-dir*))
560           (target-dir (merge-pathnames "src/org/armedbear/lisp/java/awt/" target-root))
561           (*default-pathname-defaults* source-dir)
562           (files (mapcar #'file-namestring (directory "*.java"))))
563      (copy-files files source-dir target-dir))
564    target-root))
565
566(defun make-dist (version-string)
567  (let* ((dist-dir (make-dist-dir version-string))
568         (parent-dir (merge-pathnames (make-pathname :directory '(:relative :back))
569                                      dist-dir)))
570    (let* ((command (format nil "tar czf ~A~A.tar.gz ~A"
571                            (namestring parent-dir)
572                            version-string version-string))
573           (status (run-shell-command command :directory parent-dir)))
574      (unless (zerop status)
575        (format t "~A returned ~S~%" command status)))
576    (let* ((command (format nil "zip -q -r ~A~A.zip ~A"
577                            (namestring parent-dir)
578                            version-string version-string))
579           (status (run-shell-command command :directory parent-dir)))
580      (unless (zerop status)
581        (format t "~A returned ~S~%" command status)))))
Note: See TracBrowser for help on using the repository browser.