Skip to content

Commit

Permalink
Add support for UnsafeRawPointer
Browse files Browse the repository at this point in the history
This commit introduces support for the UnsafeRawPointer type. It also adds a test case for the existing CryptoKit bindings.
  • Loading branch information
kotlarmilos committed Mar 18, 2024
1 parent 2002932 commit aa55c63
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void EmitModule(ModuleDecl moduleDecl)
writer.WriteLine($"{{");

writer.Indent++;
writer.WriteLine($"public class {moduleDecl.Name} {{");
writer.WriteLine($"public unsafe class {moduleDecl.Name} {{");

writer.Indent++;
foreach (MethodDecl methodDecl in moduleDecl.Methods)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using Newtonsoft.Json;
using Swift.Runtime;

namespace BindingsGeneration
{
Expand Down Expand Up @@ -45,11 +46,13 @@ public sealed class SwiftABIParser : ISwiftParser
{
private readonly string _filePath;
private readonly int _verbose;
private readonly TypeDatabase _typeDatabase;

public SwiftABIParser(string filePath, int verbose = 0)
public SwiftABIParser(string filePath, TypeDatabase typeDatabase, int verbose = 0)
{
_filePath = filePath;
_verbose = verbose;
_typeDatabase = typeDatabase;
}

/// <summary>
Expand Down Expand Up @@ -115,8 +118,8 @@ public MethodDecl CreateMethodDecl(Node node)
new TypeDecl
{
Name = paramNames[i],
FullyQualifiedName = child.PrintedName == "()" ? "Void" : child.PrintedName,
IsValueType = true,
FullyQualifiedName = child.Name,
IsValueType = _typeDatabase.IsValueType(child.Name),
TypeKind = TypeKind.Named
}).ToList() ?? new List<TypeDecl>()
};
Expand Down
6 changes: 3 additions & 3 deletions src/Swift.Bindings/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ public static void Main(string[] args)
/// <param name="outputDirectory">Output directory for generated bindings.</param>
/// <param name="verbose">Verbosity level for logging information.</param>
public static void GenerateBindings(string swiftAbiPath, string outputDirectory, int verbose = 0)
{
{
TypeDatabase typeDatabase = new TypeDatabase(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TypeDatabase.xml"));
if (verbose > 0)
Console.WriteLine("Starting bindings generation...");

ISwiftParser swiftParser = new SwiftABIParser(swiftAbiPath, verbose);
ISwiftParser swiftParser = new SwiftABIParser(swiftAbiPath, typeDatabase, verbose);
var decl = swiftParser.GetModuleDecl();

if (verbose > 1)
Console.WriteLine("Parsed Swift ABI file successfully.");


TypeDatabase typeDatabase = new TypeDatabase(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TypeDatabase.xml"));

ICSharpEmitter csharpEmitter = new StringCSharpEmitter(outputDirectory, typeDatabase, verbose);
csharpEmitter.EmitModule(decl);
Expand Down
25 changes: 14 additions & 11 deletions src/Swift.Bindings/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
cmake_minimum_required(VERSION 3.24)

project(PInvokeTests)
project(SwiftTests)

set(SOURCE PInvokeTests)
# Find all Swift source files in the directory
file(GLOB SOURCES "*.swift")

if (NOT SWIFT_COMPILER_TARGET)
set(SWIFT_PLATFORM "macosx")
Expand All @@ -16,12 +17,14 @@ if (NOT SWIFT_COMPILER_TARGET)
set(SWIFT_COMPILER_TARGET "${CMAKE_OSX_ARCHITECTURES}-apple-${SWIFT_PLATFORM}${SWIFT_DEPLOYMENT_TARGET}")
endif()

add_custom_target(${SOURCE} ALL
COMMAND xcrun swiftc -target ${SWIFT_COMPILER_TARGET} -emit-module -emit-library -enable-library-evolution -emit-module-interface ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}.swift -o ${CMAKE_CURRENT_BINARY_DIR}/lib${SOURCE}.dylib
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}.swift
COMMENT "Generating ${SOURCE} library"
)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib${SOURCE}.dylib
DESTINATION bin
)
foreach(SOURCE_FILE ${SOURCES})
get_filename_component(SOURCE_BASE_NAME ${SOURCE_FILE} NAME_WE)
add_custom_target(${SOURCE_BASE_NAME} ALL
COMMAND xcrun swiftc -target ${SWIFT_COMPILER_TARGET} -emit-module -emit-library -enable-library-evolution -emit-module-interface ${SOURCE_FILE} -o ${CMAKE_CURRENT_BINARY_DIR}/lib${SOURCE_BASE_NAME}.dylib
DEPENDS ${SOURCE_FILE}
COMMENT "Generating ${SOURCE_BASE_NAME} library"
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib${SOURCE_BASE_NAME}.dylib
DESTINATION bin
)
endforeach()
Loading

0 comments on commit aa55c63

Please sign in to comment.