Manually define broken constants

This commit is contained in:
Susko3 2024-04-06 13:32:07 +02:00
parent a4ecfd1c75
commit d2bc4e7605
6 changed files with 52 additions and 9 deletions

View File

@ -0,0 +1,17 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Diagnostics;
namespace SDL
{
/// <summary>
/// Denotes a manually defined constant.
/// Such consants should be excluded from ClangSharp generation to prevent warnings or duplicate definitions.
/// Handled by <c>get_manually_written_symbols()</c> in generate_bindings.py.
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
[Conditional("NEVER")]
public class ConstantAttribute : Attribute;
}

View File

@ -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);

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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()