08 — csharpc as a drop-in toolchain
The goal is for an ordinary .NET project to become a native executable through csharpc
without changing its code or project layout, the way PublishAot=true works for NativeAOT. This
page covers what works today, how it’s built, and what “drop-in” still lacks.
Target and end state (ADR 0016): drop-in means
100 % parity with NativeAOT — everything that works under PublishAot=true works with csharpc,
with the same behaviour; what NativeAOT can’t do (run-time code generation) stays an explicit
diagnostic. Csharpc.Sdk honours NativeAOT’s MSBuild properties under their NativeAOT names as it
gains them (a property it doesn’t honour yet is a warning, not silently ignored). The end state is a
fork of dotnet in which the PublishAot step runs csharpc instead of ILCompiler, with the same inputs,
parameters and outputs (roadmap M8).
Usage
1. CLI on a project
csharpc path/to/App.csproj # -> path/to/bin/csharpc/App
csharpc path/to/dir/ # the single .csproj in that directory
csharpc # the single .csproj in the current directory
csharpc App.csproj -o out/ # -> out/App (trailing '/' or an existing directory)
csharpc App.csproj -o app.native # explicit file name
csharpc App.csproj -c Release # Release symbols/properties (Debug is the default, as in dotnet build); -O/-g are separate
csharpc App.csproj -p:TargetFramework=net11.0 -p:Foo=Bar # MSBuild global properties
csharpc App.csproj --emit llvm-ir # every --emit kind works with projects too
Loose files keep working as before (csharpc a.cs b.cs -o app).
2. dotnet build / dotnet run / dotnet publish
Add the MSBuild package (src/Csharpc.Sdk):
<PackageReference Include="Csharpc.Sdk" Version="0.1.0-alpha" PrivateAssets="all" />
dotnet build -p:UseCsharpc=true # native exe replaces the apphost: bin/<cfg>/<tfm>/App
dotnet run -p:UseCsharpc=true # ...so `dotnet run` runs the native image
dotnet publish -p:PublishCsharpc=true # publish dir holds one native executable (like PublishAot)
Both properties can be set in the project file instead (<UseCsharpc>true</UseCsharpc>).
Optional knobs:
| Property | Meaning |
|---|---|
CsharpcArgs |
extra compiler flags, e.g. -O3 -v |
CsharpcCompilerPath |
use a locally built csharpc.dll instead of the one in the package |
CsharpcOutputPath |
where dotnet build puts the native binary (default: the apphost path) |
CsharpcConfiguration |
override the -c passed to csharpc (default $(Configuration)) |
CsharpcKeepManagedPublishOutput |
true keeps the managed *.dll files in the publish dir |
You can skip the package by importing the targets directly, which is what the repo’s tests do:
dotnet build -p:UseCsharpc=true \
-p:CustomAfterMicrosoftCommonTargets=$REPO/src/Csharpc.Sdk/build/Csharpc.Sdk.targets \
-p:CsharpcCompilerPath=$REPO/src/Csharpc/bin/Release/net11.0/csharpc.dll
3. Packages
dotnet pack src/Csharpc.Sdk -o artifacts/packages # Csharpc.Sdk: build/*.targets + tools/csharpc/**
dotnet pack src/Csharpc -o artifacts/packages # csharpc: a .NET tool
dotnet tool install -g csharpc --add-source artifacts/packages --prerelease
Both packages carry src/runtime/*.c. csrt is compiled on the user’s machine on first use and
cached in ~/.cache/csharpc, so the packages are architecture-neutral. You need clang, or
CSHARPC_CC, plus libLLVM 18–20.
Published builds (.github/workflows/release.yml, after green CI on main, and on v* tags) push
RID-specific NativeAOT tool packages (csharpc.<rid> plus a pointer csharpc package) to
GitHub Packages, and v* tags (only) to nuget.org — the same binaries as the
GitHub Release archives (libLLVM under lib/).
dotnet tool install -g csharpc therefore installs a ready native compiler for the host RID; clang
is still required on the machine to compile csrt and link. nuget.org OIDC lives in release.yml
(no GitHub environment; variable NUGET_USER). See the README Install section.
How it works
Project evaluation (src/Csharpc/Driver/ProjectLoader.cs)
csharpc doesn’t try to interpret project files itself. It asks MSBuild:
dotnet msbuild App.csproj -p:Configuration=Release \
-getProperty:AssemblyName -getProperty:DefineConstants -getProperty:LangVersion ... \
-getItem:Compile -getItem:Using -getItem:ProjectReference -getItem:PackageReference
That’s a JSON evaluation dump, available since .NET 8. As a result, globs,
Directory.Build.props/targets, conditions, SDK defaults (TRACE;DEBUG, implicit usings) and
<Compile Remove> are exactly what dotnet build sees. From the result:
Compileitems become the source files.Usingitems (<Using Include="X" Alias="Y" Static="true"/>, including the SDK’s implicit usings) become a synthesizedglobal usingfile. This is what the SDK’sGenerateGlobalUsingstarget writes toobj/during a real build; csharpc derives it from evaluation, so no build is needed.DefineConstants,LangVersion,Nullable,AllowUnsafeBlocksandAssemblyNameconfigure Roslyn.OutputTypemust beExe/WinExefor--emit exe.ProjectReferences are evaluated recursively, and their sources are folded into the same compilation. csharpc is a closed-world, whole-program compiler (ADR 0006). A library isn’t a separate binary artifact; it’s more code in the one LLVM module, which LLVM can inline across. Global usings are unioned across projects, because they’re compilation-wide in a single compilation. Language settings come from the root project.<Reference>items (with aHintPath, or a.dllpath) andPackageReferences are assemblies without sources. For packages csharpc runsdotnet msbuild -restore -t:ResolvePackageAssetsand takesRuntimeCopyLocalItems, the implementation DLLsdotnet buildwould copy next to the app. All of them are bound by Roslyn and compiled from IL on demand (ADR 0010).
MSBuild targets (src/Csharpc.Sdk/build/Csharpc.Sdk.targets)
CsharpcCompilerunsAfterTargets="Build". The normalCoreCompilestill runs, so diagnostics, analyzers, IDE behaviour and the managedApp.dllare unaffected. The target runs csharpc on$(MSBuildProjectFullPath)and writesobj/<cfg>/<tfm>/csharpc/App. It’s incremental: its inputs are@(Compile),@(ReferencePath)(so changes in referenced projects count), the project file and the compiler.CsharpcCopyToOutputcopies the native image over the apphost on every build, because the regular build may have refreshed it.RunCommandalready points at the apphost, sodotnet run -p:UseCsharpc=trueruns native code with no further integration.- Library projects are skipped silently. When a global
UseCsharpc=trueflows into referenced projects, they contribute their sources to the executable instead. CsharpcPublishrunsAfterTargets="Publish". It writes$(PublishDir)Appand deletes the managed payload (*.dll,*.deps.json,*.runtimeconfig.json,*.pdb), the same UX as NativeAOT.
Diagnostics and exit codes
Messages use MSBuild’s canonical format (file(line,col): error ID: message,
csharpc: error : message), so MSBuild, IDEs and CI parse them. They’re colored on a TTY
unless NO_COLOR is set. Exit codes:
| Code | Meaning |
|---|---|
| 0 | success |
| 1 | compile errors |
| 2 | bad command line |
| 70 | internal compiler error |
Feature-switch defaults
csharpc compiles in the feature switches .NET would set for the same kind of program, because programs observe them
(AppContext.TryGetSwitch, and code paths the switches select). A single file gets what dotnet run app.cs writes (a
file-based app defaults to PublishAot=true and its AOT switches); a project gets the RuntimeHostConfigurationOption
items of its MSBuild evaluation; an explicit property, runtimeconfig value or #:property always wins.
- LINQ.
System.Linq.Enumerable.IsSizeOptimizedis true for single files and for projects withPublishAot=true(UseSizeOptimizedLinq=trueinMicrosoft.NETCore.Native.targets), false for other projects. It changes observable results such asTryGetNonEnumeratedCount. For information, on the LINQ throughput program the speed-optimized path takes 0.79 s against 1.80 s of user CPU, and the binary is 1 369 against 969 KB stripped (ADR 0023, Linq row);UseSizeOptimizedLinq=falsepicks it. csharpc’s own build setsUseSizeOptimizedLinq=true, because its self-compile needs it to fit in the CI runner’s memory.
Tests
tests/projects.sh builds tests/projects/HelloProject four ways and compares each against
dotnet run. The sample has multiple files, file-scoped namespaces, a partial class split
across files, #if on a DefineConstants symbol, <Using> items including an alias, and a
ProjectReference to tests/projects/MathLib. The four ways:
csharpc HelloProject.csprojdotnet build -p:UseCsharpc=true. The test checks that the output really is the native ELF and not the apphost.dotnet run -p:UseCsharpc=truedotnet packCsharpc.Sdk into a temporary local feed,dotnet add package, thendotnet publish -p:PublishCsharpc=true. The test checks that the publish directory contains only the native executable.
Known limits of the drop-in
The toolchain side is drop-in: project files, multi-project solutions, dotnet build/run/publish, packaging and
diagnostics work. What differs from PublishAot is the language and library side, and the open items are tracked in
06-roadmap.md. In rough order of impact:
- BCL coverage. User code binds against the real .NET reference assemblies, so every API resolves, but only the
members csharpc implements compile (
csharpc --intrinsicslists those mapped to the runtime). The library surface is the C# corelib compiled together with the program (12-corelib.md): calls bound to the reference-assembly symbols are redirected to it by matching signatures, and framework assemblies csharpc implements none of are compiled from the shared framework’s IL (ADR 0013). A missing member is aCSLLVM0002diagnostic; the ranked gaps are the roadmap’s M2 list. - NuGet packages. Binary packages are IL; the IL front-end compiles the parts a program reaches (ADR 0010), with RID-specific assemblies and native assets selected for the target. What still fails is mostly BCL surface the packages call (the same members a source program would need) and the shapes listed under M5 in the roadmap.
- Runtime services a native closed world can’t offer directly:
Assembly.Load,System.Reflection.Emitand run-time code generation. NativeAOT has the same constraints, and the answer is the same: reflection over compile-time metadata for what is statically knowable (ADR 0018),dynamicbound at compile time (ADR 0019), and diagnostics for the rest. - Source generators and analyzers. Analyzers run in the regular
CoreCompileand don’t affect the native build. Source generators run inside csharpc (Roslyn’sGeneratorDriver) with the inputs csc gets from the project: its analyzers, additional files, analyzer config and features, read from the csc command line of the SDK’sCompiletarget (for each referenced project too, run on that project’s own compilation) (16-interop-io.md). The SDK’s JSON andLibraryImportgenerators and the regex generator are built into csharpc. - Platforms.
--targetpasses the triple through; Linux x86-64 and ARM64, macOS ARM64, Windows x64 andwasm32-wasip1are tested in CI (06-roadmap.md). A native toolchain is required on the host: clang (and lld when available), LLVM-MinGW on Windows, the WASI SDK for WebAssembly. - Build performance. A release build of csharpc is ReadyToRun-compiled and a compilation is multi-threaded; hello
world compiles in about a second at
-O0. Project input adds onedotnet msbuildevaluation per project, and the compile time of large programs is dominated by LLVM (ADR 0014).