Add purego support for unix
Thanks, does it work now, did you test it? For darwin, it would need .dylib, that tag can be replaced with openbsd which is missing.
It does work! I just spun up this test program:
func main() {
rl.InitWindow(800, 450, "purego")
defer rl.CloseWindow()
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText(fmt.Sprintf("CGO_ENABLED=%v", os.Getenv("CGO_ENABLED")), 190, 200, 20, rl.LightGray)
rl.EndDrawing()
}
}
Running with CGO_ENABLED=0 go run . resulted in the following window (after cloning and building raylib and copying the .so into current directory)!
Edit: Just wanted a little more proof to be certain: test is the same executable as above
pmap <test-pid>
501339: ./test
608K r-x-- test
780K r---- test
40K rw--- test
...
64K rw--- [ anon ]
300K r---- raylib.so
1144K r-x-- raylib.so
472K r---- raylib.so
16K r---- raylib.so
24K rw--- raylib.so
...
total 2683988K
Nice. As it is, it will work only if you are starting the binary from that directory, right? It should probably use something like dirname, _ := filepath.Abs(filepath.Dir(os.Args[0])); filepath.Join(dirname, libname). With just the libname it will search system directories, it would be nice to first try to load it from binary dir, and if it fails to fall back to system dirs.
For darwin, no idea what locations it searches, probably best to just check binary dir.
That was the intention, but your suggestion is definitely better, I'll update with this.
I have no idea about darwin either :D but I'll update to not search current directory
How does this sound, having the loadLibrary function check current directory for a library, then goes for the executable directory?
If we just go with exe dir, then those using go run might have problems if they expect the library to be in the local directory (the dirname from your suggestion will be somewhere in the tmp directory when using go run`
Yes, that is fine. I never use go run so didn't think about it.
I use it almost exclusively :D, good both bases are covered, will update in a few minutes
So I think this is a good iteration. If someone is using go run, they're going to have to set CGO_ENABLED anyway, so what's also setting LD_LIBRARY_PATH.
I've tested the current iteration and it loads it correctly
Purego still doesn't work with structs (datatypes lager 64 bits). Maybe it is possible to get libffi to work with purego.
That's the purego issue for supporting structs on linux: https://github.com/ebitengine/purego/issues/236
@gen2brain I managed to get a prototype running with purego and libffi:
You can find the example here: https://github.com/JupiterRider/ffi/tree/main/examples/raylib