0.8.7.15:
[sbcl.git] / contrib / sb-grovel / def-to-lisp.lisp
index 337d5c7..b16560b 100644 (file)
@@ -5,52 +5,57 @@
   (destructuring-bind (c-name &rest elements) c-struct
     (format stream "printf(\"(sb-grovel::define-c-struct ~A %d)\\n\",sizeof (~A));~%" lisp-name c-name)
     (dolist (e elements)
-      (destructuring-bind (lisp-type lisp-el-name c-type c-el-name) e
-        (format stream "printf(\"(sb-grovel::define-c-accessor ~A-~A ~A ~A \");~%"
+      (destructuring-bind (lisp-type lisp-el-name c-type c-el-name &key distrust-length) e
+       ;; FIXME: this format string doesn't actually guarantee
+       ;; non-multilined-string-constantness, it just makes it more
+       ;; likely.  Sort out the required behaviour (and maybe make
+       ;; the generated C more readable, while we're at it...) --
+       ;; CSR, 2003-05-27
+        (format stream "printf(\"(sb-grovel::define-c-accessor ~A-~A\\n\\~%  ~
+                        ~A ~A \");~%"
                 lisp-name lisp-el-name lisp-name lisp-type)
         ;; offset
         (format stream "{ ~A t;printf(\"%d \",((unsigned long)&(t.~A)) - ((unsigned long)&(t)) ); }~%"
                 c-name c-el-name)
         ;; length
-        (format stream "{ ~A t;printf(\"%d\",(sizeof t.~A));}~%"
-                c-name c-el-name)
+       (if distrust-length
+           (format stream "printf(\"|CL|:|NIL|\");")
+           (format stream "{ ~A t;printf(\"%d\",(sizeof t.~A));}~%"
+                   c-name c-el-name))
         (format stream "printf(\")\\n\");~%")))))
 
 (defun c-for-function (stream lisp-name alien-defn)
   (destructuring-bind (c-name &rest definition) alien-defn
-    (let ((*print-right-margin* nil))
-      (format stream "printf(\"(cl:declaim (cl:inline ~A))\\n\");~%"
-              lisp-name)
-      (princ "printf(\"(sb-grovel::define-foreign-routine (" stream)
-      (princ "\\\"" stream) (princ c-name stream) (princ "\\\" " stream)
-      (princ lisp-name stream)
-      (princ " ) " stream)
-      (terpri stream)
-      (dolist (d definition)
-        (write d :length nil
-               :right-margin nil :stream stream)
-        (princ " " stream))
-      (format stream ")\\n\");")
-      (terpri stream))))
-
+    (format stream "printf(\"(cl:declaim (cl:inline ~A))\\n\");~%" lisp-name)
+    (format stream
+           "printf(\"(sb-grovel::define-foreign-routine (\\\"~A\\\" ~A)\\n\\~%~
+            ~{  ~W~^\\n\\~%~})\\n\");~%"
+           c-name lisp-name definition)))
 
 (defun print-c-source (stream headers definitions package-name)
   (let ((*print-right-margin* nil))
     (format stream "#define SIGNEDP(x) (((x)-1)<0)~%")
     (format stream "#define SIGNED_(x) (SIGNEDP(x)?\"\":\"un\")~%")
-    (loop for i in headers
+    (loop for i in (cons "stdio.h" headers)
           do (format stream "#include <~A>~%" i))
     (format stream "main() { ~%
 printf(\"(in-package ~S)\\\n\");~%" package-name)  
     (format stream "printf(\"(cl:deftype int () '(%ssigned-byte %d))\\\n\",SIGNED_(int),8*sizeof (int));~%")
-    (format stream "printf(\"(cl:deftype char () '(unsigned-byte %d))\\\n\",SIGNED_(char),8*sizeof (char));~%")
-    (format stream "printf(\"(cl:deftype long () '(unsigned-byte %d))\\\n\",SIGNED_(long),8*sizeof (long));~%")
+    (format stream "printf(\"(cl:deftype char () '(%ssigned-byte %d))\\\n\",SIGNED_(char),8*sizeof (char));~%")
+    (format stream "printf(\"(cl:deftype long () '(%ssigned-byte %d))\\\n\",SIGNED_(long),8*sizeof (long));~%")
+    (format stream "printf(\"(cl:defconstant size-of-int %d)\\\n\",sizeof (int));~%")
+    (format stream "printf(\"(cl:defconstant size-of-char %d)\\\n\",sizeof (char));~%")
+    (format stream "printf(\"(cl:defconstant size-of-long %d)\\\n\",sizeof (long));~%")
     (dolist (def definitions)
       (destructuring-bind (type lispname cname &optional doc) def
         (cond ((eq type :integer)
                (format stream
-                       "printf(\"(cl:defconstant ~A %d \\\"~A\\\")\\\n\",~A);~%"
-                       lispname doc cname))
+                       "#ifdef ~A~%~
+                        printf(\"(cl:defconstant ~A %d \\\"~A\\\")\\\n\",~A);~%~
+                        #else~%~
+                        printf(\"(sb-int:style-warn \\\"Couln't grovel definition for ~A (unknown to the C compiler).\\\")\\n\");~%~
+                        #endif~%"
+                       cname lispname doc cname cname))
              ((eq type :type)
               (format stream
                        "printf(\"(sb-alien:define-alien-type ~A (sb-alien:%ssigned %d))\\\n\",SIGNED_(~A),8*(sizeof(~A)));~%"
@@ -66,7 +71,7 @@ printf(\"(in-package ~S)\\\n\");~%" package-name)
               (t
                (format stream
                        "printf(\";; Non hablo Espagnol, Monsieur~%")))))
-    (format stream "exit(0);~%}")))
+    (format stream "exit(0);~%}~%")))
 
 (defun c-constants-extract  (filename output-file package)
   (with-open-file (f output-file :direction :output)
@@ -99,8 +104,12 @@ printf(\"(in-package ~S)\\\n\");~%" package-name)
     (funcall (intern "C-CONSTANTS-EXTRACT" (find-package "SB-GROVEL"))
             filename tmp-c-source (constants-package component))
     (and               
-     (= (run-shell-command "gcc -o ~S ~S" (namestring tmp-a-dot-out)
-        (namestring tmp-c-source)) 0)
+     (= (run-shell-command "gcc ~A -o ~S ~S"
+                          (if (sb-ext:posix-getenv "EXTRA_CFLAGS")
+                              (sb-ext:posix-getenv "EXTRA_CFLAGS")
+                               "")
+                          (namestring tmp-a-dot-out)
+                          (namestring tmp-c-source)) 0)
      (= (run-shell-command "~A >~A"
                           (namestring tmp-a-dot-out)
                           (namestring tmp-constants)) 0)