Generate bindings using ctypes-stubs instead of ctypes-foreign (Minimize the use of libffi)#109
Generate bindings using ctypes-stubs instead of ctypes-foreign (Minimize the use of libffi)#109pirbo wants to merge 8 commits into
Conversation
Can you remind me exactly why we want to get rid of libffi ? I remember being mentioned that it's not supported on Windows but that's not what I read here. Something that would drive me to switch would be users of the binding complaining that
They look rather minimal. IIRC I suggested you could simply try to use
Well I suppose nothing prevents you from doing one manual (non-ctypes) binding on the side for that one no ? |
|
I'm afraid my experience with Windows is way too old to be of interest now, sorry. Back in the times I was able to code some SDL games in Windows with vanilla ocaml+cygwin, but this was not with |
|
@sanette the opinion I'm seeking is more about having ctypes-generated bindings vs libffi baked ones. |
|
Preamble: I just don't care about Windows (or MacOS or any other proprietary OS), and I even prefer if it does not work on these OS :-) Regarding my use of TSdl in Stk, I don't think the libffi performance is a problem (I did not know there was such a difference between libffi and ctypes stubs), so event if I would be interested in comparing both, I have no real opinion. As it seems like a quite big work, which could introduce bugs, I can help for testing if needed. |
|
I'm really not a specialist of this question. Theoretically, a library like SDL which focuses on performance would benefit from static ctypes stubs, so if this is feasible, that seems a good idea. However, this means also working on |
|
|
The OCaml 4.14.1 compatibility raised by @sanette is a big deal indeed! In So the preprocessor "happily" rewrites the mention of the field of |
I think so, yes, because if I try to compile |
999d648 to
2f1ab70
Compare
Should be better now. At least, works for me :) (And it is actually less changes in the build system so this is good!) |
| type _button_bind | ||
| let button_bind : _button_bind structure typ = | ||
| structure "SDL_GameControllerButtonBind" | ||
| let button_bind_bind_type = field button_bind "bindType" int | ||
| let button_bind_value1 = field button_bind "value1" int (* simplified enum *) | ||
| let button_bind_value2 = field button_bind "value2" int | ||
| let () = seal button_bind |
There was a problem hiding this comment.
This struct is defined here and not in Type_description because it contains an anonymous struct as a field (https://wiki.libsdl.org/SDL2/SDL_GameControllerButtonBind) and ctypes-stubs does not handle that...
| let framebuffer_acceleration = | ||
| F.foreign_value "SDL_HINT_FRAMEBUFFER_ACCELERATION" (array 29 char) | ||
| let idle_timer_disabled = | ||
| F.foreign_value "SDL_HINT_IDLE_TIMER_DISABLED" (array 28 char) | ||
| let mouse_focus_clickthrough = | ||
| F.foreign_value "SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH" (array 29 char) | ||
| let orientations = | ||
| F.foreign_value "SDL_HINT_ORIENTATIONS" (array 21 char) | ||
| let render_driver = | ||
| F.foreign_value "SDL_HINT_RENDER_DRIVER" (array 18 char) | ||
| let render_opengl_shaders = | ||
| F.foreign_value "SDL_HINT_RENDER_OPENGL_SHADERS" (array 26 char) | ||
| let render_logical_size_mode = | ||
| F.foreign_value "SDL_HINT_RENDER_LOGICAL_SIZE_MODE" (array 29 char) | ||
| let render_scale_quality = | ||
| F.foreign_value "SDL_HINT_RENDER_SCALE_QUALITY" (array 25 char) | ||
| let render_vsync = | ||
| F.foreign_value "SDL_HINT_RENDER_VSYNC" (array 17 char) | ||
|
|
||
| let no_signal_handlers = | ||
| F.foreign_value "SDL_HINT_NO_SIGNAL_HANDLERS" (array 23 char) | ||
| let thread_stack_size = | ||
| F.foreign_value "SDL_HINT_THREAD_STACK_SIZE" (array 22 char) | ||
| let window_frame_usable_while_cursor_hidden = | ||
| F.foreign_value | ||
| "SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN" (array 44 char) | ||
|
|
||
| let audio_resampling_mode = | ||
| F.foreign_value "SDL_HINT_AUDIO_RESAMPLING_MODE" (array 26 char) | ||
| let mouse_normal_speed_scale = | ||
| F.foreign_value "SDL_HINT_MOUSE_NORMAL_SPEED_SCALE" (array 29 char) | ||
| let mouse_relative_speed_scale = | ||
| F.foreign_value "SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE" (array 31 char) | ||
| let touch_mouse_events = | ||
| F.foreign_value "SDL_HINT_TOUCH_MOUSE_EVENTS" (array 23 char) | ||
| let mouse_touch_events = | ||
| F.foreign_value "SDL_HINT_MOUSE_TOUCH_EVENTS" (array 23 char) |
There was a problem hiding this comment.
I'm very unhappy with hardwiring the length of the strings here but I cannot find an other to bind them...
There was a problem hiding this comment.
Why dont you define a wrapper fn for F.foreign_value?
There was a problem hiding this comment.
warning: I also believe that some of your lengths are not correct.
There was a problem hiding this comment.
Sorry for the delay but I'm very interested by understanding better this because, as I said, I don't know exactly what I'm doing and should do here. What do you mean? How have you detected that?/What raised suspicions of that? (On which platform/SDL version are you? (I really hope this is not a question of having these definitions move... I'm on debian testing with SDL 2.32.2).
On my side, if I try to put a different length to any of these definitions. For example
diff --git a/src/function_description.ml b/src/function_description.ml
index 09dcff1..a142045 100644
--- a/src/function_description.ml
+++ b/src/function_description.ml
@@ -39,7 +39,7 @@ module Functions (F : FOREIGN) = struct
let render_scale_quality =
F.foreign_value "SDL_HINT_RENDER_SCALE_QUALITY" (array 25 char)
let render_vsync =
- F.foreign_value "SDL_HINT_RENDER_VSYNC" (array 17 char)
+ F.foreign_value "SDL_HINT_RENDER_VSYNC" (array 42 char)
let no_signal_handlers =
F.foreign_value "SDL_HINT_NO_SIGNAL_HANDLERS" (array 23 char)
Immediately, the compilation fails with
src/functions_stubs.c: In function ‘sdl2_stubs_17_SDL_HINT_RENDER_VSYNC’:
src/functions_stubs.c:93:22: error: initialization of ‘char (*)[42]’ from incompatible pointer type ‘char (*)[17]’ [-Wincompatible-pointer-types]
93 | char(* x38)[42] = &SDL_HINT_RENDER_VSYNC;
| ^
That's what convinced me I was kind of """safe"""...
| module Vulkan = struct | ||
| type _surface | ||
| type surface = _surface Ctypes_static.structure Ctypes_static.ptr | ||
|
|
||
| let raw_surface : _surface Ctypes_static.structure F.typ = | ||
| F.structure "VkSurfaceKHR_T" | ||
| let surface : surface F.typ = F.ptr raw_surface | ||
| end |
There was a problem hiding this comment.
Require <SDL_Vulkan.h> which makes me wonder if it is an independent .h because it's not always available and therefore should be optionally bound (how?).
did not complain that it was missing under mingw windows though... Maybe worth trying under MacOS/with msvc...
Fixed |
|
great! I made some test with |
Depends on #108
Pros:
tsdl.mliis not touched at all, so compatibility is fully preservedMeh:
You cannot do without libffi (the
~thread_registration:true ~runtime_lock:trueofin
audio_specsCons:
The direction taken by the discussion convinces me more and more to take the plunge but still, I repeat, this PR was open as a request for comments now that I've spent on these hours working on it...
For the one interested (because it was the first version of this PR description so I don't erase it but I now think that we don't care), here is my story for drowning into this mess:
ctypesis not compatible with msvc so no way to get tsdl)))gccdon't see any static use of are erased (while libffi will use them dynamically here) unless you explicitly put theno-as-neededflag which (as far as I understand) does not exist in mingw gcc/ld/...)Conclusion: See above I still don't have a successful build under mingw windows port... Not because of missing definitions this time but because of the ocaml compiler that hardwired the argument
-municodewhen calling the C compiler while SDL would need-mwindows...