From 2ccdcb04b34df18526d88f3e3f5119f05699ca4b Mon Sep 17 00:00:00 2001 From: Susko3 Date: Sat, 6 Apr 2024 01:31:08 +0200 Subject: [PATCH] Add platform specific generation for `SDL_main.h` and `SDL_system.h` Doesn't compile because of duplicate `_XEvent` definitions. Linux fails crossgen on Windows due to missing `endian.h` --- SDL3-CS/SDL3/ClangSharp/SDL_main.Windows.g.cs | 48 +++++++++ .../SDL3/ClangSharp/SDL_system.Android.g.cs | 101 ++++++++++++++++++ SDL3-CS/SDL3/ClangSharp/SDL_system.Linux.g.cs | 0 SDL3-CS/SDL3/ClangSharp/SDL_system.WinRT.g.cs | 68 ++++++++++++ .../SDL3/ClangSharp/SDL_system.Windows.g.cs | 71 ++++++++++++ SDL3-CS/SDL3/ClangSharp/SDL_system.iOS.g.cs | 50 +++++++++ SDL3-CS/generate_bindings.py | 53 ++++++++- 7 files changed, 389 insertions(+), 2 deletions(-) create mode 100644 SDL3-CS/SDL3/ClangSharp/SDL_main.Windows.g.cs create mode 100644 SDL3-CS/SDL3/ClangSharp/SDL_system.Android.g.cs create mode 100644 SDL3-CS/SDL3/ClangSharp/SDL_system.Linux.g.cs create mode 100644 SDL3-CS/SDL3/ClangSharp/SDL_system.WinRT.g.cs create mode 100644 SDL3-CS/SDL3/ClangSharp/SDL_system.Windows.g.cs create mode 100644 SDL3-CS/SDL3/ClangSharp/SDL_system.iOS.g.cs diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_main.Windows.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_main.Windows.g.cs new file mode 100644 index 0000000..c7cd868 --- /dev/null +++ b/SDL3-CS/SDL3/ClangSharp/SDL_main.Windows.g.cs @@ -0,0 +1,48 @@ +/* + + C# bindings for Simple DirectMedia Layer. + Original copyright notice of input files: + + Simple DirectMedia Layer + Copyright (C) 1997-2024 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace SDL +{ + public static unsafe partial class SDL3 + { + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Windows")] + public static extern int SDL_RegisterApp([NativeTypeName("const char *")] sbyte* name, [NativeTypeName("Uint32")] uint style, void* hInst); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Windows")] + public static extern void SDL_UnregisterApp(); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Windows")] + public static extern void SDL_GDKSuspendComplete(); + + [NativeTypeName("#define main SDL_main")] + public static readonly delegate* main = &SDL_main; + } +} diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_system.Android.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_system.Android.g.cs new file mode 100644 index 0000000..b28f7a1 --- /dev/null +++ b/SDL3-CS/SDL3/ClangSharp/SDL_system.Android.g.cs @@ -0,0 +1,101 @@ +/* + + C# bindings for Simple DirectMedia Layer. + Original copyright notice of input files: + + Simple DirectMedia Layer + Copyright (C) 1997-2024 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace SDL +{ + [StructLayout(LayoutKind.Explicit)] + public partial struct _XEvent + { + } + + public static unsafe partial class SDL3 + { + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Android")] + public static extern void* SDL_AndroidGetJNIEnv(); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Android")] + public static extern void* SDL_AndroidGetActivity(); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Android")] + public static extern int SDL_GetAndroidSDKVersion(); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [return: NativeTypeName("SDL_bool")] + [SupportedOSPlatform("Android")] + public static extern int SDL_IsAndroidTV(); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [return: NativeTypeName("SDL_bool")] + [SupportedOSPlatform("Android")] + public static extern int SDL_IsChromebook(); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [return: NativeTypeName("SDL_bool")] + [SupportedOSPlatform("Android")] + public static extern int SDL_IsDeXMode(); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Android")] + public static extern void SDL_AndroidBackButton(); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [return: NativeTypeName("const char *")] + [SupportedOSPlatform("Android")] + public static extern sbyte* SDL_AndroidGetInternalStoragePath(); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Android")] + public static extern int SDL_AndroidGetExternalStorageState([NativeTypeName("Uint32 *")] uint* state); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [return: NativeTypeName("const char *")] + [SupportedOSPlatform("Android")] + public static extern sbyte* SDL_AndroidGetExternalStoragePath(); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Android")] + public static extern int SDL_AndroidRequestPermission([NativeTypeName("const char *")] sbyte* permission, [NativeTypeName("SDL_AndroidRequestPermissionCallback")] delegate* unmanaged[Cdecl] cb, void* userdata); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Android")] + public static extern int SDL_AndroidShowToast([NativeTypeName("const char *")] sbyte* message, int duration, int gravity, int xoffset, int yoffset); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Android")] + public static extern int SDL_AndroidSendMessage([NativeTypeName("Uint32")] uint command, int param1); + + [NativeTypeName("#define SDL_ANDROID_EXTERNAL_STORAGE_READ 0x01")] + public const int SDL_ANDROID_EXTERNAL_STORAGE_READ = 0x01; + + [NativeTypeName("#define SDL_ANDROID_EXTERNAL_STORAGE_WRITE 0x02")] + public const int SDL_ANDROID_EXTERNAL_STORAGE_WRITE = 0x02; + } +} diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_system.Linux.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_system.Linux.g.cs new file mode 100644 index 0000000..e69de29 diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_system.WinRT.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_system.WinRT.g.cs new file mode 100644 index 0000000..1d1e376 --- /dev/null +++ b/SDL3-CS/SDL3/ClangSharp/SDL_system.WinRT.g.cs @@ -0,0 +1,68 @@ +/* + + C# bindings for Simple DirectMedia Layer. + Original copyright notice of input files: + + Simple DirectMedia Layer + Copyright (C) 1997-2024 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace SDL +{ + [StructLayout(LayoutKind.Explicit)] + public partial struct _XEvent + { + } + + public enum SDL_WinRT_Path + { + SDL_WINRT_PATH_INSTALLED_LOCATION, + SDL_WINRT_PATH_LOCAL_FOLDER, + SDL_WINRT_PATH_ROAMING_FOLDER, + SDL_WINRT_PATH_TEMP_FOLDER, + } + + public enum SDL_WinRT_DeviceFamily + { + SDL_WINRT_DEVICEFAMILY_UNKNOWN, + SDL_WINRT_DEVICEFAMILY_DESKTOP, + SDL_WINRT_DEVICEFAMILY_MOBILE, + SDL_WINRT_DEVICEFAMILY_XBOX, + } + + public static unsafe partial class SDL3 + { + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [return: NativeTypeName("const wchar_t *")] + [SupportedOSPlatform("Windows")] + public static extern ushort* SDL_WinRTGetFSPathUNICODE(SDL_WinRT_Path pathType); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [return: NativeTypeName("const char *")] + [SupportedOSPlatform("Windows")] + public static extern sbyte* SDL_WinRTGetFSPathUTF8(SDL_WinRT_Path pathType); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Windows")] + public static extern SDL_WinRT_DeviceFamily SDL_WinRTGetDeviceFamily(); + } +} diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_system.Windows.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_system.Windows.g.cs new file mode 100644 index 0000000..4808a2a --- /dev/null +++ b/SDL3-CS/SDL3/ClangSharp/SDL_system.Windows.g.cs @@ -0,0 +1,71 @@ +/* + + C# bindings for Simple DirectMedia Layer. + Original copyright notice of input files: + + Simple DirectMedia Layer + Copyright (C) 1997-2024 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace SDL +{ + public partial struct tagMSG + { + } + + [StructLayout(LayoutKind.Explicit)] + public partial struct _XEvent + { + } + + public partial struct XTaskQueueObject + { + } + + public partial struct XUser + { + } + + public static unsafe partial class SDL3 + { + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Windows")] + public static extern void SDL_SetWindowsMessageHook([NativeTypeName("SDL_WindowsMessageHook")] delegate* unmanaged[Cdecl] callback, void* userdata); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Windows")] + public static extern int SDL_Direct3D9GetAdapterIndex([NativeTypeName("SDL_DisplayID")] uint displayID); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [return: NativeTypeName("SDL_bool")] + [SupportedOSPlatform("Windows")] + public static extern int SDL_DXGIGetOutputInfo([NativeTypeName("SDL_DisplayID")] uint displayID, int* adapterIndex, int* outputIndex); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Windows")] + public static extern int SDL_GDKGetTaskQueue([NativeTypeName("XTaskQueueHandle *")] XTaskQueueObject** outTaskQueue); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("Windows")] + public static extern int SDL_GDKGetDefaultUser([NativeTypeName("XUserHandle *")] XUser** outUserHandle); + } +} diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_system.iOS.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_system.iOS.g.cs new file mode 100644 index 0000000..f031f25 --- /dev/null +++ b/SDL3-CS/SDL3/ClangSharp/SDL_system.iOS.g.cs @@ -0,0 +1,50 @@ +/* + + C# bindings for Simple DirectMedia Layer. + Original copyright notice of input files: + + Simple DirectMedia Layer + Copyright (C) 1997-2024 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace SDL +{ + [StructLayout(LayoutKind.Explicit)] + public partial struct _XEvent + { + } + + public static unsafe partial class SDL3 + { + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("iOS")] + public static extern int SDL_iPhoneSetAnimationCallback(SDL_Window* window, int interval, [NativeTypeName("void (*)(void *)")] delegate* unmanaged[Cdecl] callback, void* callbackParam); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("iOS")] + public static extern void SDL_iPhoneSetEventPump([NativeTypeName("SDL_bool")] int enabled); + + [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [SupportedOSPlatform("iOS")] + public static extern void SDL_OnApplicationDidChangeStatusBarOrientation(); + } +} diff --git a/SDL3-CS/generate_bindings.py b/SDL3-CS/generate_bindings.py index 577855d..ec09944 100644 --- a/SDL3-CS/generate_bindings.py +++ b/SDL3-CS/generate_bindings.py @@ -13,6 +13,7 @@ This script should be run manually. import json import pathlib +import re import subprocess import sys @@ -90,7 +91,6 @@ headers = [ add("SDL3/SDL_loadso.h"), add("SDL3/SDL_locale.h"), add("SDL3/SDL_log.h"), - add("SDL3/SDL_main.h"), add("SDL3/SDL_messagebox.h"), add("SDL3/SDL_metal.h"), add("SDL3/SDL_misc.h"), @@ -110,7 +110,6 @@ headers = [ add("SDL3/SDL_stdinc.h"), add("SDL3/SDL_storage.h"), add("SDL3/SDL_surface.h"), - add("SDL3/SDL_system.h"), add("SDL3/SDL_thread.h"), add("SDL3/SDL_time.h"), add("SDL3/SDL_timer.h"), @@ -194,6 +193,43 @@ def run_clangsharp(command, header: Header): return header.output_file() +# regex for ClangSharp-generated SDL functions +generated_function_regex = re.compile(r"public static extern \w+\** (SDL_\w+)\(") + + +def get_generated_functions(file): + with open(file, "r", encoding="utf-8") as f: + for match in generated_function_regex.finditer(f.read()): + yield match.group(1) + + +def generate_platform_specific_headers(sdl_api, header: Header, platforms): + all_functions = list(all_funcs_from_header(sdl_api, header)) + + print(f"💠 {header} platform agnostic") + platform_agnostic_cs = run_clangsharp(base_command, header) + platform_agnostic_functions = list(get_generated_functions(platform_agnostic_cs)) + output_files = [platform_agnostic_cs] + + for (defines, suffix, platform_name) in platforms: + command = base_command + ["--define-macro"] + defines + + if platform_agnostic_functions: + command.append("--exclude") + command.extend(platform_agnostic_functions) + + if all_functions: + command.append("--with-attribute") + for f in all_functions: + command.append(f'{f["name"]}=SupportedOSPlatform("{platform_name}")') + + print(f"💠 {header} for {suffix}") + header.output_suffix = suffix + output_files.append(run_clangsharp(command, header)) + + check_generated_functions(sdl_api, header, output_files) + + def main(): sdl_api = get_sdl_api_dump() @@ -201,6 +237,19 @@ def main(): output_file = run_clangsharp(base_command, header) check_generated_functions(sdl_api, header, [output_file]) + generate_platform_specific_headers(sdl_api, add("SDL3/SDL_main.h"), [ + (["SDL_PLATFORM_WIN32", "SDL_PLATFORM_WINGDK"], "Windows", "Windows"), + ]) + + generate_platform_specific_headers(sdl_api, add("SDL3/SDL_system.h"), [ + # define macro, output_suffix, [SupportedOSPlatform] + (["SDL_PLATFORM_ANDROID"], "Android", "Android"), + (["SDL_PLATFORM_IOS"], "iOS", "iOS"), + (["SDL_PLATFORM_LINUX"], "Linux", "Linux"), + (["SDL_PLATFORM_WIN32", "SDL_PLATFORM_WINGDK"], "Windows", "Windows"), + (["SDL_PLATFORM_WINRT"], "WinRT", "Windows"), + ]) + if __name__ == "__main__": main()