2

I need to build a condition where one package is only included on Windows and another package only on Linux. Nuget supports Condition attributes to include a PackageReference only in specific cases. That's exactly what I need, but the example only checks the framework version with the $(TargetFramework) variable.

I need a variable like $(OS) but I don't know which variables exists and what's their contect to write correct conditions. For MSBuild I found a list of well-known properties and build macros (but for C++). It seems that $(Platform) is Win32, however I have no clue what it looks on Linux: Is it Linux? The distribution? Linux32/64?

For Windows I tried

<PackageReference Include="IBM.Data.DB2.Core" Version="3.1.0.400" Condition="'$(Platform)' == 'Win32'"/>

which doesn't work, I assume that this is only applied to C++ code and not C#.

I'm wondering that there must be a documentation of those variables for .NET Core with the avaliable values, sadly I can't find one.

Background: DB2

My app needs the IBM.Data.DB2.Core* package to access the database of a legacy application. The IBM.Data.DB2.Core package only works on Windows, where IBM.Data.DB2.Core-lnx works on Linux. If we use the win package on linux, it throws a Unable to load shared library 'db2app64.dll' error. Since this app should run on Win and Linux, I'd like to make it ready for both platforms.

Lion
  • 16,606
  • 23
  • 86
  • 148
  • 1
    Does this help you? https://stackoverflow.com/questions/52612790/different-nuget-package-based-on-operating-system – mao Feb 19 '21 at 20:55

1 Answers1

1

Using the process explorer, I was able to see the environment variables of Visual Studio. Those variables are avaliable in the csproj file as $(VARNAME). It contains a variable OS which is set to Windows_NT on Windows 10, so I could create a condition for Windows:

<PackageReference Include="IBM.Data.DB2.Core" Version="3.1.0.400" Condition="'$(OS)' == 'WINDOWS_NT'"/>

After I'd like to change to Alpine from my Docker base image, I ran into other issues and found this question which investigated that OS is set to UNIX on Linux systems, which leads me to the second condition:

<PackageReference Include="IBM.Data.DB2.Core-lnx" Version="3.1.0.400" Condition="'$(OS)' == 'UNIX'"/>

However, I'm still wondering not to found an official documentation about the avaliable variables in C#, this workaround has fixed my problem.

Lion
  • 16,606
  • 23
  • 86
  • 148