forked from go-gl/glow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
242 lines (210 loc) · 6.89 KB
/
main.go
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
// Command glow generates Go OpenGL bindings. See http://github.com/errcw/glow.
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
var specURL = "https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api"
var specRegexp = regexp.MustCompile(`^(gl|glx|egl|wgl)\.xml$`)
var docURLs = []string{
"https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man2",
"https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man3",
"https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/docs/man4"}
var docRegexp = regexp.MustCompile(`^[ew]?gl[^u_].*\.xml$`)
func download(name string, args []string) {
flags := flag.NewFlagSet(name, flag.ExitOnError)
xmlDir := flags.String("d", "xml", "XML directory")
flags.Parse(args)
specDir := filepath.Join(*xmlDir, "spec")
if err := os.MkdirAll(specDir, 0755); err != nil {
log.Fatalln("error creating specification output directory:", err)
}
docDir := filepath.Join(*xmlDir, "doc")
if err := os.MkdirAll(docDir, 0755); err != nil {
log.Fatalln("error creating documentation output directory:", err)
}
rev, err := DownloadSvnDir(specURL, specRegexp, specDir)
if err != nil {
log.Fatalln("error downloading specification files:", err)
}
specVersionFile := filepath.Join(specDir, "REVISION")
if err := ioutil.WriteFile(specVersionFile, []byte(rev), 0644); err != nil {
log.Fatalln("error writing spec revision metadata file:", err)
}
for _, url := range docURLs {
if _, err := DownloadSvnDir(url, docRegexp, docDir); err != nil {
log.Fatalln("error downloading documentation files:", err)
}
}
}
func generate(name string, args []string) {
flags := flag.NewFlagSet(name, flag.ExitOnError)
var (
xmlDir = flags.String("xml", importPathToDir("github.com/go-gl/glow/xml"), "XML directory")
tmplDir = flags.String("tmpl", importPathToDir("github.com/go-gl/glow/tmpl"), "Template directory")
outDir = flags.String("out", "gl", "Output directory")
api = flags.String("api", "", "API to generate (e.g., gl)")
ver = flags.String("version", "", "API version to generate (e.g., 4.1)")
profile = flags.String("profile", "", "API profile to generate (e.g., core)")
addext = flags.String("addext", "", "If non-empty, a regular expression describing which extensions to include in addition to those supported by the selected profile; takes precedence over explicit removal")
remext = flags.String("remext", "", "If non-empty, a regular expression describing which extensions to exclude")
restrict = flags.String("restrict", "", "JSON file of symbols to restrict symbol generation")
lenientInit = flags.Bool("lenientInit", false, "When true missing functions do not fail Init")
)
flags.Parse(args)
version, err := ParseVersion(*ver)
if err != nil {
log.Fatalln("error parsing version:", err)
}
var addExtRegexp *regexp.Regexp = nil
if *addext != "" {
addExtRegexp, err = regexp.Compile(*addext)
if err != nil {
log.Fatalln("error parsing extension inclusion regexp:", err)
}
}
var remExtRegexp *regexp.Regexp = nil
if *remext != "" {
remExtRegexp, err = regexp.Compile(*remext)
if err != nil {
log.Fatalln("error parsing extension exclusion regexp:", err)
}
}
packageSpec := &PackageSpec{
API: *api,
Version: version,
Profile: *profile,
TmplDir: *tmplDir,
AddExtRegexp: addExtRegexp,
RemExtRegexp: remExtRegexp,
LenientInit: *lenientInit,
}
specs, rev := parseSpecifications(*xmlDir)
docs := parseDocumentation(*xmlDir)
var pkg *Package
for _, spec := range specs {
if spec.HasPackage(packageSpec) {
pkg = spec.ToPackage(packageSpec)
pkg.SpecRev = rev
docs.AddDocs(pkg)
if len(*restrict) > 0 {
performRestriction(pkg, *restrict)
}
if err := pkg.GeneratePackage(*outDir); err != nil {
log.Fatalln("error generating package:", err)
}
break
}
}
if pkg == nil {
log.Fatalln("unable to generate package:", packageSpec)
}
log.Println("generated package in", *outDir)
}
// Converts a slice string into a simple lookup map.
func lookupMap(s []string) map[string]bool {
lookup := make(map[string]bool, len(s))
for _, str := range s {
lookup[str] = true
}
return lookup
}
type jsonRestriction struct {
Enums []string
Functions []string
}
// Reads the given JSON file path into jsonRestriction and filters the package
// accordingly.
func performRestriction(pkg *Package, jsonPath string) {
data, err := ioutil.ReadFile(jsonPath)
if err != nil {
log.Fatalln("error reading JSON restriction file:", err)
}
var r jsonRestriction
if err = json.Unmarshal(data, &r); err != nil {
log.Fatalln("error parsing JSON restriction file:", err)
}
pkg.Filter(lookupMap(r.Enums), lookupMap(r.Functions))
}
func parseSpecifications(xmlDir string) ([]*Specification, string) {
specDir := filepath.Join(xmlDir, "spec")
specFiles, err := ioutil.ReadDir(specDir)
if err != nil {
log.Fatalln("error reading spec file entries:", err)
}
specs := make([]*Specification, 0, len(specFiles))
for _, specFile := range specFiles {
if !strings.HasSuffix(specFile.Name(), "xml") {
continue
}
spec, err := NewSpecification(filepath.Join(specDir, specFile.Name()))
if err != nil {
log.Fatalln("error parsing specification:", specFile.Name(), err)
}
specs = append(specs, spec)
}
rev, err := ioutil.ReadFile(filepath.Join(specDir, "REVISION"))
if err != nil {
log.Fatalln("error reading spec revision file:", err)
}
return specs, string(rev)
}
func parseDocumentation(xmlDir string) Documentation {
docDir := filepath.Join(xmlDir, "doc")
docFiles, err := ioutil.ReadDir(docDir)
if err != nil {
log.Fatalln("error reading doc file entries:", err)
}
docs := make([]string, 0, len(docFiles))
for _, docFile := range docFiles {
docs = append(docs, filepath.Join(docDir, docFile.Name()))
}
doc, err := NewDocumentation(docs)
if err != nil {
log.Fatalln("error parsing documentation:", err)
}
return doc
}
// PackageSpec describes a package to be generated.
type PackageSpec struct {
API string
Version Version
Profile string // If "all" overrides the version spec
TmplDir string
AddExtRegexp *regexp.Regexp
RemExtRegexp *regexp.Regexp
LenientInit bool
}
func printUsage(name string) {
fmt.Printf("Usage: %s command [arguments]\n", name)
fmt.Println("Commands:")
fmt.Println(" download Downloads specification and documentation XML files")
fmt.Println(" generate Generates bindings")
fmt.Printf("Use %s <command> -help for a detailed command description\n", name)
}
func main() {
name := os.Args[0]
args := os.Args[1:]
if len(args) < 1 {
printUsage(name)
os.Exit(-1)
}
command := args[0]
switch command {
case "download":
download("download", args[1:])
case "generate":
generate("generate", args[1:])
default:
fmt.Printf("Unknown command: '%s'\n", command)
printUsage(name)
os.Exit(-1)
}
}