1 ;; You should nominially invoke this file via ./generators/make-opengl-bindings.rb
2 ;; as that performs the necessary setup.
4 (declaim (optimize (speed 0) (space 0) (debug 3)))
7 (defparameter *reports
* '(:type-map nil
12 :function-category-counts nil
))
14 (defun make-version-syms (&rest versions
)
15 (loop for version in versions
16 collecting
(intern (concatenate 'string
"VERSION_" version
))))
18 (defparameter *core-opengl-versions
*
19 (make-version-syms "1_0" "1_1"))
21 (defparameter *opengl-versions
*
22 (make-version-syms "1_0" "1_1" "1_2" "1_3" "1_4" "1_5"
24 "3_0" "3_1" "3_2" "3_3"
26 "List of versioned extensions for dependency generation.
27 Must be in the correct order.")
29 (defparameter *source-filename
* (or #.
*compile-file-truename
*
30 (load-time-value *load-truename
*)))
32 (defparameter *extension-names
* nil
)
34 (defparameter *base
* (merge-pathnames #P
"../" *source-filename
*))
35 (defparameter *spec
* nil
)
36 (defparameter *type-map
* nil
)
38 (defparameter *enum-specs
* nil
)
39 (defparameter *function-specs
* nil
)
41 (defparameter *exports
* nil
)
43 (defparameter *function-categories
* nil
)
45 (defparameter *predefined-enumerants
* (make-hash-table))
50 (defun plist-keys (plist)
51 "Return all of the keys of a plist"
52 (loop for key in plist by
#'cddr collect key
))
54 (defun plist-values (plist)
55 "Return all of the values of a plist"
56 (loop for key in
(cdr plist
) by
#'cddr collect key
))
58 (defun constantize (symbol)
59 "Converts a symbol into a nice constant-style symbol,
60 changing non-alphanumeric characters to - and surrounding it
62 (intern (format nil
"+~a+"
63 (map 'string
#'(lambda (c) (if (alphanumericp c
) c
#\-
))
64 (string-upcase (string symbol
))))))
66 (defun deconstant (symbol)
67 "Sometimes argument names of OpenGLâ„¢ functions have silly names like
68 't', this is a generalised way to rename them to something more sensible."
69 (if (not (constantp symbol
))
71 (deconstant (intern (concatenate 'string
"_" (symbol-name symbol
))))))
76 (defun c-name-of (func-spec) (first func-spec
))
77 (defun lisp-name-of (func-spec) (second func-spec
))
78 (defun freturn-of (func-spec) (getf (cddr func-spec
) :return
))
79 (defun args-of (func-spec) (getf (cddr func-spec
) :args
))
80 (defun category-of (func-spec) (intern (getf (cddr func-spec
) :category
)))
84 (defparameter *strippable-type-endings
*
85 (list "NV" "ARB" "SGIX" "EXT" "ATI" "IBM" "3DFX" "SGIS"
86 "SUNX" "HP" "GREMEDY" "APPLE" "MESA" "SUN" "INTEL"
89 (defun string-ends-with (string ending
)
90 "Returns t if string ends with ending."
91 (and (>= (length string
) (length ending
))
92 (string= string ending
:start1
(- (length string
) (length ending
)))))
94 (defun string-strip-ending (string ending
)
95 "Returns string (with ending removed, if it was there)."
96 (if (string-ends-with string ending
)
97 (subseq string
0 (- (length string
) (length ending
)))
100 (defun string-strip-endings (string endings
)
101 "Removes any of multiple endings from string, if it has any of them."
103 (string-strip-endings (string-strip-ending string
(car endings
)) (cdr endings
))
104 (string-strip-ending string
(first endings
))))
106 (defun type-map-type-to-gl-type (type-map-type)
107 "Strips the extension suffix off a type and returns an appropriate type symbol
108 suitable for cl-glfw-types or CFFI."
109 (let ((s (string-strip-endings (symbol-name type-map-type
) *strippable-type-endings
*)))
110 (cond ((equal s
"*") :void
)
111 ((equal s
"const GLubyte *") 'string
)
112 ((find #\
* (format nil
"~a" s
)) 'pointer
)
113 ((equal (subseq s
0 2) "GL") (intern (string-upcase (subseq s
2))))
114 ((equal s
"_GLfuncptr") 'pointer
)
117 (defun set-type-maps ()
118 "Fix mappings of specification type names onto valid cl-glfw-types/CFFI symbols."
121 (loop for src-type in
(getf *spec
* :type-map
) by
#'cddr
122 for dst-type in
(cdr (getf *spec
* :type-map
)) by
#'cddr
123 nconc
(list src-type
(type-map-type-to-gl-type dst-type
)))))
127 ;;; {{{ FIX ENUM SPECS
128 (defun set-enum-specs ()
129 "Extract the enum specs from *spec* and resolve all the values"
132 (labels ((resolve-enum (enum-name enum-value
&optional used-groups
)
134 ;; the only end-value type (there are no strings or anything)
135 ((numberp enum-value
) enum-value
)
136 ;; nil value means we have to look everywhere for a value
141 (loop for enum-group-name in
(getf *spec
* :enum-spec
) by
#'cddr
142 for enum-group in
(cdr (getf *spec
* :enum-spec
)) by
#'cddr
143 do
(unless (find enum-group-name used-groups
)
144 (let ((resolved-value (getf enum-group enum-name
)))
146 (push enum-group-name used-groups
)
147 (return-from find-value resolved-value
)))))
148 (return-from resolve-enum
:unable-to-resolve
))
150 ;; it's a name of another symbol, re-resolve with that name
151 ((symbolp enum-value
) (resolve-enum enum-value nil
))
152 ;; a use list means we look in another group for it
153 ((and (listp enum-value
)
154 (eql (first enum-value
) :use
))
157 (getf (getf (getf *spec
* :enum-spec
) (second enum-value
))
160 (t (error "I don't know what to do with the enum definition ~s -> ~s" enum-name enum-value
)))))
161 (loop for enum-group-name in
(getf *spec
* :enum-spec
) by
#'cddr
162 for enum-group in
(cdr (getf *spec
* :enum-spec
)) by
#'cddr
163 unless
(eql enum-group-name
:extensions
)
166 (list enum-group-name
167 (loop for enum-name in enum-group by
#'cddr
168 for enum-value in
(cdr enum-group
) by
#'cddr
171 (resolve-enum enum-name enum-value
(list enum-group-name
)))))))))
174 ;;; {{{ SET FUNC SPECS
175 (defun set-func-specs ()
176 (setf *function-specs
*
177 (loop for func-spec in
(getf *spec
* :functions
)
180 (list (first (first func-spec
))
181 (second (first func-spec
))
182 :return
(first (getf (rest func-spec
) :return
))
183 :args
(loop while
(getf (rest func-spec
) :param
) collect
184 (prog1 (getf (rest func-spec
) :param
)
185 (remf (rest func-spec
) :param
)))
186 :category
(string-strip-ending (first (getf (rest func-spec
) :category
)) "_DEPRECATED")
187 :deprecated
(first (getf (rest func-spec
) :deprecated
))
188 :version
(first (getf (rest func-spec
) :version
))))))
193 (setf *spec
* (with-open-file (in (merge-pathnames #P
"src/gl.spec.lisp" *base
*)) (read in
)))
196 (when (getf *reports
* :type-map
)
198 (sort (loop for name in
*type-map
* by
#'cddr
199 for value in
(cdr *type-map
*) by
#'cddr
200 collect
(cons name value
))
202 (string-lessp (string (cdr a
)) (string (cdr b
)))))
203 do
(format t
"~& ~s:~40t~s~%" (car n-v
) (cdr n-v
))))
207 (remf *enum-specs
* :extensions
)
209 ;; print out initial statistics
210 (format t
"~a functions~%" (length *function-specs
*))
211 (format t
"~a type-maps~%" (/ (length *type-map
*) 2))
212 (format t
"~a enum-specs~%" (length *enum-specs
*))
215 (when (getf *reports
* :property-counts
)
216 ;; count up the properties of functions, what's useful for parsing?
217 (let ((property-counts ()))
218 (dolist (function-spec *function-specs
*)
219 (dolist (property (plist-keys (rest function-spec
)))
220 (incf (getf property-counts property
0))))
221 (let ((*print-pretty
* t
))
222 (format t
"Property counts: ~a~%" property-counts
))))
224 ;; categorize functions
225 (dolist (function-spec *function-specs
*)
227 (getf *function-categories
* (category-of function-spec
))))
229 ;;Work out which categories are actually extensions we want
230 (dolist (category-sym (nconc (plist-keys *function-categories
*)
231 (plist-keys *enum-specs
*)))
232 (let* ((category-string (string category-sym
))
233 (underscore-pos (position #\_ category-string
)))
234 (when (and (integerp underscore-pos
)
235 (plusp underscore-pos
)
236 (every (lambda (char)
237 (or (upper-case-p char
)
238 (digit-char-p char
)))
239 (subseq category-string
0 underscore-pos
))
240 (not (find category-sym
*extension-names
*)))
241 (push category-sym
*extension-names
*))))
243 (when (getf *reports
* :function-category-counts
)
244 (format t
"Category function counts:~%")
245 (loop for cat-name in
*function-categories
* by
#'cddr
246 for cat-contents in
(cdr *function-categories
*) by
#'cddr
247 do
(format t
" ~S: ~S~%" cat-name
(length cat-contents
)))))
251 (defun gl-extension-function-definition (func-spec)
252 (push (lisp-name-of func-spec
) *exports
*)
253 `(defglextfun ,@func-spec
))
255 (defun gl-function-definition (func-spec)
256 (push (lisp-name-of func-spec
) *exports
*)
257 `(defglfun ,@func-spec
))
263 ;; this is the real template opengl defpackage
264 (defun make-opengl-defpackage (exports)
265 "Returns the defpackage for opengl with the exports list given."
266 `(defpackage #:cl-glfw-opengl
267 (:use
#:cffi
#:cl
#:cl-glfw-types
#:cl-glfw-scaffolding
)
268 (:nicknames
#:gl
#:opengl
)
269 (:shadowing-import-from
#:cl-glfw-types
#:boolean
#:byte
#:float
#:char
#:string
#:pointer
)
271 #:enum
#:boolean
#:bitfield
#:byte
#:short
#:int
#:sizei
#:ubyte
#:ushort
#:uint
272 #:float
#:clampf
#:double
#:clampd
#:void
#:uint64
#:int64
277 ,@(mapcar #'make-symbol
(mapcar #'string-upcase
(mapcar #'string exports
))))))
280 (defmacro with-output-file
((out name
) &body forms
)
281 (declare (type symbol out
))
282 `(with-open-file (,out
(merge-pathnames ,name
*base
*) :direction
:output
:if-exists
:supersede
)
283 (when (getf *reports
* :files-output
)
284 (format t
"Generating ~s~%" (truename ,out
)))
285 (format ,out
";;;; This file was automatically generated by ~a~%" *source-filename
*)
295 (defun output-core ()
296 ;; write the main bindings file...
297 (with-output-file (out #P
"lib/opengl-core.lisp")
299 (print `(in-package #:cl-glfw-opengl
) out
)
301 ;; dump all enumerations not in an extension
302 (loop for enum-group-name in
*enum-specs
* by
#'cddr
303 for enum-group in
(cdr *enum-specs
*) by
#'cddr
304 unless
(find enum-group-name
*extension-names
*)
306 (let ((enums-to-define
307 (loop for enum-name in enum-group by
#'cddr
308 for enum-value in
(cdr enum-group
) by
#'cddr
310 (let ((existing (gethash enum-name
*predefined-enumerants
*)))
313 (setf (gethash enum-name
*predefined-enumerants
*) enum-value
)
314 (list (cons enum-name enum-value
)))
315 ((eql existing enum-value
)
318 (warn "Won't redefine enum ~A as ~A, because it is already ~A"
319 enum-name enum-value existing
)
321 ;; when this group is not empty and there is a name that isn't already defined
322 (when enums-to-define
323 (format out
"~&~%;;;; {{{ ~A~%" (string enum-group-name
))
324 (loop for
(enum-name . enum-value
) in enums-to-define do
325 (let ((constant-name (constantize enum-name
)))
326 (push constant-name
*exports
*)
327 (print `(defconstant ,constant-name
,enum-value
) out
)))
328 (format out
"~&~%;;;; }}}~%"))
329 (remf *enum-specs
* enum-group-name
)))))
331 (defun output-category (name category-names
)
332 "write out the extension named by category name"
334 (let ((enum-specs (copy-tree *enum-specs
*))
335 (function-categories (copy-tree *function-categories
*)))
337 ;; collect up the elements of the extension, the enums and functions
339 (loop for category-name in category-names nconcing
340 (loop while
(getf enum-specs category-name
) nconcing
341 (prog1 (loop for enum-name in
(getf enum-specs category-name
) by
#'cddr
342 for enum-value in
(cdr (getf enum-specs category-name
)) by
#'cddr
343 unless
(gethash enum-name
*predefined-enumerants
*)
345 (let ((constant-name (constantize enum-name
)))
346 (push constant-name
*exports
*)
347 `(defconstant ,constant-name
,enum-value
)))
348 (remf enum-specs category-name
)))))
350 (loop for category-name in category-names nconcing
351 (loop while
(getf function-categories category-name
) nconcing
353 (loop for function in
(getf function-categories category-name
)
354 unless
(let ((deprecated-at (getf function
:deprecated
)))
356 (find (intern (concatenate 'string
"VERSION_" (substitute #\_
#\. deprecated-at
)))
359 (remf function-categories category-name
)))))
362 (extension-definitions))
365 (loop for function-spec in function-specs do
367 ((find (category-of function-spec
) *core-opengl-versions
*)
368 (push (gl-function-definition function-spec
) core-definitions
))
370 (push function-spec extension-specs
)
371 (push (gl-extension-function-definition function-spec
) extension-definitions
))))
373 (setf extension-specs
(nreverse extension-specs
)
374 core-definitions
(nreverse core-definitions
)
375 extension-definitions
(nreverse extension-definitions
))
377 (format t
"~A from ~A:~D functions, ~D enumerations~%"
378 (string name
) (mapcar 'string category-names
)
379 (+ (length core-definitions
)
380 (length extension-definitions
))
381 (length enumerations
))
383 ;; only when we have either of these components, actually generate a system
384 (when (or enumerations core-definitions extension-definitions
)
385 (let* ((core-version (find name
*opengl-versions
*))
386 (top-level-asd core-version
))
387 ;; write out the ASD definition
388 (with-output-file (out (format nil
"~acl-glfw-opengl-~a.asd" (if top-level-asd
"" "lib/") name
))
389 (let* ((system-name (string-downcase (format nil
"cl-glfw-opengl-~a" name
)))
390 (system-package (make-symbol (string-upcase (concatenate 'string system-name
"-system")))))
391 (print `(defpackage ,system-package
(:use
#:asdf
#:cl
)) out
)
392 (print `(in-package ,system-package
) out
)
393 (print `(defsystem ,(intern (string-upcase system-name
))
394 :description
,(format nil
"cl-glfw's ~a binding" name
)
395 :author
,(format nil
"Generated by cl-glfw's ~a" *source-filename
*)
396 :licence
"Public Domain"
397 :depends-on
(cl-glfw-opengl-core)
398 :components
((:file
,(concatenate 'string
(if top-level-asd
"lib/" "") "opengl-" (string-downcase (symbol-name name
))))))
401 ;; write the enumerations and function bindings
402 (with-output-file (out (format nil
"lib/opengl-~a.lisp" name
))
403 (print '(in-package #:cl-glfw-opengl
) out
)
404 (format out
"~&~%;;;; ~a~&" name
)
406 (print `(eval-when (:load-toplevel
)
407 (when (and (boundp '*version-loaded
*)
408 (not (eq ',name
*version-loaded
*)))
409 (warn "Loading cl-glfw-opengl-~a over the top of already-loaded cl-glfw-opengl-~a~%" ',name
*version-loaded
*))
410 (defparameter *version-loaded
* ',name
)) out
))
411 (dolist (enumeration enumerations
) (print enumeration out
))
412 (dolist (function core-definitions
) (print function out
))
413 (dolist (function extension-definitions
) (print function out
))
414 (when extension-specs
415 (push (format nil
"LOAD-~A" name
) *exports
*)
416 (print `(make-extension-loader ,name
,extension-specs
) out
))))))))
419 (defun output-everything ()
420 ;; some nice printing options
421 (let ((*print-case
* :downcase
)
427 ;;Write the bindings for the core versions
428 (let (current-categories)
429 (loop for extension-name in
*opengl-versions
* do
430 (let ((deprecated-extension-name (intern (concatenate 'string
(string extension-name
) "_DEPRECATED")))
432 (when (find extension-name
*extension-names
*)
433 (push extension-name current-categories
)
435 (when (find deprecated-extension-name
*extension-names
*)
436 (push deprecated-extension-name current-categories
)
439 (output-category extension-name
(reverse current-categories
)))))
440 ;;Remove them from the lists to be processed
441 (loop for name in current-categories do
442 (loop while
(getf *function-categories
* name
) do
(remf *function-categories
* name
))
443 (loop while
(getf *enum-specs
* name
) do
(remf *enum-specs
* name
))))
445 ;;Process all the extension categories
446 (dolist (category-name *extension-names
*)
447 (output-category category-name
(list category-name
))
448 (loop while
(getf *function-categories
* category-name
) do
(remf *function-categories
* category-name
))
449 (loop while
(getf *enum-specs
* category-name
) do
(remf *enum-specs
* category-name
)))
451 (with-output-file (out #P
"lib/opengl-type-map.lisp")
452 (print `(in-package #:cl-glfw-opengl
) out
)
453 (print `(setf *type-map
* ',*type-map
*) out
))
455 (with-output-file (out #P
"lib/opengl-package.lisp")
456 (print (make-opengl-defpackage (remove-duplicates (nreverse *exports
*))) out
)))
458 (when (and (getf *reports
* :leftover-functions
)
459 *function-categories
*)
460 (format t
"~&Leftover functions:~% ~s~%" *function-categories
*))
462 (when (and (getf *reports
* :leftover-enums
)
464 (format t
"~&Leftover enums:~% ~s~%" *enum-specs
*)))