-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.go
58 lines (52 loc) · 1.12 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
// Command enumgen generates C headers from Go enum declarations.
package main
import (
"flag"
"log"
"os"
"text/template"
)
func main() {
var ta templateArgs
flag.StringVar(&ta.Guard, "guard", "", "#include guard name")
outputFlag := flag.String("out", "", "output filename")
flag.Parse()
if flag.NArg() != 1 || *outputFlag == "" {
log.Fatal("Usage: enumgen -guard=INCLUDE_GUARD -out=output.h path/to/package")
}
ta.RecognizeFiles(flag.Arg(0))
outFile, e := os.Create(*outputFlag)
if e != nil {
log.Fatal("cannot open output file", e)
}
defer outFile.Close()
outputTpl.Execute(outFile, ta)
}
type templateArgs struct {
pkgConsts
Guard string
}
const outputTemplate = `
{{- /* */ -}}
// <auto-generated> ndn-dpdk/mk/enumgen
#ifndef {{.Guard}}
#define {{.Guard}}
#include <stdbool.h>
{{range .Enums}}
{{- if .Typename}}
typedef enum {{.Typename}} {
{{- else}}
enum {
{{- end}}
{{- range .Definitions}}
{{.Key}} = {{.Value}},
{{- end}}
{{- if .Typename}}
} __attribute__((packed)) {{.Typename}};
{{- else}}
};
{{- end}}
{{end}}
#endif // {{.Guard}}
`
var outputTpl = template.Must(template.New("").Parse(outputTemplate))