I want to ship a single .NET assembly generated from several C# projects via .netmodules.
I've experimented with ILmerge, but it has other problems. I also had a look at the AssemblyResolve way, but I don't really understand it (both covered here: How to merge multiple assemblies into one?).
I've found a possible solution that would work fine for the task via .netmodules. No external programs, standard tools, the resulting assembly looks like it came from one project only (in ildasm).
Here's a MWE: Lib.csproj
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutputType>Module</OutputType>
<OutputPath>bin\</OutputPath>
...
</PropertyGroup>
...
<ItemGroup>
<Compile Include="Lib.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Exe.csproj
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutputType>Module</OutputType>
<OutputPath>bin\</OutputPath>
...
</PropertyGroup>
...
<ItemGroup>
<AddModules Include="..\Lib\bin\Lib.netmodule" />
<Compile Include="Program.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
The output type of both projects is set to module. The "Exe" project uses the "Lib" netmodule via the AddModules switch (required for compilation). This results in two .netmodules in the Exe output directory.
In the final step, the linker is used to link all .netmodules into one Assembly (see https://learn.microsoft.com/en-us/cpp/build/reference/netmodule-files-as-linker-input?view=vs-2017):
link Lib.netmodule Exe.netmodule -subsystem:console -out:Exe.exe -ltcg -entry:Exe.Program.Main
The question: Can this last step be performed by MSBuild? A CMake solution would also be appreciated, but I could not get output type "Module" from CMake.