Deliver each contrib as a single FASL. Don't implicitly require ASDF or source code...
[sbcl.git] / contrib / sb-grovel / def-to-lisp.lisp
1 (in-package #:sb-grovel)
2
3 (defvar *default-c-stream* nil)
4
5 (defun escape-for-string (string)
6   (c-escape string))
7
8 (defun split-cflags (string)
9   (remove-if (lambda (flag)
10                (zerop (length flag)))
11              (loop
12                 for start = 0 then (if end (1+ end) nil)
13                 for end = (and start (position #\Space string :start start))
14                 while start
15                 collect (subseq string start end))))
16
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
22                 collect c)
23           'string))
24
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)))
28
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
32 printf.
33
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.
37
38 There is no error checking done, unless you pass too few FORMAT
39 clause args. I recommend using this formatting convention in
40 code:
41
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~});~%"
47            (c-escape formatter)
48            args)))
49
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)))
55   (printf ")))")
56   (when export
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))))))
62
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))
67     (dolist (e elements)
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)
70         ;; offset
71         (as-c "{" cname "t;")
72         (printf "  %d"
73                 (format nil "((unsigned long)&(t.~A)) - ((unsigned long)&(t))" c-el-name))
74         (as-c "}")
75         ;; length
76         (if distrust-length
77             (printf "  0)")
78             (progn
79               (as-c "{" cname "t;")
80               (printf "  %d)"
81                       (format nil "sizeof(t.~A)" c-el-name))
82               (as-c "}")))))
83     (printf "))")))
84
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[]) {")
94     (as-c "    FILE *out;")
95     (as-c "    if (argc != 2) {")
96     (as-c "        printf(\"Invalid argcount!\");")
97     (as-c "        return 1;")
98     (as-c "    } else")
99     (as-c "        out = fopen(argv[1], \"w\");")
100     (as-c "    if (!out) {")
101     (as-c "        printf(\"Error opening output file!\");")
102     (as-c "        return 1;")
103     (as-c "    }")
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
109                     ))
110       (printf "  (cl:setf (cl:gethash %d *integer-sizes*) 'sb-alien:~A)" (substitute #\- #\Space type)
111               (format nil "sizeof(~A)" type)))
112     (printf ")")
113     (dolist (def definitions)
114       (destructuring-bind (type lispname cname &optional doc export) def
115         (case type
116           ((:integer :errno)
117            (as-c "#ifdef" cname)
118            (printf "(cl:defconstant ~A %d \"~A\")" lispname doc
119                    cname)
120            (when (eql type :errno)
121              (printf "(cl:setf (get '~A 'errno) t)" lispname))
122            (as-c "#else")
123            (printf "(sb-int:style-warn \"Couldn't grovel for ~~A (unknown to the C compiler).\" \"~A\")" cname)
124            (as-c "#endif"))
125           (:enum
126            (c-for-enum lispname cname export))
127           (:type
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)))
131           (:string
132            (printf "(cl:defparameter ~A %s \"~A\"" lispname doc
133                    cname))
134           (:function
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)))
139           (:structure
140            (c-for-structure lispname cname))
141           (otherwise
142            ;; should we really not sprechen espagnol, monsieurs?
143            (error "Unknown grovel keyword encountered: ~A" type)))
144         (when export
145           (printf "(cl:export '~A)" lispname))))
146     (as-c "return 0;")
147     (as-c "}")))
148
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)))))
155
156 (defclass grovel-constants-file (cl-source-file)
157   ((package :accessor constants-package :initarg :package)
158    (do-not-grovel :accessor do-not-grovel
159                   :initform nil
160                   :initarg :do-not-grovel)))
161 (defclass asdf::sb-grovel-constants-file (grovel-constants-file) ())
162
163 (define-condition c-compile-failed (compile-file-error)
164   ((description :initform "C compiler failed")))
165 (define-condition a-dot-out-failed (compile-file-error)
166   ((description :initform "a.out failed")))
167
168 (defmethod perform ((op compile-op)
169                     (component grovel-constants-file))
170   ;; we want to generate all our temporary files in the fasl directory
171   ;; because that's where we have write permission.  Can't use /tmp;
172   ;; it's insecure (these files will later be owned by root)
173   (let* ((output-files (output-files op component))
174          (output-file (first output-files))
175          (warnings-file (second output-files))
176          (filename (component-pathname component))
177          (context-format "~/asdf-action::format-action/")
178          (context-arguments `((,op . ,component)))
179          (condition-arguments `(:context-format ,context-format
180                                 :context-arguments ,context-arguments))
181          (real-output-file
182           (if (typep output-file 'logical-pathname)
183               (translate-logical-pathname output-file)
184               (pathname output-file)))
185          (tmp-c-source (merge-pathnames #p"foo.c" real-output-file))
186          (tmp-a-dot-out (merge-pathnames #-win32 #p"a.out" #+win32 #p"a.exe"
187                                          real-output-file))
188          (tmp-constants (merge-pathnames #p"constants.lisp-temp"
189                                          real-output-file)))
190     (princ (list filename output-file real-output-file
191                  tmp-c-source tmp-a-dot-out tmp-constants))
192     (terpri)
193     (funcall (intern "C-CONSTANTS-EXTRACT" (find-package "SB-GROVEL"))
194              filename tmp-c-source (constants-package component))
195     (unless (do-not-grovel component)
196       (let* ((cc (or (and (string/= (sb-ext:posix-getenv "CC") "")
197                           (sb-ext:posix-getenv "CC"))
198                      (if (member :sb-building-contrib *features*)
199                          (error "~@<The CC environment variable not set during ~
200                                  SB-GROVEL build.~:@>")
201                          (sb-int:style-warn
202                           "CC environment variable not set, SB-GROVEL falling back to \"cc\"."))
203                      "cc"))
204              (code (sb-ext:process-exit-code
205                     (sb-ext:run-program
206                      cc
207                      (append
208                       (split-cflags (sb-ext:posix-getenv "EXTRA_CFLAGS"))
209                       #+(and linux largefile)
210                       '("-D_LARGEFILE_SOURCE"
211                         "-D_LARGEFILE64_SOURCE"
212                         "-D_FILE_OFFSET_BITS=64")
213                       #+(and (or x86 ppc) linux) '("-m32")
214                       #+(and x86-64 darwin inode64)
215                       '("-arch" "x86_64"
216                         "-mmacosx-version-min=10.5"
217                         "-D_DARWIN_USE_64_BIT_INODE")
218                       #+(and x86-64 darwin (not inode64))
219                       '("-arch" "x86_64"
220                         "-mmacosx-version-min=10.4")
221                       #+(and x86 darwin)
222                       '("-arch" "i386"
223                         "-mmacosx-version-min=10.4")
224                       #+(and x86-64 sunos) '("-m64")
225                       (list "-o"
226                             (namestring tmp-a-dot-out)
227                             (namestring tmp-c-source)))
228                      :search t
229                      :input nil
230                      :output *trace-output*))))
231         (unless (= code 0)
232           (apply 'error 'c-compile-failed condition-arguments)))
233       (let ((code (sb-ext:process-exit-code
234                    (sb-ext:run-program (namestring tmp-a-dot-out)
235                                        (list (namestring tmp-constants))
236                                        :search nil
237                                        :input nil
238                                        :output *trace-output*))))
239         (unless (= code 0)
240           (apply 'error 'a-dot-out-failed condition-arguments)))
241     (multiple-value-bind (output warnings-p failure-p)
242         (compile-file* tmp-constants :output-file output-file :warnings-file warnings-file)
243       (check-lisp-compile-results output warnings-p failure-p context-format context-arguments)))))