bottlenoselabs / c2cs

Generate C# bindings from a C header.
MIT License
245 stars 18 forks source link

v4 #108

Closed lithiumtoast closed 1 year ago

lithiumtoast commented 2 years ago

How it works:

  1. Build a C# library and drop its bin folder into the ./plugins directory of where C2CS is executed from. E.g.
Screen Shot 2022-09-05 at 10 05 04 PM

Each plugin has to be its own folder. Above I have the helloworld-bindgen-plugin bin folder in the plugins folder of the helloworld-my_c_library bin folder. The working directory of C2CS in this example is the helloworld-my_c_library bin folder.

  1. When C2CS startups it will load your C# library (and any dependencies) and use its code to control bindgen. I am experimenting with what's the best way to hook into C2CS to control bindgen from C#. For now I have the following C# interfaces which replaces loading the configuration file from disk.
public interface IReaderCCode
{
    ReaderCCodeOptions Options { get; }
}
public interface IWriterCSharpCode
{
    WriterCSharpCodeOptions Options { get; set; }
}

You would implement these interface and change the properties of the Options instance to your liking. This replaces the need for having .json configuration files. E.g. in the helloworld-bindgen-plugin C# library:

public class ReaderCCode : IReaderCCode
{
    public ReaderCCodeOptions Options { get; } = new();

    public ReaderCCode()
    {
        Options.InputHeaderFilePath =
            "../../../src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h";
        Options.OutputAbstractSyntaxTreesFileDirectory =
            "../../../src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/ast";
    }
}
public class WriterCSharpCode : IWriterCSharpCode
{
    public WriterCSharpCodeOptions Options { get; set; } = new();

    public WriterCSharpCode()
    {
        Options.InputAbstractSyntaxTreesFileDirectory =
            "../../../src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/ast";
        Options.OutputCSharpCodeFilePath =
            "../../../src/cs/examples/helloworld/helloworld-app/my_c_library.cs";
        Options.NamespaceName = "my_c_library_namespace";
    }
}

Some notes: