1 (in-package #:sb-grovel)
3 (defvar *default-c-stream* nil)
5 (defun escape-for-string (string)
8 (defun split-cflags (string)
9 (remove-if (lambda (flag)
10 (zerop (length flag)))
12 for start = 0 then (if end (1+ end) nil)
13 for end = (and start (position #\Space string :start start))
15 collect (subseq string start end))))
17 (defun c-escape (string &optional (dangerous-chars '(#\")) (escape-char #\\))
18 "Escape DANGEROUS-CHARS in STRING, with ESCAPE-CHAR."
19 (declare (simple-string string))
20 (coerce (loop for c across string
21 if (member c dangerous-chars) collect escape-char
25 (defun as-c (&rest args)
26 "Pretty-print ARGS into the C source file, separated by #\Space"
27 (format *default-c-stream* "~A~{ ~A~}~%" (first args) (rest args)))
29 (defun printf (formatter &rest args)
30 "Emit C code to fprintf the quoted code, via FORMAT.
31 The first argument is the C string that should be passed to
34 The rest of the arguments are consumed by FORMAT clauses, until
35 there are no more FORMAT clauses to fill. If there are more
36 arguments, they are emitted as printf arguments.
38 There is no error checking done, unless you pass too few FORMAT
39 clause args. I recommend using this formatting convention in
42 (printf \"string ~A ~S %d %d\" format-arg-1 format-arg-2
43 printf-arg-1 printf-arg-2)"
44 (let ((*print-pretty* nil))
45 (apply #'format *default-c-stream*
46 " fprintf (out, \"~@?\\n\"~@{, ~A~});~%"
50 (defun c-for-enum (lispname elements export)
51 (printf "(cl:eval-when (:compile-toplevel :load-toplevel :execute) (sb-alien:define-alien-type ~A (sb-alien:enum nil" lispname)
52 (dolist (element elements)
53 (destructuring-bind (lisp-element-name c-element-name) element
54 (printf " (~S %d)" lisp-element-name c-element-name)))
57 (dolist (element elements)
58 (destructuring-bind (lisp-element-name c-element-name) element
59 (declare (ignore c-element-name))
60 (unless (keywordp lisp-element-name)
61 (printf "(export '~S)" lisp-element-name))))))
63 (defun c-for-structure (lispname cstruct)
64 (destructuring-bind (cname &rest elements) cstruct
65 (printf "(cl:eval-when (:compile-toplevel :load-toplevel :execute) (sb-grovel::define-c-struct ~A %d" lispname
66 (format nil "sizeof(~A)" cname))
68 (destructuring-bind (lisp-type lisp-el-name c-type c-el-name &key distrust-length) e
69 (printf " (~A ~A \"~A\"" lisp-el-name lisp-type c-type)
73 (format nil "((unsigned long)&(t.~A)) - ((unsigned long)&(t))" c-el-name))
81 (format nil "sizeof(t.~A)" c-el-name))
85 (defun print-c-source (stream headers definitions package-name)
86 (declare (ignorable definitions package-name))
87 (let ((*default-c-stream* stream)
88 (*print-right-margin* nil))
89 (loop for i in (cons "stdio.h" headers)
90 do (format stream "#include <~A>~%" i))
91 (as-c "#define SIGNEDP(x) (((x)-1)<0)")
92 (as-c "#define SIGNED_(x) (SIGNEDP(x)?\"\":\"un\")")
93 (as-c "int main(int argc, char *argv[]) {")
95 (as-c " if (argc != 2) {")
96 (as-c " printf(\"Invalid argcount!\");")
99 (as-c " out = fopen(argv[1], \"w\");")
100 (as-c " if (!out) {")
101 (as-c " printf(\"Error opening output file!\");")
104 (printf "(cl:in-package #:~A)" package-name)
105 (printf "(cl:eval-when (:compile-toplevel)")
106 (printf " (cl:defparameter *integer-sizes* (cl:make-hash-table))")
107 (dolist (type '("char" "short" "long" "int"
108 #+nil"long long" ; TODO: doesn't exist in sb-alien yet
110 (printf " (cl:setf (cl:gethash %d *integer-sizes*) 'sb-alien:~A)" (substitute #\- #\Space type)
111 (format nil "sizeof(~A)" type)))
113 (dolist (def definitions)
114 (destructuring-bind (type lispname cname &optional doc export) def
117 (as-c "#ifdef" cname)
118 (printf "(cl:defconstant ~A %d \"~A\")" lispname doc
120 (when (eql type :errno)
121 (printf "(cl:setf (get '~A 'errno) t)" lispname))
123 (printf "(sb-int:style-warn \"Couldn't grovel for ~A (unknown to the C compiler).\")" cname)
126 (c-for-enum lispname cname export))
128 (printf "(cl:eval-when (:compile-toplevel :load-toplevel :execute) (sb-alien:define-alien-type ~A (sb-alien:%ssigned %d)))" lispname
129 (format nil "SIGNED_(~A)" cname)
130 (format nil "(8*sizeof(~A))" cname)))
132 (printf "(cl:defparameter ~A %s \"~A\"" lispname doc
135 (printf "(cl:declaim (cl:inline ~A))" lispname)
136 (destructuring-bind (f-cname &rest definition) cname
137 (printf "(sb-grovel::define-foreign-routine (\"~A\" ~A)" f-cname lispname)
138 (printf "~{ ~W~^\\n~})" definition)))
140 (c-for-structure lispname cname))
142 ;; should we really not sprechen espagnol, monsieurs?
143 (error "Unknown grovel keyword encountered: ~A" type)))
145 (printf "(cl:export '~A)" lispname))))
149 (defun c-constants-extract (filename output-file package)
150 (with-open-file (f output-file :direction :output :if-exists :supersede)
151 (with-open-file (i filename :direction :input)
152 (let* ((headers (read i))
153 (definitions (read i)))
154 (print-c-source f headers definitions package)))))
156 (defclass grovel-constants-file (asdf:cl-source-file)
157 ((package :accessor constants-package :initarg :package)
158 (do-not-grovel :accessor do-not-grovel
160 :initarg :do-not-grovel)))
162 (define-condition c-compile-failed (compile-failed) ()
163 (:report (lambda (c s)
164 (format s "~@<C compiler failed when performing ~A on ~A.~@:>"
165 (error-operation c) (error-component c)))))
166 (define-condition a-dot-out-failed (compile-failed) ()
167 (:report (lambda (c s)
168 (format s "~@<a.out failed when performing ~A on ~A.~@:>"
169 (error-operation c) (error-component c)))))
171 (defmethod asdf:perform ((op asdf:compile-op)
172 (component grovel-constants-file))
173 ;; we want to generate all our temporary files in the fasl directory
174 ;; because that's where we have write permission. Can't use /tmp;
175 ;; it's insecure (these files will later be owned by root)
176 (let* ((output-file (car (output-files op component)))
177 (filename (component-pathname component))
179 (if (typep output-file 'logical-pathname)
180 (translate-logical-pathname output-file)
181 (pathname output-file)))
182 (tmp-c-source (merge-pathnames #p"foo.c" real-output-file))
183 (tmp-a-dot-out (merge-pathnames #-win32 #p"a.out" #+win32 #p"a.exe"
185 (tmp-constants (merge-pathnames #p"constants.lisp-temp"
187 (princ (list filename output-file real-output-file
188 tmp-c-source tmp-a-dot-out tmp-constants))
190 (funcall (intern "C-CONSTANTS-EXTRACT" (find-package "SB-GROVEL"))
191 filename tmp-c-source (constants-package component))
192 (unless (do-not-grovel component)
193 (let* ((cc (or (and (string/= (sb-ext:posix-getenv "CC") "")
194 (sb-ext:posix-getenv "CC"))
195 ;; It might be nice to include a CONTINUE or
196 ;; USE-VALUE restart here, but ASDF seems to insist
197 ;; on handling the errors itself.
198 (error "The CC environment variable has not been set in SB-GROVEL. Since this variable should always be set during the SBCL build process, this might indicate an SBCL with a broken contrib installation.")))
199 (code (sb-ext:process-exit-code
203 (split-cflags (sb-ext:posix-getenv "EXTRA_CFLAGS"))
204 #+(and linux largefile)
205 '("-D_LARGEFILE_SOURCE"
206 "-D_LARGEFILE64_SOURCE"
207 "-D_FILE_OFFSET_BITS=64")
208 #+(and x86-64 darwin)
212 #+(and x86-64 sunos) '("-m64")
214 (namestring tmp-a-dot-out)
215 (namestring tmp-c-source)))
218 :output *trace-output*))))
220 (case (operation-on-failure op)
221 (:warn (warn "~@<C compiler failure when performing ~A on ~A.~@:>"
224 (error 'c-compile-failed :operation op :component component)))))
225 (let ((code (sb-ext:process-exit-code
226 (sb-ext:run-program (namestring tmp-a-dot-out)
227 (list (namestring tmp-constants))
230 :output *trace-output*))))
232 (case (operation-on-failure op)
233 (:warn (warn "~@<a.out failure when performing ~A on ~A.~@:>"
236 (error 'a-dot-out-failed :operation op :component component))))))
237 (multiple-value-bind (output warnings-p failure-p)
238 (compile-file tmp-constants :output-file output-file)
240 (case (operation-on-warnings op)
242 (formatter "~@<COMPILE-FILE warned while ~
243 performing ~A on ~A.~@:>")
245 (:error (error 'compile-warned :component component :operation op))
248 (case (operation-on-failure op)
250 (formatter "~@<COMPILE-FILE failed while ~
251 performing ~A on ~A.~@:>")
253 (:error (error 'compile-failed :component component :operation op))
256 (error 'compile-error :component component :operation op)))))