ab15c5d9 |
1 | (in-package :asdf) |
2 | |
9f47de56 |
3 | (export '(*dso-extension* *operation* *system* *component*)) |
65d26e3e |
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 |
824e0c2e |
11 | ;;; has been renamed from unix-dso to shared-object as this code is no |
12 | ;;; longer specific to unix |
ab15c5d9 |
13 | |
fbeb759c |
14 | (defclass shared-object (module) |
824e0c2e |
15 | ((ldflags :initform nil :initarg :ldflags) |
16 | (absolute :initform nil :initarg :absolute :reader absolute-p))) |
fbeb759c |
17 | |
18 | (defun ensure-namestring (pathname) |
ab15c5d9 |
19 | (namestring |
20 | (typecase pathname |
21 | (logical-pathname (translate-logical-pathname pathname)) |
22 | (t pathname)))) |
23 | |
fbeb759c |
24 | (defmethod input-files ((operation compile-op) (dso shared-object)) |
ab15c5d9 |
25 | (mapcar #'component-pathname (module-components dso))) |
26 | |
fbeb759c |
27 | (defmethod output-files ((operation compile-op) (dso shared-object)) |
ab15c5d9 |
28 | (let ((dir (component-pathname dso))) |
29 | (list |
65d26e3e |
30 | (make-pathname :type *dso-extension* |
9bc0a84e |
31 | :name (component-name dso) |
ab15c5d9 |
32 | :directory (butlast (pathname-directory dir)) |
33 | :defaults dir)))) |
34 | |
fbeb759c |
35 | (defmethod perform :after ((operation compile-op) (dso shared-object)) |
3f246c3a |
36 | (let ((output (first (output-files operation dso))) |
fbeb759c |
37 | (inputs (mapcar #'ensure-namestring |
3f246c3a |
38 | (mapcan #'(lambda (c) |
39 | (output-files operation c)) |
40 | (module-components dso))))) |
ab15c5d9 |
41 | (unless (zerop |
0eed49b9 |
42 | (run-shell-command "gcc ~A -o ~S ~{~S~^ ~} ~{~A~^ ~}" |
43 | #-(or darwin win32)"-shared" |
3f246c3a |
44 | #+darwin "-bundle" |
45 | #+win32 |
46 | (format nil "-shared -Wl,--out-implib,~S" |
fbeb759c |
47 | (ensure-namestring |
3f246c3a |
48 | (make-pathname |
49 | :type "a" |
50 | :name (format nil "lib~Adll" (pathname-name output)) |
51 | :defaults output))) |
fbeb759c |
52 | (ensure-namestring output) |
0eed49b9 |
53 | inputs |
54 | (slot-value dso 'ldflags))) |
ab15c5d9 |
55 | (error 'operation-error :operation operation :component dso)))) |
56 | |
5eda2e76 |
57 | #+clisp |
58 | (defvar *loaded-libraries* ()) |
3d36c5d6 |
59 | |
824e0c2e |
60 | (defun load-shared-object (pathname &optional (absolute-p t)) |
61 | (let* ((namestring (ensure-namestring pathname)) |
62 | (directory (namestring (pathname-sans-name+type namestring))) |
63 | (name+type (subseq namestring (length directory)))) |
64 | #+sbcl |
65 | (progn |
66 | (sb-alien:load-shared-object namestring) |
67 | (unless absolute-p |
68 | (let ((shared-object (find namestring sb-alien::*shared-objects* |
69 | :key #'sb-alien::shared-object-file |
70 | :test #'equal))) |
71 | (setf (sb-alien::shared-object-file shared-object) name+type)))) |
72 | #+cmu |
73 | (progn |
74 | (ext:load-foreign namestring) |
75 | (unless absolute-p |
76 | (let ((shared-object (rassoc namestring system::*global-table* |
77 | :test #'equal))) |
78 | (setf (cdr shared-object) name+type)))) |
79 | #+clisp |
80 | (progn |
fbeb759c |
81 | (ffi::foreign-library namestring) |
824e0c2e |
82 | (pushnew |
83 | (if absolute-p namestring name+type) |
84 | *loaded-libraries* :test #'string=)))) |
81814a23 |
85 | |
86 | |
824e0c2e |
87 | (defmethod perform ((o load-op) (dso shared-object)) |
ab15c5d9 |
88 | (let ((co (make-instance 'compile-op))) |
824e0c2e |
89 | (let ((pathname (car (output-files co dso)))) |
90 | (load-shared-object pathname (absolute-p dso))))) |
ab15c5d9 |
91 | |
92 | |
93 | |
94 | (defclass c-source-file (source-file) |
95 | ((cflags :initform nil :initarg :cflags) |
96 | (optimization :initform 2 :initarg :optimization) |
97 | (definitions :initform nil :initarg :definitions) |
98 | (include-paths :initform nil :initarg :include-paths))) |
99 | |
100 | |
101 | (defmethod output-files ((op compile-op) (c c-source-file)) |
3d36c5d6 |
102 | (list (make-pathname :type "o" :defaults (component-pathname c)))) |
ab15c5d9 |
103 | |
104 | |
105 | (defmethod perform ((op compile-op) (c c-source-file)) |
106 | (unless |
3f246c3a |
107 | (= 0 (run-shell-command "gcc ~A~{ ~A~} -o ~S -c ~S" |
108 | #-win32 "-fPIC" |
109 | #+win32 "-DBUILD_DLL" |
110 | (nconc |
111 | (when (slot-value c 'optimization) |
112 | (list (format nil "-O~A" (slot-value c 'optimization)))) |
113 | (loop |
114 | for symbol in (slot-value c 'definitions) |
115 | collect (format nil "-D~A" symbol)) |
116 | (loop |
117 | for path in (slot-value c 'include-paths) |
118 | collect (format nil "-I~A" path)) |
119 | (slot-value c 'cflags)) |
fbeb759c |
120 | (ensure-namestring (first (output-files op c))) |
121 | (ensure-namestring (component-pathname c)))) |
ab15c5d9 |
122 | (error 'operation-error :operation op :component c))) |
123 | |
124 | |
125 | (defmethod perform ((operation load-op) (c c-source-file)) |
126 | t) |
127 | |
128 | |
9025e0d1 |
129 | ;;; Shared libraries |
81814a23 |
130 | |
9025e0d1 |
131 | (defclass library (component) |
fbeb759c |
132 | ((libdir :initarg :libdir :initform nil) |
824e0c2e |
133 | (libname :initarg :libname :initform nil) |
134 | (absolute :initform nil :initarg :absolute :reader absolute-p))) |
81814a23 |
135 | |
136 | |
b008da5a |
137 | (defun split-path (path) |
138 | (labels ((split (path) |
139 | (unless (zerop (length path)) |
140 | (let ((slash (position #\/ path))) |
141 | (if slash |
142 | (cons (subseq path 0 slash) (split (subseq path (1+ slash)))) |
143 | (list path)))))) |
144 | (if (and (not (zerop (length path))) (char= (char path 0) #\/)) |
145 | (cons :absolute (split (subseq path 1))) |
146 | (cons :relative (split path))))) |
147 | |
81814a23 |
148 | |
149 | (defmethod component-pathname ((lib library)) |
65d26e3e |
150 | (make-pathname :type *dso-extension* |
d1237548 |
151 | :name (or (slot-value lib 'libname) (component-name lib)) |
b008da5a |
152 | :directory (split-path (slot-value lib 'libdir)))) |
81814a23 |
153 | |
824e0c2e |
154 | (defmethod perform ((o load-op) (lib library)) |
155 | (load-shared-object (component-pathname lib) (absolute-p lib))) |
9025e0d1 |
156 | |
824e0c2e |
157 | (defmethod perform ((operation operation) (lib library)) |
9025e0d1 |
158 | nil) |
159 | |
824e0c2e |
160 | (defmethod operation-done-p ((o load-op) (lib library)) |
161 | (let* ((namestring (ensure-namestring (component-pathname lib))) |
162 | (directory (namestring (pathname-sans-name+type namestring))) |
163 | (name+type (subseq namestring (length directory))) |
164 | (stored-name (if (absolute-p lib) namestring name+type))) |
165 | |
166 | #+sbcl(find stored-name sb-alien::*shared-objects* :key #'sb-alien::shared-object-file :test #'equal) |
167 | #+cmu(rassoc stored-name system::*global-table* :test #'equal) |
168 | #+clisp(find stored-name *loaded-libraries* :test #'equal))) |
9025e0d1 |
169 | |
824e0c2e |
170 | (defmethod operation-done-p ((o operation) (lib library)) |
9025e0d1 |
171 | t) |
9f47de56 |
172 | |
173 | |
174 | ;;; Binding of dynamic variables during perform |
175 | |
176 | (defvar *operation* nil) |
177 | (defvar *system* nil) |
178 | (defvar *component* nil) |
179 | |
180 | (defmethod perform :around ((operation operation) (c component)) |
181 | (let ((*operation* operation) |
182 | (*component* c) |
183 | (*system* (component-system c))) |
184 | (call-next-method))) |