Skip to content

Commit 255ede5

Browse files
committed
Pass 1 of SilkTouch Scraper - Joint Infrastructure and XML Generation (#559)
1 parent 4cb0069 commit 255ede5

File tree

59 files changed

+3351
-3101
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3351
-3101
lines changed

.gitmodules

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
[submodule "build/submodules/SDL"]
2-
path = build/submodules/SDL
3-
url = https://github.com/libsdl-org/SDL
4-
[submodule "build/submodules/Vulkan-Headers"]
5-
path = build/submodules/Vulkan-Headers
6-
url = https://github.com/KhronosGroup/Vulkan-Headers
7-
[submodule "build/submodules/GLFW"]
8-
path = build/submodules/GLFW
9-
url = https://github.com/glfw/glfw.git
1+
[submodule "src/submodules/Vulkan-Headers"]
2+
path = src/submodules/Vulkan-Headers
3+
url = https://github.com/KhronosGroup/Vulkan-Headers.git

Silk.NET.sln

Lines changed: 243 additions & 3091 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SilkTouch
2+
3+
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.
4+
5+
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.
6+
7+
Learn more about each individual cornerstone of the SilkTouch Stack:
8+
- [Shared Infrastructure](shared-infrastructure.md)
9+
- [Emitter](emitter.md)
10+
- [Overloader](overloader.md)
11+
- [Scraper](scraper.md)
12+
13+
TODO: Add a link to the original proposal once the proposals folder has been reorganised according to the Software Development Plan.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Emitter
2+
3+
The Emitter.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Overloader
2+
3+
The Overloader.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Scraper
2+
3+
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.
4+
5+
## ClangSharp
6+
7+
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.
8+
9+
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.
10+
11+
## The Subagent
12+
13+
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.
14+
15+
### The `ISubagent` abstraction
16+
17+
`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.
18+
19+
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.
20+
21+
### The `silktouch clangsharp` command
22+
23+
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.
24+
25+
## XML Mods
26+
27+
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.
28+
29+
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.
30+
31+
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.
32+
33+
TODO: Write words here about each XML mod once XML mods have been implemented
34+
35+
## Transformation
36+
37+
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.
38+
39+
TODO: Write more words here once this stage is implemented.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Shared Infrastructure
2+
3+
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.
4+
5+
## "Form Factor"
6+
7+
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.
8+
9+
## Configuration
10+
11+
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).
12+
13+
### Project Configuration
14+
15+
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 is used to override this with a new file name or file path (either absolute or relative).
16+
17+
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.
18+
19+
The project configuration also provides a (relative or absolute) path to a file containing the global configuration.
20+
21+
### Global Configuration
22+
23+
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.
24+
25+
## Generation
26+
27+
### SilkTouch "Context"
28+
29+
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.
30+
31+
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.
32+
33+
### The Generator Object
34+
35+
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.
36+
37+
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.
38+
39+
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.

src/Directory.Build.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<SilkDocumentationDir>$(MSBuildThisFileDirectory)../documentation</SilkDocumentationDir>
1919
<SilkTestsDir>$(MSBuildThisFileDirectory)../documentation</SilkTestsDir>
2020
</PropertyGroup>
21+
<!-- Compiler Options -->
22+
<PropertyGroup>
23+
<Nullable>enable</Nullable>
24+
<SilkDocumentationWarningsAsErrors>CS1591;CS1573</SilkDocumentationWarningsAsErrors>
25+
</PropertyGroup>
2126
<!-- NuGet -->
2227
<PropertyGroup>
2328
<PackageIcon>logo.png</PackageIcon>

src/Directory.Build.targets

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project>
1+
<Project InitialTargets="PostImportDefinitions">
22
<!-- NuGet Stuff -->
33
<ItemGroup>
44
<Content Include="$(MSBuildThisFileDirectory)..\documentation\branding\logo.png"
@@ -9,4 +9,10 @@
99
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1010
</Content>
1111
</ItemGroup>
12+
<!-- Force Documentation -->
13+
<Target Name="PostImportDefinitions">
14+
<PropertyGroup Condition="'$(SilkDocumentationExempt)' != 'true'">
15+
<WarningsAsErrors>$(SilkDocumentationWarningsAsErrors);$(WarningsAsErrors)</WarningsAsErrors>
16+
</PropertyGroup>
17+
</Target>
1218
</Project>

src/bindings/Directory.Build.targets

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project>
2+
<ItemGroup>
3+
<None Remove="$(MSBuildProjectDirectory)/silktouch.json" />
4+
<AdditionalFiles Include="$(MSBuildProjectDirectory)/silktouch.json" />
5+
</ItemGroup>
6+
<Import Project="$(MSBuildThisFileDirectory)../Directory.Build.targets" />
7+
</Project>

0 commit comments

Comments
 (0)