Adds plugin mechanics. Removes configuration files. At startup C2CS loads a previously built C# library on disk and uses said C# library to control bindgen.
Major restructuring.
How it works:
Build a C# library and drop its bin folder into the ./plugins directory of where C2CS is executed from. E.g.
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.
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; }
}
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:
The .deps.json file is important. This file is used to know of dependencies to resolve and load when the plugin is loaded.
When referencing the C2CS.Plugin C# library directly or as a NuGet package add the following. This ensures that the C2CS.Plugin C# library is shared between the host and your C# library. Not using these will cause the C2CS.Plugin C# library to be duplicated in different assemblies resulting in technically different types. This will cause the code of your plugin to not be found.
How it works:
./plugins
directory of where C2CS is executed from. E.g.Each plugin has to be its own folder. Above I have the
helloworld-bindgen-plugin
bin folder in theplugins
folder of thehelloworld-my_c_library
bin folder. The working directory of C2CS in this example is thehelloworld-my_c_library
bin folder.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 thehelloworld-bindgen-plugin
C# library:Some notes:
.deps.json
file is important. This file is used to know of dependencies to resolve and load when the plugin is loaded..csproj
file for thehelloworld-bindgen-plugin
C# library must have the following. This ensures that all the dependencies of the plugin are copied to the bin folder and does some other things which we want so we can use it as a plugin. See https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#enabledynamicloading for more details.C2CS.Plugin
C# library directly or as a NuGet package add the following. This ensures that theC2CS.Plugin
C# library is shared between the host and your C# library. Not using these will cause theC2CS.Plugin
C# library to be duplicated in different assemblies resulting in technically different types. This will cause the code of your plugin to not be found.