diff --git a/gl/doc.go b/gl/doc.go index a4b6cfd..8a8123b 100644 --- a/gl/doc.go +++ b/gl/doc.go @@ -33,3 +33,5 @@ The gldebug tracing has very high overhead, so make sure to remove the build tag before deploying any binaries. */ package gl + +//go:generate go run gendebug.go -o gldebug.go diff --git a/gl/gendebug.go b/gl/gendebug.go index 940c492..10f378f 100644 --- a/gl/gendebug.go +++ b/gl/gendebug.go @@ -12,16 +12,20 @@ package main import ( "bytes" "fmt" + "flag" "go/ast" "go/format" "go/parser" "go/printer" "go/token" + "io/ioutil" "log" "os" "strconv" ) +var outfile = flag.String("o", "", "result will be written to the file instead of stdout.") + var fset = new(token.FileSet) func typeString(t ast.Expr) string { @@ -46,18 +50,23 @@ func typePrinterArg(t, name string) string { return name } +func die(err error) { + fmt.Fprintf(os.Stderr, err.Error()) + os.Exit(1) +} + func main() { + flag.Parse() + f, err := parser.ParseFile(fset, "consts.go", nil, parser.ParseComments) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) + die(err) } entries := enum(f) f, err = parser.ParseFile(fset, "gl.go", nil, parser.ParseComments) if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) + die(err) } buf := new(bytes.Buffer) @@ -193,18 +202,23 @@ func main() { b, err := format.Source(buf.Bytes()) if err != nil { os.Stdout.Write(buf.Bytes()) - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) + die(err) } - os.Stdout.Write(b) + if *outfile == "" { + os.Stdout.Write(b) + return + } + if err := ioutil.WriteFile(*outfile, b, 0666); err != nil { + die(err) + } } const preamble = `// Copyright 2014 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Generated from gl.go. DO NOT EDIT. +// Generated from gl.go using go generate. DO NOT EDIT. // See doc.go for details. // +build gldebug diff --git a/gl/gldebug.go b/gl/gldebug.go index 08652a2..d2a8017 100644 --- a/gl/gldebug.go +++ b/gl/gldebug.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Generated from gl.go. DO NOT EDIT. +// Generated from gl.go using go generate. DO NOT EDIT. // See doc.go for details. // +build gldebug