diff --git a/SDL3-CS/ConstantAttribute.cs b/SDL3-CS/ConstantAttribute.cs new file mode 100644 index 0000000..9dce031 --- /dev/null +++ b/SDL3-CS/ConstantAttribute.cs @@ -0,0 +1,17 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Diagnostics; + +namespace SDL +{ + /// + /// Denotes a manually defined constant. + /// Such consants should be excluded from ClangSharp generation to prevent warnings or duplicate definitions. + /// Handled by get_manually_written_symbols() in generate_bindings.py. + /// + [AttributeUsage(AttributeTargets.Field)] + [Conditional("NEVER")] + public class ConstantAttribute : Attribute; +} diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_pen.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_pen.g.cs index effec96..a2dabb4 100644 --- a/SDL3-CS/SDL3/ClangSharp/SDL_pen.g.cs +++ b/SDL3-CS/SDL3/ClangSharp/SDL_pen.g.cs @@ -94,9 +94,6 @@ namespace SDL [NativeTypeName("#define SDL_PEN_INVALID ((SDL_PenID)0)")] public const SDL_PenID SDL_PEN_INVALID = ((SDL_PenID)(0)); - [NativeTypeName("#define SDL_PEN_MOUSEID ((SDL_MouseID)-2)")] - public const SDL_MouseID SDL_PEN_MOUSEID = ((SDL_MouseID)(-2)); - [NativeTypeName("#define SDL_PEN_INFO_UNKNOWN (-1)")] public const int SDL_PEN_INFO_UNKNOWN = (-1); diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_touch.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_touch.g.cs index 5aee4bb..777f015 100644 --- a/SDL3-CS/SDL3/ClangSharp/SDL_touch.g.cs +++ b/SDL3-CS/SDL3/ClangSharp/SDL_touch.g.cs @@ -63,11 +63,5 @@ namespace SDL [DllImport("SDL3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] public static extern SDL_Finger* SDL_GetTouchFinger(SDL_TouchID touchID, int index); - - [NativeTypeName("#define SDL_TOUCH_MOUSEID ((SDL_MouseID)-1)")] - public const SDL_MouseID SDL_TOUCH_MOUSEID = ((SDL_MouseID)(-1)); - - [NativeTypeName("#define SDL_MOUSE_TOUCHID ((SDL_TouchID)-1)")] - public const SDL_TouchID SDL_MOUSE_TOUCHID = ((SDL_TouchID)(-1)); } } diff --git a/SDL3-CS/SDL3/SDL_pen.cs b/SDL3-CS/SDL3/SDL_pen.cs index 2848637..ec3da99 100644 --- a/SDL3-CS/SDL3/SDL_pen.cs +++ b/SDL3-CS/SDL3/SDL_pen.cs @@ -7,4 +7,10 @@ namespace SDL { [Typedef] public enum SDL_PenID : UInt32; + + public static partial class SDL3 + { + [Constant] + public const SDL_MouseID SDL_PEN_MOUSEID = unchecked((SDL_MouseID)(-2)); + } } diff --git a/SDL3-CS/SDL3/SDL_touch.cs b/SDL3-CS/SDL3/SDL_touch.cs index d3f473e..b07724f 100644 --- a/SDL3-CS/SDL3/SDL_touch.cs +++ b/SDL3-CS/SDL3/SDL_touch.cs @@ -10,4 +10,13 @@ namespace SDL [Typedef] public enum SDL_TouchID : UInt64; + + public static partial class SDL3 + { + [Constant] + public const SDL_MouseID SDL_TOUCH_MOUSEID = unchecked((SDL_MouseID)(-1)); + + [Constant] + public const SDL_TouchID SDL_MOUSE_TOUCHID = unchecked((SDL_TouchID)(-1)); + } } diff --git a/SDL3-CS/generate_bindings.py b/SDL3-CS/generate_bindings.py index 8e34780..834f085 100644 --- a/SDL3-CS/generate_bindings.py +++ b/SDL3-CS/generate_bindings.py @@ -166,6 +166,21 @@ def check_generated_functions(sdl_api, header, generated_file_paths): print(f"[⚠️ Warning] Function {name} not found in generated files:", *generated_file_paths) +defined_constant_regex = re.compile(r"\[Constant]\s*public (const|static readonly) \w+ (SDL_\w+) = ", re.MULTILINE) + + +def get_manually_written_symbols(header): + """Returns symbols names whose definitions are manually written in C#.""" + cs_file = header.cs_file() + if cs_file.is_file(): + with open(cs_file, "r", encoding="utf-8") as f: + text = f.read() + for match in defined_constant_regex.finditer(text): + m = match.group(2) + assert m.startswith("SDL_") + yield m + + typedef_enum_regex = re.compile(r"\[Typedef]\s*public enum (SDL_\w+)", re.MULTILINE) @@ -217,6 +232,11 @@ def run_clangsharp(command, header: Header): if rsp.is_file(): cmd.append(f"@{rsp}") + to_exclude = list(get_manually_written_symbols(header)) + if to_exclude: + cmd.append("--exclude") + cmd.extend(to_exclude) + subprocess.run(cmd) return header.output_file()