Alexander Tauenis
Alexander Tauenis

Reputation: 373

What MSBuild condition should be used to detect target OS?

What Condition Expression for PropertyGroup/ItemGroup should be used to differ target OS (-r argument of dotnet publish)? E.g. on these commands:

dotnet publish -c Release -r win-x86 --self-contained false
dotnet publish -c Release -r linux-arm --self-contained false

Currently I've forced to use different Configurations and build using these commands:

dotnet publish -c ReleaseWin32 -r win-x86 --self-contained false
dotnet publish -c ReleaseLinux -r linux-arm --self-contained false

I know that MSBuild can define even target .NET Core/Framework version (e.g.Condition="'$(TargetFramework)' == 'netcoreapp3.1'"), so probably should also define target OS (something like Condition="'$(TargetOS)' == 'win-x86'").

Does there may be somehow used direct detection of target OS in CSPROJ file without using -c ReleaseWin32 / -c ReleaseLinux for builds for different platforms? Shortly, does MSBuild syntax have any Condition about target OS?

Upvotes: 4

Views: 3643

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100581

The CLI's -r linux-arm translates to MSBUild -property:RuntimeIdentifier=linux-x64 so you can use $(RuntimeIdentifier) in conditions:

<PropertyGroup Condition="'$(RuntimeIdentifier)' == 'linux-arm'">
</PropertyGroup>

<ItemGroup Condition="$(RuntimeIdentifier.StartsWith('win'))">
</ItemGroup>

Upvotes: 4

Related Questions