3 (export '*dso-extension*)
5 (defparameter *dso-extension*
6 #-(or darwin win32)"so" #+darwin"dylib" #+win32"dll")
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
11 ;;; has been renamed from unix-dso to shared-object as this code no
12 ;;; longer is unix specific
14 (defclass shared-object (module)
15 ((ldflags :initform nil :initarg :ldflags)))
17 ;; For backwards compatibility
18 (defclass unix-dso (shared-object)
21 (defun ensure-namestring (pathname)
24 (logical-pathname (translate-logical-pathname pathname))
27 (defmethod input-files ((operation compile-op) (dso shared-object))
28 (mapcar #'component-pathname (module-components dso)))
30 (defmethod output-files ((operation compile-op) (dso shared-object))
31 (let ((dir (component-pathname dso)))
33 (make-pathname :type *dso-extension*
34 :name (component-name dso)
35 :directory (butlast (pathname-directory dir))
38 (defmethod perform :after ((operation compile-op) (dso shared-object))
39 (let ((output (first (output-files operation dso)))
40 (inputs (mapcar #'ensure-namestring
42 (output-files operation c))
43 (module-components dso)))))
45 (run-shell-command "gcc ~A -o ~S ~{~S~^ ~} ~{~A~^ ~}"
46 #-(or darwin win32)"-shared"
49 (format nil "-shared -Wl,--out-implib,~S"
53 :name (format nil "lib~Adll" (pathname-name output))
55 (ensure-namestring output)
57 (slot-value dso 'ldflags)))
58 (error 'operation-error :operation operation :component dso))))
61 (defvar *loaded-libraries* ())
63 (defun load-shared-object (pathname)
64 (let ((namestring (ensure-namestring pathname)))
65 #+sbcl(sb-alien:load-shared-object namestring)
66 #+cmu(ext:load-foreign namestring)
68 (unless (find namestring *loaded-libraries* :test #'equal)
69 (ffi::foreign-library namestring)
70 (push namestring *loaded-libraries*))))
73 (defmethod perform ((o load-op) (c shared-object))
74 (let ((co (make-instance 'compile-op)))
75 (let ((pathname (car (output-files co c))))
76 (load-shared-object pathname))))
80 (defclass c-source-file (source-file)
81 ((cflags :initform nil :initarg :cflags)
82 (optimization :initform 2 :initarg :optimization)
83 (definitions :initform nil :initarg :definitions)
84 (include-paths :initform nil :initarg :include-paths)))
87 (defmethod output-files ((op compile-op) (c c-source-file))
88 (list (make-pathname :type "o" :defaults (component-pathname c))))
91 (defmethod perform ((op compile-op) (c c-source-file))
93 (= 0 (run-shell-command "gcc ~A~{ ~A~} -o ~S -c ~S"
97 (when (slot-value c 'optimization)
98 (list (format nil "-O~A" (slot-value c 'optimization))))
100 for symbol in (slot-value c 'definitions)
101 collect (format nil "-D~A" symbol))
103 for path in (slot-value c 'include-paths)
104 collect (format nil "-I~A" path))
105 (slot-value c 'cflags))
106 (ensure-namestring (first (output-files op c)))
107 (ensure-namestring (component-pathname c))))
108 (error 'operation-error :operation op :component c)))
111 (defmethod perform ((operation load-op) (c c-source-file))
117 (defclass library (component)
118 ((libdir :initarg :libdir :initform nil)
119 (libname :initarg :libname :initform nil)))
122 (defun split-path (path)
123 (labels ((split (path)
124 (unless (zerop (length path))
125 (let ((slash (position #\/ path)))
127 (cons (subseq path 0 slash) (split (subseq path (1+ slash))))
129 (if (and (not (zerop (length path))) (char= (char path 0) #\/))
130 (cons :absolute (split (subseq path 1)))
131 (cons :relative (split path)))))
134 (defmethod component-pathname ((lib library))
135 (make-pathname :type *dso-extension*
136 :name (or (slot-value lib 'libname) (component-name lib))
137 :directory (split-path (slot-value lib 'libdir))))
139 (defmethod perform ((o load-op) (c library))
140 (load-shared-object (component-pathname c)))
142 (defmethod perform ((operation operation) (c library))
145 (defmethod operation-done-p ((o load-op) (c library))
146 (let ((namestring (ensure-namestring (component-pathname c))))
147 #+sbcl(find namestring sb-alien::*shared-objects* :key #'sb-alien::shared-object-file :test #'equal)
148 #+cmu(rassoc namestring system::*global-table* :test #'equal)
149 #+clisp(find namestring *loaded-libraries* :test #'equal)))
151 (defmethod operation-done-p ((o operation) (c library))