Skip to content

Add purego support for unix#361

Open
dusk125 wants to merge 1 commit into
gen2brain:masterfrom
dusk125:purego-linux
Open

Add purego support for unix#361
dusk125 wants to merge 1 commit into
gen2brain:masterfrom
dusk125:purego-linux

Conversation

@dusk125

@dusk125 dusk125 commented Mar 14, 2024

Copy link
Copy Markdown

No description provided.

@gen2brain

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

dusk125 commented Mar 14, 2024

Copy link
Copy Markdown
Author

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
Copy Markdown
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

dusk125 commented Mar 14, 2024

Copy link
Copy Markdown
Author

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

dusk125 commented Mar 14, 2024

Copy link
Copy Markdown
Author

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
Copy Markdown
Owner

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

@dusk125

dusk125 commented Mar 14, 2024

Copy link
Copy Markdown
Author

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

@dusk125

dusk125 commented Mar 15, 2024

Copy link
Copy Markdown
Author

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
Copy Markdown
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
Copy Markdown
Contributor

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

@JupiterRider

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

@BrownNPC

BrownNPC commented Apr 4, 2025

Copy link
Copy Markdown
Contributor

Hey, is there any ETA on this being merged? thanks.

@JupiterRider

Copy link
Copy Markdown
Contributor

Hey, is there any ETA on this being merged? thanks.

Hey @BrownNPC ,

this PR cannot be merged, because it doesn't work.

@BrownNPC

Copy link
Copy Markdown
Contributor

Can be closed since it's made obsolete by #544

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.

4 participants