chiark / gitweb /
Modified to not use custom C functions to create GClosures
[clg] / tools / asdf-extensions.lisp
CommitLineData
77dd2192 1(in-package :asdf)
2
1a9c1e08 3(export 'load-dso)
4
77dd2192 5(defun concatenate-strings (strings &optional delimiter)
6 (if (not (rest strings))
7 (first strings)
8 (concatenate
9 'string
10 (first strings)
11 (if delimiter (string delimiter) "")
12 (concatenate-strings (rest strings) delimiter))))
13
14;;; The following code is more or less copied frm sb-bsd-sockets.asd,
73572c12 15;;; but extended to allow flags to be set in a general way
77dd2192 16
17(defclass unix-dso (module) ())
18(defun unix-name (pathname)
19 (namestring
20 (typecase pathname
21 (logical-pathname (translate-logical-pathname pathname))
22 (t pathname))))
23
1a9c1e08 24(defmethod input-files ((operation compile-op) (dso unix-dso))
77dd2192 25 (mapcar #'component-pathname (module-components dso)))
26
27(defmethod output-files ((operation compile-op) (dso unix-dso))
28 (let ((dir (component-pathname dso)))
29 (list
30 (make-pathname :type "so"
31 :name (car (last (pathname-directory dir)))
32 :directory (butlast (pathname-directory dir))
33 :defaults dir))))
34
35
36(defmethod perform :after ((operation compile-op) (dso unix-dso))
37 (let ((dso-name (unix-name (car (output-files operation dso)))))
38 (unless (zerop
39 (run-shell-command
40 "gcc ~A -o ~S ~{~S ~}"
41 (concatenate 'string
42;; (sb-ext:posix-getenv "EXTRA_LDFLAGS")
43;; " "
44 #+sunos "-shared -lresolv -lsocket -lnsl"
45 #+darwin "-bundle"
46 #-(or darwin sunos) "-shared")
47 dso-name
48 (mapcar #'unix-name
49 (mapcan (lambda (c)
50 (output-files operation c))
51 (module-components dso)))))
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
86 (= 0 (run-shell-command "gcc ~A -o ~S -c ~S"
87 (concatenate-strings
88 (append
89 (list "-fPIC")
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 #\sp)
100 (unix-name (car (output-files op c)))
101 (unix-name (component-pathname c))))
102 (error 'operation-error :operation op :component c)))
103
104
105(defmethod perform ((operation load-op) (c c-source-file))
106 t)
107
108
fd9d29a4 109;;; Shared libraries
1a9c1e08 110
fd9d29a4 111(defclass library (component)
1a9c1e08 112 ((libdir :initarg :libdir)))
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))
128 (make-pathname :type "so"
129 :name (component-name lib)
3e9e71e7 130 :directory (split-path (slot-value lib 'libdir))))
1a9c1e08 131
132(defmethod perform ((o load-op) (c library))
133 (load-dso (component-pathname c)))
fd9d29a4 134
135(defmethod perform ((operation operation) (c library))
136 nil)
137
138(defmethod operation-done-p ((o load-op) (c library))
139 #+sbcl(find (sb-ext::unix-namestring (component-pathname c)) sb-alien::*shared-objects* :key #'sb-alien::shared-object-file :test #'equal)
140 #+cmu(rassoc (unix::unix-namestring (component-pathname c))
141 system::*global-table*
142 :key #'(lambda (pathname)
143 (when pathname (unix::unix-namestring pathname)))
dfdb198f 144 :test #'equal)
145 #+clisp(find (component-pathname c) *loaded-libraries* :test #'equal))
fd9d29a4 146
147(defmethod operation-done-p ((o operation) (c library))
148 t)