From 1f94a68432dce90a1780ca2be44f12dc5514c622 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 25 May 2014 14:04:03 -0400 Subject: Fixed issues handling INVALID_HANDLE_VALUE in the Windows constant generator, and built the first build with generated constants! --- experiments/windowsconstgen.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'experiments/windowsconstgen.go') diff --git a/experiments/windowsconstgen.go b/experiments/windowsconstgen.go index d9fd3bb..4671111 100644 --- a/experiments/windowsconstgen.go +++ b/experiments/windowsconstgen.go @@ -72,6 +72,31 @@ func gatherNames(pkg *ast.Package) { } } +// some constants confuse cgo into thinking they're external symbols for some reason +// TODO debug cgo +var hacknames = map[string]string{ + "_INVALID_HANDLE_VALUE": "x_INVALID_HANDLE_VALUE", +} + +func hacknamesPreamble() string { + if len(hacknames) == 0 { + return "" + } + // keep sorted for git + hn := make([]string, 0, len(hacknames)) + for origname, _ := range hacknames { + hn = append(hn, origname) + } + sort.Strings(hn) + s := "// /* because cgo has issues with these */\n" + s += "// #include \n" + for _, origname := range hn { + s += "// uintptr_t " + hacknames[origname] + " = (uintptr_t) (" + + origname[1:] + ");\n" // strip leading _ + } + return s +} + func preamble(pkg string) string { return "// autogenerated by windowsconstgen; do not edit\n" + "package " + pkg + "\n" @@ -104,6 +129,10 @@ func main() { // keep sorted for git consts := make([]string, 0, len(unknown)) for ident, _ := range unknown { + if hackname, ok := hacknames[ident]; ok { + consts = append(consts, hackname) + continue + } consts = append(consts, ident) } sort.Strings(consts) @@ -122,11 +151,18 @@ func main() { "import \"fmt\"\n" + "// #include \n" + "// #include \n" + + "%s" + "import \"C\"\n" + "func main() {\n" + " fmt.Println(%q)\n", - preamble("main"), preamble("ui")) + preamble("main"), hacknamesPreamble(), preamble("ui")) for _, ident := range consts { + if ident[0] == 'x' { + // hack name; strip the leading x (but not the _ after it) from the constant name but keep the value name unchanged + fmt.Fprintf(f, " fmt.Println(\"const %s =\", C.%s)\n", ident[1:], ident) + continue + } + // not a hack name; strip the leading _ from the value name but keep the constant name unchanged fmt.Fprintf(f, " fmt.Println(\"const %s =\", C.%s)\n", ident, ident[1:]) } fmt.Fprintf(f, "}\n") -- cgit v1.2.3