-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove Cgo dependency on Windows #102
Changes from 5 commits
09b7832
82c14b3
5e0daf8
11618d0
cabcdf4
8f47bcd
b844e0e
ecaf743
97a0597
471a800
2143010
522711e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package main | ||
|
||
import "fmt" | ||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// A Function definition. | ||
type Function struct { | ||
|
@@ -10,6 +13,33 @@ type Function struct { | |
Return Type | ||
} | ||
|
||
// IsImplementedForSyscall reports whether the function is implemented for syscall or not. | ||
func (f Function) IsImplementedForSyscall() bool { | ||
// TODO: Use syscall.Syscall18 when Go 1.12 is the minimum supported version. | ||
if len(f.Parameters) > 15 { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// Syscall returns a syscall expression for Windows. | ||
func (f Function) Syscall() string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this would be more idiomatic/clear if it returned an error when !IsImplementedForSyscall (rather than having a separate func that needs to be called)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is called at templates to generate |
||
var ps []string | ||
for _, p := range f.Parameters { | ||
ps = append(ps, p.Type.ConvertGoToUintptr(p.GoName())) | ||
} | ||
for len(ps) == 0 || len(ps)%3 != 0 { | ||
ps = append(ps, "0") | ||
} | ||
|
||
post := "" | ||
if len(ps) > 3 { | ||
post = fmt.Sprintf("%d", len(ps)) | ||
} | ||
|
||
return fmt.Sprintf("syscall.Syscall%s(gp%s, %d, %s)", post, f.GoName, len(f.Parameters), strings.Join(ps, ", ")) | ||
} | ||
|
||
// A Parameter to a Function. | ||
type Parameter struct { | ||
Name string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,14 +68,26 @@ func (pkg *Package) GeneratePackage(dir string) error { | |
if err := pkg.generateFile("package", dir); err != nil { | ||
return err | ||
} | ||
if err := pkg.generateFile("package_notwindows", dir); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having all these files is super unfortunate, but I'm not sure whether there is anything to be done for it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps something like:
|
||
return err | ||
} | ||
if err := pkg.generateFile("package_windows", dir); err != nil { | ||
return err | ||
} | ||
if err := pkg.generateFile("conversions", dir); err != nil { | ||
return err | ||
} | ||
if err := pkg.generateFile("procaddr", dir); err != nil { | ||
if err := pkg.generateFile("procaddr_notwindows", dir); err != nil { | ||
return err | ||
} | ||
if err := pkg.generateFile("procaddr_windows", dir); err != nil { | ||
return err | ||
} | ||
if pkg.HasDebugCallbackFeature() { | ||
if err := pkg.generateFile("debug", dir); err != nil { | ||
if err := pkg.generateFile("debug_notwindows", dir); err != nil { | ||
return err | ||
} | ||
if err := pkg.generateFile("debug_windows", dir); err != nil { | ||
return err | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//glow:keepspace | ||
// Code generated by glow (https://github.com/go-gl/glow). DO NOT EDIT. | ||
|
||
package {{.Name}} | ||
//glow:rmspace | ||
|
||
import "unsafe" | ||
|
||
type DebugProc func( | ||
source uint32, | ||
gltype uint32, | ||
id uint32, | ||
severity uint32, | ||
length int32, | ||
message string, | ||
userParam unsafe.Pointer) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//glow:keepspace | ||
// +build !windows | ||
|
||
// Code generated by glow (https://github.com/go-gl/glow). DO NOT EDIT. | ||
|
||
package {{.Name}} | ||
//glow:rmspace | ||
|
||
{{define "paramsCDecl"}}{{range $i, $p := .}}{{if ne $i 0}}, {{end}}{{$p.Type.CType}} {{$p.CName}}{{end}}{{end}} | ||
{{define "paramsCCall"}}{{range $i, $p := .}}{{if ne $i 0}}, {{end}}{{if $p.Type.IsDebugProc}}glowCDebugCallback{{else}}{{$p.CName}}{{end}}{{end}}{{end}} | ||
|
||
{{define "paramsGoDecl"}}{{range $i, $p := .}}{{if ne $i 0}}, {{end}}{{$p.GoName}} {{$p.Type.GoType}}{{end}}{{end}} | ||
{{define "paramsGoCall"}}{{range $i, $p := .}}{{if ne $i 0}}, {{end}}{{$p.Type.ConvertGoToC $p.GoName}}{{end}}{{end}} | ||
|
||
// #cgo darwin LDFLAGS: -framework OpenGL | ||
// #cgo linux freebsd LDFLAGS: -lGL | ||
// #cgo windows LDFLAGS: -lopengl32 | ||
// | ||
// #if defined(_WIN32) && !defined(APIENTRY) && !defined(__CYGWIN__) && !defined(__SCITECH_SNAP__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. presumably these windows-specific bits are no longer necessary in the not-windows files? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
// #ifndef WIN32_LEAN_AND_MEAN | ||
// #define WIN32_LEAN_AND_MEAN 1 | ||
// #endif | ||
// #include <windows.h> | ||
// #endif | ||
// | ||
// #ifndef APIENTRY | ||
// #define APIENTRY | ||
// #endif | ||
// #ifndef APIENTRYP | ||
// #define APIENTRYP APIENTRY * | ||
// #endif | ||
// #ifndef GLAPI | ||
// #define GLAPI extern | ||
// #endif | ||
// | ||
// {{range .Typedefs}} | ||
// {{replace .CTypedef "\n" "\n// " -1}} | ||
// {{end}} | ||
// | ||
// {{if .HasDebugCallbackFeature}} | ||
// extern void glowDebugCallback_{{.UniqueName}}(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam); | ||
// static void APIENTRY glowCDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) { | ||
// glowDebugCallback_{{.UniqueName}}(source, type, id, severity, length, message, userParam); | ||
// } | ||
// {{end}} | ||
// | ||
// {{range .Functions}} | ||
// typedef {{.Return.CType}} (APIENTRYP GP{{toUpper .GoName}})({{template "paramsCDecl" .Parameters}}); | ||
// {{end}} | ||
// | ||
// {{range .Functions}} | ||
// static {{.Return.CType}} glow{{.GoName}}(GP{{toUpper .GoName}} fnptr{{if ge (len .Parameters) 1}}, {{end}}{{template "paramsCDecl" .Parameters}}) { | ||
// {{if not .Return.IsVoid}}return {{end}}(*fnptr)({{template "paramsCCall" .Parameters}}); | ||
// } | ||
// {{end}} | ||
// | ||
import "C" | ||
import ( | ||
{{if .HasRequiredFunctions}} | ||
"errors" | ||
{{end}} | ||
"unsafe" | ||
) | ||
|
||
var ( | ||
{{range .Functions}} | ||
gp{{.GoName}} C.GP{{toUpper .GoName}} | ||
{{end}} | ||
) | ||
|
||
// Helper functions | ||
func boolToInt(b bool) int { | ||
if b { return 1 } | ||
return 0 | ||
} | ||
|
||
{{define "bridgeCall"}}C.glow{{.GoName}}(gp{{.GoName}}{{if ge (len .Parameters) 1}}, {{end}}{{template "paramsGoCall" .Parameters}}){{end}} | ||
{{range .Functions}} | ||
{{.Comment}} | ||
func {{.GoName}}({{template "paramsGoDecl" .Parameters}}){{if not .Return.IsVoid}} {{.Return.GoType}}{{end}} { | ||
{{range .Parameters}} | ||
{{if .Type.IsDebugProc}}userDebugCallback = {{.GoName}}{{end}} | ||
{{end}} | ||
{{if .Return.IsVoid}}{{template "bridgeCall" .}} | ||
{{else}} | ||
ret := {{template "bridgeCall" .}} | ||
return {{.Return.ConvertCToGo "ret"}} | ||
{{end}} | ||
} | ||
{{end}} | ||
|
||
|
||
// InitWithProcAddrFunc intializes the package using the specified OpenGL | ||
// function pointer loading function. For more cases Init should be used | ||
// instead. | ||
func InitWithProcAddrFunc(getProcAddr func(name string) unsafe.Pointer) error { | ||
{{range .Functions}} | ||
gp{{.GoName}} = (C.GP{{toUpper .GoName}})(getProcAddr("{{.Name}}")) | ||
{{if .Required}} | ||
if gp{{.GoName}} == nil { | ||
return errors.New("{{.Name}}") | ||
} | ||
{{end}} | ||
{{end}} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like Go 1.12 is the lowest supported version now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least in glow. The gl package's mod file still says 1.9. Also, in 1.18 all the syscall functions except SyscallN are deprecated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, go-gl/gl#144