From 5227b62690f268ec9bc2a55ac3c2006a49e912e0 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Tue, 22 Oct 2024 17:52:46 +0900 Subject: [PATCH 01/13] Add dockerfile + generation wrapper script --- Dockerfile | 11 +++++++++++ SDL3-CS/SDL3-CS.csproj | 10 +++++++++- SDL3-CS/SDL3/SDL_stdinc.rsp | 1 + SDL3-CS/generate_bindings.py | 15 +++++++++++++++ SDL3-CS/generate_bindings.sh | 14 ++++++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 Dockerfile create mode 100755 SDL3-CS/generate_bindings.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..62e25d1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM ubuntu:24.04 + +RUN apt-get update && apt-get install -y \ + dotnet-sdk-8.0 \ + python3 \ + git \ + build-essential + +RUN ln -s /usr/bin/python3 /usr/bin/python + +SHELL ["/bin/bash", "-c"] \ No newline at end of file diff --git a/SDL3-CS/SDL3-CS.csproj b/SDL3-CS/SDL3-CS.csproj index 04e55fd..dd15bdd 100644 --- a/SDL3-CS/SDL3-CS.csproj +++ b/SDL3-CS/SDL3-CS.csproj @@ -25,7 +25,15 @@ - + + + + all + + + + all + diff --git a/SDL3-CS/SDL3/SDL_stdinc.rsp b/SDL3-CS/SDL3/SDL_stdinc.rsp index 375011b..be9a12d 100644 --- a/SDL3-CS/SDL3/SDL_stdinc.rsp +++ b/SDL3-CS/SDL3/SDL_stdinc.rsp @@ -6,6 +6,7 @@ SDL_SLOW_MEMCPY SDL_SLOW_MEMMOVE SDL_SLOW_MEMSET +SDL_DISABLE_ALLOCA # Prevent SDL from using compiler intrinsics __builtin_mul_overflow and __builtin_add_overflow. --additional diff --git a/SDL3-CS/generate_bindings.py b/SDL3-CS/generate_bindings.py index fa06475..5b888e1 100644 --- a/SDL3-CS/generate_bindings.py +++ b/SDL3-CS/generate_bindings.py @@ -260,12 +260,27 @@ base_command = [ "char=byte", "wchar_t *=IntPtr", # wchar_t has a platform-defined size "bool=SDLBool", # treat bool as C# helper type + "__va_list=byte*", "--define-macro", "SDL_FUNCTION_POINTER_IS_VOID_POINTER", + "SDL_DECLSPEC=", # Not supported by llvm + # Undefine platform-specific macros - these will be defined on a per-case basis later. "--additional", "--undefine-macro=_WIN32", + "--undefine-macro=linux", + "--undefine-macro=__linux", + "--undefine-macro=__linux__", + "--undefine-macro=unix", + "--undefine-macro=__unix", + "--undefine-macro=__unix__", + "--undefine-macro=__APPLE__", + # GCC and LLVM use `long int` => int64, whereas MSVC uses `long long int` => int64. + # In terms of C# code gen, it's more accurate if we disable these to force LL, so + # that they're transformed to longs instead of ints. + "--undefine-macro=__LP64__", + "--undefine-macro=_LP64", ] diff --git a/SDL3-CS/generate_bindings.sh b/SDL3-CS/generate_bindings.sh new file mode 100755 index 0000000..0bb0ff4 --- /dev/null +++ b/SDL3-CS/generate_bindings.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -eu +pushd "$(dirname "$0")/../" >/dev/null + +dotnet tool restore +dotnet restore --ucr SDL3-CS/SDL3-CS.csproj + +export LD_LIBRARY_PATH="$(echo ~/.nuget/packages/libclang.runtime.*/*/runtimes/*/native):$(echo ~/.nuget/packages/libclangsharp.runtime.*/*/runtimes/*/native):${LD_LIBRARY_PATH:-}" +export CPLUS_INCLUDE_PATH="$(echo /usr/lib/gcc/*/*/include):/usr/include:${CPLUS_INCLUDE_PATH:-}" + +python3 SDL3-CS/generate_bindings.py + +popd >/dev/null From a64cdd9e4f409cfcd30722bb95f9bb6cc44a1f44 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Tue, 22 Oct 2024 18:10:10 +0900 Subject: [PATCH 02/13] Fix SDL_thread generation --- SDL3-CS/SDL3/ClangSharp/SDL_thread.g.cs | 6 ------ SDL3-CS/SDL3/SDL_thread.rsp | 4 ++++ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_thread.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_thread.g.cs index 49b61b3..6ba1b5f 100644 --- a/SDL3-CS/SDL3/ClangSharp/SDL_thread.g.cs +++ b/SDL3-CS/SDL3/ClangSharp/SDL_thread.g.cs @@ -79,12 +79,6 @@ namespace SDL [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] public static extern void SDL_CleanupTLS(); - [NativeTypeName("#define SDL_BeginThreadFunction NULL")] - public const int SDL_BeginThreadFunction = 0; - - [NativeTypeName("#define SDL_EndThreadFunction NULL")] - public const int SDL_EndThreadFunction = 0; - [NativeTypeName("#define SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER \"SDL.thread.create.entry_function\"")] public static ReadOnlySpan SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER => "SDL.thread.create.entry_function"u8; diff --git a/SDL3-CS/SDL3/SDL_thread.rsp b/SDL3-CS/SDL3/SDL_thread.rsp index 6b497c5..49e043b 100644 --- a/SDL3-CS/SDL3/SDL_thread.rsp +++ b/SDL3-CS/SDL3/SDL_thread.rsp @@ -8,3 +8,7 @@ SDL_CreateThread=System.Runtime.Versioning --with-attribute SDL_CreateThread=UnsupportedOSPlatform("windows") SDL_CreateThreadWithStackSize=UnsupportedOSPlatform("windows") + +--exclude +SDL_BeginThreadFunction +SDL_EndThreadFunction \ No newline at end of file From c991b5bd061a44e44d4697fc767bebf4bbf84cf3 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Tue, 22 Oct 2024 19:42:16 +0900 Subject: [PATCH 03/13] Forward args to python script Co-authored-by: Susko3 --- SDL3-CS/generate_bindings.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDL3-CS/generate_bindings.sh b/SDL3-CS/generate_bindings.sh index 0bb0ff4..a24ee04 100755 --- a/SDL3-CS/generate_bindings.sh +++ b/SDL3-CS/generate_bindings.sh @@ -9,6 +9,6 @@ dotnet restore --ucr SDL3-CS/SDL3-CS.csproj export LD_LIBRARY_PATH="$(echo ~/.nuget/packages/libclang.runtime.*/*/runtimes/*/native):$(echo ~/.nuget/packages/libclangsharp.runtime.*/*/runtimes/*/native):${LD_LIBRARY_PATH:-}" export CPLUS_INCLUDE_PATH="$(echo /usr/lib/gcc/*/*/include):/usr/include:${CPLUS_INCLUDE_PATH:-}" -python3 SDL3-CS/generate_bindings.py +python3 SDL3-CS/generate_bindings.py "$@" popd >/dev/null From 140e73678dc5791b2ec3a6d9cfbf5c686a2f977d Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Wed, 23 Oct 2024 17:03:09 +0900 Subject: [PATCH 04/13] Fix va_list for another platform --- SDL3-CS/generate_bindings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/SDL3-CS/generate_bindings.py b/SDL3-CS/generate_bindings.py index 5b888e1..5552f54 100644 --- a/SDL3-CS/generate_bindings.py +++ b/SDL3-CS/generate_bindings.py @@ -261,6 +261,7 @@ base_command = [ "wchar_t *=IntPtr", # wchar_t has a platform-defined size "bool=SDLBool", # treat bool as C# helper type "__va_list=byte*", + "__va_list_tag=byte", "--define-macro", "SDL_FUNCTION_POINTER_IS_VOID_POINTER", From ed43193103faaef258fbf935edef1fdc8db166aa Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Wed, 23 Oct 2024 17:03:25 +0900 Subject: [PATCH 05/13] Force `int` as base type --- SDL3-CS/generate_bindings.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/SDL3-CS/generate_bindings.py b/SDL3-CS/generate_bindings.py index 5552f54..c025ecc 100644 --- a/SDL3-CS/generate_bindings.py +++ b/SDL3-CS/generate_bindings.py @@ -263,6 +263,12 @@ base_command = [ "__va_list=byte*", "__va_list_tag=byte", + "--nativeTypeNamesToStrip", + "unsigned int", + + "--with-type", + "*=int", # all types should be ints by default + "--define-macro", "SDL_FUNCTION_POINTER_IS_VOID_POINTER", "SDL_DECLSPEC=", # Not supported by llvm From 520ae0ebc7eb383b8d2e26ebeff7c33dd33a072d Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Thu, 24 Oct 2024 16:26:44 +0900 Subject: [PATCH 06/13] Remove duplicate definition --- SDL3-CS/generate_bindings.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/SDL3-CS/generate_bindings.py b/SDL3-CS/generate_bindings.py index 54dbd48..d6ea2c1 100644 --- a/SDL3-CS/generate_bindings.py +++ b/SDL3-CS/generate_bindings.py @@ -263,9 +263,6 @@ base_command = [ "__va_list=byte*", "__va_list_tag=byte", - "--nativeTypeNamesToStrip", - "unsigned int", - "--with-type", "*=int", # all types should be ints by default From 69c7e23842d441b058b5624472752d7dcd7272df Mon Sep 17 00:00:00 2001 From: Susko3 Date: Thu, 24 Oct 2024 10:44:19 +0100 Subject: [PATCH 07/13] Fix 64-bit integers on `x86_64-linux-gnu` The __LP64__ macros work fine on aarch64, but they don't seem to do much on x86_64 --- SDL3-CS/generate_bindings.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/SDL3-CS/generate_bindings.py b/SDL3-CS/generate_bindings.py index d6ea2c1..004ff66 100644 --- a/SDL3-CS/generate_bindings.py +++ b/SDL3-CS/generate_bindings.py @@ -262,9 +262,11 @@ base_command = [ "bool=SDLBool", # treat bool as C# helper type "__va_list=byte*", "__va_list_tag=byte", + "Sint64=long", + "Uint64=ulong", "--with-type", - "*=int", # all types should be ints by default + "*=int", # all enum types should be ints by default "--nativeTypeNamesToStrip", "unsigned int", @@ -273,7 +275,7 @@ base_command = [ "SDL_FUNCTION_POINTER_IS_VOID_POINTER", "SDL_SINT64_C(c)=c ## LL", "SDL_UINT64_C(c)=c ## ULL", - "SDL_DECLSPEC=", # Not supported by llvm + "SDL_DECLSPEC=", # Not supported by llvm # Undefine platform-specific macros - these will be defined on a per-case basis later. "--additional", From 04c71afe675a6e9724c5d6e6b91db44f75b72ae1 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Thu, 24 Oct 2024 19:19:00 +0900 Subject: [PATCH 08/13] Remove now unnecessary undefines --- SDL3-CS/generate_bindings.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/SDL3-CS/generate_bindings.py b/SDL3-CS/generate_bindings.py index 004ff66..317f020 100644 --- a/SDL3-CS/generate_bindings.py +++ b/SDL3-CS/generate_bindings.py @@ -287,11 +287,6 @@ base_command = [ "--undefine-macro=__unix", "--undefine-macro=__unix__", "--undefine-macro=__APPLE__", - # GCC and LLVM use `long int` => int64, whereas MSVC uses `long long int` => int64. - # In terms of C# code gen, it's more accurate if we disable these to force LL, so - # that they're transformed to longs instead of ints. - "--undefine-macro=__LP64__", - "--undefine-macro=_LP64", ] From dcea5f47c3a4e225c1cd62e5a274352fccda23f1 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Thu, 24 Oct 2024 19:34:51 +0900 Subject: [PATCH 09/13] Hack around missing process.h on Unix systems Co-authored-by: Susko3 --- SDL3-CS/generate_bindings.py | 6 ++++++ SDL3-CS/include/process.h | 1 + 2 files changed, 7 insertions(+) create mode 100644 SDL3-CS/include/process.h diff --git a/SDL3-CS/generate_bindings.py b/SDL3-CS/generate_bindings.py index 317f020..d855256 100644 --- a/SDL3-CS/generate_bindings.py +++ b/SDL3-CS/generate_bindings.py @@ -24,6 +24,7 @@ Example: import json import pathlib +import platform import re import subprocess import sys @@ -362,6 +363,11 @@ def should_skip(solo_headers: list[Header], header: Header): def main(): solo_headers = [make_header_fuzzy(header_name) for header_name in sys.argv[1:]] + if platform.system() != "Windows": + base_command.extend([ + "--include-directory", csproj_root / "include" + ]) + prepare_sdl_source() sdl_api = get_sdl_api_dump() diff --git a/SDL3-CS/include/process.h b/SDL3-CS/include/process.h new file mode 100644 index 0000000..9791d02 --- /dev/null +++ b/SDL3-CS/include/process.h @@ -0,0 +1 @@ +// dummy process.h when building for SDL_PLATFORM_WINDOWS on non-windows systems From a7f3fe450ddd207bd158a995d208fcf96b7961a8 Mon Sep 17 00:00:00 2001 From: Susko3 Date: Thu, 24 Oct 2024 12:04:00 +0100 Subject: [PATCH 10/13] Generate bindings on Linux This will be the canonical representation going forward. --- SDL3-CS/SDL3/ClangSharp/SDL_audio.g.cs | 1 - SDL3-CS/SDL3/ClangSharp/SDL_pixels.g.cs | 2 -- 2 files changed, 3 deletions(-) diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_audio.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_audio.g.cs index addb1b8..de63476 100644 --- a/SDL3-CS/SDL3/ClangSharp/SDL_audio.g.cs +++ b/SDL3-CS/SDL3/ClangSharp/SDL_audio.g.cs @@ -28,7 +28,6 @@ using System.Runtime.InteropServices; namespace SDL { - [NativeTypeName("int")] public enum SDL_AudioFormat : uint { SDL_AUDIO_UNKNOWN = 0x0000U, diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_pixels.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_pixels.g.cs index b963cec..aae40df 100644 --- a/SDL3-CS/SDL3/ClangSharp/SDL_pixels.g.cs +++ b/SDL3-CS/SDL3/ClangSharp/SDL_pixels.g.cs @@ -89,7 +89,6 @@ namespace SDL SDL_PACKEDLAYOUT_1010102, } - [NativeTypeName("int")] public enum SDL_PixelFormat : uint { SDL_PIXELFORMAT_UNKNOWN = 0, @@ -240,7 +239,6 @@ namespace SDL SDL_CHROMA_LOCATION_TOPLEFT = 3, } - [NativeTypeName("int")] public enum SDL_Colorspace : uint { SDL_COLORSPACE_UNKNOWN = 0, From f46a595008682eedfbc78357af50bf02f5bb73ba Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Mon, 28 Oct 2024 20:23:25 +0900 Subject: [PATCH 11/13] Improve Dockerfile --- Dockerfile | 23 ++++++++++++++++------- External/.dockerignore | 0 SDL3-CS/generate_bindings.sh | 10 +++++----- 3 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 External/.dockerignore diff --git a/Dockerfile b/Dockerfile index 62e25d1..a3103eb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,20 @@ FROM ubuntu:24.04 -RUN apt-get update && apt-get install -y \ - dotnet-sdk-8.0 \ - python3 \ - git \ - build-essential +RUN apt-get update && \ + apt-get install -y dotnet-sdk-8.0 python3 git build-essential && \ + ln -s /usr/bin/python3 /usr/bin/python -RUN ln -s /usr/bin/python3 /usr/bin/python +WORKDIR /app +COPY . . +RUN dotnet tool restore && \ + dotnet restore --ucr SDL3-CS/SDL3-CS.csproj && \ + rm -r /app -SHELL ["/bin/bash", "-c"] \ No newline at end of file +WORKDIR / +RUN echo '#!/bin/bash' >> entrypoint.sh && \ + echo 'export LD_LIBRARY_PATH="$(echo ~/.nuget/packages/libclang.runtime.*/*/runtimes/*/native):$(echo ~/.nuget/packages/libclangsharp.runtime.*/*/runtimes/*/native):${LD_LIBRARY_PATH:-}"' >> entrypoint.sh && \ + echo 'export CPLUS_INCLUDE_PATH="$(echo /usr/lib/gcc/*/*/include):/usr/include:${CPLUS_INCLUDE_PATH:-}"' >> entrypoint.sh && \ + echo 'python3 SDL3-CS/generate_bindings.py "$@"' >> entrypoint.sh && \ + chmod +x entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file diff --git a/External/.dockerignore b/External/.dockerignore new file mode 100644 index 0000000..e69de29 diff --git a/SDL3-CS/generate_bindings.sh b/SDL3-CS/generate_bindings.sh index a24ee04..009e19a 100755 --- a/SDL3-CS/generate_bindings.sh +++ b/SDL3-CS/generate_bindings.sh @@ -3,11 +3,11 @@ set -eu pushd "$(dirname "$0")/../" >/dev/null -dotnet tool restore -dotnet restore --ucr SDL3-CS/SDL3-CS.csproj - -export LD_LIBRARY_PATH="$(echo ~/.nuget/packages/libclang.runtime.*/*/runtimes/*/native):$(echo ~/.nuget/packages/libclangsharp.runtime.*/*/runtimes/*/native):${LD_LIBRARY_PATH:-}" -export CPLUS_INCLUDE_PATH="$(echo /usr/lib/gcc/*/*/include):/usr/include:${CPLUS_INCLUDE_PATH:-}" +#dotnet tool restore +#dotnet restore --ucr SDL3-CS/SDL3-CS.csproj +# +#export LD_LIBRARY_PATH="$(echo ~/.nuget/packages/libclang.runtime.*/*/runtimes/*/native):$(echo ~/.nuget/packages/libclangsharp.runtime.*/*/runtimes/*/native):${LD_LIBRARY_PATH:-}" +#export CPLUS_INCLUDE_PATH="$(echo /usr/lib/gcc/*/*/include):/usr/include:${CPLUS_INCLUDE_PATH:-}" python3 SDL3-CS/generate_bindings.py "$@" From 81e5c4cb7f27063b4991de2fee2be11a6f1c15d9 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Mon, 28 Oct 2024 20:34:10 +0900 Subject: [PATCH 12/13] Remove unused script --- SDL3-CS/generate_bindings.sh | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100755 SDL3-CS/generate_bindings.sh diff --git a/SDL3-CS/generate_bindings.sh b/SDL3-CS/generate_bindings.sh deleted file mode 100755 index 009e19a..0000000 --- a/SDL3-CS/generate_bindings.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -set -eu -pushd "$(dirname "$0")/../" >/dev/null - -#dotnet tool restore -#dotnet restore --ucr SDL3-CS/SDL3-CS.csproj -# -#export LD_LIBRARY_PATH="$(echo ~/.nuget/packages/libclang.runtime.*/*/runtimes/*/native):$(echo ~/.nuget/packages/libclangsharp.runtime.*/*/runtimes/*/native):${LD_LIBRARY_PATH:-}" -#export CPLUS_INCLUDE_PATH="$(echo /usr/lib/gcc/*/*/include):/usr/include:${CPLUS_INCLUDE_PATH:-}" - -python3 SDL3-CS/generate_bindings.py "$@" - -popd >/dev/null From 22c6c797011c0b1a9cef0a9357068f9304c72a32 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Mon, 28 Oct 2024 21:47:31 +0900 Subject: [PATCH 13/13] Fix up caching + remove python3 symlink --- Dockerfile | 11 +++++------ External/.dockerignore | 0 2 files changed, 5 insertions(+), 6 deletions(-) delete mode 100644 External/.dockerignore diff --git a/Dockerfile b/Dockerfile index a3103eb..38286b4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,13 @@ FROM ubuntu:24.04 RUN apt-get update && \ - apt-get install -y dotnet-sdk-8.0 python3 git build-essential && \ - ln -s /usr/bin/python3 /usr/bin/python + apt-get install -y dotnet-sdk-8.0 python3 git build-essential -WORKDIR /app -COPY . . +WORKDIR /tmp +COPY .config/dotnet-tools.json .config/dotnet-tools.json +COPY SDL3-CS/SDL3-CS.csproj SDL3-CS/SDL3-CS.csproj RUN dotnet tool restore && \ - dotnet restore --ucr SDL3-CS/SDL3-CS.csproj && \ - rm -r /app + dotnet restore --ucr SDL3-CS/SDL3-CS.csproj WORKDIR / RUN echo '#!/bin/bash' >> entrypoint.sh && \ diff --git a/External/.dockerignore b/External/.dockerignore deleted file mode 100644 index e69de29..0000000