Re-registering custom signals and class closures when loading saved images
[clg] / tools / asdf-extensions.lisp
CommitLineData
ab15c5d9 1(in-package :asdf)
2
43c60305 3(eval-when (:load-toplevel :compile-toplevel :execute)
4 (use-package :pkg-config))
65d26e3e 5
43c60305 6(export '(*search-library-path-on-reload* *dso-extension*
7 *operation* *system* *component* library shared-object
8 install-init-hook))
9
10(defvar *dso-extension*
0eed49b9 11 #-(or darwin win32)"so" #+darwin"dylib" #+win32"dll")
65d26e3e 12
43c60305 13(defvar *search-library-path-on-reload* t)
14
15
16;;; Since Common Lisp implementations doesn't seem to agree on how to
17;;; run init hooks, we have to add our own compatibility layer.
18
19(defvar *init-hooks* ())
20
21(defun install-init-hook (func &optional (only-once t))
22 (if only-once
23 (pushnew func *init-hooks*)
24 (push func *init-hooks*)))
25
26(defun run-init-hooks ()
27 (mapcar #'funcall (reverse *init-hooks*)))
28
29(pushnew 'run-init-hooks
30 #+cmu ext:*after-save-initializations*
31 #+sbcl sb-ext:*init-hooks*
32 #+clisp custom:*init-hooks*)
33
34(defvar *reload-shared-objects* ()
35 "List of shared objects which should be reloaded from library search
36 path in saved images.")
37
38#?-(sbcl>= 1 0 22)
39(defvar *dont-save-shared-objects* ()
40 "List of shared objects which should not be saved in images.")
41
42(defun namestring-name (namestring)
43 (let ((pos (position #\/ namestring :from-end t)))
44 (if pos
45 (subseq namestring (1+ pos))
46 namestring)))
47
48(defun load-shared-object (pathname &optional dont-save-p (reload-p dont-save-p))
49 (let* ((namestring (ensure-namestring pathname)))
50 #?(sbcl< 1 0 22)(sb-alien:load-shared-object namestring)
51 #?(sbcl>= 1 0 22)
52 (sb-alien:load-shared-object namestring :dont-save dont-save-p)
53 #+cmu(ext:load-foreign namestring)
54 #?(clisp< 2 45)(ffi::foreign-library namestring)
55 #?(clisp>= 2 45)(ffi:open-foreign-library namestring)
56 (when dont-save-p
57 #?-(sbcl>= 1 0 22)
58 (pushnew namestring *dont-save-shared-objects* :test #'string=)
59 (when reload-p
60 (pushnew (namestring-name namestring)
61 *reload-shared-objects* :test #'string=)))))
62
63#?(or (sbcl< 1 0 22) (featurep :cmu))
64(progn
65 (defun remove-shared-objects ()
66 (dolist (namestring *dont-save-shared-objects*)
67 #+sbcl
68 (setf sb-alien::*shared-objects*
69 (remove namestring sb-alien::*shared-objects*
70 :key #'sb-alien::shared-object-file
71 :test #'string=))
72 #+cmu
73 (setf system::*global-table*
74 (remove namestring system::*global-table*
75 :key #'cdr :test #'string=))))
76 (pushnew 'remove-shared-objects
77 #+sbcl sb-ext:*save-hooks*
78 #+cmu ext:*before-save-initializations*))
79
80(defun reload-shared-objects ()
81 (handler-bind (#+sbcl (style-warning #'muffle-warning))
82 (dolist (namestring (reverse *reload-shared-objects*))
83 (load-shared-object namestring))))
84
85(install-init-hook 'reload-shared-objects)
86
87
81814a23 88
fbeb759c 89;;; The following code is more or less copied from sb-bsd-sockets.asd,
90;;; but extended to allow flags to be set in a general way. The class
824e0c2e 91;;; has been renamed from unix-dso to shared-object as this code is no
92;;; longer specific to unix
ab15c5d9 93
fbeb759c 94(defclass shared-object (module)
824e0c2e 95 ((ldflags :initform nil :initarg :ldflags)
43c60305 96 (search :initform *search-library-path-on-reload* :initarg :search
97 :reader search-library-path-on-reload)))
fbeb759c 98
99(defun ensure-namestring (pathname)
ab15c5d9 100 (namestring
101 (typecase pathname
102 (logical-pathname (translate-logical-pathname pathname))
103 (t pathname))))
104
fbeb759c 105(defmethod input-files ((operation compile-op) (dso shared-object))
ab15c5d9 106 (mapcar #'component-pathname (module-components dso)))
107
fbeb759c 108(defmethod output-files ((operation compile-op) (dso shared-object))
ab15c5d9 109 (let ((dir (component-pathname dso)))
110 (list
65d26e3e 111 (make-pathname :type *dso-extension*
9bc0a84e 112 :name (component-name dso)
ab15c5d9 113 :directory (butlast (pathname-directory dir))
114 :defaults dir))))
115
fbeb759c 116(defmethod perform :after ((operation compile-op) (dso shared-object))
3f246c3a 117 (let ((output (first (output-files operation dso)))
fbeb759c 118 (inputs (mapcar #'ensure-namestring
3f246c3a 119 (mapcan #'(lambda (c)
120 (output-files operation c))
121 (module-components dso)))))
ab15c5d9 122 (unless (zerop
0eed49b9 123 (run-shell-command "gcc ~A -o ~S ~{~S~^ ~} ~{~A~^ ~}"
124 #-(or darwin win32)"-shared"
3f246c3a 125 #+darwin "-bundle"
126 #+win32
127 (format nil "-shared -Wl,--out-implib,~S"
fbeb759c 128 (ensure-namestring
3f246c3a 129 (make-pathname
130 :type "a"
131 :name (format nil "lib~Adll" (pathname-name output))
132 :defaults output)))
fbeb759c 133 (ensure-namestring output)
0eed49b9 134 inputs
135 (slot-value dso 'ldflags)))
ab15c5d9 136 (error 'operation-error :operation operation :component dso))))
137
824e0c2e 138(defmethod perform ((o load-op) (dso shared-object))
ab15c5d9 139 (let ((co (make-instance 'compile-op)))
824e0c2e 140 (let ((pathname (car (output-files co dso))))
43c60305 141 (load-shared-object pathname (search-library-path-on-reload dso)))))
ab15c5d9 142
143
144
145(defclass c-source-file (source-file)
146 ((cflags :initform nil :initarg :cflags)
147 (optimization :initform 2 :initarg :optimization)
148 (definitions :initform nil :initarg :definitions)
149 (include-paths :initform nil :initarg :include-paths)))
150
151
152(defmethod output-files ((op compile-op) (c c-source-file))
3d36c5d6 153 (list (make-pathname :type "o" :defaults (component-pathname c))))
ab15c5d9 154
155
156(defmethod perform ((op compile-op) (c c-source-file))
157 (unless
676c12d3 158 (= 0 (run-shell-command "gcc -Wall ~A~{ ~A~} -o ~S -c ~S"
3f246c3a 159 #-win32 "-fPIC"
160 #+win32 "-DBUILD_DLL"
161 (nconc
162 (when (slot-value c 'optimization)
163 (list (format nil "-O~A" (slot-value c 'optimization))))
164 (loop
165 for symbol in (slot-value c 'definitions)
166 collect (format nil "-D~A" symbol))
167 (loop
168 for path in (slot-value c 'include-paths)
169 collect (format nil "-I~A" path))
170 (slot-value c 'cflags))
fbeb759c 171 (ensure-namestring (first (output-files op c)))
172 (ensure-namestring (component-pathname c))))
ab15c5d9 173 (error 'operation-error :operation op :component c)))
174
175
176(defmethod perform ((operation load-op) (c c-source-file))
177 t)
178
179
9025e0d1 180;;; Shared libraries
81814a23 181
9025e0d1 182(defclass library (component)
fbeb759c 183 ((libdir :initarg :libdir :initform nil)
824e0c2e 184 (libname :initarg :libname :initform nil)
43c60305 185 (search :initform *search-library-path-on-reload* :initarg :search
186 :reader search-library-path-on-reload)))
81814a23 187
188
b008da5a 189(defun split-path (path)
676c12d3 190 (when path
191 (labels ((split (path)
192 (unless (zerop (length path))
193 (let ((slash (position #\/ path)))
194 (if slash
195 (cons (subseq path 0 slash) (split (subseq path (1+ slash))))
196 (list path))))))
197 (if (and (not (zerop (length path))) (char= (char path 0) #\/))
198 (cons :absolute (split (subseq path 1)))
199 (cons :relative (split path))))))
200
81814a23 201
202(defmethod component-pathname ((lib library))
676c12d3 203 (or
204 (when (slot-value lib 'libname)
205 (let ((filename (format nil "~A~A" (namestring (make-pathname :directory (split-path (slot-value lib 'libdir)))) (slot-value lib 'libname))))
206 (when (probe-file filename)
207 (pathname filename))))
208
209 (make-pathname
210 :type *dso-extension*
211 :name (or (slot-value lib 'libname) (component-name lib))
212 :directory (split-path (slot-value lib 'libdir)))))
81814a23 213
43c60305 214
215(defvar *loaded-libraries* ())
216
824e0c2e 217(defmethod perform ((o load-op) (lib library))
43c60305 218 (load-shared-object (component-pathname lib)
219 (search-library-path-on-reload lib))
220 (pushnew lib *loaded-libraries*))
9025e0d1 221
824e0c2e 222(defmethod perform ((operation operation) (lib library))
9025e0d1 223 nil)
224
824e0c2e 225(defmethod operation-done-p ((o load-op) (lib library))
43c60305 226 (find lib *loaded-libraries*))
9025e0d1 227
824e0c2e 228(defmethod operation-done-p ((o operation) (lib library))
9025e0d1 229 t)
9f47de56 230
231
232;;; Binding of dynamic variables during perform
233
234(defvar *operation* nil)
235(defvar *system* nil)
236(defvar *component* nil)
237
238(defmethod perform :around ((operation operation) (c component))
239 (let ((*operation* operation)
240 (*component* c)
241 (*system* (component-system c)))
242 (call-next-method)))