|
| 1 | +// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using Microsoft.CodeAnalysis; |
| 7 | +using Microsoft.CodeAnalysis.CSharp; |
| 8 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 9 | +using Microsoft.CodeAnalysis.Text; |
| 10 | + |
| 11 | +namespace ImageMagick.SourceGenerator; |
| 12 | + |
| 13 | +[Generator] |
| 14 | +internal class NativeInteropGenerator : IIncrementalGenerator |
| 15 | +{ |
| 16 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
| 17 | + { |
| 18 | + context.RegisterPostInitializationOutput(context => context.AddAttributeSource<NativeInteropAttribute>()); |
| 19 | + context.RegisterAttributeCodeGenerator<NativeInteropAttribute, NativeInteropInfo>(GetClass, GenerateCode); |
| 20 | + } |
| 21 | + |
| 22 | + private static NativeInteropInfo GetClass(GeneratorAttributeSyntaxContext context) |
| 23 | + => new(context.TargetNode); |
| 24 | + |
| 25 | + private static void GenerateCode(SourceProductionContext context, NativeInteropInfo info) |
| 26 | + { |
| 27 | + var codeBuilder = new CodeBuilder(); |
| 28 | + codeBuilder.AppendLine("#nullable enable"); |
| 29 | + |
| 30 | + codeBuilder.AppendLine(); |
| 31 | + codeBuilder.AppendLine("using System.Runtime.InteropServices;"); |
| 32 | + |
| 33 | + codeBuilder.AppendLine(); |
| 34 | + codeBuilder.AppendLine("namespace ImageMagick;"); |
| 35 | + codeBuilder.AppendLine(); |
| 36 | + |
| 37 | + codeBuilder.Append("public partial class "); |
| 38 | + codeBuilder.AppendLine(info.ParentClassName); |
| 39 | + codeBuilder.AppendLine("{"); |
| 40 | + codeBuilder.Indent++; |
| 41 | + |
| 42 | + AppendNativeInterop(codeBuilder, info); |
| 43 | + |
| 44 | + codeBuilder.AppendLine(); |
| 45 | + |
| 46 | + AppendNativeClass(codeBuilder, info); |
| 47 | + |
| 48 | + codeBuilder.Indent--; |
| 49 | + codeBuilder.AppendLine("}"); |
| 50 | + |
| 51 | + context.AddSource($"{info.ParentClassName}.g.cs", SourceText.From(codeBuilder.ToString(), Encoding.UTF8)); |
| 52 | + } |
| 53 | + |
| 54 | + private static void AppendNativeInterop(CodeBuilder codeBuilder, NativeInteropInfo info) |
| 55 | + { |
| 56 | + codeBuilder.AppendLine("private unsafe static class NativeMethods"); |
| 57 | + codeBuilder.AppendLine("{"); |
| 58 | + codeBuilder.Indent++; |
| 59 | + AppendNativeInterop(codeBuilder, info, "x64"); |
| 60 | + AppendNativeInterop(codeBuilder, info, "arm64"); |
| 61 | + AppendNativeInterop(codeBuilder, info, "x86"); |
| 62 | + codeBuilder.Indent--; |
| 63 | + codeBuilder.AppendLine("}"); |
| 64 | + } |
| 65 | + |
| 66 | + private static void AppendNativeInterop(CodeBuilder codeBuilder, NativeInteropInfo info, string platform) |
| 67 | + { |
| 68 | + var name = platform.ToUpperInvariant(); |
| 69 | + |
| 70 | + codeBuilder.Append("#if PLATFORM_"); |
| 71 | + codeBuilder.Append(platform); |
| 72 | + codeBuilder.AppendLine(" || PLATFORM_AnyCPU"); |
| 73 | + codeBuilder.Append("public static class "); |
| 74 | + codeBuilder.AppendLine(name); |
| 75 | + codeBuilder.AppendLine("{"); |
| 76 | + codeBuilder.Indent++; |
| 77 | + |
| 78 | + foreach (var method in info.Methods) |
| 79 | + { |
| 80 | + codeBuilder.Append("[DllImport(NativeLibrary."); |
| 81 | + codeBuilder.Append(name); |
| 82 | + codeBuilder.AppendLine("Name, CallingConvention = CallingConvention.Cdecl)]"); |
| 83 | + codeBuilder.Append("public static extern "); |
| 84 | + codeBuilder.Append(method.ReturnType.ToString()); |
| 85 | + codeBuilder.Append(" "); |
| 86 | + codeBuilder.Append(info.ParentClassName); |
| 87 | + codeBuilder.Append("_"); |
| 88 | + codeBuilder.Append(method.Identifier.Text); |
| 89 | + codeBuilder.Append("("); |
| 90 | + for (var i = 0; i < method.ParameterList.Parameters.Count; i++) |
| 91 | + { |
| 92 | + var parameter = method.ParameterList.Parameters[i]; |
| 93 | + if (i > 0) |
| 94 | + codeBuilder.Append(", "); |
| 95 | + |
| 96 | + codeBuilder.Append(parameter.Type.ToNativeString()); |
| 97 | + codeBuilder.Append(" "); |
| 98 | + codeBuilder.Append(parameter.Identifier.Text); |
| 99 | + } |
| 100 | + |
| 101 | + codeBuilder.Append(")"); |
| 102 | + codeBuilder.AppendLine(";"); |
| 103 | + } |
| 104 | + |
| 105 | + codeBuilder.Indent--; |
| 106 | + codeBuilder.AppendLine("}"); |
| 107 | + codeBuilder.AppendLine("#endif"); |
| 108 | + } |
| 109 | + |
| 110 | + private static void AppendNativeClass(CodeBuilder codeBuilder, NativeInteropInfo info) |
| 111 | + { |
| 112 | + codeBuilder.Append("private unsafe partial class "); |
| 113 | + codeBuilder.AppendLine(info.ClassName); |
| 114 | + codeBuilder.AppendLine("{"); |
| 115 | + codeBuilder.Indent++; |
| 116 | + |
| 117 | + codeBuilder.Append("static "); |
| 118 | + codeBuilder.Append(info.ClassName); |
| 119 | + codeBuilder.AppendLine("() { Environment.Initialize(); }"); |
| 120 | + |
| 121 | + foreach (var method in info.Methods) |
| 122 | + { |
| 123 | + var isVoid = method.ReturnType.ToString() == "void"; |
| 124 | + |
| 125 | + codeBuilder.Append("public "); |
| 126 | + if (method.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.StaticKeyword))) |
| 127 | + codeBuilder.Append("static "); |
| 128 | + codeBuilder.Append("partial "); |
| 129 | + codeBuilder.Append(method.ReturnType.ToString()); |
| 130 | + codeBuilder.Append(" "); |
| 131 | + codeBuilder.Append(method.Identifier.Text); |
| 132 | + codeBuilder.Append("("); |
| 133 | + for (var i = 0; i < method.ParameterList.Parameters.Count; i++) |
| 134 | + { |
| 135 | + var parameter = method.ParameterList.Parameters[i]; |
| 136 | + if (i > 0) |
| 137 | + codeBuilder.Append(", "); |
| 138 | + |
| 139 | + codeBuilder.Append(parameter.Type!.ToString()); |
| 140 | + codeBuilder.Append(" "); |
| 141 | + codeBuilder.Append(parameter.Identifier.Text); |
| 142 | + } |
| 143 | + |
| 144 | + codeBuilder.Append(")"); |
| 145 | + codeBuilder.AppendLine(); |
| 146 | + codeBuilder.AppendLine("{"); |
| 147 | + codeBuilder.Indent++; |
| 148 | + |
| 149 | + if (!isVoid) |
| 150 | + { |
| 151 | + codeBuilder.Append(method.ReturnType.ToNativeString()); |
| 152 | + codeBuilder.AppendLine(" result;"); |
| 153 | + } |
| 154 | + |
| 155 | + codeBuilder.AppendLine("#if PLATFORM_AnyCPU"); |
| 156 | + codeBuilder.AppendLine("if (Runtime.IsArm64)"); |
| 157 | + codeBuilder.AppendLine("#endif"); |
| 158 | + AppendMethodImplementation(codeBuilder, info, method, "arm64"); |
| 159 | + codeBuilder.AppendLine("#if PLATFORM_AnyCPU"); |
| 160 | + codeBuilder.AppendLine("else if (Runtime.Is64Bit)"); |
| 161 | + codeBuilder.AppendLine("#endif"); |
| 162 | + AppendMethodImplementation(codeBuilder, info, method, "x64"); |
| 163 | + codeBuilder.AppendLine("#if PLATFORM_AnyCPU"); |
| 164 | + codeBuilder.AppendLine("else"); |
| 165 | + codeBuilder.AppendLine("#endif"); |
| 166 | + AppendMethodImplementation(codeBuilder, info, method, "x86"); |
| 167 | + |
| 168 | + if (!isVoid) |
| 169 | + codeBuilder.AppendLine("return result;"); |
| 170 | + |
| 171 | + codeBuilder.Indent--; |
| 172 | + codeBuilder.AppendLine("}"); |
| 173 | + } |
| 174 | + |
| 175 | + codeBuilder.Indent--; |
| 176 | + codeBuilder.AppendLine("}"); |
| 177 | + } |
| 178 | + |
| 179 | + private static void AppendMethodImplementation(CodeBuilder codeBuilder, NativeInteropInfo info, MethodDeclarationSyntax method, string platform) |
| 180 | + { |
| 181 | + codeBuilder.Append("#if PLATFORM_"); |
| 182 | + codeBuilder.Append(platform); |
| 183 | + codeBuilder.AppendLine(" || PLATFORM_AnyCPU"); |
| 184 | + if (method.ReturnType.ToString() != "void") |
| 185 | + codeBuilder.Append("result = "); |
| 186 | + codeBuilder.Append("NativeMethods."); |
| 187 | + codeBuilder.Append(platform.ToUpperInvariant()); |
| 188 | + codeBuilder.Append("."); |
| 189 | + codeBuilder.Append(info.ParentClassName); |
| 190 | + codeBuilder.Append("_"); |
| 191 | + codeBuilder.Append(method.Identifier.Text); |
| 192 | + codeBuilder.Append("("); |
| 193 | + for (var i = 0; i < method.ParameterList.Parameters.Count; i++) |
| 194 | + { |
| 195 | + var parameter = method.ParameterList.Parameters[i]; |
| 196 | + if (i > 0) |
| 197 | + codeBuilder.Append(", "); |
| 198 | + |
| 199 | + codeBuilder.Append(parameter.Identifier.Text); |
| 200 | + } |
| 201 | + |
| 202 | + codeBuilder.AppendLine(");"); |
| 203 | + codeBuilder.AppendLine("#endif"); |
| 204 | + } |
| 205 | +} |
0 commit comments