add remaining non-trivial copyright notices
[clinton/guile-figl.git] / maint / update-enumerations
1 #!/usr/bin/env guile
2 !#
3
4 ;;; figl
5 ;;; Copyright (C) 2013 Andy Wingo <wingo@pobox.com>
6 ;;; Copyright (C) 2013 Daniel Hartwig <mandyke@gmail.com>
7 ;;;
8 ;;; Figl is free software: you can redistribute it and/or modify it
9 ;;; under the terms of the GNU Lesser General Public License as
10 ;;; published by the Free Software Foundation, either version 3 of the
11 ;;; License, or (at your option) any later version.
12 ;;;
13 ;;; Figl is distributed in the hope that it will be useful, but WITHOUT
14 ;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 ;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
16 ;;; Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU Lesser General Public
19 ;;; License along with this program. If not, see
20 ;;; <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23 ;;
24 ;; Generate Scheme enumerations for the GL API.
25 ;;
26 ;;; Code:
27
28 (use-modules (figl parse)
29 (figl config)
30 (ice-9 match)
31 (sxml fold)
32 ((srfi srfi-1) #:select (append-map))
33 (srfi srfi-69) ; alist->hash-table
34 (texinfo serialize)
35 (texinfo plain-text)
36 (ice-9 pretty-print))
37
38 (setlocale LC_ALL "")
39
40 (print-disable 'escape-newlines)
41
42 (define (list-intersperse src-l elem)
43 (if (null? src-l) src-l
44 (let loop ((l (cdr src-l)) (dest (cons (car src-l) '())))
45 (if (null? l) (reverse dest)
46 (loop (cdr l) (cons (car l) (cons elem dest)))))))
47
48 (define (module-name->scm-name mod-name)
49 (string-join (list (abs-top-srcdir)
50 "figl"
51 (symbol->string mod-name)
52 "enums.scm")
53 "/"))
54
55 (define (module-name->texi-name mod-name)
56 (in-vicinity
57 (in-vicinity (abs-top-srcdir) "doc")
58 (string-append "low-level-" (symbol->string mod-name) "-enums.texi")))
59
60 (define (strip-bit name)
61 (let ((str (symbol->string name)))
62 (cond
63 ((string-suffix? "-bit" str)
64 (string->symbol (substring str 0 (- (string-length str) 4))))
65 ((string-suffix? "-bits" str)
66 (string->symbol (substring str 0 (- (string-length str) 5))))
67 (else #f))))
68
69 (define type-map-table (make-parameter #f))
70
71 (define (use-type-map tm)
72 (type-map-table (alist->hash-table tm eq? hashq)))
73
74 (define (type-map type)
75 (hash-table-ref/default (type-map-table) type #f))
76
77 ;; TODO: Some guesswork is applied here due to inconsistency between
78 ;; the various .spec and type map files. Ideally, the type map can
79 ;; determine whether a type is a bitfield or enum. However, some
80 ;; definitions in enum.spec use a different name to those in gl.spec
81 ;; and gl.tm. For example, BufferAccessMask is known as
82 ;; ARB_map_buffer_range in enum.spec.
83 ;;
84 ;; Perhaps provide an additional map to translate these odd enum.spec
85 ;; names.
86
87 (define (bitfield? enum)
88 (let* ((type (make-gl-param-type (gl-enumeration-category enum)
89 'in
90 'value))
91 (mapped-type (type-map type)))
92 (if mapped-type
93 (eq? (gl-param-type-type mapped-type) 'GLbitfield)
94 ;; otherwise, resort to guesswork
95 (and-map (match-lambda ((name . value) (strip-bit name)))
96 (gl-enumeration-values enum)))))
97
98 (define (write-scm mod-name enums port)
99 (display "\
100 ;;; figl -*- mode: scheme; coding: utf-8 -*-
101 ;;; Copyright (C) 2013 Andy Wingo <wingo@pobox.com>
102 ;;; Copyright (C) 2013 Daniel Hartwig <mandyke@gmail.com>
103 ;;;
104 ;;; Figl is free software: you can redistribute it and/or modify it
105 ;;; under the terms of the GNU Lesser General Public License as
106 ;;; published by the Free Software Foundation, either version 3 of the
107 ;;; License, or (at your option) any later version.
108 ;;;
109 ;;; Figl is distributed in the hope that it will be useful, but WITHOUT
110 ;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
111 ;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
112 ;;; Public License for more details.
113 ;;;
114 ;;; You should have received a copy of the GNU Lesser General Public
115 ;;; License along with this program. If not, see
116 ;;; <http://www.gnu.org/licenses/>.
117 ;;;
118 ;;; Derived from the API specifications at www.opengl.org/registry/api/.
119 ;;;
120 ;;; Automatically generated; you probably don't want to edit this. To
121 ;;; update, run \"make update-enums\" in the top-level build tree.
122 ;;;
123
124 " port)
125 (pretty-print
126 `(define-module (figl ,mod-name enums)
127 #:use-module (figl runtime)
128 #:export ,(map gl-enumeration-category enums))
129 port)
130 (newline port)
131 (for-each
132 (lambda (enum)
133 (pretty-print
134 (if (bitfield? enum)
135 `(define-bitfield ,(gl-enumeration-category enum)
136 ,@(map (match-lambda
137 ((name . value) (list (or (strip-bit name) name)
138 value)))
139 (gl-enumeration-values enum)))
140 `(define-enumeration ,(gl-enumeration-category enum)
141 ,@(map (match-lambda
142 ((name . value) (list name value)))
143 (gl-enumeration-values enum))))
144 port)
145 (newline port))
146 enums))
147
148 (define (write-texi mod-name enums port)
149 ;; FIXME: stexi->texi seems to have some issues with multiline
150 ;; comments, otherwise this notice should appear in the fragment
151 ;; itself. Maybe I am not doing it right.
152 (display
153 (string-append
154 "@c This is part of the Figl Reference Manual.\n"
155 "@c Copyright (c) 2013 Andy Wingo and others\n"
156 "@c See the file figl.texi for copying conditions.\n")
157 port)
158 (display
159 (stexi->texi
160 `(*fragment*
161 (para "The functions from this section may be had by loading "
162 "the module:")
163 (example "(use-modules (figl " ,(object->string mod-name) " enums)")
164 ,@(map
165 (lambda (enum)
166 (if (bitfield? enum)
167 `(defmac (% (name ,(symbol->string (gl-enumeration-category enum)))
168 (arguments "bit..."))
169 (para
170 "Bitfield constructor. The symbolic " (var "bit")
171 " arguments are replaced with their corresponding numeric "
172 "values and combined with " (code "logior") " at "
173 "compile-time. The symbolic arguments known to this "
174 "bitfield constructor are:")
175 (para
176 ,@(list-intersperse
177 (map (lambda (name)
178 `(code ,(symbol->string (or (strip-bit name)
179 name))))
180 (map car (gl-enumeration-values enum)))
181 ", ")
182 "."))
183 `(defmac (% (name ,(symbol->string (gl-enumeration-category enum)))
184 (arguments "enum"))
185 (para
186 "Enumerated value. The symbolic " (var "enum") " argument "
187 "is replaced with its corresponding numeric value at "
188 "compile-time. The symbolic arguments known to this "
189 "enumerated value form are:")
190 (para
191 ,@(list-intersperse
192 (map (lambda (name) `(code ,(symbol->string name)))
193 (map car (gl-enumeration-values enum)))
194 ", ")
195 "."))))
196 enums)))
197 port))
198
199 (define (write-enumerations mod-name enums)
200 (call-with-output-file (module-name->scm-name mod-name)
201 (lambda (port)
202 (write-scm mod-name enums port)))
203 (call-with-output-file (module-name->texi-name mod-name)
204 (lambda (port)
205 (write-texi mod-name enums port))))
206
207 (define* (main arg0)
208 (use-type-map (parse-gl-type-map "gl.tm"))
209 (write-enumerations 'gl (parse-gl-enumerations "enum.spec"))
210 (use-type-map (parse-gl-type-map "glx.tm"))
211 (write-enumerations 'glx (parse-gl-enumerations "glxenum.spec")))
212
213 (when (batch-mode?)
214 (apply main (command-line)))