Trying to install tsdl on windows

I’m trying to have tsdl package installed on windows.

$ opam --version
2.3.0

$ opam switch
#  switch   compiler                                                                        description
→  default  arch-x86_64.1,ocaml-base-compiler.5.2.1,ocaml-options-vanilla.1,system-mingw.1  ocaml >= 4.05.0

As usual with OCaml packages, everything becomes difficult when installing/linking foreign language code; SDL2 library, in this case.

According to tsdl package (opam show tsdl):

Tsdl depends on the [SDL 2.0.10][sdl] C library (or later)

I later discovered in tsdl changelog that we need 2.0.18:

$ head CHANGES.md | ag "SDL "
Require SDL >= 2.0.18.

TODO: PR against tsdl opam file

So, I’m going to try SDL 2.0.18. Unfortunately, I can’t use conf-sdl2 package without modification: it relies on depext, but cygwin package is an older version.

So, as an experiment, I’m going manually. I wonder wether if we could have conf-sdl2 (or another package) do the same thing?

SIDE STEP: because conf-wget depext is not defined for cygwin:

$ opam source conf-wget
$ cd conf-wget.1
$ cat <<FIN > wget.patch
> --- opam        2024-12-25 10:07:28.339965700 +0100
+++ opam.new    2024-12-24 17:09:48.385631000 +0100
@@ -23,4 +23,5 @@
   ["wget"] {os-distribution = "alpine"}
   ["wget"] {os-distribution = "centos"}
   ["wget"] {os = "freebsd"}
+  ["wget"] {os-distribution = "cygwin"}
 ]
FIN
$ patch < wget.patch
$ opam install .

Note: conf-wget PR submitted as Add depext for cygwin to conf-wget by ttamttam · Pull Request #27176 · ocaml/opam-repository · GitHub.

Back to SDL2. Not sure why this patch is needed?

$ opam source conf-sdl2
$ cd conf-sdl2.1
$ wget https://github.com/libsdl-org/SDL/releases/download/release-2.0.18/SDL2-devel-2.0.18-mingw.tar.gz
$ sha256sum SDL2-devel-2.0.18-mingw.tar.gz
bbad7c6947f6ca3e05292f065852ed8b62f319fc5533047e7708769c4dbae394 *SDL2-devel-2.0.18-mingw.tar.gz
$ tar xzf SDL2-devel-2.0.18-mingw.tar.gz
$ cd SDL2-2.0.18
$ cat <<FIN > pkgconfig.patch
--- x86_64-w64-mingw32/lib/pkgconfig/sdl2.pc    2021-11-30 05:24:06.000000000 +0100
+++ /usr/local/x86_64-w64-mingw32/lib/pkgconfig/sdl2.pc 2024-12-25 10:59:53.557284700 +0100
@@ -10,6 +10,6 @@
 Version: 2.0.18
 Requires:
 Conflicts:
-Libs: -L\${libdir}  -lmingw32 -lSDL2main -lSDL2 -mwindows 
+Libs: -L\${libdir}  -lSDL2
 Libs.private:  -Wl,--dynamicbase -Wl,--nxcompat -Wl,--high-entropy-va -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid
 Cflags: -I\${includedir}/SDL2  -Dmain=SDL_main
FIN
$ patch -p0 < pkgconfig.patch
$ mkdir -p /usr/local/x86_64-w64-mingw32
$ make cross

Now, tsdl:

$ opam source tsdl
$ cd tsdl.1.1.0/
$ opam install ocamlfind ocamlbuild topkg ctypes ctypes-foreign conf-pkg-config -y

Unfortunately, conf-pkg-config installs pkgconf.exe. But tsdl expects pkg-config.exe.

$ ln -s "$(dirname $(which pkgconf.exe))/pkgconf.exe" "$(dirname $(which pkgconf.exe))/pkg-config.exe"

Actual tsdl compilation:

$ ocaml pkg/pkg.ml build --tests true

Running tests one by one.

  • test_audio: I don’t know how to interrupt this test. When I close the example’s window, the `Quit event is catched, but Sdl.quit(); does not return
  • test_tsdl: needs some adjustments (because of warnings “-27” and “-32”, mainly). And fails in some places:
    • Getting ‘Unknown sensor type’ error just after Sdl.init Sdl.Init.everything.
    • assert (Sdl.get_clipboard_text () = Ok "öpooo"); fails. But in a strange way: I was able to retrieve and log the expected string. So, seems like it is a multithreading/synchronisation error?

Too many issues. How could we solve them, step by step?

Best regards

Matt

Hello, If you have difficulties to compile tsdl on windows, maybe you could try ocamlsdl2.
This other package doesn’t have other dependencies than ocaml, and sdl2. It uses the default ffi from ocaml, not ctypes.
And if you group all your sdl2 calls inside a display callback, you can easily rewrite it later when you get tsdl working.
Personally, I had no difficulties to compile “ocamlsdl2” on windows.
The only issue I got with ocamlsdl2 is that I can not access the joypad on crostini, but this a probably an issue with sdl2, so we will probably get the same issue with any bindings.
But if you really prefer the tsdl interface, we could also imagine to fork “ocamlsdl2” as “ocamlsdl2t” where we rewrite the interface with tsdl function names, in hope to get compatible code.