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 (coerce (loop for c across string
20 if (member c dangerous-chars) collect escape-char
24 (defun as-c (&rest args)
25 "Pretty-print ARGS into the C source file, separated by #\Space"
26 (format *default-c-stream* "~A~{ ~A~}~%" (first args) (rest args)))
28 (defun printf (formatter &rest args)
29 "Emit C code to fprintf the quoted code, via FORMAT.
30 The first argument is the C string that should be passed to
33 The rest of the arguments are consumed by FORMAT clauses, until
34 there are no more FORMAT clauses to fill. If there are more
35 arguments, they are emitted as printf arguments.
37 There is no error checking done, unless you pass too few FORMAT
38 clause args. I recommend using this formatting convention in
41 (printf \"string ~A ~S %d %d\" format-arg-1 format-arg-2
42 printf-arg-1 printf-arg-2)"
43 (let ((*print-pretty* nil))
44 (apply #'format *default-c-stream*
45 " fprintf (out, \"~@?\\n\"~@{, ~A~});~%"
49 (defun c-for-enum (lispname elements export)
50 (printf "(cl:eval-when (:compile-toplevel :load-toplevel :execute) (sb-alien:define-alien-type ~A (sb-alien:enum nil" lispname)
51 (dolist (element elements)
52 (destructuring-bind (lisp-element-name c-element-name) element
53 (printf " (~S %d)" lisp-element-name c-element-name)))
56 (dolist (element elements)
57 (destructuring-bind (lisp-element-name c-element-name) element
58 (declare (ignore c-element-name))
59 (unless (keywordp lisp-element-name)
60 (printf "(export '~S)" lisp-element-name))))))
62 (defun c-for-structure (lispname cstruct)
63 (destructuring-bind (cname &rest elements) cstruct
64 (printf "(cl:eval-when (:compile-toplevel :load-toplevel :execute) (sb-grovel::define-c-struct ~A %d" lispname
65 (format nil "sizeof(~A)" cname))
67 (destructuring-bind (lisp-type lisp-el-name c-type c-el-name &key distrust-length) e
68 (printf " (~A ~A \"~A\"" lisp-el-name lisp-type c-type)
72 (format nil "((unsigned long)&(t.~A)) - ((unsigned long)&(t))" c-el-name))
80 (format nil "sizeof(t.~A)" c-el-name))
84 (defun print-c-source (stream headers definitions package-name)
85 (declare (ignorable definitions package-name))
86 (let ((*default-c-stream* stream)
87 (*print-right-margin* nil))
88 (loop for i in (cons "stdio.h" headers)
89 do (format stream "#include <~A>~%" i))
90 (as-c "#define SIGNEDP(x) (((x)-1)<0)")
91 (as-c "#define SIGNED_(x) (SIGNEDP(x)?\"\":\"un\")")
92 (as-c "int main(int argc, char *argv[]) {")
94 (as-c " if (argc != 2) {")
95 (as-c " printf(\"Invalid argcount!\");")
98 (as-c " out = fopen(argv[1], \"w\");")
100 (as-c " printf(\"Error opening output file!\");")
103 (printf "(cl:in-package #:~A)" package-name)
104 (printf "(cl:eval-when (:compile-toplevel)")
105 (printf " (cl:defparameter *integer-sizes* (cl:make-hash-table))")
106 (dolist (type '("char" "short" "long" "int"
107 #+nil"long long" ; TODO: doesn't exist in sb-alien yet
109 (printf " (cl:setf (cl:gethash %d *integer-sizes*) 'sb-alien:~A)" (substitute #\- #\Space type)
110 (format nil "sizeof(~A)" type)))
112 (dolist (def definitions)
113 (destructuring-bind (type lispname cname &optional doc export) def
116 (as-c "#ifdef" cname)
117 (printf "(cl:defconstant ~A %d \"~A\")" lispname doc
120 (printf "(sb-int:style-warn \"Couldn't grovel for ~A (unknown to the C compiler).\")" cname)
123 (c-for-enum lispname cname export))
125 (printf "(cl:eval-when (:compile-toplevel :load-toplevel :execute) (sb-alien:define-alien-type ~A (sb-alien:%ssigned %d)))" lispname
126 (format nil "SIGNED_(~A)" cname)
127 (format nil "(8*sizeof(~A))" cname)))
129 (printf "(cl:defparameter ~A %s \"~A\"" lispname doc
132 (printf "(cl:declaim (cl:inline ~A))" lispname)
133 (destructuring-bind (f-cname &rest definition) cname
134 (printf "(sb-grovel::define-foreign-routine (\"~A\" ~A)" f-cname lispname)
135 (printf "~{ ~W~^\\n~})" definition)))
137 (c-for-structure lispname cname))
139 ;; should we really not sprechen espagnol, monsieurs?
140 (error "Unknown grovel keyword encountered: ~A" type)))
142 (printf "(cl:export '~A)" lispname))))
146 (defun c-constants-extract (filename output-file package)
147 (with-open-file (f output-file :direction :output :if-exists :supersede)
148 (with-open-file (i filename :direction :input)
149 (let* ((headers (read i))
150 (definitions (read i)))
151 (print-c-source f headers definitions package)))))
153 (defclass grovel-constants-file (asdf:cl-source-file)
154 ((package :accessor constants-package :initarg :package)))
156 (define-condition c-compile-failed (compile-failed) ()
157 (:report (lambda (c s)
158 (format s "~@<C compiler failed when performing ~A on ~A.~@:>"
159 (error-operation c) (error-component c)))))
160 (define-condition a-dot-out-failed (compile-failed) ()
161 (:report (lambda (c s)
162 (format s "~@<a.out failed when performing ~A on ~A.~@:>"
163 (error-operation c) (error-component c)))))
165 (defmethod asdf:perform ((op asdf:compile-op)
166 (component grovel-constants-file))
167 ;; we want to generate all our temporary files in the fasl directory
168 ;; because that's where we have write permission. Can't use /tmp;
169 ;; it's insecure (these files will later be owned by root)
170 (let* ((output-file (car (output-files op component)))
171 (filename (component-pathname component))
173 (if (typep output-file 'logical-pathname)
174 (translate-logical-pathname output-file)
175 (pathname output-file)))
176 (tmp-c-source (merge-pathnames #p"foo.c" real-output-file))
177 (tmp-a-dot-out (merge-pathnames #-win32 #p"a.out" #+win32 #p"a.exe"
179 (tmp-constants (merge-pathnames #p"constants.lisp-temp"
181 (princ (list filename output-file real-output-file
182 tmp-c-source tmp-a-dot-out tmp-constants))
184 (funcall (intern "C-CONSTANTS-EXTRACT" (find-package "SB-GROVEL"))
185 filename tmp-c-source (constants-package component))
186 (let ((code (sb-ext:process-exit-code
188 (sb-ext:posix-getenv "CC")
190 (split-cflags (sb-ext:posix-getenv "EXTRA_CFLAGS"))
191 #+(and linux largefile)
192 '("-D_LARGEFILE_SOURCE"
193 "-D_LARGEFILE64_SOURCE"
194 "-D_FILE_OFFSET_BITS=64")
196 (namestring tmp-a-dot-out)
197 (namestring tmp-c-source)))
200 :output *trace-output*))))
202 (case (operation-on-failure op)
203 (:warn (warn "~@<C compiler failure when performing ~A on ~A.~@:>"
206 (error 'c-compile-failed :operation op :component component)))))
207 (let ((code (sb-ext:process-exit-code
208 (sb-ext:run-program (namestring tmp-a-dot-out)
209 (list (namestring tmp-constants))
212 :output *trace-output*))))
214 (case (operation-on-failure op)
215 (:warn (warn "~@<a.out failure when performing ~A on ~A.~@:>"
218 (error 'a-dot-out-failed :operation op :component component)))))
219 (multiple-value-bind (output warnings-p failure-p)
220 (compile-file tmp-constants :output-file output-file)
222 (case (operation-on-warnings op)
224 (formatter "~@<COMPILE-FILE warned while ~
225 performing ~A on ~A.~@:>")
227 (:error (error 'compile-warned :component component :operation op))
230 (case (operation-on-failure op)
232 (formatter "~@<COMPILE-FILE failed while ~
233 performing ~A on ~A.~@:>")
235 (:error (error 'compile-failed :component component :operation op))
238 (error 'compile-error :component component :operation op)))))