chiark / gitweb /
Renamed CLIPBOARD-SET-CONTENT to CLIPBOARD-SET-CONTENTS for consistency
[clg] / tools / asdf-extensions.lisp
CommitLineData
ab15c5d9 1(in-package :asdf)
2
65d26e3e 3(export '*dso-extension*)
4
3f246c3a 5(defparameter *dso-extension*
0eed49b9 6 #-(or darwin win32)"so" #+darwin"dylib" #+win32"dll")
65d26e3e 7
81814a23 8
fbeb759c 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
ab15c5d9 13
fbeb759c 14(defclass shared-object (module)
f729cb9b 15 ((ldflags :initform nil :initarg :ldflags)))
16
fbeb759c 17;; For backwards compatibility
18(defclass unix-dso (shared-object)
19 ())
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*
ab15c5d9 34 :name (car (last (pathname-directory dir)))
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
fbeb759c 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*))))
81814a23 71
72
fbeb759c 73(defmethod perform ((o load-op) (c shared-object))
ab15c5d9 74 (let ((co (make-instance 'compile-op)))
fbeb759c 75 (let ((pathname (car (output-files co c))))
76 (load-shared-object pathname))))
ab15c5d9 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))
3d36c5d6 88 (list (make-pathname :type "o" :defaults (component-pathname c))))
ab15c5d9 89
90
91(defmethod perform ((op compile-op) (c c-source-file))
92 (unless
3f246c3a 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))
fbeb759c 106 (ensure-namestring (first (output-files op c)))
107 (ensure-namestring (component-pathname c))))
ab15c5d9 108 (error 'operation-error :operation op :component c)))
109
110
111(defmethod perform ((operation load-op) (c c-source-file))
112 t)
113
114
9025e0d1 115;;; Shared libraries
81814a23 116
9025e0d1 117(defclass library (component)
fbeb759c 118 ((libdir :initarg :libdir :initform nil)
d1237548 119 (libname :initarg :libname :initform nil)))
81814a23 120
121
b008da5a 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
81814a23 133
134(defmethod component-pathname ((lib library))
65d26e3e 135 (make-pathname :type *dso-extension*
d1237548 136 :name (or (slot-value lib 'libname) (component-name lib))
b008da5a 137 :directory (split-path (slot-value lib 'libdir))))
81814a23 138
139(defmethod perform ((o load-op) (c library))
fbeb759c 140 (load-shared-object (component-pathname c)))
9025e0d1 141
142(defmethod perform ((operation operation) (c library))
143 nil)
144
145(defmethod operation-done-p ((o load-op) (c library))
fbeb759c 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)))
9025e0d1 150
151(defmethod operation-done-p ((o operation) (c library))
152 t)