Skip to content
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

Add purego support for unix #361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dusk125
Copy link

@dusk125 dusk125 commented Mar 14, 2024

No description provided.

@gen2brain
Copy link
Owner

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.

@dusk125
Copy link
Author

dusk125 commented Mar 14, 2024

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

@gen2brain
Copy link
Owner

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.

@dusk125
Copy link
Author

dusk125 commented Mar 14, 2024

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
Copy link
Author

dusk125 commented Mar 14, 2024

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`

@gen2brain
Copy link
Owner

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

@dusk125
Copy link
Author

dusk125 commented Mar 14, 2024

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

@dusk125
Copy link
Author

dusk125 commented Mar 15, 2024

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

@JupiterRider
Copy link
Contributor

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

@JupiterRider
Copy link
Contributor

That's the purego issue for supporting structs on linux:
ebitengine/purego#236

@JupiterRider
Copy link
Contributor

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants