chiark / gitweb /
Shared object files given unique names
[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
11;;; has been renamed from unix-dso to shared-object as this code no
12;;; longer is unix specific
77dd2192 13
57f6526c 14(defclass shared-object (module)
bcd6b6af 15 ((ldflags :initform nil :initarg :ldflags)))
16
57f6526c 17;; For backwards compatibility
18(defclass unix-dso (shared-object)
19 ())
20
21(defun ensure-namestring (pathname)
77dd2192 22 (namestring
23 (typecase pathname
24 (logical-pathname (translate-logical-pathname pathname))
25 (t pathname))))
26
57f6526c 27(defmethod input-files ((operation compile-op) (dso shared-object))
77dd2192 28 (mapcar #'component-pathname (module-components dso)))
29
57f6526c 30(defmethod output-files ((operation compile-op) (dso shared-object))
77dd2192 31 (let ((dir (component-pathname dso)))
32 (list
34abe734 33 (make-pathname :type *dso-extension*
b6f51030 34 :name (component-name dso)
77dd2192 35 :directory (butlast (pathname-directory dir))
36 :defaults dir))))
37
57f6526c 38(defmethod perform :after ((operation compile-op) (dso shared-object))
a4263d6d 39 (let ((output (first (output-files operation dso)))
57f6526c 40 (inputs (mapcar #'ensure-namestring
a4263d6d 41 (mapcan #'(lambda (c)
42 (output-files operation c))
43 (module-components dso)))))
77dd2192 44 (unless (zerop
e6ab30c2 45 (run-shell-command "gcc ~A -o ~S ~{~S~^ ~} ~{~A~^ ~}"
46 #-(or darwin win32)"-shared"
a4263d6d 47 #+darwin "-bundle"
48 #+win32
49 (format nil "-shared -Wl,--out-implib,~S"
57f6526c 50 (ensure-namestring
a4263d6d 51 (make-pathname
52 :type "a"
53 :name (format nil "lib~Adll" (pathname-name output))
54 :defaults output)))
57f6526c 55 (ensure-namestring output)
e6ab30c2 56 inputs
57 (slot-value dso 'ldflags)))
77dd2192 58 (error 'operation-error :operation operation :component dso))))
59
dfdb198f 60#+clisp
61(defvar *loaded-libraries* ())
73572c12 62
57f6526c 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)
67 #+clisp
68 (unless (find namestring *loaded-libraries* :test #'equal)
69 (ffi::foreign-library namestring)
70 (push namestring *loaded-libraries*))))
1a9c1e08 71
72
57f6526c 73(defmethod perform ((o load-op) (c shared-object))
77dd2192 74 (let ((co (make-instance 'compile-op)))
57f6526c 75 (let ((pathname (car (output-files co c))))
76 (load-shared-object pathname))))
77dd2192 77
78
79
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)))
85
86
87(defmethod output-files ((op compile-op) (c c-source-file))
73572c12 88 (list (make-pathname :type "o" :defaults (component-pathname c))))
77dd2192 89
90
91(defmethod perform ((op compile-op) (c c-source-file))
92 (unless
a4263d6d 93 (= 0 (run-shell-command "gcc ~A~{ ~A~} -o ~S -c ~S"
94 #-win32 "-fPIC"
95 #+win32 "-DBUILD_DLL"
96 (nconc
97 (when (slot-value c 'optimization)
98 (list (format nil "-O~A" (slot-value c 'optimization))))
99 (loop
100 for symbol in (slot-value c 'definitions)
101 collect (format nil "-D~A" symbol))
102 (loop
103 for path in (slot-value c 'include-paths)
104 collect (format nil "-I~A" path))
105 (slot-value c 'cflags))
57f6526c 106 (ensure-namestring (first (output-files op c)))
107 (ensure-namestring (component-pathname c))))
77dd2192 108 (error 'operation-error :operation op :component c)))
109
110
111(defmethod perform ((operation load-op) (c c-source-file))
112 t)
113
114
fd9d29a4 115;;; Shared libraries
1a9c1e08 116
fd9d29a4 117(defclass library (component)
57f6526c 118 ((libdir :initarg :libdir :initform nil)
1dfea3ab 119 (libname :initarg :libname :initform nil)))
1a9c1e08 120
121
3e9e71e7 122(defun split-path (path)
123 (labels ((split (path)
124 (unless (zerop (length path))
125 (let ((slash (position #\/ path)))
126 (if slash
127 (cons (subseq path 0 slash) (split (subseq path (1+ slash))))
128 (list path))))))
129 (if (and (not (zerop (length path))) (char= (char path 0) #\/))
130 (cons :absolute (split (subseq path 1)))
131 (cons :relative (split path)))))
132
1a9c1e08 133
134(defmethod component-pathname ((lib library))
34abe734 135 (make-pathname :type *dso-extension*
1dfea3ab 136 :name (or (slot-value lib 'libname) (component-name lib))
3e9e71e7 137 :directory (split-path (slot-value lib 'libdir))))
1a9c1e08 138
139(defmethod perform ((o load-op) (c library))
57f6526c 140 (load-shared-object (component-pathname c)))
fd9d29a4 141
142(defmethod perform ((operation operation) (c library))
143 nil)
144
145(defmethod operation-done-p ((o load-op) (c library))
57f6526c 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)))
fd9d29a4 150
151(defmethod operation-done-p ((o operation) (c library))
152 t)