All Articles

Choose Build Targets Depending on Build OS

Recently I decided to move my Invocation Argument Extractor from .NET Framework to .NET Core and Framework. Anyway, the only difference was how to locate the msbuild executable to use. So lets create a core library with a target of netstandard2.0 and two libraries doing the lifting to create a workspace and locating msbuild.

After getting this to work I wanted to setup CI for the project. At least it was now .NET Core compatible. Should be quite easy.

Welcome to the nuget jungle

The project depends on Microsoft.Build v16.4 and Microsoft.CodeAnalysis.Workspaces.MSBuild v3.4.0, both nuget packaged did not supply dependencies for netstandard2.0. Ony for netcoreapp2.1 and net472. This went nice on my Windows installation building locally. Building the project in a docker image using netcore 2.1 was not working as the net472 version was always checked out. Multitargeting the library to netcoreapp2.1 and net472 solved the mixup from nuget.

Conditions in csproj files to the rescue

Of course, it did not help in getting it compiled in a linux docker container. But hey, one can use OS detection in csproj files, so lets give it a try:

<PropertyGroup>
  <TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netcoreapp2.1</TargetFrameworks >
  <TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">netcoreapp2.1;net472</TargetFrameworks>
</PropertyGroup>

Now it should only target net472 when building on a Windows host. Well, it does. Great!