sasik
sasik
CC#
Created by sasik on 2/13/2023 in #help
✅ Creating a cross-platform csproj supporting any .NET and any platform -linux,osx,amd64,arm64
Hello, I have a project that I would like to be able to run on any machine, from linux/amd64 with mono installed to (dated) linux/amd64 with dotnet 5 to osx/amd64 and osx/arm64 with the latest dotnet 7. To be more specific, what I would like to achieve: - when I type dotnet build/test/run, I would like to run it using the current platform and any available framework (I can safely assume there is just one) - when I type dotnet publish it is published as a self-contained application My current csproj (excerpt)
<PropertyGroup>
<TargetFramework Condition="'$(MSBuildRuntimeType)' == 'Core'">net5</TargetFramework>
<TargetFramework Condition="'$(MSBuildRuntimeType)' != 'Core'">net461</TargetFramework>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<OutputType>Exe</OutputType>
<RollForward>Major</RollForward>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<IsPublishable>false</IsPublishable>
</PropertyGroup>
<PropertyGroup>
<TargetFramework Condition="'$(MSBuildRuntimeType)' == 'Core'">net5</TargetFramework>
<TargetFramework Condition="'$(MSBuildRuntimeType)' != 'Core'">net461</TargetFramework>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<OutputType>Exe</OutputType>
<RollForward>Major</RollForward>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<IsPublishable>false</IsPublishable>
</PropertyGroup>
And if the csproj is publishable, then I add also this:
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseApp' ">
<IsPublishable>true</IsPublishable>
<SelfContained>true</SelfContained>
<!-- PublishSingleFile messes app.config and adds no real benefit -->
<PublishSingleFile>false</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseApp' ">
<IsPublishable>true</IsPublishable>
<SelfContained>true</SelfContained>
<!-- PublishSingleFile messes app.config and adds no real benefit -->
<PublishSingleFile>false</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
Issues: - this lets me only handle .NET 4.6.1 (which I use via mono), .NET 5 and .NET 6. It won't work on .NET 7 strange thing here: <RollForward> (I've played with Major, LatestMajor) works if .NET 6 is installed but it doesn't work when .NET 7 is the only available runtime . Does it mean, that RollForward is only able to bump the major version by 1? - it doesn't work on osx. I have to manually change the <RuntimeIdentifier> - differently for osx/arm64 and for osx/amd64 How to do this well?
8 replies