chiark / gitweb /
Fix to load shared libraries from default locations in CMUCL and CLISP
[clg] / tools / asdf-extensions.lisp
CommitLineData
ab15c5d9 1(in-package :asdf)
2
f9b784eb 3(export '(*absolute-paths-as-default* *dso-extension*
676c12d3 4 *operation* *system* *component* library shared-object))
65d26e3e 5
3f246c3a 6(defparameter *dso-extension*
0eed49b9 7 #-(or darwin win32)"so" #+darwin"dylib" #+win32"dll")
65d26e3e 8
f9b784eb 9(defparameter *absolute-paths-as-default* nil)
81814a23 10
fbeb759c 11;;; The following code is more or less copied from sb-bsd-sockets.asd,
12;;; but extended to allow flags to be set in a general way. The class
824e0c2e 13;;; has been renamed from unix-dso to shared-object as this code is no
14;;; longer specific to unix
ab15c5d9 15
fbeb759c 16(defclass shared-object (module)
824e0c2e 17 ((ldflags :initform nil :initarg :ldflags)
f9b784eb 18 (absolute :initform *absolute-paths-as-default*
19 :initarg :absolute :reader absolute-p)))
fbeb759c 20
21(defun ensure-namestring (pathname)
ab15c5d9 22 (namestring
23 (typecase pathname
24 (logical-pathname (translate-logical-pathname pathname))
25 (t pathname))))
26
fbeb759c 27(defmethod input-files ((operation compile-op) (dso shared-object))
ab15c5d9 28 (mapcar #'component-pathname (module-components dso)))
29
fbeb759c 30(defmethod output-files ((operation compile-op) (dso shared-object))
ab15c5d9 31 (let ((dir (component-pathname dso)))
32 (list
65d26e3e 33 (make-pathname :type *dso-extension*
9bc0a84e 34 :name (component-name dso)
ab15c5d9 35 :directory (butlast (pathname-directory dir))
36 :defaults dir))))
37
fbeb759c 38(defmethod perform :after ((operation compile-op) (dso shared-object))
3f246c3a 39 (let ((output (first (output-files operation dso)))
fbeb759c 40 (inputs (mapcar #'ensure-namestring
3f246c3a 41 (mapcan #'(lambda (c)
42 (output-files operation c))
43 (module-components dso)))))
ab15c5d9 44 (unless (zerop
0eed49b9 45 (run-shell-command "gcc ~A -o ~S ~{~S~^ ~} ~{~A~^ ~}"
46 #-(or darwin win32)"-shared"
3f246c3a 47 #+darwin "-bundle"
48 #+win32
49 (format nil "-shared -Wl,--out-implib,~S"
fbeb759c 50 (ensure-namestring
3f246c3a 51 (make-pathname
52 :type "a"
53 :name (format nil "lib~Adll" (pathname-name output))
54 :defaults output)))
fbeb759c 55 (ensure-namestring output)
0eed49b9 56 inputs
57 (slot-value dso 'ldflags)))
ab15c5d9 58 (error 'operation-error :operation operation :component dso))))
59
5eda2e76 60#+clisp
61(defvar *loaded-libraries* ())
3d36c5d6 62
824e0c2e 63(defun load-shared-object (pathname &optional (absolute-p t))
64 (let* ((namestring (ensure-namestring pathname))
65 (directory (namestring (pathname-sans-name+type namestring)))
66 (name+type (subseq namestring (length directory))))
67 #+sbcl
68 (progn
69 (sb-alien:load-shared-object namestring)
70 (unless absolute-p
71 (let ((shared-object (find namestring sb-alien::*shared-objects*
72 :key #'sb-alien::shared-object-file
73 :test #'equal)))
74 (setf (sb-alien::shared-object-file shared-object) name+type))))
75 #+cmu
76 (progn
77 (ext:load-foreign namestring)
78 (unless absolute-p
79 (let ((shared-object (rassoc namestring system::*global-table*
80 :test #'equal)))
81 (setf (cdr shared-object) name+type))))
82 #+clisp
83 (progn
676c12d3 84 #?-(pkg-config:clisp>= 2 45)
fbeb759c 85 (ffi::foreign-library namestring)
676c12d3 86 #?(pkg-config:clisp>= 2 45)
87 (ffi:open-foreign-library namestring)
824e0c2e 88 (pushnew
89 (if absolute-p namestring name+type)
90 *loaded-libraries* :test #'string=))))
81814a23 91
92
824e0c2e 93(defmethod perform ((o load-op) (dso shared-object))
ab15c5d9 94 (let ((co (make-instance 'compile-op)))
824e0c2e 95 (let ((pathname (car (output-files co dso))))
96 (load-shared-object pathname (absolute-p dso)))))
ab15c5d9 97
98
99
100(defclass c-source-file (source-file)
101 ((cflags :initform nil :initarg :cflags)
102 (optimization :initform 2 :initarg :optimization)
103 (definitions :initform nil :initarg :definitions)
104 (include-paths :initform nil :initarg :include-paths)))
105
106
107(defmethod output-files ((op compile-op) (c c-source-file))
3d36c5d6 108 (list (make-pathname :type "o" :defaults (component-pathname c))))
ab15c5d9 109
110
111(defmethod perform ((op compile-op) (c c-source-file))
112 (unless
676c12d3 113 (= 0 (run-shell-command "gcc -Wall ~A~{ ~A~} -o ~S -c ~S"
3f246c3a 114 #-win32 "-fPIC"
115 #+win32 "-DBUILD_DLL"
116 (nconc
117 (when (slot-value c 'optimization)
118 (list (format nil "-O~A" (slot-value c 'optimization))))
119 (loop
120 for symbol in (slot-value c 'definitions)
121 collect (format nil "-D~A" symbol))
122 (loop
123 for path in (slot-value c 'include-paths)
124 collect (format nil "-I~A" path))
125 (slot-value c 'cflags))
fbeb759c 126 (ensure-namestring (first (output-files op c)))
127 (ensure-namestring (component-pathname c))))
ab15c5d9 128 (error 'operation-error :operation op :component c)))
129
130
131(defmethod perform ((operation load-op) (c c-source-file))
132 t)
133
134
9025e0d1 135;;; Shared libraries
81814a23 136
9025e0d1 137(defclass library (component)
fbeb759c 138 ((libdir :initarg :libdir :initform nil)
824e0c2e 139 (libname :initarg :libname :initform nil)
f9b784eb 140 (absolute :initform *absolute-paths-as-default*
141 :initarg :absolute :reader absolute-p)))
81814a23 142
143
b008da5a 144(defun split-path (path)
676c12d3 145 (when path
146 (labels ((split (path)
147 (unless (zerop (length path))
148 (let ((slash (position #\/ path)))
149 (if slash
150 (cons (subseq path 0 slash) (split (subseq path (1+ slash))))
151 (list path))))))
152 (if (and (not (zerop (length path))) (char= (char path 0) #\/))
153 (cons :absolute (split (subseq path 1)))
154 (cons :relative (split path))))))
155
81814a23 156
157(defmethod component-pathname ((lib library))
676c12d3 158 (or
159 (when (slot-value lib 'libname)
160 (let ((filename (format nil "~A~A" (namestring (make-pathname :directory (split-path (slot-value lib 'libdir)))) (slot-value lib 'libname))))
161 (when (probe-file filename)
162 (pathname filename))))
163
164 (make-pathname
165 :type *dso-extension*
166 :name (or (slot-value lib 'libname) (component-name lib))
167 :directory (split-path (slot-value lib 'libdir)))))
81814a23 168
824e0c2e 169(defmethod perform ((o load-op) (lib library))
170 (load-shared-object (component-pathname lib) (absolute-p lib)))
9025e0d1 171
824e0c2e 172(defmethod perform ((operation operation) (lib library))
9025e0d1 173 nil)
174
824e0c2e 175(defmethod operation-done-p ((o load-op) (lib library))
176 (let* ((namestring (ensure-namestring (component-pathname lib)))
177 (directory (namestring (pathname-sans-name+type namestring)))
178 (name+type (subseq namestring (length directory)))
179 (stored-name (if (absolute-p lib) namestring name+type)))
180
181 #+sbcl(find stored-name sb-alien::*shared-objects* :key #'sb-alien::shared-object-file :test #'equal)
182 #+cmu(rassoc stored-name system::*global-table* :test #'equal)
183 #+clisp(find stored-name *loaded-libraries* :test #'equal)))
9025e0d1 184
824e0c2e 185(defmethod operation-done-p ((o operation) (lib library))
9025e0d1 186 t)
9f47de56 187
188
189;;; Binding of dynamic variables during perform
190
191(defvar *operation* nil)
192(defvar *system* nil)
193(defvar *component* nil)
194
195(defmethod perform :around ((operation operation) (c component))
196 (let ((*operation* operation)
197 (*component* c)
198 (*system* (component-system c)))
199 (call-next-method)))