chiark / gitweb /
Small fix necessary for win32
[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
77dd2192 9;;; The following code is more or less copied frm sb-bsd-sockets.asd,
73572c12 10;;; but extended to allow flags to be set in a general way
77dd2192 11
bcd6b6af 12(defclass unix-dso (module)
13 ((ldflags :initform nil :initarg :ldflags)))
14
77dd2192 15(defun unix-name (pathname)
16 (namestring
17 (typecase pathname
18 (logical-pathname (translate-logical-pathname pathname))
19 (t pathname))))
20
1a9c1e08 21(defmethod input-files ((operation compile-op) (dso unix-dso))
77dd2192 22 (mapcar #'component-pathname (module-components dso)))
23
24(defmethod output-files ((operation compile-op) (dso unix-dso))
25 (let ((dir (component-pathname dso)))
26 (list
34abe734 27 (make-pathname :type *dso-extension*
77dd2192 28 :name (car (last (pathname-directory dir)))
29 :directory (butlast (pathname-directory dir))
30 :defaults dir))))
31
77dd2192 32(defmethod perform :after ((operation compile-op) (dso unix-dso))
a4263d6d 33 (let ((output (first (output-files operation dso)))
34 (inputs (mapcar #'unix-name
35 (mapcan #'(lambda (c)
36 (output-files operation c))
37 (module-components dso)))))
77dd2192 38 (unless (zerop
e6ab30c2 39 (run-shell-command "gcc ~A -o ~S ~{~S~^ ~} ~{~A~^ ~}"
40 #-(or darwin win32)"-shared"
a4263d6d 41 #+darwin "-bundle"
42 #+win32
43 (format nil "-shared -Wl,--out-implib,~S"
44 (unix-name
45 (make-pathname
46 :type "a"
47 :name (format nil "lib~Adll" (pathname-name output))
48 :defaults output)))
a4263d6d 49 (unix-name output)
e6ab30c2 50 inputs
51 (slot-value dso 'ldflags)))
77dd2192 52 (error 'operation-error :operation operation :component dso))))
53
dfdb198f 54#+clisp
55(defvar *loaded-libraries* ())
73572c12 56
57(defun load-dso (filename)
58 #+sbcl(sb-alien:load-shared-object filename)
dfdb198f 59 #+cmu(ext:load-foreign filename)
60 #+clisp
61 (unless (find filename *loaded-libraries* :test #'equal)
62 (ffi::foreign-library (namestring filename))
63 (push filename *loaded-libraries*)))
1a9c1e08 64
65
77dd2192 66(defmethod perform ((o load-op) (c unix-dso))
67 (let ((co (make-instance 'compile-op)))
68 (let ((filename (car (output-files co c))))
1a9c1e08 69 (load-dso filename))))
77dd2192 70
71
72
73(defclass c-source-file (source-file)
74 ((cflags :initform nil :initarg :cflags)
75 (optimization :initform 2 :initarg :optimization)
76 (definitions :initform nil :initarg :definitions)
77 (include-paths :initform nil :initarg :include-paths)))
78
79
80(defmethod output-files ((op compile-op) (c c-source-file))
73572c12 81 (list (make-pathname :type "o" :defaults (component-pathname c))))
77dd2192 82
83
84(defmethod perform ((op compile-op) (c c-source-file))
85 (unless
a4263d6d 86 (= 0 (run-shell-command "gcc ~A~{ ~A~} -o ~S -c ~S"
87 #-win32 "-fPIC"
88 #+win32 "-DBUILD_DLL"
89 (nconc
90 (when (slot-value c 'optimization)
91 (list (format nil "-O~A" (slot-value c 'optimization))))
92 (loop
93 for symbol in (slot-value c 'definitions)
94 collect (format nil "-D~A" symbol))
95 (loop
96 for path in (slot-value c 'include-paths)
97 collect (format nil "-I~A" path))
98 (slot-value c 'cflags))
99 (unix-name (first (output-files op c)))
77dd2192 100 (unix-name (component-pathname c))))
101 (error 'operation-error :operation op :component c)))
102
103
104(defmethod perform ((operation load-op) (c c-source-file))
105 t)
106
107
fd9d29a4 108;;; Shared libraries
1a9c1e08 109
fd9d29a4 110(defclass library (component)
1dfea3ab 111 ((libdir :initarg :libdir)
112 (libname :initarg :libname :initform nil)))
1a9c1e08 113
114
3e9e71e7 115(defun split-path (path)
116 (labels ((split (path)
117 (unless (zerop (length path))
118 (let ((slash (position #\/ path)))
119 (if slash
120 (cons (subseq path 0 slash) (split (subseq path (1+ slash))))
121 (list path))))))
122 (if (and (not (zerop (length path))) (char= (char path 0) #\/))
123 (cons :absolute (split (subseq path 1)))
124 (cons :relative (split path)))))
125
1a9c1e08 126
127(defmethod component-pathname ((lib library))
34abe734 128 (make-pathname :type *dso-extension*
1dfea3ab 129 :name (or (slot-value lib 'libname) (component-name lib))
3e9e71e7 130 :directory (split-path (slot-value lib 'libdir))))
1a9c1e08 131
a4263d6d 132;; --fix: is UNIX-NAME really necessary for win32? i know it will bomb
133;; without using it while doing (ASDF:OOS 'ASDF:LOAD-OP :GLIB) but
134;; loading the complete pathname for libglib-2.0-0.dll with
135;; SB-ALIEN:LOAD-SHARED-OBJECT by hand won't explode. weird.
136;; - cph 18-May-2007
1a9c1e08 137(defmethod perform ((o load-op) (c library))
a4263d6d 138 (load-dso #-win32 (component-pathname c)
139 #+win32 (unix-name (component-pathname c))))
fd9d29a4 140
141(defmethod perform ((operation operation) (c library))
142 nil)
143
144(defmethod operation-done-p ((o load-op) (c library))
145 #+sbcl(find (sb-ext::unix-namestring (component-pathname c)) sb-alien::*shared-objects* :key #'sb-alien::shared-object-file :test #'equal)
146 #+cmu(rassoc (unix::unix-namestring (component-pathname c))
147 system::*global-table*
148 :key #'(lambda (pathname)
149 (when pathname (unix::unix-namestring pathname)))
dfdb198f 150 :test #'equal)
151 #+clisp(find (component-pathname c) *loaded-libraries* :test #'equal))
fd9d29a4 152
153(defmethod operation-done-p ((o operation) (c library))
154 t)