Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .config/dotnet-tools.json

This file was deleted.

16 changes: 8 additions & 8 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@
<PackageVersion Include="Microsoft.Build.Framework" Version="16.10.0"/>
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(RoslynPackageVersion)"/>
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="$(RoslynPackageVersion)"/>
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1"/>
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta1.21308.1"/>
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0"/>
<PackageVersion Include="Nuke.Common" Version="6.0.1"/>
<PackageVersion Include="Ultz.Bcl.Half" Version="1.0.0"/>
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="1.1.0"/>
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.1"/>
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="6.0.1"/>
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="6.0.0"/>
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(MsExtensionNugetVersion)"/>
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MsExtensionNugetVersion)"/>
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="$(MsExtensionNugetVersion)"/>
<PackageVersion Include="Microsoft.Extensions.Options" Version="$(MsExtensionNugetVersion)"/>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.3.0"/>
<PackageVersion Include="xunit" Version="2.4.2"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5"/>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="16.8.0"/>
<PackageVersion Include="xunit" Version="2.4.1"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.3"/>
<PackageVersion Include="Statiq.Core" Version="1.0.0-beta.48"/>
<PackageVersion Include="HtmlAgilityPack" Version="1.11.43"/>
<PackageVersion Include="HtmlAgilityPack" Version="1.11.42"/>
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.11.0"/>
<PackageVersion Include="Statiq.App" Version="1.0.0-beta.48"/>
<PackageVersion Include="Statiq.Feeds" Version="1.0.0-beta.48"/>
<PackageVersion Include="Statiq.Markdown" Version="1.0.0-beta.62"/>
<PackageVersion Include="Statiq.Markdown" Version="1.0.0-beta.48"/>
<PackageVersion Include="Statiq.Razor" Version="1.0.0-beta.48"/>
<PackageVersion Include="Statiq.Yaml" Version="1.0.0-beta.48"/>
<PackageVersion Include="Humanizer.Core" Version="2.14.1"/>
<PackageVersion Include="Moq" Version="4.17.2"/>
<PackageVersion Include="System.Collections.Immutable" Version="6.0.0"/>
<PackageVersion Include="libclang" Version="14.0.0"/>
<PackageVersion Include="libClangSharp" Version="14.0.0-beta1"/>
<PackageVersion Include="Verify.Xunit" Version="17.9.0" />
<PackageVersion Include="Verify.Xunit" Version="16.9.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="$(MsExtensionNugetVersion)" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="$(MsExtensionNugetVersion)" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="$(MsExtensionNugetVersion)" />
Expand Down
140 changes: 96 additions & 44 deletions Silk.NET.sln

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions documentation/for-contributors/generators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

SilkTouch is the Silk.NET project's C# native interoperability code generation stack. It contains useful generators which together generate the majority of the library's codebase.

It does this by centering all parts around a [common symbol layer](./symbol-layer/README.md), which allows other parts to be used in any configuration deemed useful, parts don't have a way other then the symbol layer to exchange data, enforcing interoperability.
It does this by splitting the generation process into multiple phases, all anchored around the concept of a "shared infrastructure" - abstractions for SilkTouch generators which are agnostic of where they're running. Currently the only places SilkTouch can run is in a Roslyn Source Generator and using a `dotnet tool` Command Line Interface, but the idea is so long as no incompatibilities are introduced by each individual phase (such as being locked to .NET 6 instead of .NET Standard 2.0) the generator should be able to run in whatever form factor is desirable - the Shared Infrastructure will provide everything a generator could need for consuming a JSON configuration with C# input code and producing additional generated C# code as a result.

Learn more about each individual cornerstone of the SilkTouch Stack:

- [Symbol Layer](./symbol-layer/README.md)
- [Emitter](emitter.md)
- [Scraper](scraper.md)
- [Shared Infrastructure](shared-infrastructure.md)
- [Emitter](emitter.md)
- [Overloader](overloader.md)
- [Scraper](scraper.md)

TODO: Add a link to the original proposal once the proposals folder has been reorganised according to the Software Development Plan.
3 changes: 3 additions & 0 deletions documentation/for-contributors/generators/emitter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Emitter

The Emitter.
7 changes: 0 additions & 7 deletions documentation/for-contributors/generators/emitter/README.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Visitor

See [here](../symbol-layer/symbol-visitor.md) for details on how the base symbol visitor works.

To Transform our internal symbol layer to C# code a visitor (`CSharpEmitter.Visitor`) is used.

## State
Expand Down
3 changes: 3 additions & 0 deletions documentation/for-contributors/generators/overloader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Overloader

The Overloader.
41 changes: 32 additions & 9 deletions documentation/for-contributors/generators/scraper.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
# Scraper

The Scraper will call into ClangSharp to generate XML from the given C headers (`GenerateXML`), and then convert the output produced by ClangSharp into symbols defined by the [Symbol Layer](./symbol-layer/README.md) (`ScrapeXML`).
The SilkTouch Scraper is responsible for walking through the abstract syntax tree (AST) of a C/C++ header and all the referenced headers therein to generate C# bindings to as much of this as possible where the configuration indicates this is desirable. Only certain headers will have bindings generated for them, which can be specified by providing the paths to the headers to "traverse" in the JSON configuration.

The Scraper should stay quite small and easy to use, as it is also the window to all the complexity that comes from C++, Clang, header, etc. It should only bridge the XML output into the symbol layer.
## ClangSharp

## Scraping XML
To walk through the C/C++ AST, ClangSharp's P/Invoke Generator is used, which is a proven tool for generating C# bindings to C/C++ APIs published by Microsoft. It does this by using the clang compiler to walk through the AST.

To scrape XML there is a visitor that visits each XML node and outputs several symbols per node.
This is what happens in `XmlVisitor.cs`, called from `ScrapeXML`.
ClangSharp's P/Invoke Generator is capable of producing C# bindings on its own, however these are raw bindings that use DllImport and don't aim to lift the bindings to be more C#-friendly. However, as part of Scraper development work was done to add an "XML output mode" to ClangSharp which instead of producing pure C# code, it outputs XML which roughly represents what the C# code would've looked like.

## Generating XML using ClangSharp
## The Subagent

This is what happens in `ClangScraper.cs`, which is configured using `ClangScraperConfiguration.cs`.
The XML is generated by the `ClangSharp.PInvokeGenerator` see [dotnet/ClangSharp](https://github.com/dotnet/ClangSharp) and the [PInvokeGenerator file](https://github.com/dotnet/ClangSharp/blob/main/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs).
Check XML docs for details on what methods do.
ClangSharp uses libclang, which has funny rules over multithreading. To maintain a degree of parallelism during generation, the invocation of ClangSharp was moved to be done in a separate process. The original intention was to invoke the `ClangSharpPInvokeGenerator` tool as shipped publicly, but unfortunately `dotnet tool`s cannot be referenced by other libraries or `dotnet tool`s. As such, we created our own shim which mimics the behaviour of and duplicates a lot of code from the `ClangSharpPInvokeGenerator` tool, but within the same CLI executable.

### The `ISubagent` abstraction

`ISubagent` is responsible for launching the ClangSharp tool with certain parameters, inputs, and configuration. This is an abstraction such that the main Scraper assembly remains easily portable to another form factor should we enable the Scraper to run in another form factor in the future, as well as to prevent the library assembly knowing too much about the assembly in which its executing.

The only implementation of this interface is the `ClangSharpSubagent` class in the `SilkTouch` assembly (CLI implementation of the Shared Infrastructure) which gathers information on the currently executing assembly, and relaunches it with `clangsharp` as the first argument and a JSON-encoded record as the second.

### The `silktouch clangsharp` command

This is the command that invokes ClangSharp as described in the previous section. This deserializes the JSON-encoded record and passes the data held therein to the `PInvokeGenerator` class within ClangSharp. This will generate the XML dump and then quit, possibly with some ClangSharp-generated diagnostics and a non-zero error code if those diagnostics are severe. The implementation of this is in the `ClangSharpHandoff` class.

## XML Mods

Once an initial XML dump has been generated by The Subagent, the XML dump will be parsed by the `Silk.NET.SilkTouch.ClangSharp.Xml` library and produce a tree of records. This is done in the `ScraperGenerator` ahead of the XML mods stage.

The purpose of the XML mods stage is to walk this tree of records, and modify it depending on the project configuration (for example changing all names to be PascalCase, or adding custom attributes to cause the Overloader to generate certain overloads). Each mod must be specifically enabled, and has free range over the XML. As such, they execute in the order in which they were specified in the project configuration.

Each XML mod has access to the `ScraperJobConfiguration.ModOptions` and the full XML tree. It does not have any access to resources beyond that, such as the SilkTouch Context.

TODO: Write words here about each XML mod once XML mods have been implemented

## Transformation

After the XML mods have been applied, the transformation stage is responsible for converting the bindings represented by the XML tree back into C# code that can be output via the SilkTouch Context (and added to the project compilation). Unlike ClangSharp, this stage will output bindings which are compatible with/invoke the Emitter and Overloader instead of using DllImport directly.

TODO: Write more words here once this stage is implemented.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Shared Infrastructure

The Shared Infrastructure contains common code and abstractions for bootstrapping and running generators within the SilkTouch Stack. It aims to ensure that SilkTouch generators are easily portable, and have a unified interface over multiple generation mediums such that no extra code is required in individual generators to accomodate new form factors.

## "Form Factor"

A "Form Factor", referred to throughout the documentation, is simply a distinct type of application which invokes the SilkTouch Stack via the Shared Infrastructure (i.e. "it's a place where SilkTouch runs"). Distinct types such as Roslyn Source Generators and Command Line Interfaces, which are the two first-party form factors supported today.

## Configuration

All JSON configuration structures are represented by C# records in the Silk.NET.SilkTouch.Configuration namespace (in the Configuration folder of Silk.NET.SilkTouch.Common).

### Project Configuration

Each project has its own JSON configuration file which defines what each generator does for that particular project. This file should be added by the user as an `AdditionalFiles` file, with the name `silktouch.json` unless the `silktouch_config_file` `.editorconfig` option in the source generator format factor or the `config` option in the CLI form factor are used to override this with a new file name or file path (either absolute or relative).

Each generator has its own specific configuration structure, which is outlined in XML documentation in the `Config.cs` file as well as the markdown documents for each individual generator.

The project configuration also provides a (relative or absolute) path to a file containing the global configuration.

### Global Configuration

The global configuration contains variables which aren't specific to a particular project or generation process/job. Stuff like the License Header, which doesn't need to be duplicated across every single project configuration.

### CLI Configuration
The CLI has 3 ways to be configured:
- Command line parameters, possible formats are "key1=value1 --key2=value2 /key3=value3 --key4 value4 /key5 value5"
- A JSON file, with the path taken either from the config option `config` or by default silktouch.json (in the working directory)
- Environment Variables, prefixed with SilkTouch_ (to configure hierarchical data, use two underscores like `set SilkTouch_Parent__Root=value`)
The CLI uses the Microsoft.Extensions.Configuration package for this, for more information on how to use the above options see [here](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/) (note that we do not use user secrets)

## Generation

### SilkTouch "Context"

The SilkTouch "Context" (represented by the `SilkTouchContext` class in the `Silk.NET.SilkTouch.Generation` namespace, or Generation folder of Silk.MET.SilkTouch.Common) encapsulates all inputs and outputs passed into and retrieved from a generator running atop the Shared Infrastructure. It exists to isolate each individual generator from one another, and provide one unified data structure containing everything a generator might reasonably need to access about a project.

Some SilkTouch Contexts have access to outputs generated by other generators in the `AdditionalSyntaxTrees` property. To ease context consumption, there is a `AllSyntaxTrees` property which will contain both the Roslyn `Compilation`'s syntax trees as well as the newly-added syntax trees. Currently this is only the Emitter and Overloader if the Scraper has ran before them, though it is intended that the Shared Infrastructure will be expanded to have a full-blown dependency resolution mechanism to allow more generator modularity and dynamic definition of a generator's soft or hard dependencies.

### The Generator Object

The Generator Object (represented by the `SilkTouchGenerator` class) is responsible for the consumption of basic project information, and morphing/storing this information in a way that allows for Contexts to be created from the generator object. After a context is created, it will be consumed by a generator, and then once again "ingested" by the generator object. This generator object forwards the results to bindable events, which are subscribed to by the form factor.

The intention of this is to reduce duplicated code between the two form factors we maintain, and provide an interface to ease the bootstrapping of SilkTouch code generation - this class significantly reduces the amount of work required to implement another form factor.

Currently there are no methods on `SilkTouchGenerator` which can create a `SilkTouchContext` directly, nor are there APIs which nicely facilitate the passing of generated outputs to a stage depending on them. Both of which are things we'd like to address in the future.

This file was deleted.

This file was deleted.

Loading