Compile DotNet App to C Code
We want to compile DotNet app to C code. It is portable to all machine.
What platform would you like to target?
Did you read about the CppCodegen backend in CoreRT? https://github.com/dotnet/corert/search?l=Markdown&q=cppcodegen
How to use CppCodegen?
With C Code, I can use the C compiler on any platform to compile to native.
Follow the instructions to e.g. compile a hello world app with the default/native CoreRT codegen backend: https://github.com/dotnet/corert/blob/master/samples/HelloWorld/README.md
BUT: adjust the dotnet publish command to use Cpp Codegen (described here: https://github.com/dotnet/corert/blob/master/Documentation/how-to-build-and-run-ilcompiler-in-console-shell-prompt.md#using-cpp-code-generator).
But note that while this will produce a C++ file, this code is not going to be portable. The runtime and the framework libraries need to call into the underlying platform to do many things (reading and writing to console, file IO, threading, memory management). The generated C++ code is going to have calls into these APIs.
It's still a lot of work to bring up the entire stack on a different platform unless it's something pretty standard, like a Unix flavor.
I have followed these steps outlined above, and ran dotnet publish /p:NativeCodeGen=cpp /p:NativeCompilationDuringPublish=false -v diag as I'm only interested in a .cpp file.
I can also see IlcCompileOutput = obj/Debug/netcoreapp3.1/native/NativeLibrary.cpp in the terminal output, but such a file does not exist after building.
However, I still get bin/Debug/netcoreapp3.1/publish/NativeLibrary.dll.
What's going on?
My host is a linux-x64 machine (Arch Linux using community/dotnet-sdk 3.1.3.sdk103.2-) and I only want textual cpp code as the target machine is x86 running a custom OS (which also means I'm planning to add my own runtime functions if necessary). How would I get those portable .cpp files which contain the logic?
Am I going about this the wrong way? Are there any other recommended ways / tools to get IL to LLVM IR or C / C++? Most things I've tried only supported very poor subsets or did not work at all.
Setting NativeCompilationDuringPublish to false will disable generation of .cpp file. If you would like to trigger just the .cpp code generation without the rest, you may need to explicitly invoke IlcCompile target.
I can also see IlcCompileOutput = obj/Debug/netcoreapp3.1/native/NativeLibrary.cpp
This just sets a build script variable, without using it for anything.
I still get bin/Debug/netcoreapp3.1/publish/NativeLibrary.dll.
This is the .dll with the IL code.