raylib-go icon indicating copy to clipboard operation
raylib-go copied to clipboard

Add purego support for unix

Open dusk125 opened this issue 2 years ago • 13 comments

dusk125 avatar Mar 14 '24 22:03 dusk125

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.

gen2brain avatar Mar 14 '24 22:03 gen2brain

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)!

image

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

dusk125 avatar Mar 14 '24 22:03 dusk125

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.

gen2brain avatar Mar 14 '24 23:03 gen2brain

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

dusk125 avatar Mar 14 '24 23:03 dusk125

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`

dusk125 avatar Mar 14 '24 23:03 dusk125

Yes, that is fine. I never use go run so didn't think about it.

gen2brain avatar Mar 14 '24 23:03 gen2brain

I use it almost exclusively :D, good both bases are covered, will update in a few minutes

dusk125 avatar Mar 14 '24 23:03 dusk125

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

dusk125 avatar Mar 15 '24 00:03 dusk125

Purego still doesn't work with structs (datatypes lager 64 bits). Maybe it is possible to get libffi to work with purego.

JupiterRider avatar Mar 15 '24 20:03 JupiterRider

That's the purego issue for supporting structs on linux: https://github.com/ebitengine/purego/issues/236

JupiterRider avatar Apr 29 '24 18:04 JupiterRider

@gen2brain I managed to get a prototype running with purego and libffi: Screenshot_20240501_004355

You can find the example here: https://github.com/JupiterRider/ffi/tree/main/examples/raylib

JupiterRider avatar Apr 30 '24 22:04 JupiterRider