Unity Incremental C# Compiler
C# Compilation Server and standalone C# compiler for Unity
Documentation
This project is a Roslyn-based C# compiler that can be used as a standalone compiler drop-in replacement for csc.exe or mcs.exe. It can also be used as a compilation server, in a context where incremental compilation and caching of large amount of metadata is desirable.
Invoking the incremental compiler is done via commandline, and its syntax is the same as mono or Microsoft. see https://linux.die.net/man/1/mcs for commandline argument reference.
The incremental compiler support additional options to turn on certain features, namely to enable the compilation server.
--server Start a compiler server
--port=
Example Usage in Unity
The incremental compiler is distributed as a package, and should work "out-of-the-box" when added to your project's manifest file and your project is set to target the .NET 4.X runtime.
"dependencies":
{
"com.unity.incrementalcompiler": "0.0.42-preview.15"
}
Once you've added the compiler as a dependency and changed your project to target .NET 4.X, Unity will start using it automatically after you've restarted the application. The compiler exposes a prefrence panel under the Unity Preferences window.
Encode Code Analyzers
Will cause the compiler client to look for .analyzer folders in your project, will compile them seperately and will add them to the compiler as an "analysis step".
Enable building from IDE
Will use the incremental compiler when compiling the Unity project directly from Visual Studio or Rider.
Verbosity
Controls the amount of debug messages outputted from the compiler, both in the Unity console and by the compilation server.
Show output in console window
Will cause a seperate console window to appear in which the compiler will output it's messages. This is useful to see what is happening inside the server process.
supported on Windows only
Example Usage From command line
Compiling a single .cs source file with a single reference, outputing a class library.
com.unity.incrementalcompiler/.bin/csc.exe -i:foo.cs -i:bar.cs -r:c:\someReference.dll -out:out.dll
Starting a compilation server on port 12345...
com.unity.incrementalcompiler/.bin/csc.exe --server --port=12345
Once the server is running, you can invoke the compiler once again, but this time tell it to connect to the server. When you do this, the compilation occurs incrementally, in the server.
com.unity.incrementalcompiler/.bin/csc.exe --port=12345 -i:foo.cs -r:c:\someReference.dll -out:out.dll
In the example above, the compiler is invoked but told to connect to a server for compilation. The server will receive a formatted command line arguments and will proceed to compile the source. The server will be able to "cache" certain parts of the compilation, to enhance performance. First, the Server will only load references once and will keep them in memory. This means that compiling against a particular framework more than once in a server session will greatly enhance the performance since the framework's DLL will only be loaded and parsed once.
But the incremental compiler truly shines when you perform the same compilation many times, changing a bit of code here and there in your project. This is because the compiler also caches the parsed representation of the source files (called SyntaxTrees, or ASTs). This is the compilation pattern most commonly seen when working in Unity. This is why the compiler was optimized for this particular use-case.