Commit | Line | Data |
---|---|---|
8925f36f AW |
1 | #!/usr/bin/env guile |
2 | !# | |
3 | ||
4 | (use-modules (figl parse) | |
5 | (figl config) | |
bb894c9d | 6 | (ice-9 match) |
8925f36f AW |
7 | (sxml fold) |
8 | ((srfi srfi-1) #:select (append-map)) | |
9 | (texinfo serialize) | |
10 | (texinfo plain-text) | |
11 | (ice-9 pretty-print)) | |
12 | ||
13 | (setlocale LC_ALL "") | |
14 | ||
15 | (print-disable 'escape-newlines) | |
16 | ||
bb894c9d AW |
17 | (define (list-intersperse src-l elem) |
18 | (if (null? src-l) src-l | |
19 | (let loop ((l (cdr src-l)) (dest (cons (car src-l) '()))) | |
20 | (if (null? l) (reverse dest) | |
21 | (loop (cdr l) (cons (car l) (cons elem dest))))))) | |
22 | ||
8925f36f | 23 | (define (module-name->scm-name mod-name) |
7ec693ed AW |
24 | (string-join (list (abs-top-srcdir) |
25 | "figl" | |
26 | (symbol->string mod-name) | |
27 | "low-level.scm") | |
28 | "/")) | |
8925f36f AW |
29 | |
30 | (define (module-name->texi-name mod-name) | |
31 | (in-vicinity | |
32 | (in-vicinity (abs-top-srcdir) "doc") | |
7ec693ed | 33 | (string-append "low-level-" (symbol->string mod-name) ".texi"))) |
8925f36f AW |
34 | |
35 | (define (unique-copyrights defs) | |
36 | (let lp ((in defs) (out '())) | |
37 | (if (null? in) | |
38 | (reverse out) | |
39 | (let ((copyright (gl-definition-copyright (car in)))) | |
40 | (lp (cdr in) | |
3c9b6116 | 41 | (if (or (not copyright) (member copyright out)) |
8925f36f AW |
42 | out |
43 | (cons copyright out))))))) | |
44 | ||
45 | (define (write-scm mod-name defs port) | |
46 | (display "\ | |
47 | ;;; figl -*- mode: scheme; coding: utf-8 -*- | |
48 | ;;; Copyright (C) 2013 Andy Wingo <wingo@pobox.com> | |
49 | ;;; | |
50 | ;;; Figl is free software: you can redistribute it and/or modify it | |
51 | ;;; under the terms of the GNU Lesser General Public License as | |
52 | ;;; published by the Free Software Foundation, either version 3 of the | |
53 | ;;; License, or (at your option) any later version. | |
54 | ;;; | |
55 | ;;; Figl is distributed in the hope that it will be useful, but WITHOUT | |
56 | ;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
57 | ;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General | |
58 | ;;; Public License for more details. | |
59 | ;;; | |
60 | ;;; You should have received a copy of the GNU Lesser General Public | |
61 | ;;; License along with this program. If not, see | |
62 | ;;; <http://www.gnu.org/licenses/>. | |
63 | ;;; | |
64 | ;;; Derived from upstream OpenGL documentation. | |
65 | " port) | |
66 | (for-each | |
67 | (lambda (copyright) | |
68 | (display ";;; " port) | |
69 | (newline port) | |
70 | (for-each (lambda (line) | |
71 | (display ";;; " port) | |
72 | (display line port) | |
73 | (newline port)) | |
74 | (string-split | |
75 | (string-trim-both (stexi->plain-text copyright)) | |
76 | #\newline))) | |
77 | (unique-copyrights defs)) | |
78 | (display "\ | |
79 | ;;; | |
80 | ;;; Automatically generated; you probably don't want to edit this. To | |
81 | ;;; update, run \"make update\" in the top-level build tree. | |
82 | ;;; | |
83 | " port) | |
84 | (newline port) | |
85 | (pretty-print | |
7ec693ed AW |
86 | `(define-module (figl ,mod-name low-level) |
87 | #:use-module (figl runtime) | |
88 | #:use-module (figl ,mod-name types) | |
bb894c9d AW |
89 | #:export ,(append-map (lambda (def) |
90 | (map car (gl-definition-prototypes def))) | |
91 | defs)) | |
8925f36f AW |
92 | port) |
93 | (newline port) | |
94 | (for-each | |
95 | (lambda (def) | |
96 | (pretty-print | |
7ec693ed | 97 | `(define-foreign-procedure ,(gl-definition-prototypes def) |
8925f36f AW |
98 | ,(string-trim-both |
99 | (stexi->plain-text | |
3c9b6116 | 100 | (gl-definition-documentation def)))) |
8925f36f AW |
101 | port) |
102 | (newline port)) | |
103 | defs)) | |
104 | ||
105 | (define (write-texi mod-name defs port) | |
106 | (display | |
107 | (stexi->texi | |
108 | `(*fragment* | |
109 | (para "The functions from this section may be had by loading " | |
110 | "the module:") | |
7ec693ed | 111 | (example "(use-modules (figl " ,(object->string mod-name) " low-level)") |
8925f36f AW |
112 | (copying |
113 | (para | |
114 | "This section of the manual was derived from the upstream " | |
115 | "OpenGL documentation. Each function's documentation has " | |
116 | "its own copyright statement; for full details, see the " | |
117 | "upstream documentation. The copyright notices and licenses " | |
118 | "present in this section are as follows.") | |
119 | ,@(append-map cdr (unique-copyrights defs))) | |
120 | ,@(map | |
121 | (lambda (def) | |
bb894c9d AW |
122 | (match (gl-definition-prototypes def) |
123 | (((name (pname ptype) ... '-> return-type) | |
124 | (name* (pname* ptype*) ... '-> return-type*) | |
125 | ...) | |
126 | `(deftypefun (% (name ,(symbol->string name)) | |
127 | (data-type ,(symbol->string return-type)) | |
128 | (arguments ,@(list-intersperse | |
129 | (map symbol->string pname) | |
130 | " "))) | |
131 | ,@(map (lambda (name pname ptype return-type) | |
132 | `(deftypefunx | |
133 | (% (name ,(symbol->string name)) | |
134 | (data-type ,(symbol->string return-type)) | |
135 | (arguments ,@(list-intersperse | |
136 | (map symbol->string pname) | |
137 | " "))))) | |
138 | name* pname* ptype* return-type*) | |
139 | ,@(cdr (gl-definition-documentation def)))))) | |
8925f36f AW |
140 | defs))) |
141 | port)) | |
142 | ||
143 | (define (write-bindings mod-name defs) | |
144 | (call-with-output-file (module-name->scm-name mod-name) | |
145 | (lambda (port) | |
146 | (write-scm mod-name defs port))) | |
147 | (call-with-output-file (module-name->texi-name mod-name) | |
148 | (lambda (port) | |
149 | (write-texi mod-name defs port)))) | |
150 | ||
151 | (define (partition-definitions version) | |
152 | (fold-values | |
153 | (lambda (def gl glu glx) | |
154 | (cond | |
155 | ((string-prefix? "glu" (gl-definition-name def)) | |
156 | (values gl (cons def glu) glx)) | |
157 | ((string-prefix? "glX" (gl-definition-name def)) | |
158 | (values gl glu (cons def glx))) | |
159 | (else | |
160 | (values (cons def gl) glu glx)))) | |
161 | (parse-gl-definitions version) | |
162 | '() | |
163 | '() | |
164 | '())) | |
165 | ||
166 | (define* (main arg0 #:optional (version "2")) | |
167 | (call-with-values | |
168 | (lambda () (partition-definitions version)) | |
169 | (lambda (gl glu glx) | |
7ec693ed AW |
170 | (write-bindings 'gl (reverse gl)) |
171 | (write-bindings 'glu (reverse glu)) | |
172 | (write-bindings 'glx (reverse glx))))) | |
8925f36f AW |
173 | |
174 | (when (batch-mode?) | |
175 | (apply main (command-line))) |