/
grovel.lisp
787 lines (708 loc) · 30 KB
/
grovel.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
;;;
;;; grovel.lisp --- The CFFI Groveller.
;;;
;;; Copyright (C) 2005-2006, Dan Knap <dankna@accela.net>
;;; Copyright (C) 2005-2006, Matthew Backes <lucca@accela.net>
;;; Copyright (C) 2007, Stelian Ionescu <stelian.ionescu-zeus@poste.it>
;;; Copyright (C) 2007, Luis Oliveira <loliveira@common-lisp.net>
;;;
;;; Permission is hereby granted, free of charge, to any person
;;; obtaining a copy of this software and associated documentation
;;; files (the "Software"), to deal in the Software without
;;; restriction, including without limitation the rights to use, copy,
;;; modify, merge, publish, distribute, sublicense, and/or sell copies
;;; of the Software, and to permit persons to whom the Software is
;;; furnished to do so, subject to the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;;; DEALINGS IN THE SOFTWARE.
;;;
(in-package #:cffi-grovel)
;;;# Utils
(defun trim-whitespace (strings)
(loop for s in strings
collect (string-trim '(#\Space #\Tab #\VT) s)))
;;;# Error Conditions
;;; This warning is signalled when cffi-grovel can't find some macro.
;;; Signalled by CONSTANT or CONSTANTENUM.
(define-condition missing-definition (warning)
((%name :initarg :name :reader name-of))
(:report (lambda (condition stream)
(format stream "No definition for ~A"
(name-of condition)))))
;;;# Grovelling
(defparameter *cc*
#+(or cygwin (not windows)) "cc"
#+(and windows (not cygwin)) "c:/msys/1.0/bin/gcc.exe")
(defparameter *cc-flags* nil)
;;; The header of the intermediate C file.
(defparameter *header*
"/*
* This file has been automatically generated by cffi-grovel.
* Do not edit it by hand.
*/
")
;;; C code generated by cffi-grovel is inserted between the contents
;;; of *PROLOGUE* and *POSTSCRIPT*, inside the main function's body.
(defparameter *prologue*
"
#include <grovel/common.h>
int main(int argc, char**argv) {
FILE *output = argc > 1 ? fopen(argv[1], \"w\") : stdout;
fprintf(output, \";;;; This file has been automatically generated by \"
\"cffi-grovel.\\n;;;; Do not edit it by hand.\\n\\n\");
")
(defparameter *postscript*
"
if (output != stdout)
fclose(output);
return 0;
}
")
(defun unescape-for-c (text)
(with-output-to-string (result)
(loop for i below (length text)
for char = (char text i) do
(cond ((eql char #\") (princ "\\\"" result))
((eql char #\newline) (princ "\\n" result))
(t (princ char result))))))
(defun c-format (out fmt &rest args)
(let ((text (unescape-for-c (format nil "~?" fmt args))))
(format out "~& fprintf(output, \"~A\");~%" text)))
(defun c-printf (out fmt &rest args)
(flet ((item (item)
(format out "~A" (unescape-for-c (format nil item)))))
(format out "~& fprintf(output, \"")
(item fmt)
(format out "\"")
(loop for arg in args do
(format out ", ")
(item arg))
(format out ");~%")))
;;; TODO: handle packages in a better way. One way is to process each
;;; grovel form as it is read (like we already do for wrapper
;;; forms). This way in can expect *PACKAGE* to have sane values.
;;; This would require that "header forms" come before any other
;;; forms.
(defun c-print-symbol (out symbol &optional no-package)
(c-format out
(let ((package (symbol-package symbol)))
(cond
((eq (find-package '#:keyword) package) ":~(~A~)")
(no-package "~(~A~)")
((eq (find-package '#:cl) package) "cl:~(~A~)")
(t "~(~A~)")))
symbol))
(defun c-write (out form &key recursive)
(cond
((and (listp form)
(eq 'quote (car form)))
(c-format out "'")
(c-write out (cadr form) :recursive t))
((listp form)
(c-format out "(")
(loop for subform in form
for first-p = t then nil
unless first-p do (c-format out " ")
do (c-write out subform :recursive t))
(c-format out ")"))
((symbolp form)
(c-print-symbol out form)))
(unless recursive
(c-format out "~%")))
;;; Always NIL for now, add {ENABLE,DISABLE}-AUTO-EXPORT grovel forms
;;; later, if necessary.
(defvar *auto-export* nil)
(defun c-export (out symbol)
(when (and *auto-export* (not (keywordp symbol)))
(c-format out "(cl:export '")
(c-print-symbol out symbol t)
(c-format out ")~%")))
(defun c-section-header (out section-type section-symbol)
(format out "~% /* ~A section for ~S */~%"
section-type
section-symbol))
(defun remove-suffix (string suffix)
(let ((suffix-start (- (length string) (length suffix))))
(if (and (> suffix-start 0)
(string= string suffix :start1 suffix-start))
(subseq string 0 suffix-start)
string)))
(defun strcat (&rest strings)
(apply #'concatenate 'string strings))
(defgeneric %process-grovel-form (name out arguments)
(:method (name out arguments)
(declare (ignore out arguments))
(error "Unknown Grovel syntax: ~S" name)))
(defun process-grovel-form (out form)
(%process-grovel-form (form-kind form) out (cdr form)))
(defun form-kind (form)
;; Using INTERN here instead of FIND-SYMBOL will result in less
;; cryptic error messages when an undefined grovel/wrapper form is
;; found.
(intern (symbol-name (car form)) '#:cffi-grovel))
(defvar *header-forms* '(c include define flag typedef))
(defun header-form-p (form)
(member (form-kind form) *header-forms*))
(defun generate-c-file (input-file output-defaults)
(let ((c-file (make-pathname :type "c" :defaults output-defaults)))
(with-open-file (out c-file :direction :output :if-exists :supersede)
(with-open-file (in input-file :direction :input)
(flet ((read-forms (s)
(do ((forms ())
(form (read s nil nil) (read s nil nil)))
((null form) (nreverse forms))
(labels
((process-form (f)
(case (form-kind f)
(in-package
(setf *package* (find-package (second f)))
(push f forms))
(progn
;; flatten progn forms
(mapc #'process-form (rest f)))
(t (push f forms)))))
(process-form form)))))
(let* ((forms (read-forms in))
(header-forms (remove-if-not #'header-form-p forms))
(body-forms (remove-if #'header-form-p forms)))
(write-string *header* out)
(dolist (form header-forms)
(process-grovel-form out form))
(write-string *prologue* out)
(dolist (form body-forms)
(process-grovel-form out form))
(write-string *postscript* out)))))
c-file))
(defun exe-filename (defaults)
(let* ((path (make-pathname
:type #-cffi-features:windows nil
#+cffi-features:windows "exe"
:defaults defaults)))
;; It's necessary to prepend "./" to relative paths because some
;; implementations of INVOKE use a shell.
(when (or (not (pathname-directory path))
(eq :relative (car (pathname-directory path))))
(setf path (make-pathname
:directory (list* :relative "."
(cdr (pathname-directory path)))
:defaults path)))
path))
(defun tmp-lisp-filename (defaults)
(make-pathname :name (strcat (pathname-name defaults) ".grovel-tmp")
:type "lisp" :defaults defaults))
(cffi:defcfun "getenv" :string
(name :string))
;;; FIXME: is there a better way to detect whether these flags
;;; are necessary?
(defparameter *cpu-word-size-flags*
(ecase (cffi:foreign-type-size :long)
(4 (list "-m32"))
(8 (list "-m64"))))
(defparameter *platform-library-flag*
(list #+cffi-features:darwin "-bundle"
#-cffi-features:darwin "-shared"))
(defun cc-compile-and-link (input-file output-file &key library)
(let ((arglist
`(,(or (getenv "CC") *cc*)
,@*cpu-word-size-flags*
,@*cc-flags*
;; add the cffi directory to the include path to make common.h visible
,(format nil "-I~A"
(directory-namestring
(truename
(asdf:system-definition-pathname :cffi-grovel))))
,@(when library *platform-library-flags*)
"-fPIC" "-o"
,(native-namestring output-file)
,(native-namestring input-file))))
(apply #'invoke arglist)))
;;; *PACKAGE* is rebound so that the IN-PACKAGE form can set it during
;;; *the extent of a given grovel file.
(defun process-grovel-file (input-file &optional (output-defaults input-file))
(with-standard-io-syntax
(let* ((c-file (generate-c-file input-file output-defaults))
(exe-file (exe-filename c-file))
(lisp-file (tmp-lisp-filename c-file)))
(cc-compile-and-link c-file exe-file)
(invoke exe-file (native-namestring lisp-file))
lisp-file)))
;;; OUT is lexically bound to the output stream within BODY.
(defmacro define-grovel-syntax (name lambda-list &body body)
(with-unique-names (name-var args)
`(defmethod %process-grovel-form ((,name-var (eql ',name)) out ,args)
(declare (ignorable out))
(destructuring-bind ,lambda-list ,args
,@body))))
(define-grovel-syntax c (body)
(format out "~%~A~%" body))
(define-grovel-syntax include (&rest includes)
(format out "~{#include <~A>~%~}" includes))
(define-grovel-syntax define (name &optional value)
(format out "#define ~A~@[ ~A~]~%" name value))
(define-grovel-syntax typedef (base-type new-type)
(format out "typedef ~A ~A;~%" base-type new-type))
;;; Is this really needed?
(define-grovel-syntax ffi-typedef (new-type base-type)
(c-format out "(cffi:defctype ~S ~S)~%" new-type base-type))
(define-grovel-syntax flag (&rest flags)
(appendf *cc-flags* (trim-whitespace flags)))
;;; This form also has some "read time" effects. See GENERATE-C-FILE.
(define-grovel-syntax in-package (name)
(c-format out "(cl:in-package #:~A)~%~%" name))
(define-grovel-syntax ctype (lisp-name size-designator)
(c-section-header out "ctype" lisp-name)
(c-export out lisp-name)
(c-format out "(cffi:defctype ")
(c-print-symbol out lisp-name t)
(c-format out " ")
(format out "~& type_name(output, SIGNEDP(~A), ~:[sizeof(~A)~;~D~]);~%"
size-designator
(etypecase size-designator
(string nil)
(integer t))
size-designator)
(c-format out ")~%")
(unless (keywordp lisp-name)
(c-export out lisp-name))
(let ((size-of-constant-name (symbolicate '#:size-of- lisp-name)))
(c-export out size-of-constant-name)
(c-format out "(cl:defconstant "
size-of-constant-name lisp-name)
(c-print-symbol out size-of-constant-name)
(c-format out " (cffi:foreign-type-size '")
(c-print-symbol out lisp-name)
(c-format out "))~%")))
;;; Syntax differs from anything else in CFFI. Fix?
(define-grovel-syntax constant ((lisp-name &rest c-names)
&key (type 'integer) documentation optional)
(when (keywordp lisp-name)
(setf lisp-name (format-symbol "~A" lisp-name)))
(c-section-header out "constant" lisp-name)
(dolist (c-name c-names)
(format out "~&#ifdef ~A~%" c-name)
(c-export out lisp-name)
(c-format out "(cl:defconstant ")
(c-print-symbol out lisp-name t)
(c-format out " ")
(ecase type
(integer
(format out "~& if(SIGNED64P(~A))~%" c-name)
(format out " fprintf(output, \"%lli\", (int64_t) ~A);" c-name)
(format out "~& else~%")
(format out " fprintf(output, \"%llu\", (uint64_t) ~A);" c-name))
(double-float
(format out "~& fprintf(output, \"%s\", print_double_for_lisp((double)~A));~%" c-name)))
(when documentation
(c-format out " ~S" documentation))
(c-format out ")~%")
(format out "~&#else~%"))
(unless optional
(c-format out "(cl:warn 'cffi-grovel:missing-definition :name '~A)~%"
lisp-name))
(dotimes (i (length c-names))
(format out "~&#endif~%")))
(define-grovel-syntax cunion (union-lisp-name union-c-name &rest slots)
(let ((documentation (when (stringp (car slots)) (pop slots))))
(c-section-header out "cunion" union-lisp-name)
(c-export out union-lisp-name)
(dolist (slot slots)
(let ((slot-lisp-name (car slot)))
(c-export out slot-lisp-name)))
(c-format out "(cffi:defcunion (")
(c-print-symbol out union-lisp-name t)
(c-printf out " :size %i)" (format nil "sizeof(~A)" union-c-name))
(when documentation
(c-format out "~% ~S" documentation))
(dolist (slot slots)
(destructuring-bind (slot-lisp-name slot-c-name &key type count)
slot
(declare (ignore slot-c-name))
(c-format out "~% (")
(c-print-symbol out slot-lisp-name t)
(c-format out " ")
(c-print-symbol out type)
(etypecase count
(integer
(c-format out " :count ~D" count))
((eql :auto)
;; nb, works like :count :auto does in cstruct below
(c-printf out " :count %i"
(format nil "sizeof(~A)" union-c-name)))
(null t))
(c-format out ")")))
(c-format out ")~%")))
(defun make-from-pointer-function-name (type-name)
(symbolicate '#:make- type-name '#:-from-pointer))
;;; DEFINE-C-STRUCT-WRAPPER (in ../src/types.lisp) seems like a much
;;; cleaner way to do this. Unless I can find any advantage in doing
;;; it this way I'll delete this soon. --luis
(define-grovel-syntax cstruct-and-class-item (&rest arguments)
(process-grovel-form out (cons 'cstruct arguments))
(destructuring-bind (struct-lisp-name struct-c-name &rest slots)
arguments
(declare (ignore struct-c-name))
(let* ((slot-names (mapcar #'car slots))
(reader-names (mapcar
(lambda (slot-name)
(intern
(strcat (symbol-name struct-lisp-name) "-"
(symbol-name slot-name))))
slot-names))
(initarg-names (mapcar
(lambda (slot-name)
(intern (symbol-name slot-name) "KEYWORD"))
slot-names))
(slot-decoders (mapcar (lambda (slot)
(destructuring-bind
(lisp-name c-name
&key type count
&allow-other-keys)
slot
(declare (ignore lisp-name c-name))
(cond ((and (eq type :char) count)
'cffi:foreign-string-to-lisp)
(t nil))))
slots))
(defclass-form
`(defclass ,struct-lisp-name ()
,(mapcar (lambda (slot-name initarg-name reader-name)
`(,slot-name :initarg ,initarg-name
:reader ,reader-name))
slot-names
initarg-names
reader-names)))
(make-function-name
(make-from-pointer-function-name struct-lisp-name))
(make-defun-form
;; this function is then used as a constructor for this class.
`(defun ,make-function-name (pointer)
(cffi:with-foreign-slots
(,slot-names pointer ,struct-lisp-name)
(make-instance ',struct-lisp-name
,@(loop for slot-name in slot-names
for initarg-name in initarg-names
for slot-decoder in slot-decoders
collect initarg-name
if slot-decoder
collect `(,slot-decoder ,slot-name)
else collect slot-name))))))
(c-export out make-function-name)
(dolist (reader-name reader-names)
(c-export out reader-name))
(c-write out defclass-form)
(c-write out make-defun-form))))
(define-grovel-syntax cstruct (struct-lisp-name struct-c-name &rest slots)
(let ((documentation (when (stringp (car slots)) (pop slots))))
(c-section-header out "cstruct" struct-lisp-name)
(c-export out struct-lisp-name)
(dolist (slot slots)
(let ((slot-lisp-name (car slot)))
(c-export out slot-lisp-name)))
(c-format out "(cffi:defcstruct (")
(c-print-symbol out struct-lisp-name t)
(c-printf out " :size %i)"
(format nil "sizeof(~A)" struct-c-name))
(when documentation
(c-format out "~% ~S" documentation))
(dolist (slot slots)
(destructuring-bind (slot-lisp-name slot-c-name &key type count)
slot
(c-format out "~% (")
(c-print-symbol out slot-lisp-name t)
(c-format out " ")
(c-print-symbol out type)
(etypecase count
(null t)
(integer
(c-format out " :count ~D" count))
((eql :auto)
(c-printf out " :count %i"
(format nil "sizeof(~A) - offsetof(~A, ~A)"
struct-c-name
struct-c-name
slot-c-name)))
((or symbol string)
(format out "~&#ifdef ~A~%" count)
(c-printf out " :count %i"
(format nil "~A" count))
(format out "~&#endif~%")))
(c-printf out " :offset %i)"
(format nil "offsetof(~A, ~A)"
struct-c-name
slot-c-name))))
(c-format out ")~%")
(let ((size-of-constant-name
(symbolicate '#:size-of- struct-lisp-name)))
(c-export out size-of-constant-name)
(c-format out "(cl:defconstant "
size-of-constant-name struct-lisp-name)
(c-print-symbol out size-of-constant-name)
(c-format out " (cffi:foreign-type-size '")
(c-print-symbol out struct-lisp-name)
(c-format out "))~%"))))
(defmacro define-pseudo-cvar (str name type &key read-only)
(let ((c-parse (let ((*read-eval* nil)
(*readtable* (copy-readtable nil)))
(setf (readtable-case *readtable*) :preserve)
(read-from-string str))))
(typecase c-parse
(symbol `(cffi:defcvar (,(symbol-name c-parse) ,name) ,type
:read-only ,read-only))
(list (unless (and (= (length c-parse) 2)
(null (second c-parse))
(symbolp (first c-parse))
(eql #\* (char (symbol-name (first c-parse)) 0)))
(error "Unable to parse c-string ~s." str))
(let ((func-name (symbolicate "%" name '#:-accessor)))
`(progn
(declaim (inline ,func-name))
(cffi:defcfun (,(string-trim "*" (symbol-name (first c-parse)))
,func-name) :pointer)
(define-symbol-macro ,name
(cffi:mem-ref (,func-name) ',type)))))
(t (error "Unable to parse c-string ~s." str)))))
(defun foreign-name-to-symbol (s)
(intern (substitute #\- #\_ (string-upcase s))))
(defun choose-lisp-and-foreign-names (string-or-list)
(etypecase string-or-list
(string (values string-or-list (foreign-name-to-symbol string-or-list)))
(list (destructuring-bind (fname lname &rest args) string-or-list
(declare (ignore args))
(assert (and (stringp fname) (symbolp lname)))
(values fname lname)))))
(define-grovel-syntax cvar (name type &key read-only)
(multiple-value-bind (c-name lisp-name)
(choose-lisp-and-foreign-names name)
(c-section-header out "cvar" lisp-name)
(c-export out lisp-name)
(c-printf out "(cffi-grovel::define-pseudo-cvar \"%s\" "
(format nil "indirect_stringify(~A)" c-name))
(c-print-symbol out lisp-name t)
(c-format out " ")
(c-print-symbol out type)
(when read-only
(c-format out " :read-only t"))
(c-format out ")~%")))
;;; FIXME: where would docs on enum elements go?
(define-grovel-syntax cenum (name &rest enum-list)
(destructuring-bind (name &key base-type define-constants)
(ensure-list name)
(c-section-header out "cenum" name)
(c-export out name)
(c-format out "(cffi:defcenum (")
(c-print-symbol out name t)
(when base-type
(c-printf out " ")
(c-print-symbol out base-type t))
(c-format out ")")
(dolist (enum enum-list)
(destructuring-bind ((lisp-name &rest c-names) &key documentation)
enum
(declare (ignore documentation))
(check-type lisp-name keyword)
(loop :for c-name :in c-names :do
(check-type c-name string)
(c-format out " (")
(c-print-symbol out lisp-name)
(c-format out " ")
(c-printf out "%i" c-name)
(c-format out ")~%"))))
(c-format out ")~%")
(when define-constants
(define-constants-from-enum out enum-list))))
(define-grovel-syntax constantenum (name &rest enum-list)
(destructuring-bind (name &key base-type define-constants)
(ensure-list name)
(c-section-header out "constantenum" name)
(c-export out name)
(c-format out "(cffi:defcenum (")
(c-print-symbol out name t)
(when base-type
(c-printf out " ")
(c-print-symbol out base-type t))
(c-format out ")")
(dolist (enum enum-list)
(destructuring-bind ((lisp-name &rest c-names)
&key optional documentation) enum
(declare (ignore documentation))
(check-type lisp-name keyword)
(c-format out "~% (")
(c-print-symbol out lisp-name)
(loop for c-name in c-names do
(check-type c-name string)
(format out "~&#ifdef ~A~%" c-name)
(c-format out " ")
(c-printf out "%i" c-name)
(format out "~&#else~%"))
(unless optional
(c-format out
"~% #.(cl:progn ~
(cl:warn 'cffi-grovel:missing-definition :name '~A) ~
-1)"
lisp-name))
(dotimes (i (length c-names))
(format out "~&#endif~%"))
(c-format out ")")))
(c-format out ")~%")
(when define-constants
(define-constants-from-enum out enum-list))))
(defun define-constants-from-enum (out enum-list)
(dolist (enum enum-list)
(destructuring-bind ((lisp-name &rest c-names) &rest options)
enum
(%process-grovel-form
'constant out
`((,(intern (string lisp-name)) ,(car c-names))
,@options)))))
;;;# Wrapper Generation
;;;
;;; Here we generate a C file from a s-exp specification but instead
;;; of compiling and running it, we compile it as a shared library
;;; that can be subsequently loaded with LOAD-FOREIGN-LIBRARY.
;;;
;;; Useful to get at macro functionality, errno, system calls,
;;; functions that handle structures by value, etc...
;;;
;;; Matching CFFI bindings are generated along with said C file.
(defun process-wrapper-form (out form)
(%process-wrapper-form (form-kind form) out (cdr form)))
;;; The various operators push Lisp forms onto this list which will be
;;; written out by PROCESS-WRAPPER-FILE once everything is processed.
(defvar *lisp-forms*)
(defun generate-c-lib-file (input-file output-defaults)
(let ((*lisp-forms* nil)
(c-file (make-pathname :type "c" :defaults output-defaults)))
(with-open-file (out c-file :direction :output :if-exists :supersede)
(with-open-file (in input-file :direction :input)
(write-string *header* out)
(loop for form = (read in nil nil) while form
do (process-wrapper-form out form))))
(values c-file (nreverse *lisp-forms*))))
(defun generate-bindings-file (lib-file lisp-forms output-defaults)
(let ((lisp-file (tmp-lisp-filename output-defaults)))
(with-open-file (out lisp-file :direction :output :if-exists :supersede)
(format out ";;;; This file was automatically generated by cffi-grovel.~%~
;;;; Do not edit by hand.~%")
(let ((*package* (find-package '#:cl)))
(format out "~%~S~%" `(cffi:load-foreign-library ,lib-file)))
(dolist (form lisp-forms)
(print form out))
(terpri out))
lisp-file))
(defun lib-filename (defaults)
(make-pathname :type (subseq (cffi::default-library-suffix) 1)
:defaults defaults))
;;; *PACKAGE* is rebound so that the IN-PACKAGE form can set it during
;;; *the extent of a given wrapper file.
(defun process-wrapper-file (input-file &optional (output-defaults input-file))
(with-standard-io-syntax
(let ((lib-file (lib-filename output-defaults)))
(multiple-value-bind (c-file lisp-forms)
(generate-c-lib-file input-file output-defaults)
(cc-compile-and-link c-file lib-file :library t)
;; FIXME: hardcoded library path.
(values (generate-bindings-file lib-file lisp-forms output-defaults)
lib-file)))))
(defgeneric %process-wrapper-form (name out arguments)
(:method (name out arguments)
(declare (ignore out arguments))
(error "Unknown Grovel syntax: ~S" name)))
;;; OUT is lexically bound to the output stream within BODY.
(defmacro define-wrapper-syntax (name lambda-list &body body)
(with-unique-names (name-var args)
`(defmethod %process-wrapper-form ((,name-var (eql ',name)) out ,args)
(declare (ignorable out))
(destructuring-bind ,lambda-list ,args
,@body))))
(define-wrapper-syntax progn (&rest forms)
(dolist (form forms)
(process-wrapper-form out form)))
(define-wrapper-syntax in-package (name)
(setq *package* (find-package name))
(push `(in-package ,name) *lisp-forms*))
(define-wrapper-syntax c (&rest strings)
(dolist (string strings)
(write-line string out)))
(define-wrapper-syntax flag (&rest flags)
(appendf *cc-flags* (strip-whitespace flags)))
(define-wrapper-syntax proclaim (&rest proclamations)
(push `(proclaim ,@proclamations) *lisp-forms*))
(define-wrapper-syntax declaim (&rest declamations)
(push `(declaim ,@declamations) *lisp-forms*))
(define-wrapper-syntax define (name &optional value)
(format out "#define ~A~@[ ~A~]~%" name value))
(define-wrapper-syntax include (&rest includes)
(format out "~{#include <~A>~%~}" includes))
;;; FIXME: this function is not complete. Should probably follow
;;; typedefs? Should definitely understand pointer types.
(defun c-type-name (typespec)
(let ((spec (ensure-list typespec)))
(if (stringp (car spec))
(car spec)
(case (car spec)
((:uchar :unsigned-char) "unsigned char")
((:unsigned-short :ushort) "unsigned short")
((:unsigned-int :uint) "unsigned int")
((:unsigned-long :ulong) "unsigned long")
((:long-long :llong) "long long")
((:unsigned-long-long :ullong) "unsigned long long")
(:pointer "void*")
(:string "char*")
(t (cffi::foreign-name (car spec)))))))
(defun cffi-type (typespec)
(if (and (listp typespec) (stringp (car typespec)))
(second typespec)
typespec))
(define-wrapper-syntax defwrapper (name-and-options rettype &rest args)
(multiple-value-bind (lisp-name foreign-name options)
(cffi::parse-name-and-options name-and-options)
(let* ((foreign-name-wrap (strcat foreign-name "_cffi_wrap"))
(fargs (mapcar (lambda (arg)
(list (c-type-name (second arg))
(cffi::foreign-name (first arg))))
args))
(fargnames (mapcar #'second fargs)))
;; output C code
(format out "~A ~A" (c-type-name rettype) foreign-name-wrap)
(format out "(~{~{~A ~A~}~^, ~})~%" fargs)
(format out "{~% return ~A(~{~A~^, ~});~%}~%~%" foreign-name fargnames)
;; matching bindings
(push `(cffi:defcfun (,foreign-name-wrap ,lisp-name ,@options)
,(cffi-type rettype)
,@(mapcar (lambda (arg)
(list (cffi::lisp-name (first arg))
(cffi-type (second arg))))
args))
*lisp-forms*))))
(define-wrapper-syntax defwrapper* (name-and-options rettype args &rest c-lines)
;; output C code
(multiple-value-bind (lisp-name foreign-name options)
(cffi::parse-name-and-options name-and-options)
(let ((foreign-name-wrap (strcat foreign-name "_cffi_wrap"))
(fargs (mapcar (lambda (arg)
(list (c-type-name (second arg))
(cffi::foreign-name (first arg))))
args)))
(format out "~A ~A" (c-type-name rettype)
foreign-name-wrap)
(format out "(~{~{~A ~A~}~^, ~})~%" fargs)
(format out "{~%~{ ~A~%~}}~%~%" c-lines)
;; matching bindings
(push `(cffi:defcfun (,foreign-name-wrap ,lisp-name ,@options)
,(cffi-type rettype)
,@(mapcar (lambda (arg)
(list (cffi::lisp-name (first arg))
(cffi-type (second arg))))
args))
*lisp-forms*))))