Skip to content

Commit

Permalink
Fix generation and make generator deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
ccherng committed Jul 25, 2012
1 parent a9d4fc8 commit 1a11290
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 27 deletions.
78 changes: 57 additions & 21 deletions generator.go
Expand Up @@ -9,6 +9,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
) )


Expand Down Expand Up @@ -44,7 +45,24 @@ func writePackage(w io.Writer, packageName string, pak *Package, functsInfo *Fun


fmt.Fprintf(w, "// Automatically generated OpenGL binding.\n// \n") fmt.Fprintf(w, "// Automatically generated OpenGL binding.\n// \n")
fmt.Fprintf(w, "// Categories in this package: \n// \n") fmt.Fprintf(w, "// Categories in this package: \n// \n")
for cat, _ := range pak.Functions {
sortedFunctionCategories := make([]string, len(pak.Functions))
i := 0
for k, _ := range pak.Functions {
sortedFunctionCategories[i] = k
i++
}
sort.Strings(sortedFunctionCategories)

sortedEnumCategories := make([]string, len(pak.Enums))
i = 0
for k, _ := range pak.Enums {
sortedEnumCategories[i] = k
i++
}
sort.Strings(sortedEnumCategories)

for _, cat := range sortedFunctionCategories {
pc, _ := ParseCategoryString(cat) pc, _ := ParseCategoryString(cat)
switch pc.Type { switch pc.Type {
case CategoryExtension: case CategoryExtension:
Expand Down Expand Up @@ -130,15 +148,15 @@ func writePackage(w io.Writer, packageName string, pak *Package, functsInfo *Fun
fmt.Fprintf(w, "// #endif\n") fmt.Fprintf(w, "// #endif\n")
fmt.Fprintf(w, "// }\n// \n") fmt.Fprintf(w, "// }\n// \n")


if err := writeCFuncDeclarations(w, pak.Functions, typeMap); err != nil { if err := writeCFuncDeclarations(w, sortedFunctionCategories, pak.Functions, typeMap); err != nil {
return err return err
} }


if err := writeCFuncDefinitions(w, pak.Functions, typeMap); err != nil { if err := writeCFuncDefinitions(w, sortedFunctionCategories, pak.Functions, typeMap); err != nil {
return err return err
} }


if err := writeCFuncGetProcAddrs(w, pak.Functions); err != nil { if err := writeCFuncGetProcAddrs(w, sortedFunctionCategories, pak.Functions); err != nil {
return err return err
} }


Expand Down Expand Up @@ -171,13 +189,13 @@ func writePackage(w io.Writer, packageName string, pak *Package, functsInfo *Fun
fmt.Fprintf(w, " Sizeiptr C.GLsizeiptr\n") fmt.Fprintf(w, " Sizeiptr C.GLsizeiptr\n")
fmt.Fprintf(w, ")\n\n") fmt.Fprintf(w, ")\n\n")


writeGoEnumDefinitions(w, pak.Enums) writeGoEnumDefinitions(w, sortedEnumCategories, pak.Enums)


if err := writeGoFuncDefinitions(w, pak.Functions, typeMap, highestMajorVersion); err != nil { if err := writeGoFuncDefinitions(w, sortedFunctionCategories, pak.Functions, typeMap, highestMajorVersion); err != nil {
return err return err
} }


writeGoInitDefinitions(w, pak.Functions) writeGoInitDefinitions(w, sortedFunctionCategories, pak.Functions)


writeUtilityFunctions(w) writeUtilityFunctions(w)


Expand All @@ -186,8 +204,9 @@ func writePackage(w io.Writer, packageName string, pak *Package, functsInfo *Fun
return nil return nil
} }


func writeCFuncDeclarations(w io.Writer, functions FunctionCategories, typeMap TypeMap) error { func writeCFuncDeclarations(w io.Writer, sortedFunctionCategories []string, functions FunctionCategories, typeMap TypeMap) error {
for cat, fs := range functions { for _, cat := range sortedFunctionCategories {
fs := functions[cat]
fmt.Fprintf(w, "// // %s\n", cat) fmt.Fprintf(w, "// // %s\n", cat)
for _, f := range fs { for _, f := range fs {
if err := writeCFuncDeclaration(w, f, typeMap, false); err != nil { if err := writeCFuncDeclaration(w, f, typeMap, false); err != nil {
Expand Down Expand Up @@ -228,8 +247,9 @@ func writeCFuncDeclaration(w io.Writer, f *Function, typeMap TypeMap, prototype
return nil return nil
} }


func writeCFuncDefinitions(w io.Writer, functions FunctionCategories, typeMap TypeMap) error { func writeCFuncDefinitions(w io.Writer, sortedFunctionCategories []string, functions FunctionCategories, typeMap TypeMap) error {
for cat, fs := range functions { for _, cat := range sortedFunctionCategories {
fs := functions[cat]
fmt.Fprintf(w, "// // %s\n", cat) fmt.Fprintf(w, "// // %s\n", cat)
for _, f := range fs { for _, f := range fs {
if err := writeCFuncDefinition(w, f, typeMap, false); err != nil { if err := writeCFuncDefinition(w, f, typeMap, false); err != nil {
Expand Down Expand Up @@ -284,8 +304,9 @@ func writeCFuncDefinition(w io.Writer, f *Function, typeMap TypeMap, prototype b
return nil return nil
} }


func writeCFuncGetProcAddrs(w io.Writer, functions FunctionCategories) error { func writeCFuncGetProcAddrs(w io.Writer, sortedFunctionCategories []string, functions FunctionCategories) error {
for cat, fs := range functions { for _, cat := range sortedFunctionCategories {
fs := functions[cat]
fmt.Fprintf(w, "// int init_%s() {\n", cat) fmt.Fprintf(w, "// int init_%s() {\n", cat)
for _, f := range fs { for _, f := range fs {
fmt.Fprintf(w, "// ptrgl%s = goglGetProcAddress(\"gl%s\");\n", f.Name, f.Name) fmt.Fprintf(w, "// ptrgl%s = goglGetProcAddress(\"gl%s\");\n", f.Name, f.Name)
Expand All @@ -298,11 +319,25 @@ func writeCFuncGetProcAddrs(w io.Writer, functions FunctionCategories) error {
return nil return nil
} }


func writeGoEnumDefinitions(w io.Writer, enumCats EnumCategories) { func writeGoEnumDefinitions(w io.Writer, sortedEnumCategories []string, enumCats EnumCategories) {
for cat, enums := range enumCats { for _, cat := range sortedEnumCategories {
enums := enumCats[cat]
fmt.Fprintf(w, "// %s\n", cat) fmt.Fprintf(w, "// %s\n", cat)
fmt.Fprintf(w, "const (\n") fmt.Fprintf(w, "const (\n")
for e, v := range enums {
sortedEnums := make([]string, len(enums))
i := 0
for k, _ := range enums {
if strings.HasPrefix(k, "COPY_READ") {
fmt.Println(k)
}
sortedEnums[i] = k
i++
}
sort.Strings(sortedEnums)

for _, e := range sortedEnums {
v := enums[e]
if !enumCats.IsAlreadyDefined(e, cat) { if !enumCats.IsAlreadyDefined(e, cat) {
fmt.Fprintf(w, "\t%s = %s\n", CleanEnumName(e), v) fmt.Fprintf(w, "\t%s = %s\n", CleanEnumName(e), v)
} }
Expand All @@ -311,8 +346,9 @@ func writeGoEnumDefinitions(w io.Writer, enumCats EnumCategories) {
} }
} }


func writeGoFuncDefinitions(w io.Writer, functions FunctionCategories, typeMap TypeMap, majorVersion int) error { func writeGoFuncDefinitions(w io.Writer, sortedFunctionCategories []string, functions FunctionCategories, typeMap TypeMap, majorVersion int) error {
for cat, fs := range functions { for _, cat := range sortedFunctionCategories {
fs := functions[cat]
fmt.Fprintf(w, "// %s\n\n", cat) fmt.Fprintf(w, "// %s\n\n", cat)
for _, f := range fs { for _, f := range fs {
if err := writeGoFuncDefinition(w, f, typeMap, majorVersion, false); err != nil { if err := writeGoFuncDefinition(w, f, typeMap, majorVersion, false); err != nil {
Expand Down Expand Up @@ -394,8 +430,8 @@ func writeGoFuncDefinition(w io.Writer, f *Function, typeMap TypeMap, majorVersi
return nil return nil
} }


func writeGoInitDefinitions(w io.Writer, functions FunctionCategories) error { func writeGoInitDefinitions(w io.Writer, sortedFunctionCategories []string, functions FunctionCategories) error {
for cat, _ := range functions { for _, cat := range sortedFunctionCategories {
fmt.Fprintf(w, "func Init%s() error {\n", GoName(cat)) fmt.Fprintf(w, "func Init%s() error {\n", GoName(cat))
fmt.Fprintf(w, "\tvar ret C.int\n") fmt.Fprintf(w, "\tvar ret C.int\n")
fmt.Fprintf(w, "\tif ret = C.init_%s(); ret != 0 {\n", cat) fmt.Fprintf(w, "\tif ret = C.init_%s(); ret != 0 {\n", cat)
Expand All @@ -406,7 +442,7 @@ func writeGoInitDefinitions(w io.Writer, functions FunctionCategories) error {
} }
fmt.Fprintf(w, "func Init() error {\n") fmt.Fprintf(w, "func Init() error {\n")
fmt.Fprintf(w, "\tvar err error\n") fmt.Fprintf(w, "\tvar err error\n")
for cat, _ := range functions { for _, cat := range sortedFunctionCategories {
fmt.Fprintf(w, "\tif err = Init%s(); err != nil {\n", GoName(cat)) fmt.Fprintf(w, "\tif err = Init%s(); err != nil {\n", GoName(cat))
fmt.Fprintf(w, "\t\treturn err\n") fmt.Fprintf(w, "\t\treturn err\n")
fmt.Fprintf(w, "\t}\n") fmt.Fprintf(w, "\t}\n")
Expand Down
10 changes: 5 additions & 5 deletions gl42/gl42.go
Expand Up @@ -3953,7 +3953,7 @@ const (
DEPTH_STENCIL = 0x84F9 DEPTH_STENCIL = 0x84F9
DEPTH_STENCIL_ATTACHMENT = 0x821A DEPTH_STENCIL_ATTACHMENT = 0x821A
DRAW_FRAMEBUFFER = 0x8CA9 DRAW_FRAMEBUFFER = 0x8CA9
DRAW_FRAMEBUFFER_BINDING = FRAMEBUFFER_BINDING DRAW_FRAMEBUFFER_BINDING = 0x8CA6
FIXED_ONLY = 0x891D FIXED_ONLY = 0x891D
FLOAT_32_UNSIGNED_INT_24_8_REV = 0x8DAD FLOAT_32_UNSIGNED_INT_24_8_REV = 0x8DAD
FRAMEBUFFER = 0x8D40 FRAMEBUFFER = 0x8D40
Expand Down Expand Up @@ -4125,8 +4125,8 @@ const (
const ( const (
ACTIVE_UNIFORM_BLOCKS = 0x8A36 ACTIVE_UNIFORM_BLOCKS = 0x8A36
ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH = 0x8A35 ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH = 0x8A35
COPY_READ_BUFFER = COPY_READ_BUFFER_BINDING COPY_READ_BUFFER = 0x8F36
COPY_WRITE_BUFFER = COPY_WRITE_BUFFER_BINDING COPY_WRITE_BUFFER = 0x8F37
INT_SAMPLER_2D_RECT = 0x8DCD INT_SAMPLER_2D_RECT = 0x8DCD
INT_SAMPLER_BUFFER = 0x8DD0 INT_SAMPLER_BUFFER = 0x8DD0
INVALID_INDEX = 0xFFFFFFFF INVALID_INDEX = 0xFFFFFFFF
Expand Down Expand Up @@ -4347,8 +4347,8 @@ const (
TEXTURE_CUBE_MAP_ARRAY = 0x9009 TEXTURE_CUBE_MAP_ARRAY = 0x9009
TRANSFORM_FEEDBACK = 0x8E22 TRANSFORM_FEEDBACK = 0x8E22
TRANSFORM_FEEDBACK_BINDING = 0x8E25 TRANSFORM_FEEDBACK_BINDING = 0x8E25
TRANSFORM_FEEDBACK_BUFFER_ACTIVE = TRANSFORM_FEEDBACK_ACTIVE TRANSFORM_FEEDBACK_BUFFER_ACTIVE = 0x8E24
TRANSFORM_FEEDBACK_BUFFER_PAUSED = TRANSFORM_FEEDBACK_PAUSED TRANSFORM_FEEDBACK_BUFFER_PAUSED = 0x8E23
UNIFORM_BLOCK_REFERENCED_BY_TESS_CONTROL_SHADER = 0x84F0 UNIFORM_BLOCK_REFERENCED_BY_TESS_CONTROL_SHADER = 0x84F0
UNIFORM_BLOCK_REFERENCED_BY_TESS_EVALUATION_SHADER = 0x84F1 UNIFORM_BLOCK_REFERENCED_BY_TESS_EVALUATION_SHADER = 0x84F1
UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY = 0x900F UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY = 0x900F
Expand Down
2 changes: 1 addition & 1 deletion util.go
Expand Up @@ -146,7 +146,7 @@ func CTypeToGoType(cType string, out bool, mod ParamModifier) (goType, cgoType s
err = errors.New("Unsupported out parameter.") err = errors.New("Unsupported out parameter.")
} }
return return
case "GLchar*", "GLcharARB*": case "GLchar*", "GLchar* const", "GLcharARB*":
goType = "*Char" goType = "*Char"
cgoType = "*C.GLchar" cgoType = "*C.GLchar"
if mod == ParamModifierArray || mod == ParamModifierReference { if mod == ParamModifierArray || mod == ParamModifierReference {
Expand Down

0 comments on commit 1a11290

Please sign in to comment.