manual: add Clasp to "Implementation Support"
[cffi.git] / toolchain / c-toolchain.lisp
blob21b3ea3e4b84d566eb249526652faf74ebd85f26
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; c-toolchain.lisp --- Generic support compiling and linking C code.
4 ;;;
5 ;;; Copyright (C) 2005-2006, Dan Knap <dankna@accela.net>
6 ;;; Copyright (C) 2005-2006, Emily Backes <lucca@accela.net>
7 ;;; Copyright (C) 2007, Stelian Ionescu <sionescu@cddr.org>
8 ;;; Copyright (C) 2007, Luis Oliveira <loliveira@common-lisp.net>
9 ;;;
10 ;;; Permission is hereby granted, free of charge, to any person
11 ;;; obtaining a copy of this software and associated documentation
12 ;;; files (the "Software"), to deal in the Software without
13 ;;; restriction, including without limitation the rights to use, copy,
14 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
15 ;;; of the Software, and to permit persons to whom the Software is
16 ;;; furnished to do so, subject to the following conditions:
17 ;;;
18 ;;; The above copyright notice and this permission notice shall be
19 ;;; included in all copies or substantial portions of the Software.
20 ;;;
21 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 ;;; DEALINGS IN THE SOFTWARE.
29 ;;;
31 (in-package #:cffi-toolchain)
33 ;;; Utils
35 (defun parse-command-flags (flags)
36 (let ((separators '(#\Space #\Tab #\Newline #\Return)))
37 (remove-if 'emptyp (split-string flags :separator separators))))
39 (defun parse-command-flags-list (strings)
40 (loop for flags in strings append (parse-command-flags flags)))
42 (defun program-argument (x)
43 (etypecase x
44 (string x)
45 (pathname (native-namestring x))))
47 (defun invoke (command &rest args)
48 (when (pathnamep command)
49 (setf command (native-namestring command))
50 #+os-unix
51 (unless (absolute-pathname-p command)
52 (setf command (strcat "./" command))))
53 (let ((cmd (cons command (mapcar 'program-argument args))))
54 (safe-format! *debug-io* "; ~A~%" (escape-command cmd))
55 (run-program cmd :output :interactive :error-output :interactive)))
58 ;;; C support
60 (defparameter *cc* nil "C compiler")
61 (defparameter *cc-flags* nil "flags for the C compiler")
62 (defparameter *ld* nil "object linker") ;; NB: can actually be the same as *cc*
63 (defparameter *ld-exe-flags* nil "flags for linking executables via *ld*")
64 (defparameter *ld-dll-flags* nil "flags for linking shared library via *ld*")
65 (defparameter *linkkit-start* nil "flags for the implementation linkkit, start")
66 (defparameter *linkkit-end* nil "flags for the implementation linkkit, end")
68 (defun link-all-library (lib)
69 ;; Flags to provide to cc to link a whole library into an executable
70 (when lib
71 (if (featurep :darwin) ;; actually, LLVM ld vs GNU ld
72 `("-Wl,-force_load" ,lib)
73 `("-Wl,--whole-archive" ,lib "-Wl,--no-whole-archive"))))
75 (defun normalize-flags (directory flags)
76 (loop for val in (parse-command-flags flags) collect
77 (cond
78 ((find (first-char val) "-+/") val)
79 ((probe-file* (subpathname directory val)))
80 (t val))))
82 (defun implementation-file (file &optional type)
83 (subpathname (lisp-implementation-directory) file
84 :type (bundle-pathname-type type)))
86 ;; TODO: on CCL, extract data from
87 ;; (pathname (strcat "ccl:lisp-kernel/" (ccl::kernel-build-directory) "/Makefile")) ?
89 #+clisp
90 (progn
91 (defparameter *clisp-toolchain-parameters*
92 '(("CC" *cc*)
93 ("CFLAGS" *cc-flags* t)
94 ("CLFLAGS" *cc-exe-flags* t)
95 ("LIBS" *linkkit-start* t)
96 ("X_LIBS" *linkkit-end* t)))
97 (defun clisp-toolchain-parameters (&optional linkset)
98 (nest
99 (let* ((linkset (ensure-pathname
100 (or linkset "base")
101 :defaults (lisp-implementation-directory)
102 :ensure-absolute t
103 :ensure-directory t
104 :want-existing t))
105 (makevars (subpathname linkset "makevars"))))
106 (with-input-file (params makevars :if-does-not-exist nil))
107 (when params)
108 (loop for l = (read-line params nil nil) while l
109 finally (appendf *linkkit-start* (normalize-flags linkset "modules.o")) do)
110 (if-let (p (position #\= l)))
111 (let ((var (subseq l 0 p))
112 ;; strip the start and end quote characters
113 (val (subseq l (+ p 2) (- (length l) 1)))))
114 (if-let (param (cdr (assoc var *clisp-toolchain-parameters* :test 'equal))))
115 (destructuring-bind (sym &optional normalizep) param
116 (setf (symbol-value sym)
117 (if normalizep (normalize-flags linkset val) val))))
118 (setf *ld* *cc*
119 *ld-exe-flags* `(,@*cc-flags* #-darwin "-Wl,--export-dynamic")
120 *ld-dll-flags* (list* #+darwin "-dynamiclib" ;; -bundle ?
121 #-darwin "-shared"
122 *cc-flags*))))
124 ;; TODO: for CMUCL, see whatever uses its linker.sh,
125 ;; and teach it to accept additional objects / libraries
126 ;; as it links a runtime plus a core into an executable
128 #+ecl
129 (defun ecl-toolchain-parameters ()
130 (setf *cc* c:*cc*
131 *cc-flags* `(,@(parse-command-flags c::*cc-flags*)
132 ,@(parse-command-flags c:*user-cc-flags*))
133 ;; For the below, we just use c::build-FOO
134 *ld* *cc*
135 *ld-exe-flags* *cc-flags*
136 *ld-dll-flags* *cc-flags*
137 *linkkit-start* nil
138 *linkkit-end* nil))
140 #+mkcl
141 (defun mkcl-toolchain-parameters ()
142 (setf *cc* compiler::*cc*
143 *cc-flags* (parse-command-flags compiler::*cc-flags*)
144 ;; For the below, we just use compiler::build-FOO
145 *ld* *cc*
146 *ld-exe-flags* *cc-flags*
147 *ld-dll-flags* *cc-flags*
148 *linkkit-start* nil
149 *linkkit-end* nil))
151 #+sbcl
152 (progn
153 (defparameter *sbcl-toolchain-parameters*
154 '(("CC" *cc*)
155 ("CFLAGS" *cc-flags* t)
156 ("LINKFLAGS" *ld-exe-flags* t)
157 ("USE_LIBSBCL" *linkkit-start* t)
158 ("LIBS" *linkkit-end* t)))
159 (defun sbcl-toolchain-parameters ()
160 (nest
161 (let* ((sbcl-home (lisp-implementation-directory))
162 (sbcl.mk (subpathname sbcl-home "sbcl.mk"))))
163 (with-input-file (params sbcl.mk :if-does-not-exist nil))
164 (when params)
165 (loop for l = (read-line params nil nil) while l
166 finally (appendf *linkkit-end* '("-lm")) do)
167 (if-let (p (position #\= l)))
168 (let ((var (subseq l 0 p))
169 (val (subseq l (1+ p)))))
170 (if-let (param (cdr (assoc var *sbcl-toolchain-parameters* :test 'equal))))
171 (destructuring-bind (sym &optional normalizep) param
172 (setf (symbol-value sym)
173 (if normalizep (normalize-flags sbcl-home val) val))))
174 (unless (featurep :sb-linkable-runtime)
175 (setf *linkkit-start* nil *linkkit-end* nil))
176 (setf *ld* *cc* ;; !
177 *ld-dll-flags* (list* #+darwin "-dynamiclib" #-darwin "-shared"
178 *cc-flags*))))
180 (defun default-toolchain-parameters ()
181 ;; The values below are legacy guesses from previous versions of CFFI.
182 ;; It would be nice to clean them up, remove unneeded guesses,
183 ;; annotate every guess with some comment explaining the context.
184 ;; TODO: have proper implementation-provided linkkit parameters
185 ;; for all implementations as above, and delete the below altogether.
186 (let ((arch-flags
187 ;; Former *cpu-word-size-flags*
188 #+arm '("-marm")
189 #+arm64 '()
190 #-(or arm arm64)
191 (ecase (cffi:foreign-type-size :pointer)
192 (4 '("-m32"))
193 (8 '("-m64")))))
194 (setf *cc*
195 (or (getenvp "CC")
196 #+(or cygwin (not windows)) "cc"
197 "gcc")
198 *cc-flags*
199 (or (getenv "CFLAGS")
200 (append
201 arch-flags
202 ;; For MacPorts
203 #+darwin (list "-I" "/opt/local/include/")
204 ;; ECL internal flags
205 #+ecl (parse-command-flags c::*cc-flags*)
206 ;; FreeBSD non-base header files
207 #+freebsd (list "-I" "/usr/local/include/")))
208 *ld* *cc*
209 *ld-exe-flags* `(,@arch-flags #-darwin "-Wl,--export-dynamic")
210 *ld-dll-flags* (list* #+darwin "-dynamiclib" ;; -bundle ?
211 #-darwin "-shared"
212 *cc-flags*)
213 *linkkit-start* nil
214 *linkkit-end* nil)))
216 (defun ensure-toolchain-parameters ()
217 #+clisp (unless *cc* (clisp-toolchain-parameters))
218 #+ecl (unless *cc* (ecl-toolchain-parameters))
219 #+mkcl (unless *cc* (mkcl-toolchain-parameters))
220 #+sbcl (unless *cc* (sbcl-toolchain-parameters))
221 (unless *cc* (default-toolchain-parameters)))
223 ;; Actually initialize toolchain parameters
224 (ignore-errors (ensure-toolchain-parameters))
227 (defun call-with-temporary-output (output-file fun)
228 (let ((output-file (ensure-pathname output-file :want-file t :ensure-absolute t :truenamize t)))
229 (with-temporary-file
230 (:pathname tmp :direction :output
231 :prefix (strcat (native-namestring (pathname-directory-pathname output-file))
232 (pathname-name output-file) "-tmp")
233 :suffix ""
234 :type (pathname-type output-file))
235 (funcall fun tmp)
236 (rename-file-overwriting-target tmp output-file))))
238 (defmacro with-temporary-output ((output-file-var &optional (output-file-val output-file-var))
239 &body body)
240 "Create an output file atomically, by executing the BODY while OUTPUT-FILE-VAR
241 is bound to a temporary file name, then atomically renaming that temporary file to OUTPUT-FILE-VAL."
242 `(call-with-temporary-output ,output-file-val (lambda (,output-file-var) ,@body)))
244 (defun invoke-builder (builder output-file &rest args)
245 "Invoke the C Compiler with given OUTPUT-FILE and arguments ARGS"
246 (with-temporary-output (output-file)
247 (apply 'invoke `(,@builder ,output-file ,@args))))
249 (defun cc-compile (output-file inputs)
250 (apply 'invoke-builder (list *cc* "-o") output-file
251 "-c" (append *cc-flags* #-windows '("-fPIC") inputs)))
253 (defun link-executable (output-file inputs)
254 (apply 'invoke-builder (list *ld* "-o") output-file
255 (append *ld-exe-flags* inputs)))
257 (defun link-lisp-executable (output-file inputs)
258 #+ecl
259 (let ((c::*ld-flags*
260 (format nil "-Wl,--export-dynamic ~@[ ~A~]"
261 c::*ld-flags*)))
262 (c::build-program output-file :lisp-files inputs))
263 #+mkcl (compiler::build-program
264 output-file :lisp-object-files (mapcar 'program-argument inputs)
265 :on-missing-lisp-object-initializer nil)
266 #-(or ecl mkcl)
267 (link-executable output-file `(,@*linkkit-start* ,@inputs ,@*linkkit-end*)))
269 (defun link-static-library (output-file inputs)
270 #+ecl (c::build-static-library output-file :lisp-files inputs)
271 #+mkcl (compiler::build-static-library
272 output-file :lisp-object-files (mapcar 'program-argument inputs)
273 :on-missing-lisp-object-initializer nil)
274 #-(or ecl mkcl)
275 (with-temporary-output (output-file)
276 (delete-file-if-exists output-file)
277 #+(or bsd linux windows)
278 (apply 'invoke
279 `(;; TODO: make it portable to BSD.
280 ;; ar D is also on FreeBSD, but not on OpenBSD or Darwin, dunno about NetBSD;
281 ;; ar T seems to only be on Linux (means something different on Darwin). Sigh.
282 ;; A MRI script might be more portable... not, only supported by GNU binutils.
283 ;; I couldn't get libtool to work, and it's not ubiquitous anyway.
284 ;; ,@`("libtool" "--mode=link" ,*cc* ,@*cc-flags* "-static" "-o" ,output-file)
285 ;; "Solution": never link .a's into further .a's, only link .o's into .a's,
286 ;; which implied changes that are now the case in ASDF 3.2.0.
287 #+bsd ,@`("ar" "rcs" ,output-file) ;; NB: includes darwin
288 #+linux ,@`("ar" "rcsDT" ,output-file)
289 #+windows ,@`("lib" "-nologo" ,(strcat "-out:" (native-namestring output-file)))
290 ,@inputs))
291 #-(or bsd linux windows)
292 (error "Not implemented on your system")))
294 (defun link-shared-library (output-file inputs)
295 ;; remove the library so we won't possibly be overwriting
296 ;; the code of any existing process
297 (delete-file-if-exists output-file)
298 #+ecl (c::build-shared-library output-file :lisp-files inputs)
299 #+mkcl (compiler::build-shared-library
300 output-file :lisp-object-files (mapcar 'program-argument inputs)
301 :on-missing-lisp-object-initializer nil)
302 #-(or ecl mkcl)
303 ;; Don't use a temporary file, because linking is sensitive to the output file name :-/ (or put it in a temporary directory?)
304 (apply 'invoke *ld* "-o" output-file
305 (append *ld-dll-flags* inputs)))
308 ;;; Computing file names
310 (defun make-c-file-name (output-defaults &optional suffix)
311 (make-pathname :type "c"
312 :name (strcat (pathname-name output-defaults) suffix)
313 :defaults output-defaults))
315 (defun make-o-file-name (output-defaults &optional suffix)
316 (make-pathname :type (bundle-pathname-type :object)
317 :name (format nil "~A~@[~A~]" (pathname-name output-defaults) suffix)
318 :defaults output-defaults))
320 (defun make-so-file-name (defaults)
321 (make-pathname :type (bundle-pathname-type :shared-library)
322 :defaults defaults))
324 (defun make-exe-file-name (defaults)
325 (make-pathname :type (bundle-pathname-type :program)
326 :defaults defaults))
329 ;;; Implement link-op on image-based platforms.
330 #-(or clasp ecl mkcl)
331 (defmethod perform ((o link-op) (c system))
332 (let* ((inputs (input-files o c))
333 (output (first (output-files o c)))
334 (kind (bundle-type o)))
335 (when output ;; some operations skip any output when there is no input
336 (ecase kind
337 (:program (link-executable output inputs))
338 ((:lib :static-library) (link-static-library output inputs))
339 ((:dll :shared-library) (link-shared-library output inputs))))))
341 (defclass c-file (source-file)
342 ((cflags :initarg :cflags :initform :default)
343 (type :initform "c")))
345 (defmethod output-files ((o compile-op) (c c-file))
346 (let* ((i (first (input-files o c)))
347 (base (format nil "~(~{~a~^__~}~)"
348 (mapcar (lambda (x) (substitute-if #\_ (complement #'alphanumericp) x))
349 (component-find-path c))))
350 (path (make-pathname :defaults i :name base)))
351 (list (make-o-file-name path)
352 (make-so-file-name path))))
354 (defmethod perform ((o compile-op) (c c-file))
355 (let ((i (first (input-files o c))))
356 (destructuring-bind (.o .so) (output-files o c)
357 (cc-compile .o (list i))
358 (link-shared-library .so (list .o)))))
360 (defmethod perform ((o load-op) (c c-file))
361 (let ((o (second (input-files o c))))
362 (cffi:load-foreign-library (file-namestring o) :search-path (list (pathname-directory-pathname o)))))
364 (setf (find-class 'asdf::c-file) (find-class 'c-file))
366 (defclass o-file (source-file)
367 ((cflags :initarg :cflags :initform :default)
368 (type :initform (bundle-pathname-type :object)))
369 (:documentation "class for pre-compile object components"))
371 (defmethod output-files ((op compile-op) (c o-file))
372 (let* ((o (first (input-files op c)))
373 (so (apply-output-translations (make-so-file-name o))))
374 (values (list o so) t)))
376 (defmethod perform ((o load-op) (c o-file))
377 (let ((so (second (input-files o c))))
378 (cffi:load-foreign-library (file-namestring so) :search-path (list (pathname-directory-pathname so)))))
380 (setf (find-class 'asdf::o-file) (find-class 'o-file))