sanette / tsdl-image

OCaml SDL2_image bindings to go with Tsdl
https://sanette.github.io/tsdl-image/
BSD 3-Clause "New" or "Revised" License
2 stars 4 forks source link

Detect system, use it to pick the right extension for the shared library #3

Closed toots closed 2 years ago

toots commented 2 years ago

The shared library name is different on macox. This PR adds a build config which allows to pick the right name.

sanette commented 2 years ago

thanks for the fix. In fact, since the ~from workaround is needed only for Toplevel (afaik), we could also add something like this

let from = if !Sys.interactive
   then Some (Dl.(dlopen ~filename:"libSDL2_image-2.0.so.0" ~flags:[RTLD_NOW]))
   else None

let foreign name typ = foreign name typ ?from

Then for any system that has yet another different filename, it should still work at least for compiled programs. (of course we can combine this with your fix)

sanette commented 2 years ago

Btw since you have a mac, could you have a look at this other config: https://github.com/sanette/tsdl-ttf/pull/1 ?

toots commented 2 years ago

thanks for the fix. In fact, since the ~from workaround is needed only for Toplevel (afaik), we could also add something like this

let from = if !Sys.interactive
   then Some (Dl.(dlopen ~filename:"libSDL2_image-2.0.so.0" ~flags:[RTLD_NOW]))
   else None

let foreign name typ = foreign name typ ?from

Then for any system that has yet another different filename, it should still work at least for compiled programs. (of course we can combine this with your fix)

As far as I can tell, the filename is also required for regular compilation, this issue is coming from a production build of https://github.com/savonet/liquidsoap/

sanette commented 2 years ago

on my system, if I ignore the ~from argument of foreign, it compiles fine, and I can link the library to other programs. I only get an error when doing #require "tsdl-image"; from the toplevel. What you are saying is that you get an error without the ~from, outside of toplevel; is this correct?

sanette commented 2 years ago

put in another way, did you have the same issue with version 0.2?

toots commented 2 years ago

on my system, if I ignore the ~from argument of foreign, it compiles fine, and I can link the library to other programs. I only get an error when doing #require "tsdl-image"; from the toplevel. What you are saying is that you get an error without the ~from, outside of toplevel; is this correct?

Confirmed. I have updated the PR to override foreign only when running as top-level!

sanette commented 2 years ago

sounds good. Thanks!

sanette commented 1 year ago
let from = if !Sys.interactive

in fact this was a bad idea of mine... If you have a "toplevel file" like this one

#use "topfind";;
#thread;;

Printf.printf "Sys.interactive = %b\n" !Sys.interactive;;

#require "tsdl-image";;

Tsdl_image.Image.(init Init.empty) |> ignore

and execute this with ocaml, the bug reappears because it is not considered interactive...

$ ocaml bug.ml
Sys.interactive = false
Exception:
Dl.DL_error
 "/home/san/.opam/4.08.1/lib/stublibs/dllctypes-foreign_stubs.so: undefined symbol: IMG_Init".
File "./bug.ml", line 1:
Error: Reference to undefined global `Tsdl_image'