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