split up sdl libraries into separate projects

This commit is contained in:
WizzardMaker 2024-12-03 13:32:37 +01:00
parent af476b4cc4
commit 52c2c1b31e
13 changed files with 294 additions and 32 deletions

View File

@ -3,6 +3,8 @@
"path": "SDL3-CS.sln",
"projects": [
"SDL3-CS\\SDL3-CS.csproj",
"SDL3_ttf-CS\\SDL3-CS_ttf.csproj",
"SDL3_image-CS\\SDL3_image-CS.csproj",
"SDL3-CS.SourceGeneration\\SDL3-CS.SourceGeneration.csproj",
"SDL3-CS.Android\\SDL3-CS.Android.csproj",
"SDL3-CS.Tests\\SDL3-CS.Tests.csproj",

View File

@ -3,6 +3,8 @@
"path": "SDL3-CS.sln",
"projects": [
"SDL3-CS\\SDL3-CS.csproj",
"SDL3_ttf-CS\\SDL3-CS_ttf.csproj",
"SDL3_image-CS\\SDL3_image-CS.csproj",
"SDL3-CS.SourceGeneration\\SDL3-CS.SourceGeneration.csproj",
"SDL3-CS.Tests\\SDL3-CS.Tests.csproj",
"SDL3-CS.Tests.Desktop\\SDL3-CS.Tests.Desktop.csproj"

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
@ -32,9 +33,14 @@ using System;
foreach (var kvp in finder.Methods)
{
if (kvp.Value.Count == 0)
return;
string filename = kvp.Key;
var foundMethods = kvp.Value;
string className = ClassNameFromMethod(foundMethods.First().NativeMethod);
var result = new StringBuilder();
result.Append(file_header);
result.Append(
@ -42,7 +48,7 @@ using System;
SyntaxFactory.IdentifierName("SDL"))
.WithMembers(
SyntaxFactory.SingletonList<MemberDeclarationSyntax>(
SyntaxFactory.ClassDeclaration("SDL3")
SyntaxFactory.ClassDeclaration(className)
.WithModifiers(
SyntaxFactory.TokenList(
SyntaxFactory.Token(SyntaxKind.UnsafeKeyword),
@ -54,6 +60,16 @@ using System;
}
}
private static string ClassNameFromMethod(MethodDeclarationSyntax methodNode)
{
if (methodNode.Parent is ClassDeclarationSyntax classDeclaration)
{
return classDeclaration.Identifier.Text;
}
return "SDL3"; // fallback!
}
private static MemberDeclarationSyntax makeFriendlyMethod(GeneratedMethod gm)
{
var returnType = gm.RequiredChanges.HasFlag(Changes.ChangeReturnTypeToString)

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
@ -15,33 +16,19 @@ namespace SDL.SourceGeneration
{
public readonly Dictionary<string, List<GeneratedMethod>> Methods = new Dictionary<string, List<GeneratedMethod>>();
private static readonly string[] sdlPrefixes = ["SDL", "TTF", "IMG"];
/// <summary>
/// Checks whether the method is from any SDL library.
/// It identifies those by checking if the method has a DllImport attribute for a library starting with "SDL".
/// It identifies those by checking the SDL prefix in the method name.
/// </summary>
private static bool IsMethodFromSDL(MethodDeclarationSyntax methodNode)
{
if (methodNode.AttributeLists.Count == 0)
string? libraryPrefix = methodNode.Identifier.ValueText.Split('_').FirstOrDefault();
if (libraryPrefix == null)
return false;
foreach (var attributeList in methodNode.AttributeLists)
{
foreach (var attribute in attributeList.Attributes)
{
if (attribute.Name.ToString() != "DllImport") continue;
if (attribute.ArgumentList == null || attribute.ArgumentList.Arguments.Count <= 0) continue; // this should never happen, but rather continue than throw
var libraryNameArgument = attribute.ArgumentList.Arguments[0];
if (libraryNameArgument.Expression is LiteralExpressionSyntax literal &&
literal.Token.ValueText.StartsWith("SDL", StringComparison.Ordinal))
{
return true;
}
}
}
return false;
return sdlPrefixes.Contains(libraryPrefix);
}
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)

View File

@ -3,6 +3,8 @@
"path": "SDL3-CS.sln",
"projects": [
"SDL3-CS\\SDL3-CS.csproj",
"SDL3_ttf-CS\\SDL3-CS_ttf.csproj",
"SDL3_image-CS\\SDL3_image-CS.csproj",
"SDL3-CS.SourceGeneration\\SDL3-CS.SourceGeneration.csproj",
"SDL3-CS.Tests\\SDL3-CS.Tests.csproj",
"SDL3-CS.Tests.iOS\\SDL3-CS.Tests.iOS.csproj"

View File

@ -27,6 +27,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SDL3-CS.Tests.iOS", "SDL3-C
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SDL3-CS.Tests.Desktop", "SDL3-CS.Tests.Desktop\SDL3-CS.Tests.Desktop.csproj", "{7E8D719A-5B69-43B7-A9D5-385B6FE7F411}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SDL3_ttf-CS", "SDL3_ttf-CS\SDL3_ttf-CS.csproj", "{8E37EB82-ACC4-4656-A6E5-DB298AE72066}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SDL3_image-CS", "SDL3_image-CS\SDL3_image-CS.csproj", "{A0D6FC5F-BA26-4298-ABF0-234D2481E323}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -61,6 +65,14 @@ Global
{7E8D719A-5B69-43B7-A9D5-385B6FE7F411}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E8D719A-5B69-43B7-A9D5-385B6FE7F411}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E8D719A-5B69-43B7-A9D5-385B6FE7F411}.Release|Any CPU.Build.0 = Release|Any CPU
{8E37EB82-ACC4-4656-A6E5-DB298AE72066}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E37EB82-ACC4-4656-A6E5-DB298AE72066}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E37EB82-ACC4-4656-A6E5-DB298AE72066}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E37EB82-ACC4-4656-A6E5-DB298AE72066}.Release|Any CPU.Build.0 = Release|Any CPU
{A0D6FC5F-BA26-4298-ABF0-234D2481E323}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0D6FC5F-BA26-4298-ABF0-234D2481E323}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0D6FC5F-BA26-4298-ABF0-234D2481E323}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0D6FC5F-BA26-4298-ABF0-234D2481E323}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -73,8 +73,8 @@
<PackagePath>runtimes/linux-arm/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\ios\**\*">
<PackagePath>runtimes/ios/native</PackagePath>
<None Include="$(MSBuildThisFileDirectory)..\native\ios\SDL3_ttf.xcframework\**\*">
<PackagePath>runtimes/ios/native/SDL3.xcframework</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\android\armeabi-v7a\libSDL3.so">

View File

@ -73,22 +73,22 @@ class Header:
def output_file(self):
"""Location of generated C# file."""
if self.output_suffix is None:
return csproj_root / f"{self.base}/ClangSharp/{self.name}.g.cs"
return repository_root / f"{self.base}-CS" / f"{self.base}/ClangSharp/{self.name}.g.cs"
else:
return csproj_root / f"{self.base}/ClangSharp/{self.name}.{self.output_suffix}.g.cs"
return repository_root / f"{self.base}-CS" / f"{self.base}/ClangSharp/{self.name}.{self.output_suffix}.g.cs"
def rsp_files(self):
"""Location of ClangSharp response files."""
yield csproj_root / f"{self.base}/{self.name}.rsp"
yield repository_root / f"{self.base}-CS" / f"{self.base}/{self.name}.rsp"
if self.output_suffix is not None:
yield csproj_root / f"{self.base}/{self.name}.{self.output_suffix}.rsp"
yield repository_root / f"{self.base}-CS" / f"{self.base}/{self.name}.{self.output_suffix}.rsp"
def cs_file(self):
"""Location of the manually-written C# file that implements some parts of the header."""
if self.output_suffix is None:
return csproj_root / f"{self.base}/{self.name}.cs"
return repository_root / f"{self.base}-CS" / f"{self.base}/{self.name}.cs"
else:
return csproj_root / f"{self.base}/{self.name}.{self.output_suffix}.cs"
return repository_root / f"{self.base}-CS" / f"{self.base}/{self.name}.{self.output_suffix}.cs"
def make_header_fuzzy(s: str) -> Header:
@ -265,7 +265,6 @@ base_command = [
"--include-directory", repository_root / SDL_lib_include_root["SDL3"],
"--include-directory", repository_root / SDL_lib_include_root["SDL3_image"],
"--include-directory", repository_root / SDL_lib_include_root["SDL3_ttf"],
"--methodClassName", "SDL3",
"--namespace", "SDL",
"--remap",
@ -308,6 +307,8 @@ def run_clangsharp(command, header: Header):
"--file", header.input_file(),
"--output", header.output_file(),
"--libraryPath", header.base,
"--methodClassName", header.base,
]
for rsp in header.rsp_files():

View File

@ -0,0 +1,138 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>SDL</RootNamespace>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);SYSLIB1054;CA1401</NoWarn>
</PropertyGroup>
<PropertyGroup Label="NuGet">
<Authors>ppy Pty Ltd</Authors>
<Company>ppy Pty Ltd</Company>
<Copyright>Copyright (c) 2024 ppy Pty Ltd</Copyright>
<Product>ppy.SDL3_image-CS</Product>
<PackageId>ppy.SDL3_image-CS</PackageId>
<PackageReleaseNotes>Automated release.</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/ppy/SDL3-CS</PackageProjectUrl>
<RepositoryUrl>https://github.com/ppy/SDL3-CS</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SDL3-CS.SourceGeneration\SDL3-CS.SourceGeneration.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0"/>
<PackageReference Include="libclang" Version="17.0.4">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="libClangSharp" Version="17.0.4">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="ppy.SDL3-CS" Version="*">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)..\native\win-x64\SDL3_image.dll">
<PackagePath>runtimes/win-x64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-arm64\SDL3_image.dll">
<PackagePath>runtimes/win-arm64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-x86\SDL3_image.dll">
<PackagePath>runtimes/win-x86/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-x64\libwebp.dll">
<PackagePath>runtimes/win-x64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-arm64\libwebp.dll">
<PackagePath>runtimes/win-arm64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-x86\libwebp.dll">
<PackagePath>runtimes/win-x86/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-x64\libwebpdemux.dll">
<PackagePath>runtimes/win-x64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-arm64\libwebpdemux.dll">
<PackagePath>runtimes/win-arm64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-x86\libwebpdemux.dll">
<PackagePath>runtimes/win-x86/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-x64\tiff.dll">
<PackagePath>runtimes/win-x64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-arm64\tiff.dll">
<PackagePath>runtimes/win-arm64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-x86\tiff.dll">
<PackagePath>runtimes/win-x86/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\osx-x64\libSDL3_image.dylib">
<PackagePath>runtimes/osx-x64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\osx-arm64\libSDL3_image.dylib">
<PackagePath>runtimes/osx-arm64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\linux-x64\libSDL3_image.so">
<PackagePath>runtimes/linux-x64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\linux-x86\libSDL3_image.so">
<PackagePath>runtimes/linux-x86/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\linux-arm64\libSDL3_image.so">
<PackagePath>runtimes/linux-arm64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\linux-arm\libSDL3_image.so">
<PackagePath>runtimes/linux-arm/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\ios\SDL3_image.xcframework\**\*">
<PackagePath>runtimes/ios/native/SDL3.xcframework</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\android\armeabi-v7a\libSDL3_image.so">
<PackagePath>runtimes/android-arm/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\android\arm64-v8a\libSDL3_image.so">
<PackagePath>runtimes/android-arm64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\android\x86_64\libSDL3_image.so">
<PackagePath>runtimes/android-x64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\android\x86\libSDL3_image.so">
<PackagePath>runtimes/android-x86/native</PackagePath>
<Pack>true</Pack>
</None>
</ItemGroup>
</Project>

View File

@ -40,7 +40,7 @@ namespace SDL
public int* delays;
}
public static unsafe partial class SDL3
public static unsafe partial class SDL3_image
{
[DllImport("SDL3_image", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int IMG_Version();

View File

@ -0,0 +1,102 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>SDL</RootNamespace>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);SYSLIB1054;CA1401</NoWarn>
</PropertyGroup>
<PropertyGroup Label="NuGet">
<Authors>ppy Pty Ltd</Authors>
<Company>ppy Pty Ltd</Company>
<Copyright>Copyright (c) 2024 ppy Pty Ltd</Copyright>
<Product>ppy.SDL3_ttf-CS</Product>
<PackageId>ppy.SDL3_ttf-CS</PackageId>
<PackageReleaseNotes>Automated release.</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/ppy/SDL3-CS</PackageProjectUrl>
<RepositoryUrl>https://github.com/ppy/SDL3-CS</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SDL3-CS.SourceGeneration\SDL3-CS.SourceGeneration.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0"/>
<PackageReference Include="libclang" Version="17.0.4">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="libClangSharp" Version="17.0.4">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="ppy.SDL3-CS" Version="*">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)..\native\win-x64\SDL3_ttf.dll">
<PackagePath>runtimes/win-x64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-arm64\SDL3_ttf.dll">
<PackagePath>runtimes/win-arm64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\win-x86\SDL3_ttf.dll">
<PackagePath>runtimes/win-x86/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\osx-x64\libSDL3_ttf.dylib">
<PackagePath>runtimes/osx-x64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\osx-arm64\libSDL3_ttf.dylib">
<PackagePath>runtimes/osx-arm64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\linux-x64\libSDL3_ttf.so">
<PackagePath>runtimes/linux-x64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\linux-x86\libSDL3_ttf.so">
<PackagePath>runtimes/linux-x86/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\linux-arm64\libSDL3_ttf.so">
<PackagePath>runtimes/linux-arm64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\linux-arm\libSDL3_ttf.so">
<PackagePath>runtimes/linux-arm/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\ios\SDL3_ttf.xcframework\**\*">
<PackagePath>runtimes/ios/native/SDL3.xcframework</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\android\armeabi-v7a\libSDL3_ttf.so">
<PackagePath>runtimes/android-arm/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\android\arm64-v8a\libSDL3_ttf.so">
<PackagePath>runtimes/android-arm64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\android\x86_64\libSDL3_ttf.so">
<PackagePath>runtimes/android-x64/native</PackagePath>
<Pack>true</Pack>
</None>
<None Include="$(MSBuildThisFileDirectory)..\native\android\x86\libSDL3_ttf.so">
<PackagePath>runtimes/android-x86/native</PackagePath>
<Pack>true</Pack>
</None>
</ItemGroup>
</Project>

View File

@ -84,7 +84,7 @@ namespace SDL
public SDL_Rect rect;
}
public static unsafe partial class SDL3
public static unsafe partial class SDL3_ttf
{
[DllImport("SDL3_ttf", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int TTF_Version();